content: package codingquestions.leetcode;
  
  /**
   * 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.
   * <p>
   * Return the length of the smallest subarray that you need to remove, or -1 if it's impossible.
   * <p>
   * A subarray is defined as a contiguous block of elements in the array.
   **/
  
  import java.util.HashMap;
  
  public 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;
      }
  }
  