给定一个数组arr []和一个整数K ,任务是打印数组元素的位置,其中结果中的第i个值是在精确地执行了K次以下操作后原始数组中第i个元素的索引:
- 删除第一个数组元素,并将其减1 。
- 如果递减后它大于0 ,则将其放在数组的末尾,并将元素的位置向左移动。
例子:
Input: arr[] = {3, 1, 3, 2}, K = 4
Output: {0, 2, 3}
Explanation:
Operation 1 -> arr[] = {3, 1, 3, 2} (position {0, 1, 2, 3}) -> {1, 3, 2, 2} (position {1, 2, 3, 0}).
Operation 2 -> arr[] = {1, 3, 2, 2} (position {1, 2, 3, 0})-> {3, 2, 2} (position {2, 3, 0}), since the first element became zero.
Operation 3 -> arr[] = {3, 2, 2} (position {2, 3, 0}) -> {2, 2, 2} (position {3, 0, 2}).
Operation 4 -> ar[] = {2, 2, 2} (position {3, 0, 2}) -> {2, 2, 1} (position {0, 2, 3}).
Input: arr[] = {1, 2, 3}, K = 3
Output: {1, 2}
Explanation:
Operation 1 -> arr[] = {1, 2, 3} (position {0, 1, 2}) -> {2, 3} (position {1, 2}).
Operation 2 -> arr[] = {2, 3} (position {1, 2}) -> {3, 1} (position {2, 1}), since the first element became zero.
Operation 3 -> arr[] = {3, 1} (position {2, 1}) -> {1, 2} (position {1, 2}).
方法:想法是使用队列来模拟K个操作。请按照以下步骤解决问题:
- 初始化一个队列,以存储成对的{arr [i],i} 。
- 迭代范围[0,K – 1]并执行以下操作:
- 弹出Queue的最前面的元素,并将其值减1 。
- 将更新后的元素推回队列。
- 使用该对中的第二个成员,通过弹出元素直到队列为空来打印元素的位置。
下面是上述方法的实现:
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)