VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-removals-required-such-that-sum-of-remaining-array-modulo-m-is-x/

⇱ Minimum removals required such that sum of remaining array modulo M is X - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum removals required such that sum of remaining array modulo M is X

Last Updated : 23 Jul, 2025

Given an array arr[] consisting of N positive integers and the integers X and M, where 0 <= X < M, the task is to find the minimum number of elements required to be removed such that sum of the remaining array modulo M is equal to X. Print -1 if not possible.

Examples: 

Input: arr[] = {3, 2, 1, 2}, M = 4, X = 2
Output: 1
Explanation: One of the elements at indices (0-based) 1 or 3 can be removed. If arr[1] is removed, then arr[] modifies to {3, 1, 2} and sum % M = 6 % 4 = 2 which is equal to X = 2.

Input: arr[] = {3, 2, 1, 3}, M = 4, X = 3
Output: 1
Explanation: Remove element arr[1]( = 2). Therefore, arr[] modifies to {3, 1, 3} and sum % M = 7 % 4 = 3 which is equal to X = 3. 

Naive Approach: The simplest approach is to generate all possible subsets of the given array and for each subset, check if sum modulo M of the array after removal of the subset is equal to X or not. If found to be true, store its size. Print minimum size among all such subsets obtained.

Time Complexity: O(2N) where N is the length of the given array.
Auxiliary Space: O(N)

Efficient Approach: To optimize the above approach, the idea is to use dynamic programming based on the following observations:

  • If S % M > X, then the minimum number of elements having sum S % M - X must be removed from the array to make the sum modulo M equal to X.
  • Otherwise, the minimum number of elements having sum S % M + M - X must be removed to make the sum modulo M equal to X.

Follow the steps below to solve the problem: 

  • Initialize a dp[] table, table[N + 1][X + 1] where table[i][j] represents the minimum number of elements to remove having indices in the range [0, i] such that their sum is j where X is the sum so be removed.
  • Initialize dp[0][i] for each i in the range [1, X] with some large value.
  • The dp transitions are as follows:

dp[i][j] = min(dp[i-1][j], dp[i][j-arr[i-1]]+1) 
where, i is in the range [1, N] and j is in the range [1, X].

  • Print dp[N][X] as the minimum elements to be removed.

Below is the implementation of the above approach:


Output
1

Time Complexity: O(N*X) where N is the length of the given array and X is the given integer.
Auxiliary Space: O(N*X) 

Efficient approach : Space optimization

In previous approach we use 2d matrix to store the computation of subproblems but the current computation is only depend upon the previous row and con current row so to optimize the space complexity we use a 1D vector of size X+1 to get the computation of previous row.

Implementation steps :

  • Create a vectors table of size x+1 to store previous row computation of matrix.
  • For setting the Base Case initialize the table with INT_MAX - 1.
  • Now iterate over subproblem and store the current with the help of table.
  • Update the current value of table previous computations.
  • At last check if answer exists and return table[x] else return -1.
     

Implementation:


Output
1

Time Complexity: O(N*X) 
Auxiliary Space: O(X) 

Comment