Skip to content

Commit

Permalink
Solution added for 'Make Sum Divisible by P' Leetcode Problem 1590
Browse files Browse the repository at this point in the history
  • Loading branch information
Saumitra Pathak committed Oct 4, 2024
1 parent fa8ee88 commit 31ba0eb
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Java/sum_divisible_by_p.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.HashMap;

public class sum_divisible_by_p {
public static int minSubarray(int[] arr, int divisor) {
long totalSum = 0;
for (int num : arr) {
totalSum += num;
}
int remainder = (int)(totalSum % divisor);

// If the remainder is 0, the total sum is already divisible by divisor
if (remainder == 0) return 0;

// HashMap to store moduli of prefix sums and their indices
HashMap<Integer, Integer> prefixModMap = new HashMap<>();
prefixModMap.put(0, -1);

long cumulativeSum = 0;
int minLength = arr.length;

for (int i = 0; i < arr.length; ++i) {
cumulativeSum += arr[i];
int currentMod = (int)(cumulativeSum % divisor);

// Calculate the modulus we're looking for to find a subarray that, when removed, makes the sum divisible by divisor
int targetMod = (currentMod - remainder + divisor) % divisor;

// If the target modulus exists in the map, we found a subarray
if (prefixModMap.containsKey(targetMod)) {
minLength = Math.min(minLength, i - prefixModMap.get(targetMod));
}
prefixModMap.put(currentMod, i);
}
return minLength == arr.length ? -1 : minLength;
}

public static void main(String args[]){
int ans = minSubarray(new int[]{3, 1, 4, 2}, 6);
System.out.println(ans);
}
}

/*
Demo Outputs:
Input: nums = [3,1,4,2], p = 6
Output: 1
Input: nums = [6,3,5,2], p = 9
Output: 2
Input: nums = [1,2,3], p = 3
Output: 0
*/

0 comments on commit 31ba0eb

Please sign in to comment.