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
#include <bits/stdc++.h>
using namespace std;
int findIndex( int arr[], int n, int K)
{
int sum = 0;
int res = -1;
int mini = 1e9;
for ( int i = 0; i < n; i++) {
sum += arr[i];
}
for ( int i = 0; i < n; i++) {
int temp = sum - arr[i];
if (temp % K == 0) {
if (res == -1 || mini > arr[i]) {
res = i + 1;
mini = arr[i];
}
}
}
return res;
}
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;
}
|
Java
import java.util.*;
class GFG
{
static int findIndex( int arr[], int n, int K)
{
int sum = 0 ;
int res = - 1 ;
int mini = ( int ) 1e9;
for ( int i = 0 ; i < n; i++)
{
sum += arr[i];
}
for ( int i = 0 ; i < n; i++)
{
int temp = sum - arr[i];
if (temp % K == 0 )
{
if (res == - 1 || mini > arr[i])
{
res = i + 1 ;
mini = arr[i];
}
}
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 14 , 7 , 8 , 2 , 4 };
int K = 7 ;
int N = arr.length;
System.out.print(findIndex(arr, N, K));
}
}
|
Python3
def findIndex(arr, n, K) :
sum = 0
res = - 1
mini = 1e9
for i in range (n):
sum + = arr[i]
for i in range (n):
temp = sum - arr[i]
if (temp % K = = 0 ) :
if (res = = - 1 or mini > arr[i]) :
res = i + 1
mini = arr[i]
return res;
arr = [ 14 , 7 , 8 , 2 , 4 ]
K = 7
N = len (arr)
print (findIndex(arr, N, K))
|
C#
using System;
class GFG{
static int findIndex( int [] arr, int n, int K)
{
int sum = 0;
int res = -1;
int mini = ( int )1e9;
for ( int i = 0; i < n; i++)
{
sum += arr[i];
}
for ( int i = 0; i < n; i++)
{
int temp = sum - arr[i];
if (temp % K == 0)
{
if (res == -1 || mini > arr[i])
{
res = i + 1;
mini = arr[i];
}
}
}
return res;
}
static void Main()
{
int [] arr = { 14, 7, 8, 2, 4 };
int K = 7;
int N = arr.Length;
Console.WriteLine(findIndex(arr, N, K));
}
}
|
Javascript
<script>
function findIndex(arr,n,K)
{
let sum = 0;
let res = -1;
let mini = parseInt( 1e9);
for (let i = 0; i < n; i++)
{
sum += arr[i];
}
for (let i = 0; i < n; i++)
{
let temp = sum - arr[i];
if (temp % K == 0)
{
if (res == -1 || mini > arr[i])
{
res = i + 1;
mini = arr[i];
}
}
}
return res;
}
let arr = [ 14, 7, 8, 2, 4 ];
let K = 7;
let N = arr.length;
document.write(findIndex(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Jun, 2021
Like Article
Save Article