Restore the original array from another array generated by given operations
Last Updated :
05 Feb, 2024
Given an array b. The array b is obtained initially by array a, by doing the following operations.
- Choose a fixed point x from array a.
- Cyclically rotate the array a to the left exactly x times.
Fixed point x is that point where (a[i] = i ). These operations are performed on the array a k times, determine if you will be able to restore the array a given array b.
Examples:
Input : n = 5 , k = 3 , b = { 4,3, 3, 2, 3 }
Output: Yes
Explanation: The array ‘a’ was initially given as [3, 2, 3, 4, 3]. In the first operation, a specific position, denoted as x=2, was selected. After performing 2 left shifts at this position, the array transformed into [3, 4, 3, 3, 2]. For the second operation, a different fixed point x=3 was chosen, and executing 3 left shifts resulted in the array [3, 2, 3, 4, 3]. Subsequently, in the third operation, the same fixed point x=3 was selected, and after 3 left shifts, the array transformed into [4, 3, 3, 2, 3]. Notably, this final array is equivalent to the array ‘b‘.
Input : n = 5 , k = 5 , b = { 6, 1, 1, 1, 1 }
Output : No
Explanation : the size of the array is 5 and we are given with 6 as our array element, and our condition specifically says a[i] = i , hence 6 can never satisfy our condition for the cyclic rotation of array.
Approach:
The Idea here is that the last element will always be the one which was our fixed point one step before and now is at the last because we cyclically rotated the array towards left which results in the fixed point array element being at the last of the array. If we try to right rotate the array k times then we will surely be getting our original array a, but if we found and element at the end whose value is greater then the size of the array , then we stop and report that we cannot generate our array a, else we continue right shifting k times. Since right shifting can be a time consuming task we just shift the last index pointer in our array, and % remainder modulus it so that it does not goes out of bounds from the indexes.
Follow the steps to solve the above problem:
- Begin by setting a boolean flag to true and initializing the variable
lastindex
to the index of the last element in the array ‘b‘.
- Iterate for a maximum of
min(n, k)
times.
- Inside the loop, check if the element at the current
lastindex
is greater than n
. If true, set the flag to false and break out of the loop.
- Otherwise, calculate the distance needed to move
lastindex
to a new position based on the current element and update lastindex
accordingly, moving the calculated distance steps clockwise in the array.
- Finally, based on the value of the flag, output “Yes” if the array ‘a‘ can be restored or “No” otherwise.
Below is the implementation of above approach:
C++14
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
void solve( int n, int k, vector< int > arr)
{
bool flag = true ;
int lastindex = n - 1;
for ( int i = 0; i < min(n, k); i++) {
if (arr[lastindex] > n) {
flag = false ;
break ;
}
int dist = n - arr[lastindex];
lastindex = (lastindex + dist) % n;
}
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
int n = 5, k = 3;
vector< int > arr = { 4, 3, 3, 2, 3 };
solve(n, k, arr);
}
|
Java
public class SolveProblem {
static void solve( int n, int k, int [] arr) {
boolean flag = true ;
int lastIndex = n - 1 ;
for ( int i = 0 ; i < Math.min(n, k); i++) {
if (arr[lastIndex] > n) {
flag = false ;
break ;
}
int dist = n - arr[lastIndex];
lastIndex = (lastIndex + dist) % n;
}
if (flag) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
public static void main(String[] args) {
int n = 5 ;
int k = 3 ;
int [] arr = { 4 , 3 , 3 , 2 , 3 };
solve(n, k, arr);
}
}
|
Python3
def solve(n, k, arr):
flag = True
lastindex = n - 1
for i in range ( min (n, k)):
if arr[lastindex] > n:
flag = False
break
dist = n - arr[lastindex]
lastindex = (lastindex + dist) % n
if flag:
print ( "Yes" )
else :
print ( "No" )
n = 5
k = 3
arr = [ 4 , 3 , 3 , 2 , 3 ]
solve(n, k, arr)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void Solve( int n, int k, List< int > arr)
{
bool flag = true ;
int lastindex = n - 1;
for ( int i = 0; i < Math.Min(n, k); i++)
{
if (arr[lastindex] > n)
{
flag = false ;
break ;
}
int dist = n - arr[lastindex];
lastindex = (lastindex + dist) % n;
}
if (flag)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static void Main()
{
int n = 5, k = 3;
List< int > arr = new List< int > { 4, 3, 3, 2, 3 };
Solve(n, k, arr);
}
}
|
Javascript
function solve(n, k, arr) {
let flag = true ;
let lastindex = n - 1;
for (let i = 0; i < Math.min(n, k); i++) {
if (arr[lastindex] > n) {
flag = false ;
break ;
}
let dist = n - arr[lastindex];
lastindex = (lastindex + dist) % n;
}
if (flag)
console.log( "Yes" );
else
console.log( "No" );
}
let n = 5, k = 3;
let arr = [4, 3, 3, 2, 3];
solve(n, k, arr);
|
Time Complexity : O(n), where n is the length of the array.
Auxiliary space : O(1).
Share your thoughts in the comments
Please Login to comment...