Skip to content

Commit 2d24317

Browse files
author
hariniparth
committed
Product of Other Elements
1 parent c1f6430 commit 2d24317

7 files changed

Lines changed: 241 additions & 0 deletions

File tree

‎InterviewCake/ProductOfOthers/.idea/description.html‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎InterviewCake/ProductOfOthers/.idea/misc.xml‎

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎InterviewCake/ProductOfOthers/.idea/modules.xml‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎InterviewCake/ProductOfOthers/.idea/project-template.xml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎InterviewCake/ProductOfOthers/.idea/workspace.xml‎

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.company;
2+
3+
import java.util.*;
4+
5+
// https://www.interviewcake.com/question/java/product-of-other-numbers
6+
7+
// Time Complexity: O(n) and Space Complexity: O(n)
8+
9+
public class Main {
10+
11+
public int[] productOfOthers(int[] arr){
12+
13+
if(arr.length < 2)
14+
return arr;
15+
16+
int n = arr.length;
17+
int[] resultProduct = new int[n];
18+
int product = 1;
19+
20+
for(int i=0;i<n;i++){
21+
resultProduct[i] = product;
22+
product *= arr[i];
23+
}
24+
25+
product = 1;
26+
for(int i=n-1;i>=0;i--){
27+
resultProduct[i] *= product;
28+
product *= arr[i];
29+
}
30+
31+
return resultProduct;
32+
}
33+
}

0 commit comments

Comments
 (0)