Skip to content

Commit

Permalink
Create MakeSumDivisible.java
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektripathi66 authored Oct 3, 2024
1 parent 69dcb47 commit fa8ffee
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Leetcode/MakeSumDivisible.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
1590. Make Sum Divisible by P
Solved
Medium
Topics
Companies
Hint
Given an array of positive integers nums, remove the smallest subarray (possibly empty) such that the sum of the remaining elements is divisible by p. It is not allowed to remove the whole array.
Return the length of the smallest subarray that you need to remove, or -1 if it's impossible.
A subarray is defined as a contiguous block of elements in the array.
**/

import java.util.HashMap;

class MakeSumDivisible {
public int minSubarray(int[] nums, int p) {
long totalSum = 0;
for (int num : nums) {
totalSum += num;
}

// Find remainder when total sum is divided by p
int rem = (int)(totalSum % p);
if (rem == 0) return 0; // If remainder is 0, no subarray needs to be removed

HashMap<Integer, Integer> prefixMod = new HashMap<>();
prefixMod.put(0, -1); // Initialize to handle full prefix
long prefixSum = 0;
int minLength = nums.length;

for (int i = 0; i < nums.length; ++i) {
prefixSum += nums[i];
int currentMod = (int)(prefixSum % p);
int targetMod = (currentMod - rem + p) % p;

if (prefixMod.containsKey(targetMod)) {
minLength = Math.min(minLength, i - prefixMod.get(targetMod));
}

prefixMod.put(currentMod, i);
}

return minLength == nums.length ? -1 : minLength;
}
}

0 comments on commit fa8ffee

Please sign in to comment.