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;
// 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);
}
Java
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);
}
}
Python3
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)
C#
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);
}
}
JavaScript
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);
Time Complexity : O(n), where n is the length of the array.
Auxiliary space : O(1).
Similar Reads
Reduce all array elements to zero by performing given operations thrice Given an array arr[] of size N, the task is to convert every array element to 0 by applying the following operations exactly three times: Select a subarray.Increment every element of the subarray by the integer multiple of its length. Finally, print the first and last indices of the subarray involve
9 min read
Find original array from given array which is obtained after P prefix reversals | Set-2 Given an array arr[] of size N and an integer P, the task is to find the original array from the array obtained by P prefix reversals where in ith reversal the prefix of size i of the array containing indices in range [0, i-1] was reversed. Note: P is less than or equal to N Examples: Input: arr[] =
7 min read
Array obtained by repeatedly reversing array after every insertion from given array Given an array arr[], the task is to print the array obtained by inserting elements of arr[] one by one into an initially empty array, say arr1[], and reversing the array arr1[] after every insertion. Examples: Input: arr[] = {1, 2, 3, 4}Output: 4 2 1 3Explanation:Operations performed on the array a
6 min read
Deleting Elements in an Array - Array Operations In this post, we will look into deletion operation in an Array, i.e., how to delete an element from an Array, such as:Delete an Element from the Beginning of an ArrayDelete an Element from a Given Position in an ArrayDelete First Occurrence of Given Element from an ArrayRemove All Occurrences of an
4 min read
Minimum p[i] = p[arr[i]] operations to regain the given Array Given an array, A[] (1 - indexed) of size 'N' which contains a permutation of [1, N], the task is to find the minimum number of operations to be applied on any array P[] to get back the original array P[]. The operation must be applied at least once. In each operation, for every index of P[] we set
12 min read