-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
data_structures/neetcode_150/src/ProductOfArrayExceptSelf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package neetcode_150.src; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Product of Array Except Self | ||
* | ||
* @apiNote | ||
* - Given an intger array `nums`, return an array such that | ||
* `answers[i]` is equal to the product of all the elements of `nums` | ||
* except `nums[i]` | ||
* - The product of any prefix or suffix of nums is guaranteed to fit | ||
* in a 32-bit integer. | ||
* - You must write an algorithm that runs in `O(n)` time and without | ||
* using the division operation. | ||
* @example | ||
* - Inputs: nums = [1 , 2, 3, 4] | ||
* - Output: [24, 12, 8, 6] | ||
* @explain | ||
* - index | nums[i] | product of all elements except nums[i] | ||
* - 0 | 1 | 2 * 3 * 4 = 24 | ||
*/ | ||
public class ProductOfArrayExceptSelf { | ||
public static void main(String[] args) { | ||
int[] nums = { 1, 2, 3, 4 }; | ||
System.out.println("Brute Force ==> " + Arrays.toString(bruteForce(nums))); | ||
System.out.println("Prefix PostFix ==> " + Arrays.toString(productExceptSelf(nums))); | ||
} | ||
|
||
private static int[] productExceptSelf(int[] nums) { | ||
int n = nums.length; | ||
int[] result = new int[n]; | ||
Arrays.fill(result, 1); | ||
|
||
int pre = 1, post = 1; | ||
|
||
for (int i = 0; i < n; i++) { | ||
result[i] = pre; | ||
pre *= nums[i]; | ||
} | ||
|
||
for (int i = n - 1; i >= 0; i--) { | ||
result[i] *= post; | ||
post *= nums[i]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Brute Force Approach (Incorrect Approach) | ||
*/ | ||
private static int[] bruteForce(int[] nums) { | ||
System.out.println("\nBrute Force Approach ==> "); | ||
int n = nums.length; | ||
int[] ans = new int[n]; | ||
|
||
for (int i = 0; i < n; i++) { | ||
int product = 1; | ||
for (int j = 0; j < n; j++) { | ||
if (i != j) { | ||
product *= nums[j]; | ||
} | ||
} | ||
ans[i] = product; | ||
} | ||
|
||
return ans; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
data_structures/src/KeyPatterns/Prefix/Examples/BasicPrefixSum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package KeyPatterns.Prefix.Examples; | ||
|
||
/** | ||
* Basic Prefix Sum | ||
* | ||
* @apiNote | ||
* - Problem: Given an array, find the sum of elements from index `l` | ||
* to `r`. | ||
*/ | ||
public class BasicPrefixSum { | ||
public static void main(String[] args) { | ||
System.out.println("\nBasic Prefix Sum ===> "); | ||
|
||
int[] nums = { 2, 4, 5, 1, 6 }; | ||
int[] prefix = computePrefixSum(nums); | ||
System.out.println(rangeSum(prefix, 1, 3)); // Output: 10 (4+5+1) | ||
} | ||
|
||
private static int[] computePrefixSum(int[] nums) { | ||
int n = nums.length; | ||
int[] prefix = new int[n]; | ||
prefix[0] = nums[0]; | ||
|
||
for (int i = 1; i < n; i++) { | ||
prefix[i] = prefix[i - 1] + nums[i]; | ||
} | ||
|
||
return prefix; | ||
} | ||
|
||
private static int rangeSum(int[] prefix, int l, int r) { | ||
if (l == 0) { | ||
return prefix[r]; | ||
} | ||
return prefix[r] - prefix[l - 1]; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
data_structures/src/KeyPatterns/Prefix/Examples/CountEvenNumbers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package KeyPatterns.Prefix.Examples; | ||
|
||
/** | ||
* Count of Even Numbers in a Range | ||
* - Problem : Find the count of even numbers in a subarray [l, r] | ||
*/ | ||
public class CountEvenNumbers { | ||
public static void main(String[] args) { | ||
int[] nums = { 2, 4, 5, 1, 6, 3 }; | ||
int[] prefixEven = computeEvenPrefix(nums); | ||
System.out.println(countEvens(prefixEven, 1, 4)); | ||
} | ||
|
||
private static int countEvens(int[] prefixEven, int l, int r) { | ||
if (l == 0) { | ||
return prefixEven[r]; | ||
} | ||
return prefixEven[r] - prefixEven[l - 1]; | ||
} | ||
|
||
private static int[] computeEvenPrefix(int[] nums) { | ||
int n = nums.length; | ||
int[] prefixEven = new int[n]; | ||
prefixEven[0] = (nums[0] % 2 == 0) ? 1 : 0; | ||
|
||
for (int i = 1; i < n; i++) { | ||
prefixEven[i] = prefixEven[i - 1] + (nums[i] % 2 == 0 ? 1 : 0); | ||
} | ||
|
||
return prefixEven; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
data_structures/src/KeyPatterns/Prefix/Examples/PrefixXOR.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package KeyPatterns.Prefix.Examples; | ||
|
||
public class PrefixXOR { | ||
public static void main(String[] args) { | ||
int[] nums = { 3, 8, 2, 6, 4 }; | ||
int[] prefixXor = computePrefixXor(nums); | ||
System.out.println(rangeXor(prefixXor, 1, 4)); | ||
} | ||
|
||
private static int[] computePrefixXor(int[] nums) { | ||
int n = nums.length; | ||
int[] prefixXOR = new int[n]; | ||
prefixXOR[0] = nums[0]; | ||
|
||
for (int i = 1; i < n; i++) { | ||
prefixXOR[i] = prefixXOR[i - 1] ^ nums[i]; | ||
} | ||
|
||
return prefixXOR; | ||
} | ||
|
||
private static int rangeXor(int[] prefixXor, int l, int r) { | ||
if (l == 0) { | ||
return prefixXor[r]; | ||
} | ||
return prefixXor[r] ^ prefixXor[l - 1]; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
data_structures/src/KeyPatterns/Prefix/Examples/RunningSum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package KeyPatterns.Prefix.Examples; | ||
|
||
/** | ||
* Running Sum (Simple Prefix Sum) | ||
* - Problem: Convert an array into its running sum (prefix sum). | ||
* - Approach: Directly modify an array to store cumulative sum. | ||
*/ | ||
public class RunningSum { | ||
public static void main(String[] args) { | ||
int[] nums = { 1, 2, 3, 4 }; | ||
int[] result = runningSum(nums); | ||
for (int r : result) { | ||
System.out.println("result ==> " + r); | ||
} | ||
} | ||
|
||
private static int[] runningSum(int[] nums) { | ||
for (int i = 1; i < nums.length; i++) { | ||
nums[i] += nums[i - 1]; | ||
} | ||
return nums; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Prefix Technique |