Find the index which is the last to be reduced to zero after performing a given operation
Last Updated :
29 May, 2022
Given an integer array arr[] of size N and an integer K, the task is to find the index which will be the last to be reduced to zero after performing a given operation. The operation is described as follows:
- Starting from arr[0] to arr[N – 1], update each element as arr[i] = arr[i] – K.
- If arr[i] < K then set arr[i] = 0 and no further operation will be performed on arr[i] once it is 0.
- Repeat the above steps till all the elements are reduced to 0.
Print the index which will be the last to become zero.
Examples:
Input: arr[] = { 3, 2, 5, 7, 2, 9 }, K = 4
Output: 5
Operation 1: arr[] = {0, 0, 1, 3, 0, 5}
Operation 2: arr[] = {0, 0, 0, 0, 0, 1}
Operation 3: arr[] = {0, 0, 0, 0, 0, 0}
Index 5 is the last to reduce.
Input: arr[] = { 31, 12, 25, 27, 32, 19 }, K = 5
Output: 4
Approach: At each step the element at a particular index is subtracted by K. So, a particular element takes ceil(arr[i] / K) or (arr[i] + K – 1) / K steps to reduce to zero. So the required index is given by the array index with maximum (arr[i] + K – 1)/K value. If the maximum value is present more than once then return the largest index as the operation is performed from 0 to N – 1.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
int findIndex( int a[], int n, int k)
{
int index = -1, max_ceil = INT_MIN;
for ( int i = 0; i < n; i++) {
a[i] = (a[i] + k - 1) / k;
}
for ( int i = 0; i < n; i++) {
if (a[i] >= max_ceil) {
max_ceil = a[i];
index = i;
}
}
return index;
}
int main()
{
int arr[] = { 31, 12, 25, 27, 32, 19 };
int K = 5;
int N = sizeof (arr) / sizeof (arr[0]);
cout << findIndex(arr, N, K);
return 0;
}
|
Java
import java .io.*;
class GFG
{
static int findIndex( int [] a, int n, int k)
{
int index = - 1 , max_ceil = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++)
{
a[i] = (a[i] + k - 1 ) / k;
}
for ( int i = 0 ; i < n; i++)
{
if (a[i] >= max_ceil)
{
max_ceil = a[i];
index = i;
}
}
return index;
}
static public void main (String[] args)
{
int []arr = { 31 , 12 , 25 , 27 , 32 , 19 };
int K = 5 ;
int N = arr.length ;
System.out.print(findIndex(arr, N, K));
}
}
|
Python
def findIndex(a, n, k):
index = - 1
max_ceil = - 10 * * 9
for i in range (n):
a[i] = (a[i] + k - 1 ) / / k
for i in range (n):
if (a[i] > = max_ceil):
max_ceil = a[i]
index = i
return index
arr = [ 31 , 12 , 25 , 27 , 32 , 19 ]
K = 5
N = len (arr)
print (findIndex(arr, N, K))
|
C#
using System;
class GFG
{
static int findIndex( int [] a, int n, int k)
{
int index = -1, max_ceil = int .MinValue;
for ( int i = 0; i < n; i++)
{
a[i] = (a[i] + k - 1) / k;
}
for ( int i = 0; i < n; i++)
{
if (a[i] >= max_ceil)
{
max_ceil = a[i];
index = i;
}
}
return index;
}
static public void Main ()
{
int []arr = { 31, 12, 25, 27, 32, 19 };
int K = 5;
int N = arr.Length ;
Console.WriteLine(findIndex(arr, N, K));
}
}
|
Javascript
<script>
function findIndex(a, n, k)
{
var index = -1, max_ceil = Number.MIN_VALUE;
for (i = 0; i < n; i++)
{
a[i] = (a[i] + k - 1) / k;
}
for (i = 0; i < n; i++)
{
if (a[i] >= max_ceil)
{
max_ceil = a[i];
index = i;
}
}
return index;
}
var arr = [ 31, 12, 25, 27, 32, 19 ];
var K = 5;
var N = arr.length ;
document.write(findIndex(arr, N, K));
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time
Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...