The Wayback Machine - https://web.archive.org/web/20210126025548/https://www.geeksforgeeks.org/find-the-index-of-the-smallest-element-to-be-removed-to-make-sum-of-array-divisible-by-k/
Related Articles

Related Articles

Find the index of the smallest element to be removed to make sum of array divisible by K
  • Difficulty Level : Expert
  • Last Updated : 25 Jan, 2021

Given an array arr[] of size N and a positive integer K, the task is to find the index of the smallest array element required to be removed to make the sum of remaining array divisible by K. If multiple solutions exist, then print the smallest index. Otherwise, print -1.

Examples:

Input: arr[ ] = {6, 7, 5, 1}, K = 7
Output: 2
Explanation:
Removing arr[0] from arr[] modifies arr[] to { 7, 5, 1 }. Therefore, sum = 13
Removing arr[1] from arr[] modifies arr[] to { 6, 5, 1 }. Therefore, sum = 12
Removing arr[2] from arr[] modifies arr[] to { 6, 7, 1 }. Therefore, sum = 14
Since the sum (= 14) is divisible by K(= 7), the required output is the index 2.

Input: arr[ ] = {14, 7, 8, 2, 4}, K = 7
Output: 1

Naive Approach: The simplest approach to solve this problem is to traverse the array and calculate the sum by removing the current element from the array. If the obtained sum is divisible by K, then print the current index. Otherwise, insert the removed element into the array.
Time Complexity: O(N2)
Auxiliary Space: O(1)



Approach: The above approach can be optimized by precalculating the sum of the array. Finally, traverse the array and check if (sum – arr[i]) is divisible by K or not. If found to be true, then print the current index. Follow the steps below to solve the problem:

  • Calculate the sum of the array and store it in a variable say, sum.
  • Initialize two variables say, res and mini to store the index of the smallest element and the element such that removing the elements makes the sum divisible by K.
  • Traverse the array, and check if (sum – arr[i]) is divisible by K or not. If found to be true, then check if arr[i] is less than mini or not. If found to be true, then update mini = arr[i] and res = i.
  • Finally, print the res.

Below is the implementation of the above approach:

C++14

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find index of the smallest array element
// required to be removed to make sum divisible by K
int findIndex(int arr[], int n, int K)
{
  
    // Stores sum of array elements
    int sum = 0;
  
    // Stores index of the smallest element
    // removed from the array to make sum
    // divisible by K
    int res = -1;
  
    // Stores the smallest element removed
    // from the array to make sum divisible by K
    int mini = 1e9;
  
    // Traverse the array, arr[]
    for (int i = 0; i < n; i++) {
  
        // Update sum
        sum += arr[i];
    }
  
    // Traverse the array arr[]
    for (int i = 0; i < n; i++) {
  
        // Calculate remaining sum
        int temp = sum - arr[i];
  
        // If sum is divisible by K
        if (temp % K == 0) {
  
            // If res ==-1 or mini is greater
            // than arr[i]
            if (res == -1 || mini > arr[i]) {
  
                // Update res and mini
                res = i + 1;
                mini = arr[i];
            }
        }
    }
  
    return res;
}
  
// Driver Code
int main()
{
    int arr[] = { 14, 7, 8, 2, 4 };
    int K = 7;
    int N = sizeof(arr) / sizeof(arr[0]);
  
    cout << findIndex(arr, N, K);
  
    return 0;
}

chevron_right


Output:

2

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :