-
Notifications
You must be signed in to change notification settings - Fork 29
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
1 parent
69dcb47
commit fa8ffee
Showing
1 changed file
with
48 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
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; | ||
} | ||
} |