Find the index which is the last to be reduced to zero after performing a given operation | C ++ implementation of the approach ; Function that returns the index which will be the last to become zero after performing given operation ; Initialize the result ; Finding the ceil value of each index ; Finding the index with maximum ceil value ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }