- Related Questions & Answers
- Check if the given array can be reduced to zeros with the given operation performed given number of times in Python
- Find the time which is palindromic and comes after the given time in Python
- Check if at least half array is reducible to zero by performing some operation in Python
- Count the numbers that can be reduced to zero or less in a gamein C++
- How to find out the PLANS which will be impacted if a index is dropped?
- Program to make all elements equal by performing given operation in Python
- Golang program to delete the ith index node, when the index is the last index in the linked list.
- Performing the subtraction operation without the subtraction operator in JavaScript
- Final string after performing given operations in C++
- Golang Program to update the ith index node value, when index is at the last index.
- Find the closest index to given value in JavaScript
- Program to find starting index of the child who receives last balloon in Python?
- Find the number of consecutive zero at the end after multiplying n numbers in Python
- Get the index of last substring in a given string in MySQL?
- Program to find the index of first Recurring Character in the given string in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an array A with n numbers and another input K, we have to find the index which will be the last to be reduced to zero after performing a given operation. The operation is explained as follows −

Starting from A[0] to A[N – 1], update each element as A[i] = A[i] – K. Now, if A[i] < K then put A[i] = 0 and no further operation will be done on A[i] once it is 0.

We have to repeat the operation until all of the elements are reduced to 0. And return the index which will be the last to become zero.

So, if the input is like A = [4, 3, 6, 8, 3, 10] and K = 4 , then the output will be 5 as operations are like below − Operation 1 − A = {0, 0, 2, 4, 0, 6} Operation 2 − A = {0, 0, 0, 0, 0, 2} Operation 3 − A = {0, 0, 0, 0, 0, 0}

To solve this, we will follow these steps −

n := size of A

idx := -1

for i in range 0 to n, do

A[i] :=(A[i] + k - 1) / k

for i in range 0 to n, do

if A[i] >= x, then

x := A[i]

idx := i

return idx

Let us see the following implementation to get better understanding −

def search_index(A, k): n = len(A) idx = -1 x = -10**9 for i in range(n): A[i] = (A[i] + k - 1) // k for i in range(n): if (A[i] >= x): x = A[i] idx = i return idx arr = [4, 3, 6, 8, 3, 10] K = 4 print(search_index(arr, K))

[4, 3, 6, 8, 3, 10], 4

5

Advertisements