-
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
7 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
data_structures/src/KeyPatterns/Prefix/Examples/CountSubarraysWithXorK.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,25 @@ | ||
package KeyPatterns.Prefix.Examples; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class CountSubarraysWithXorK { | ||
public static void main(String[] args) { | ||
int[] nums = { 4, 2, 2, 6, 4 }; | ||
System.out.println(countSubarraysWithXorK(nums, 6)); // Output: 4 | ||
} | ||
|
||
private static int countSubarraysWithXorK(int[] nums, int k) { | ||
Map<Integer, Integer> prefixXorCount = new HashMap<>(); | ||
prefixXorCount.put(0, 1); | ||
int prefixXor = 0, count = 0; | ||
|
||
for (int num : nums) { | ||
prefixXor ^= num; | ||
count += prefixXorCount.getOrDefault(prefixXor ^ k, 0); | ||
prefixXorCount.put(prefixXor, prefixXorCount.getOrDefault(prefixXor, 0) + 1); | ||
} | ||
|
||
return count; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
data_structures/src/KeyPatterns/Prefix/Examples/Easy/EvenCountInSubarray.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.Easy; | ||
|
||
/** | ||
* Find Even Numbers in a Subarray | ||
* - Given an array `nums`, | ||
* count the number of even numbers in a subarray `[L,R]` | ||
*/ | ||
public class EvenCountInSubarray { | ||
public static void main(String[] args) { | ||
int[] nums = { 2, 1, 4, 6, 5 }; | ||
System.out.println(countEvenNumbers(nums, 1, 3)); | ||
} | ||
|
||
private static int countEvenNumbers(int[] nums, int L, int R) { | ||
int n = nums.length; | ||
int[] prefix = new int[n]; | ||
prefix[0] = (nums[0] % 2 == 0) ? 1 : 0; | ||
for (int i = 1; i < n; i++) { | ||
prefix[i] = prefix[i - 1] + ((nums[i] % 2 == 0) ? 1 : 0); | ||
} | ||
return (L == 0) ? prefix[R] : prefix[R] - prefix[L - 1]; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
data_structures/src/KeyPatterns/Prefix/Examples/Easy/LeftRightSum.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,25 @@ | ||
package KeyPatterns.Prefix.Examples.Easy; | ||
|
||
public class LeftRightSum { | ||
public static void main(String[] args) { | ||
int[] nums = { 1, 7, 3, 6, 5, 6 }; | ||
System.out.println(findEquilibriumIndex(nums)); | ||
} | ||
|
||
private static int findEquilibriumIndex(int[] nums) { | ||
int totalSum = 0, leftSum = 0; | ||
|
||
for (int num : nums) { | ||
totalSum += num; | ||
} | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
if (leftSum == totalSum - leftSum - nums[i]) { | ||
return i; | ||
} | ||
leftSum += nums[i]; | ||
} | ||
|
||
return -1; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
data_structures/src/KeyPatterns/Prefix/Examples/Easy/PrefixSumArray.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,22 @@ | ||
package KeyPatterns.Prefix.Examples.Easy; | ||
|
||
import java.util.Arrays; | ||
|
||
public class PrefixSumArray { | ||
public static void main(String[] args) { | ||
int[] nums = { 1, 2, 3, 4, 5 }; | ||
System.out.println(Arrays.toString(computePrefixSum(nums))); | ||
} | ||
|
||
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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
data_structures/src/KeyPatterns/Prefix/Examples/Easy/SmallestIndexWithSum.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.Easy; | ||
|
||
public class SmallestIndexWithSum { | ||
public static void main(String[] args) { | ||
int[] nums = { 3, 1, 4, 2, 8 }; | ||
System.out.println(findSmallestIndex(nums, 7)); | ||
} | ||
|
||
private static int findSmallestIndex(int[] nums, int target) { | ||
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]; | ||
if (prefix[i] >= target) { | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
data_structures/src/KeyPatterns/Prefix/Examples/Easy/SubarraySum.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,25 @@ | ||
package KeyPatterns.Prefix.Examples.Easy; | ||
|
||
/** | ||
* Sum of a Subarray using Prefix Sum | ||
* - Given an array `nums` and a range `[L, R]`, | ||
* - find the sum of elements from `nums[L]` to `nums[R]` | ||
*/ | ||
public class SubarraySum { | ||
public static void main(String[] args) { | ||
int[] nums = { 2, 4, 6, 8, 10 }; | ||
System.out.println(subArraySum(nums, 1, 3)); | ||
} | ||
|
||
private static int subArraySum(int[] nums, int L, int R) { | ||
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 (L == 0) ? prefix[R] : prefix[R] - prefix[L - 1]; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
data_structures/src/KeyPatterns/Prefix/Examples/SubArrayXor.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,19 @@ | ||
package KeyPatterns.Prefix.Examples; | ||
|
||
public class SubArrayXor { | ||
public static void main(String[] args) { | ||
int[] nums = { 4, 8, 2, 10 }; | ||
System.out.println(xorInRange(nums, 2, 4)); | ||
} | ||
|
||
public static int xorInRange(int[] nums, int L, int R) { | ||
int n = nums.length; | ||
int[] prefix = new int[n + 1]; | ||
|
||
for (int i = 1; i <= n; i++) { | ||
prefix[i] = prefix[i - 1] ^ nums[i - 1]; | ||
} | ||
|
||
return prefix[R] ^ prefix[L - 1]; | ||
} | ||
} |