Skip to content

Commit 18649ce

Browse files
committed
product of all array elements except current
1 parent 139e96a commit 18649ce

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

README.md

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
- [x] [Find the second-largest number in an array](https://www.digitalocean.com/community/tutorials/java-programming-interview-questions#19-how-do-you-find-the-second-largest-number-in-an-array-in-java)
9797
- [x] [Compare two arrays' elements](https://www.digitalocean.com/community/tutorials/java-programming-interview-questions#17-write-java-program-that-checks-if-two-arrays-contain-the-same-elements)
9898
- [x] [Find a pair with the given sum in an array](https://www.techiedelight.com/find-pair-with-given-sum-array/)
99+
- [x] [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)
99100

100101
### Lists
101102

@@ -126,12 +127,8 @@
126127

127128
### Graphs
128129

129-
<!-- BFS, DFS, topological sort, shortest path, etc. -->
130-
131130
### Matrix
132131

133-
<!-- 2D array problems -->
134-
135132
### Trees
136133

137134
<!-- Binary trees, BSTs, traversals, etc. -->
@@ -174,7 +171,6 @@
174171

175172
## To Do
176173

177-
- [ ] Remove duplicates below 😅
178174
- Bits Manipulation
179175
- [ ] [Bit Hacks — Part 1 (Basic)](http://www.techiedelight.com/bit-hacks-part-1-basic/)
180176
- [ ] [Bit Hacks — Part 2 (Playing with k’th bit)](http://www.techiedelight.com/bit-hacks-part-2-playing-kth-bit/)
@@ -197,7 +193,6 @@
197193
- Arrays
198194
- [ ] [Two Sum](https://leetcode.com/problems/two-sum/)
199195
- [ ] [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)
200-
- [ ] [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)
201196
- [ ] [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)
202197
- [ ] [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)
203198
- [ ] [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/maximum-product-subarray/)
@@ -238,8 +233,6 @@
238233
- [ ] [Find minimum platforms needed in the station so to avoid any delay in arrival of any train](http://www.techiedelight.com/minimum-number-of-platforms-needed-avoid-delay-arrival-train)
239234
- [ ] [Length of longest continuous sequence with same sum in given binary arrays](http://www.techiedelight.com/length-longest-continuous-sequence-same-sum-binary-arrays/)
240235
- [ ] [Merging Overlapping Intervals](http://www.techiedelight.com/merging-overlapping-intervals)
241-
- [ ] [Check if given array represents min heap or not](http://www.techiedelight.com/check-given-array-represents-min-heap-not/)
242-
- [ ] [Convert Max Heap to Min Heap in linear time](http://www.techiedelight.com/convert-max-heap-min-heap-linear-time/)
243236
- [ ] [Find K’th largest element in an array](http://www.techiedelight.com/find-kth-largest-element-array/)
244237
- [ ] [Sort a K-Sorted Array](http://www.techiedelight.com/sort-k-sorted-array/)
245238
- [ ] [Find K’th smallest element in an array](http://www.techiedelight.com/find-kth-smallest-element-array/)
@@ -278,8 +271,6 @@
278271

279272
- Graphs
280273
- [ ] Given a list of edges and tasked to build your own graph from the edges
281-
- [ ] Implement BFS
282-
- [ ] Implement DFS
283274
- [ ] Implement Dijkstra’s algorithm
284275
- [ ] Implement Topological sort
285276
- [ ] Implement Bellman-Ford algorithm
@@ -297,8 +288,6 @@
297288
- [ ] [Terminology and Representations of Graphs](http://www.techiedelight.com/terminology-and-representations-of-graphs/)
298289
- [ ] [Graph Implementation using STL](http://www.techiedelight.com/graph-implementation-using-stl/)
299290
- [ ] [Graph Implementation in C++ without using STL](http://www.techiedelight.com/graph-implementation-c-without-using-stl/)
300-
- [ ] [Breadth First Search (BFS) | Iterative & Recursive Implementation](http://www.techiedelight.com/breadth-first-search/)
301-
- [ ] [Depth First Search (DFS) | Iterative & Recursive Implementation](http://www.techiedelight.com/depth-first-search/)
302291
- [ ] [Arrival and Departure Time of Vertices in DFS](http://www.techiedelight.com/arrival-departure-time-vertices-dfs/)
303292
- [ ] [Types of edges involved in DFS and relation between them](http://www.techiedelight.com/types-edges-involved-dfs-relation/)
304293
- [ ] [Bipartite Graph](http://www.techiedelight.com/bipartite-graph/)
@@ -312,7 +301,6 @@
312301
- [ ] [2-Vertex Connectivity in the graph](http://www.techiedelight.com/2-vertex-connectivity-graph/)
313302
- [ ] [Check if given digraph is a DAG (Directed Acyclic Graph) or not](http://www.techiedelight.com/check-given-digraph-dag-directed-acyclic-graph-not/)
314303
- [ ] [Disjoint-Set Data Structure (Union-Find Algorithm)](http://www.techiedelight.com/disjoint-set-data-structure-union-find-algorithm/)
315-
- [ ] [Chess Knight Problem — Find Shortest path from source to destination](http://www.techiedelight.com/chess-knight-problem-find-shortest-path-source-destination/)
316304
- [ ] [Check if given Graph is Strongly Connected or not](http://www.techiedelight.com/check-given-graph-strongly-connected-not/)
317305
- [ ] [Check if given Graph is Strongly Connected or not using one DFS Traversal](http://www.techiedelight.com/check-graph-strongly-connected-one-dfs-traversal/)
318306
- [ ] [Union-Find Algorithm for Cycle Detection in undirected graph](http://www.techiedelight.com/union-find-algorithm-cycle-detection-graph/)
@@ -407,7 +395,6 @@
407395
- [ ] [Find all possible combinations by replacing given digits with characters of the corresponding list](http://www.techiedelight.com/possible-combinations-replacing-given-digits-corresponding-list/)
408396
- [ ] [Find all words from given list that follows same order of characters as given pattern](http://www.techiedelight.com/find-words-that-follows-given-pattern/)
409397
- [ ] [Find first k non-repeating characters in a string in single traversal](http://www.techiedelight.com/first-k-non-repeating-characters-string/)
410-
- [ ] [Group anagrams together from given list of words](http://www.techiedelight.com/group-anagrams-together-given-list-words/)
411398
- [ ] [Introduction to Pattern Matching](http://www.techiedelight.com/introduction-pattern-matching/)
412399
- [ ] [Inplace remove all occurrences of ‘AB’ and ‘C’ from the string](http://www.techiedelight.com/inplace-remove-all-occurrences-ab-c-string/)
413400
- [ ] [Longest even length palidromic sum substring](http://www.techiedelight.com/longest-even-length-palidromic-sum-substring/)
@@ -443,13 +430,13 @@
443430
- [ ] [Shortest Common Supersequence | Finding all SCS](http://www.techiedelight.com/shortest-common-supersequence-finding-scs/)
444431
- [ ] [Shortest Common Supersequence | Using LCS](http://www.techiedelight.com/shortest-common-supersequence-using-lcs/)
445432
- Tree
446-
- [ ] [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
447433
- [ ] [Same Tree](https://leetcode.com/problems/same-tree/)
448-
- [ ] [Invert or Flip Binary Tree](https://leetcode.com/problems/invert-binary-tree/)
449434
- [ ] [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)
450435

451436
- Binary Tree
452437
- [ ] [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)
438+
- [ ] [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
439+
- [ ] [Invert or Flip Binary Tree](https://leetcode.com/problems/invert-binary-tree/)
453440
- [ ] [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/)
454441
- [ ] [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)
455442
- [ ] [Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
@@ -536,9 +523,6 @@
536523
- [ ] [Find K’th smallest element in an array](http://www.techiedelight.com/find-kth-smallest-element-array/)
537524
- [ ] [Find smallest range with at-least one element from each of the given lists](http://www.techiedelight.com/find-smallest-range-least-one-element-given-lists/)
538525
- [ ] [External merge sort](http://www.techiedelight.com/external-merge-sort/)
539-
- [ ] [Find first k maximum occurring words in given set of strings](http://www.techiedelight.com/find-first-k-maximum-occurring-words-given-set-strings/)
540-
- [ ] [Find first k non-repeating characters in a string in single traversal](http://www.techiedelight.com/first-k-non-repeating-characters-string/)
541-
542526

543527
- Queue
544528
- [ ] [Chess Knight Problem — Find Shortest path from source to destination](http://www.techiedelight.com/chess-knight-problem-find-shortest-path-source-destination/)
@@ -547,7 +531,6 @@
547531
- [ ] [Print all Possible Knight’s Tours in a chessboard](http://www.techiedelight.com/print-possible-knights-tours-chessboard/)
548532
- [ ] [Find Shortest Path in Maze](http://www.techiedelight.com/find-shortest-path-in-maze/)
549533
- [ ] [Find Longest Possible Route in a Matrix](http://www.techiedelight.com/find-longest-possible-route-matrix/)
550-
- [ ] [Chess Knight Problem — Find Shortest path from source to destination](http://www.techiedelight.com/chess-knight-problem-find-shortest-path-source-destination/)
551534
- [ ] [Lee algorithm | Shortest path in a Maze](http://www.techiedelight.com/lee-algorithm-shortest-path-in-a-maze/)
552535
- [ ] [Find shortest safe route in a field with sensors present](http://www.techiedelight.com/find-shortest-safe-route-field-sensors-present/)
553536
- [ ] [Flood fill Algorithm](http://www.techiedelight.com/flood-fill-algorithm/)
@@ -597,7 +580,6 @@
597580
- [ ] [External merge sort](http://www.techiedelight.com/external-merge-sort/)
598581
- [ ] [Custom Sort | Sort elements by their frequency and Index](http://www.techiedelight.com/sort-elements-by-their-frequency-and-index/)
599582
- [ ] [Custom Sort | Sort elements of the array by order of elements defined by the second array](http://www.techiedelight.com/custom-sort-sort-elements-array-order-elements-defined-second-array/)
600-
- [ ] [Inversion Count of an array](http://www.techiedelight.com/inversion-count-array/)
601583
- [ ] [Segregate positive and negative integers in linear time](http://www.techiedelight.com/positive-and-negative-integers-segregate/)
602584
- [ ] [Sort binary array in linear time](http://www.techiedelight.com/sort-binary-array-linear-time/)
603585
- [ ] [Group anagrams together from given list of words](http://www.techiedelight.com/group-anagrams-together-given-list-words/)
@@ -628,7 +610,6 @@
628610
- [ ] [Find Minimum and Maximum element in an array using minimum comparisons](http://www.techiedelight.com/find-minimum-maximum-element-array-using-minimum-comparisons/)
629611
- [ ] [Efficiently implement power function | Recursive and Iterative](http://www.techiedelight.com/power-function-implementation-recursive-iterative/)
630612
- [ ] [Merge Sort](http://www.techiedelight.com/merge-sort/)
631-
- [ ] [Inversion Count of an array](http://www.techiedelight.com/inversion-count-array/)
632613
- [ ] [Quicksort](http://www.techiedelight.com/quicksort/)
633614
- [ ] [Iterative Implementation of Quicksort](http://www.techiedelight.com/iterative-implementation-of-quicksort/)
634615

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dataStructures.arrays;
2+
3+
import java.util.Arrays;
4+
5+
public class ProductExceptSelf {
6+
public static void main(String[] args) {
7+
// int[] nums = {1,2,3,4};
8+
int[] nums = {-1,1,0,-3,3};
9+
System.out.println(Arrays.stream(productExceptSelf(nums)).boxed().toList());
10+
}
11+
12+
// Brute force: O(n^2)
13+
private static int[] productExceptSelf(int[] nums) {
14+
int[] prefixes = new int[nums.length];
15+
int[] suffixes = new int[nums.length];
16+
int[] answer = new int[nums.length];
17+
18+
// Compute prefix products and store them in answer[].
19+
prefixes[0] = 1;
20+
for (int i = 1; i < nums.length; i++) {
21+
prefixes[i] = prefixes[i - 1] * nums[i - 1];
22+
}
23+
24+
// Traverse from the end to accumulate suffix products and update answer[] with the result.
25+
suffixes[nums.length - 1] = 1;
26+
for (int i = nums.length - 2; i >= 0; i--) {
27+
suffixes[i] = suffixes[i + 1] * nums[i + 1];
28+
}
29+
30+
// final products
31+
for (int i = 0; i < nums.length; i++) {
32+
answer[i] = prefixes[i] * suffixes[i];
33+
}
34+
35+
return answer;
36+
}
37+
}

0 commit comments

Comments
 (0)