📌  相关文章
📜  在执行给定的操作K次之后,找到数组元素的索引

📅  最后修改于: 2021-05-17 19:01:16             🧑  作者: Mango

给定一个数组arr []和一个整数K ,任务是打印数组元素的位置,其中结果中的第i值是在精确地执行了K次以下操作后原始数组中第i元素的索引:

  • 删除第一个数组元素,并将其减1
  • 如果递减后它大于0 ,则将其放在数组的末尾,并将元素的位置向左移动。

例子:

方法:想法是使用队列来模拟K个操作。请按照以下步骤解决问题:

  1. 初始化一个队列,以存储成对的{arr [i],i}
  2. 迭代范围[0,K – 1]并执行以下操作:
    1. 弹出Queue的最前面的元素,并将其值减1
    2. 将更新后的元素推回队列。
  3. 使用该对中的第二个成员,通过弹出元素直到队列为空来打印元素的位置。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print position of array
// elements after performing given
// operations exactly K times
void findElementPositions(int arr[], int N, int K)
{
 
    // make the queue of pairs
    queue > que;
 
    // Convert the array
    // to queue of pairs
    for (int i = 0; i < N; i++) {
        que.push({ arr[i], i });
    }
 
    // Perform the operations
    // for K units of time
    for (int i = 0; i < K; i++) {
 
        // get the front pair
        pair value = que.front();
 
        // If the first element
        // value is one
        if (value.first == 1) {
            que.pop();
        }
 
        // Otherwise
        else {
            que.pop();
            value.first -= 1;
            que.push(value);
        }
    }
 
    // Print all the positions
    // after K operations
    while (!que.empty()) {
 
        pair value = que.front();
        que.pop();
 
        cout << value.second << " ";
    }
}
 
// Driven Program
int main()
{
   
    // Given array
    int arr[] = { 3, 1, 3, 2 };
 
    // Stores the length of array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given value of K
    int K = 4;
 
    // Function call
    findElementPositions(arr, N, K);
 
    return 0;
}
 
// This code is contributed by Kingash.


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to print position of array
  // elements after performing given
  // operations exactly K times
  static void findElementPositions(int arr[], int N,
                                   int K)
  {
 
    // make the queue of pairs
    ArrayDeque que = new ArrayDeque<>();
 
    // Convert the array
    // to queue of pairs
    for (int i = 0; i < N; i++) {
      que.addLast(new int[] { arr[i], i });
    }
 
    // Perform the operations
    // for K units of time
    for (int i = 0; i < K; i++) {
 
      // get the front pair
      int value[] = que.peekFirst();
 
      // If the first element
      // value is one
      if (value[0] == 1) {
        que.pollFirst();
      }
 
      // Otherwise
      else {
        que.pollFirst();
        value[0] -= 1;
        que.addLast(value);
      }
    }
 
    // Print all the positions
    // after K operations
    while (!que.isEmpty())
    {
 
      int value[] = que.pollFirst();
      System.out.print(value[1] + " ");
    }
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    // Given array
    int arr[] = { 3, 1, 3, 2 };
 
    // length of the array
    int N = arr.length;
 
    // Given value of K
    int K = 4;
 
    // Function call
    findElementPositions(arr, N, K);
  }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
# Function to print position of array
# elements after performing given
# operations exactly K times
def findElementPositions(que, K):
 
    # Convert the queue
    # to queue of pairs
    for i in range(len(que)):
        que[i] = [que[i], i]
 
    # Perform the operations
    # for K units of time
    for i in range(K):
 
        # If the first element
        # value is one
        if que[0][0] == 1:
            que.pop(0)
 
        # Otherwise
        else:
            temp = que.pop(0)
            temp[0] -= 1
            que.append(temp)
 
    # All the positions
    # after K operations
    ans = [i[1] for i in que]
 
    # Print the answer
    print(ans)
 
 
# Given array
arr = [3, 1, 3, 2]
 
# Given value of K
K = 4
 
findElementPositions(arr, K)


输出:
[0, 2, 3]

时间复杂度: O(max(N,K))
辅助空间: O(N)