Restore the original array from another array generated by given operations
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 thann
. 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 updatelastindex
accordingly, moving the calculated distance steps clockwise in the array.
- Inside the loop, check if the element at the current
- 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:
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
void solve(int n, int k, vector<int> arr)
{
bool flag = true;
// Initialize 'lastindex' to the index of the last
// element in the array
int lastindex = n - 1;
// Iterate for a maximum of min(n, k) times to prevent
// exceeding array bounds
for (int i = 0; i < min(n, k); i++) {
// Check if the element at the current 'lastindex'
// is greater than n
if (arr[lastindex] > n) {
// If true, set 'flag' to false and break out of
// the loop
flag = false;
break;
}
// Calculate the distance needed to move 'lastindex'
// to a new position based on the current element
int dist = n - arr[lastindex];
// Update 'lastindex' accordingly, moving dist steps
// clockwise in the array
lastindex = (lastindex + dist) % n;
}
// Output the result based on the value of 'flag'
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
// Driver Code
int main()
{
int n = 5, k = 3;
vector<int> arr = { 4, 3, 3, 2, 3 };
solve(n, k, arr);
}
using namespace std;
void solve(int n, int k, vector<int> arr)
{
bool flag = true;
// Initialize 'lastindex' to the index of the last
// element in the array
int lastindex = n - 1;
// Iterate for a maximum of min(n, k) times to prevent
// exceeding array bounds
for (int i = 0; i < min(n, k); i++) {
// Check if the element at the current 'lastindex'
// is greater than n
if (arr[lastindex] > n) {
// If true, set 'flag' to false and break out of
// the loop
flag = false;
break;
}
// Calculate the distance needed to move 'lastindex'
// to a new position based on the current element
int dist = n - arr[lastindex];
// Update 'lastindex' accordingly, moving dist steps
// clockwise in the array
lastindex = (lastindex + dist) % n;
}
// Output the result based on the value of 'flag'
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
// Driver Code
int main()
{
int n = 5, k = 3;
vector<int> arr = { 4, 3, 3, 2, 3 };
solve(n, k, arr);
}
public class SolveProblem {
static void solve(int n, int k, int[] arr) {
// Initialize 'flag' to true
boolean flag = true;
// Initialize 'lastIndex' to the index of the last
// element in the array
int lastIndex = n - 1;
// Iterate for a maximum of min(n, k) times to prevent
// exceeding array bounds
for (int i = 0; i < Math.min(n, k); i++) {
// Check if the element at the current 'lastIndex'
// is greater than n
if (arr[lastIndex] > n) {
// If true, set 'flag' to false and break out of
// the loop
flag = false;
break;
}
// Calculate the distance needed to move 'lastIndex'
// to a new position based on the current element
int dist = n - arr[lastIndex];
// Update 'lastIndex' accordingly, moving dist steps
// clockwise in the array
lastIndex = (lastIndex + dist) % n;
}
// Output the result based on the value of 'flag'
if (flag) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
// Driver Code
public static void main(String[] args) {
int n = 5;
int k = 3;
int[] arr = {4, 3, 3, 2, 3};
solve(n, k, arr);
}
}
def solve(n, k, arr):
# Initialize 'flag' to True
flag = True
# Initialize 'lastindex' to the index of the last
# element in the array
lastindex = n - 1
# Iterate for a maximum of min(n, k) times to prevent
# exceeding array bounds
for i in range(min(n, k)):
# Check if the element at the current 'lastindex'
# is greater than n
if arr[lastindex] > n:
# If true, set 'flag' to False and break out of
# the loop
flag = False
break
# Calculate the distance needed to move 'lastindex'
# to a new position based on the current element
dist = n - arr[lastindex]
# Update 'lastindex' accordingly, moving dist steps
# clockwise in the array
lastindex = (lastindex + dist) % n
# Output the result based on the value of 'flag'
if flag:
print("Yes")
else:
print("No")
# Driver Code
n = 5
k = 3
arr = [4, 3, 3, 2, 3]
solve(n, k, arr)
using System;
using System.Collections.Generic;
class Program
{
static void Solve(int n, int k, List<int> arr)
{
bool flag = true;
// Initialize 'lastindex' to the index of the last
// element in the array
int lastindex = n - 1;
// Iterate for a maximum of Math.Min(n, k) times to prevent
// exceeding array bounds
for (int i = 0; i < Math.Min(n, k); i++)
{
// Check if the element at the current 'lastindex'
// is greater than n
if (arr[lastindex] > n)
{
// If true, set 'flag' to false and break out of
// the loop
flag = false;
break;
}
// Calculate the distance needed to move 'lastindex'
// to a new position based on the current element
int dist = n - arr[lastindex];
// Update 'lastindex' accordingly, moving dist steps
// clockwise in the array
lastindex = (lastindex + dist) % n;
}
// Output the result based on the value of 'flag'
if (flag)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
// Driver Code
static void Main()
{
int n = 5, k = 3;
List<int> arr = new List<int> { 4, 3, 3, 2, 3 };
Solve(n, k, arr);
}
}
function solve(n, k, arr) {
let flag = true;
// Initialize 'lastindex' to the index of the last
// element in the array
let lastindex = n - 1;
// Iterate for a maximum of min(n, k) times to prevent
// exceeding array bounds
for (let i = 0; i < Math.min(n, k); i++) {
// Check if the element at the current 'lastindex'
// is greater than n
if (arr[lastindex] > n) {
// If true, set 'flag' to false and break out of
// the loop
flag = false;
break;
}
// Calculate the distance needed to move 'lastindex'
// to a new position based on the current element
let dist = n - arr[lastindex];
// Update 'lastindex' accordingly, moving dist steps
// clockwise in the array
lastindex = (lastindex + dist) % n;
}
// Output the result based on the value of 'flag'
if (flag)
console.log("Yes");
else
console.log("No");
}
// Driver Code
let n = 5, k = 3;
let arr = [4, 3, 3, 2, 3];
solve(n, k, arr);
Output
Yes
Time Complexity : O(n), where n is the length of the array.
Auxiliary space : O(1).