Find the index of the array elements after performing given operations K times
Last Updated :
30 Jul, 2021
Given an array arr[] and an integer K, the task is to print the position of the array elements, where the ith value in the result is the index of the ith element in the original array after applying following operations exactly K times:
- Remove the first array element and decrement it by 1.
- If it is greater than 0 after decrementing, place it at the end of the array and shift the position of the elements to left.
Examples:
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}).
Approach: The idea is to use a Queue to simulate the K operations. Follow the steps below to solve the problem:
- Initialize a Queue to store the pairs of {arr[i], i}.
- Iterate over the range [0, K – 1] and perform the following operations:
- Pop the front element of the Queue and decrement its value by 1.
- Push the updated element back into the queue.
- Use the second member of the pair to print the position of the elements by popping out the elements until the queue is empty.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findElementPositions( int arr[], int N, int K)
{
queue<pair< int , int > > que;
for ( int i = 0; i < N; i++) {
que.push({ arr[i], i });
}
for ( int i = 0; i < K; i++) {
pair< int , int > value = que.front();
if (value.first == 1) {
que.pop();
}
else {
que.pop();
value.first -= 1;
que.push(value);
}
}
while (!que.empty()) {
pair< int , int > value = que.front();
que.pop();
cout << value.second << " " ;
}
}
int main()
{
int arr[] = { 3, 1, 3, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 4;
findElementPositions(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void findElementPositions( int arr[], int N,
int K)
{
ArrayDeque< int []> que = new ArrayDeque<>();
for ( int i = 0 ; i < N; i++) {
que.addLast( new int [] { arr[i], i });
}
for ( int i = 0 ; i < K; i++) {
int value[] = que.peekFirst();
if (value[ 0 ] == 1 ) {
que.pollFirst();
}
else {
que.pollFirst();
value[ 0 ] -= 1 ;
que.addLast(value);
}
}
while (!que.isEmpty())
{
int value[] = que.pollFirst();
System.out.print(value[ 1 ] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 3 , 1 , 3 , 2 };
int N = arr.length;
int K = 4 ;
findElementPositions(arr, N, K);
}
}
|
Python3
def findElementPositions(que, K):
for i in range ( len (que)):
que[i] = [que[i], i]
for i in range (K):
if que[ 0 ][ 0 ] = = 1 :
que.pop( 0 )
else :
temp = que.pop( 0 )
temp[ 0 ] - = 1
que.append(temp)
ans = [i[ 1 ] for i in que]
print (ans)
arr = [ 3 , 1 , 3 , 2 ]
K = 4
findElementPositions(arr, K)
|
C#
using System;
using System.Collections;
class GFG {
static void findElementPositions( int [] arr, int N, int K)
{
Queue que = new Queue();
for ( int i = 0; i < N; i++) {
que.Enqueue( new Tuple< int , int >(arr[i], i));
}
for ( int i = 0; i < K; i++) {
Tuple< int , int > value = (Tuple< int , int >)que.Peek();
if (value.Item1 == 1) {
que.Dequeue();
}
else {
que.Dequeue();
value = new Tuple< int , int >(value.Item1-1, value.Item2);
que.Enqueue(value);
}
}
Console.Write( "[" );
while (que.Count > 0) {
Tuple< int , int > value = (Tuple< int , int >)que.Peek();
que.Dequeue();
if (que.Count > 0)
{
Console.Write(value.Item2 + ", " );
}
else {
Console.Write(value.Item2);
}
}
Console.Write( "]" );
}
static void Main()
{
int [] arr = { 3, 1, 3, 2 };
int N = arr.Length;
int K = 4;
findElementPositions(arr, N, K);
}
}
|
Javascript
<script>
function findElementPositions(arr, N, K)
{
let que = [];
for (let i = 0; i < N; i++) {
que.push([ arr[i], i ]);
}
for (let i = 0; i < K; i++) {
let value = que[0];
if (value[0] == 1) {
que.shift();
}
else {
que.shift();
value[0] -= 1;
que.push(value);
}
}
document.write( "[" );
while (que.length > 0) {
let value = que[0];
que.shift();
if (que.length > 0)
{
document.write(value[1] + ", " );
}
else {
document.write(value[1]);
}
}
document.write( "]" );
}
let arr = [ 3, 1, 3, 2 ];
let N = arr.length;
let K = 4;
findElementPositions(arr, N, K);
</script>
|
Time Complexity: O(max(N, K))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...