Find initial sequence that produces a given Array by cyclic increments upto index P
Last Updated :
08 Sep, 2021
Given an array arr[] consisting of N elements and an integer P, the task is to find the initial array from which given arr[] is produced by the following operations:
- An element arr[i] from the initial array is selected. The ith index is reduced to 0.
- arr[i] indices are increased by 1 in a cyclic manner such that the last index to be incremented is P.
Examples:
Input: arr[] = {4, 3, 1, 6}, P = 4
Output: 3 2 5 4
Explanation:
The element arr[2] is chosen from the initial array. The following arr[i] operations modifies the given array in the following sequence:
{3, 2, 0, 4} -> {3, 2, 0, 5} -> {4, 2, 0, 5} -> {4, 3, 0, 5} -> {4, 3, 1, 5} -> {4, 3, 1, 6}
Input: arr[] = {3, 2, 0, 2, 7}, P = 2
Output: 2 1 4 1 6
Approach: The above problem can be solved using the below steps:
- Find the minimum element in the array and subtract min – 1 from every index.
- Now start subtracting 1 from the (P – 1)th index and repeat for all indices on the left in a cyclic manner until an index becomes 0.
- Add the number of operations to that index.
- The current state of arr[] gives the required initial state. Print the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findArray( int * a,
int n,
int P)
{
int mi = *min_element(a, a + n);
int ctr = 0;
mi = max(0, mi - 1);
for ( int i = 0; i < n; i++) {
a[i] -= mi;
ctr += mi;
}
int i = P - 1;
int start = -1;
while (1) {
if (a[i] == 0) {
start = i;
break ;
}
a[i] -= 1;
ctr += 1;
i = (i - 1 + n) % n;
}
a[start] = ctr;
for ( int i = 0; i < n; i++) {
cout << a[i] << ", " ;
}
}
int main()
{
int N = 5;
int P = 2;
int arr[] = { 3, 2, 0, 2, 7 };
findArray(arr, N, P);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findArray( int []a, int n,
int P)
{
int mi = Arrays.stream(a).min().getAsInt();
int ctr = 0 ;
mi = Math.max( 0 , mi - 1 );
for ( int i = 0 ; i < n; i++)
{
a[i] -= mi;
ctr += mi;
}
int i = P - 1 ;
int start = - 1 ;
while ( true )
{
if (a[i] == 0 )
{
start = i;
break ;
}
a[i] -= 1 ;
ctr += 1 ;
i = (i - 1 + n) % n;
}
a[start] = ctr;
for (i = 0 ; i < n; i++)
{
System.out.print(a[i] + ", " );
}
}
public static void main(String[] args)
{
int N = 5 ;
int P = 2 ;
int arr[] = { 3 , 2 , 0 , 2 , 7 };
findArray(arr, N, P);
}
}
|
Python3
def findArray(a, n, P):
mi = min (a)
ctr = 0
mi = max ( 0 , mi - 1 )
for i in range (n):
a[i] - = mi
ctr + = mi
i = P - 1
start = - 1
while ( 1 ):
if (a[i] = = 0 ):
start = i
break
a[i] - = 1
ctr + = 1
i = (i - 1 + n) % n
a[start] = ctr
print ( * a, sep = ', ' )
if __name__ = = '__main__' :
N = 5
P = 2
arr = [ 3 , 2 , 0 , 2 , 7 ]
findArray(arr, N, P)
|
C#
using System;
using System.Linq;
class GFG{
static void findArray( int []a, int n,
int P)
{
int mi = a.Min();
int ctr = 0;
mi = Math.Max(0, mi - 1);
int i;
for (i = 0; i < n; i++)
{
a[i] -= mi;
ctr += mi;
}
i = P - 1;
int start = -1;
while ( true )
{
if (a[i] == 0)
{
start = i;
break ;
}
a[i] -= 1;
ctr += 1;
i = (i - 1 + n) % n;
}
a[start] = ctr;
for (i = 0; i < n; i++)
{
Console.Write(a[i] + ", " );
}
}
public static void Main(String[] args)
{
int N = 5;
int P = 2;
int []arr = { 3, 2, 0, 2, 7 };
findArray(arr, N, P);
}
}
|
Javascript
<script>
function findArray(a, n, P)
{
let mi = Math.min(...a);
let ctr = 0;
mi = Math.max(0, mi - 1);
for (let i = 0; i < n; i++)
{
a[i] -= mi;
ctr += mi;
}
let i = P - 1;
let start = -1;
while ( true )
{
if (a[i] == 0)
{
start = i;
break ;
}
a[i] -= 1;
ctr += 1;
i = (i - 1 + n) % n;
}
a[start] = ctr;
for (i = 0; i < n; i++)
{
document.write(a[i] + ", " );
}
}
let N = 5;
let P = 2;
let arr = [ 3, 2, 0, 2, 7 ];
findArray(arr, N, P);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...