📌  相关文章
📜  查找执行给定操作后最后减少为零的索引

📅  最后修改于: 2021-04-23 07:59:34             🧑  作者: Mango

给定大小为N的整数数组arr []和整数K ,任务是找到执行给定操作后将最后减少为零的索引。操作说明如下:

  • arr [0]arr [N – 1] ,将每个元素更新为arr [i] = arr [i] – K。
  • 如果ARR [I] 然后设置ARR [I] = 0并且没有进一步的操作将上ARR进行[I]一旦它是0。
  • 重复上述步骤,直到所有元素都减小为0为止。

打印将成为最后一个变为零的索引。
例子:

方法:在每个步骤中,将特定索引处的元素减去K。因此,特定元素需要ceil(arr [i] / K)(arr [i] + K – 1)/ K步减少为零。因此,所需的索引由具有最大(arr [i] + K – 1)/ K值的数组索引给出。如果存在的最大值不止一次,则在从0N – 1的操作中返回最大的索引。
下面是上述方法的实现:

CPP
// C++ implementation of the approach
#include 
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 = 5
N = 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


Javascript


输出:
4