 Open in App
Not now

# Find the original array from given array obtained after P prefix reversals

• Last Updated : 03 Aug, 2022

Given an array arr[] of size N and an integer P (P < N), 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.

Examples:

Input: arr[] = {4, 2, 1, 3, 5, 6}, P = 4.
Output:  1 2 3 4 5 6
Explanation: {1, 2, 3, 4, 5, 6} on prefix reversal P = 1 converts to {1, 2, 3, 4, 5, 6}.
{1, 2, 3, 4, 5, 6} on prefix reversal P = 2 converts to {2, 1, 3, 4, 5, 6}.
{2, 1, 3, 4, 5, 6} on prefix reversal P = 3 converts to {3, 1, 2, 4, 5, 6}
{3, 1, 2, 4, 5, 6} on prefix reversal P = 4 converts to  {4, 2, 1, 3, 5, 6}
So answer is {1, 2, 3, 4, 5, 6}

Input: arr[] = {10, 9, 8, 3, 5, 6}, P = 3
Output: 9 8 10 3 5 6

Naive Approach: To solve the problem reverse the prefix of size i in each step for i in range [1, P] starting from the P sized prefix and then gradually decrementing the size.

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: This solution is based on two pointer approach. Since there is only P prefix reversals, the first P elements of the array only gets affected remaining remains the same. So a pattern can be observed for the original and the array after P prefix reversals. Only the first P elements should be modified. Follow these steps to solve the above problem:

• Initialize two variables l = 0 and r = P-1
• Initialize a vector res to store the modified prefix and index = 0 to keep track of elements at odd and even indices.
• Using a while loop iterate through the prefix of arr[].
• If the index is even push arr[l] into the vector res and increment l.
• Else push arr[r]  into the vector res and decrement r.
• Increment the index also.
• Now reverse the res and assign the modified prefix to the prefix of length p of arr.
• Print the original array.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `void` `find_original_array(``int` `arr[], ``int` `n, ``int` `p)` `{` `    ``// Initialize the r and l` `    ``int` `r = p - 1;` `    ``int` `l = 0;`   `    ``// Initialize index = 0` `    ``// to track elements at` `    ``// odd and even positions` `    ``int` `index = 0;` `    ``vector<``int``> res;`   `    ``while` `(l <= r) {`   `        ``// If index is even` `        ``if` `(index % 2 == 0) {` `            ``res.push_back(arr[l++]);` `        ``}`   `        ``// If index is odd` `        ``else` `{` `            ``res.push_back(arr[r--]);` `        ``}`   `        ``// Increment index` `        ``index = index + 1;` `    ``}`   `    ``// Reverse the res` `    ``reverse(res.begin(), res.end());`   `    ``// Assign the modified prefix to arr` `    ``for` `(``int` `i = 0; i < res.size(); i++) {` `        ``arr[i] = res[i];` `    ``}`   `    ``// Print the array arr` `    ``// which is the original array` `    ``// modified from the` `    ``// prefix reversed array` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``cout << arr[i] << ``" "``;` `    ``}` `}`   `// Driver code` `int` `main()` `{`   `    ``int` `arr[] = { 4, 2, 1, 3, 5, 6 }, P = 4;` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);`   `    ``// Function call` `    ``find_original_array(arr, n, P);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.*;` `public` `class` `GFG` `{` `  ``static` `void` `find_original_array(``int` `arr[], ``int` `n, ``int` `p)` `  ``{`   `    ``// Initialize the r and l` `    ``int` `r = p - ``1``;` `    ``int` `l = ``0``;`   `    ``// Initialize index = 0` `    ``// to track elements at` `    ``// odd and even positions` `    ``int` `index = ``0``;` `    ``ArrayList res = ``new` `ArrayList();`   `    ``while` `(l <= r) {`   `      ``// If index is even` `      ``if` `(index % ``2` `== ``0``) {` `        ``res.add(arr[l++]);` `      ``}`   `      ``// If index is odd` `      ``else` `{` `        ``res.add(arr[r--]);` `      ``}`   `      ``// Increment index` `      ``index = index + ``1``;` `    ``}`   `    ``// Reverse the res` `    ``Collections.reverse(res);`   `    ``// Assign the modified prefix to arr` `    ``for` `(``int` `i = ``0``; i < res.size(); i++) {` `      ``arr[i] = (``int``)res.get(i);` `    ``}`   `    ``// Print the array arr` `    ``// which is the original array` `    ``// modified from the` `    ``// prefix reversed array` `    ``for` `(``int` `i = ``0``; i < n; i++) {` `      ``System.out.print(arr[i] + ``" "``);` `    ``}` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main(String args[])` `  ``{`   `    ``int` `arr[] = { ``4``, ``2``, ``1``, ``3``, ``5``, ``6` `}, P = ``4``;` `    ``int` `n = arr.length;`   `    ``// Function call` `    ``find_original_array(arr, n, P);`   `  ``}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Python3

 `# Python program for the above approach` `def` `find_original_array(arr, n, p):`   `    ``# Initialize the r and l` `    ``r ``=` `p ``-` `1``;` `    ``l ``=` `0``;`   `    ``# Initialize index = 0` `    ``# to track elements at` `    ``# odd and even positions` `    ``index ``=` `0``;` `    ``res ``=` `[]`   `    ``while` `(l <``=` `r):`   `        ``# If index is even` `        ``if` `(index ``%` `2` `=``=` `0``):` `            ``res.append(arr[l]);` `            ``l ``+``=` `1``;` `        `  `        ``# If index is odd` `        ``else``:` `            ``res.append(arr[r]);` `            ``r ``-``=` `1``;` `        `  `        ``# Increment index` `        ``index ``=` `index ``+` `1``;` `    `  `    ``# Reverse the res` `    ``res.reverse();`   `    ``# Assign the modified prefix to arr` `    ``for` `i ``in` `range``(``len``(res)):` `        ``arr[i] ``=`  `res[i];` `    `  `    ``# Print array arr` `    ``# which is the original array` `    ``# modified from the` `    ``# prefix reversed array` `    ``for` `i ``in` `range``(n):` `        ``print``(arr[i], end``=``" "``);` `    `  `# Driver code` `if` `__name__ ``=``=` `'__main__'``:`   `    ``arr ``=` `[ ``4``, ``2``, ``1``, ``3``, ``5``, ``6` `]` `    ``P ``=` `4``;` `    ``n ``=` `len``(arr);`   `    ``# Function call` `    ``find_original_array(arr, n, P);`   `# This code is contributed by gauravrajput1 `

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Collections;`   `class` `GFG` `{` `  ``static` `void` `find_original_array(``int` `[]arr, ``int` `n, ``int` `p)` `  ``{`   `    ``// Initialize the r and l` `    ``int` `r = p - 1;` `    ``int` `l = 0;`   `    ``// Initialize index = 0` `    ``// to track elements at` `    ``// odd and even positions` `    ``int` `index = 0;` `    ``ArrayList res = ``new` `ArrayList();`   `    ``while` `(l <= r) {`   `      ``// If index is even` `      ``if` `(index % 2 == 0) {` `        ``res.Add(arr[l++]);` `      ``}`   `      ``// If index is odd` `      ``else` `{` `        ``res.Add(arr[r--]);` `      ``}`   `      ``// Increment index` `      ``index = index + 1;` `    ``}`   `    ``// Reverse the res` `    ``res.Reverse();`   `    ``// Assign the modified prefix to arr` `    ``for` `(``int` `i = 0; i < res.Count; i++) {` `      ``arr[i] = (``int``)res[i];` `    ``}`   `    ``// Print the array arr` `    ``// which is the original array` `    ``// modified from the` `    ``// prefix reversed array` `    ``for` `(``int` `i = 0; i < n; i++) {` `      ``Console.Write(arr[i] + ``" "``);` `    ``}` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main()` `  ``{`   `    ``int` `[]arr = { 4, 2, 1, 3, 5, 6 };` `    ``int` `P = 4;` `    ``int` `n = arr.Length;`   `    ``// Function call` `    ``find_original_array(arr, n, P);`   `  ``}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`1 2 3 4 5 6 `

Time Complexity: O(N) where N is the length of the array.
Auxiliary Space: O(P) as the maximum size of res can be equal to P only. In a condition, when P=N, auxiliary space can reach to O(N) too…..improved by Rajat.

My Personal Notes arrow_drop_up
Related Articles