Find the index which is the last to be reduced to zero after performing a given operation
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
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns the index // which will be the last to become // zero after performing given operation int findIndex(int a[], int n, int k) { // Initialize the result int index = -1, max_ceil = INT_MIN; for (int i = 0; i < n; i++) { // Finding the ceil value // of each index a[i] = (a[i] + k - 1) / k; } for (int i = 0; i < n; i++) { // Finding the index with // maximum ceil value if (a[i] >= max_ceil) { max_ceil = a[i]; index = i; } } return index; } // Driver code 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
// Java implementation of the approach import java .io.*; class GFG { // Function that returns the index // which will be the last to become // zero after performing given operation static int findIndex(int[] a, int n, int k) { // Initialize the result int index = -1, max_ceil = Integer.MIN_VALUE; for (int i = 0; i < n; i++) { // Finding the ceil value // of each index a[i] = (a[i] + k - 1) / k; } for (int i = 0; i < n; i++) { // Finding the index with // maximum ceil value if (a[i] >= max_ceil) { max_ceil = a[i]; index = i; } } return index; } // Driver code 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)); } } // This code is contributed by anuj_67.. |
Python
# Python implementation of the approach # Function that returns the index # which will be the last to become # zero after performing given operation def findIndex(a, n, k): # Initialize the result index = -1 max_ceil = -10**9 for i in range(n): # Finding the ceil value # of each index a[i] = (a[i] + k - 1) // k for i in range(n): # Finding the index with # maximum ceil value if (a[i] >= max_ceil): max_ceil = a[i] index = i return index # Driver code arr = [31, 12, 25, 27, 32, 19] K = 5N = len(arr) print(findIndex(arr, N, K)) # This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approach using System; class GFG { // Function that returns the index // which will be the last to become // zero after performing given operation static int findIndex(int[] a, int n, int k) { // Initialize the result int index = -1, max_ceil = int.MinValue; for (int i = 0; i < n; i++) { // Finding the ceil value // of each index a[i] = (a[i] + k - 1) / k; } for (int i = 0; i < n; i++) { // Finding the index with // maximum ceil value if (a[i] >= max_ceil) { max_ceil = a[i]; index = i; } } return index; } // Driver code 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)); } } // This code is contributed by AnkitRai01 |
4
Recommended Posts:
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Minimum possible sum of array elements after performing the given operation
- Sub-string that contains all lowercase alphabets after performing the given operation
- Check if the last element of array is even or odd after performing a operation p times
- Find the final sequence of the array after performing given operations
- Find the modified array after performing k operations of given type
- Find average of two numbers using bit operation
- Find the number of different numbers in the array after applying the given operation q times
- Product of given N fractions in reduced form
- Count the numbers that can be reduced to zero or less in a game
- Largest number N which can be reduced to 0 in K steps
- Find the index of first 1 in a sorted array of 0's and 1's
- Find sum of even index binomial coefficients
- Convert an array to reduced form | Set 2 (Using vector of pairs)
- Find the index of first 1 in an infinite sorted array of 0s and 1s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



