Find the final sequence of the array after performing given operations
Given an array arr[] of size N, the task is to perform the following operation exactly N times,
Create an empty list of integers b[] and in the ith operation,
- Append arr[i] to the end of b[].
- Reverse the elements in b[].
Finally, print the contents of the list b[] after the end of all operations.
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: 4 2 1 3
Operation b[] 1 {1} 2 {2, 1} 3 {3, 1, 2} 4 {4, 2, 1, 3} Input: arr[] = {1, 2, 3}
Output: 3 1 2
Approach: We need some observations to solve this problem. Suppose the number of elements in the array is even. Say our array is {4, 8, 6, 1, 7, 9}.
| Operation | b[] |
|---|---|
| 1 | {4} |
| 2 | {8, 4} |
| 3 | {6, 4, 8} |
| 4 | {1, 8, 4, 6} |
| 5 | {7, 6, 4, 8, 1} |
| 6 | {9, 1, 8, 4, 6, 7} |
After carefully observing, we conclude that for even size of elements in the array the numbers which are at even positions (index 1 based) are reversed and added at the beginning and the numbers which are at the odd positions are kept in same order and added in the end.
While for odd-sized arrays, the elements at odd positions are reversed and added at the beginning while elements in the array at even positions are kept same and added in the end.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that generates the // array b[] when n is even void solveEven(int n, int* arr, int* b) { int left = n - 1; // Fill the first half of the final array // with reversed sequence for (int i = 0; i < (n / 2); ++i) { b[i] = arr[left]; left = left - 2; if (left < 0) break; } // Fill the second half int right = 0; for (int i = n / 2; i <= n - 1; ++i) { b[i] = arr[right]; right = right + 2; if (right > n - 2) break; } } // Function that generates the // array b[] when n is odd void solveOdd(int n, int* arr, int* b) { // Fill the first half of the final array // with reversed sequence int left = n - 1; for (int i = 0; i < (n / 2) + 1; ++i) { b[i] = arr[left]; left = left - 2; if (left < 0) break; } // Fill the second half int right = 1; for (int i = (n / 2) + 1; i <= n - 1; ++i) { b[i] = arr[right]; right = right + 2; if (right > n - 2) break; } } // Function to find the final array b[] // after n operations of given type void solve(int n, int* arr) { // Create the array b int b[n]; // If the array size is even if (n % 2 == 0) solveEven(n, arr, b); else solveOdd(n, arr, b); // Print the final array elements for (int i = 0; i <= n - 1; ++i) { cout << b[i] << " "; } } // Driver code int main() { int arr[] = { 1, 2, 3, 4 }; int n = sizeof(arr) / sizeof(arr[0]); solve(n, arr); return 0; } |
Java
// Java implementation of the approach import java.io.*; class GFG { // Function that generates the // array b[] when n is even static void solveEven(int n, int arr[], int b[]) { int left = n - 1; // Fill the first half of the final array // with reversed sequence for (int i = 0; i < (n / 2); ++i) { b[i] = arr[left]; left = left - 2; if (left < 0) break; } // Fill the second half int right = 0; for (int i = n / 2; i <= n - 1; ++i) { b[i] = arr[right]; right = right + 2; if (right > n - 2) break; } } // Function that generates the // array b[] when n is odd static void solveOdd(int n, int arr[], int b[]) { // Fill the first half of the final array // with reversed sequence int left = n - 1; for (int i = 0; i < (n / 2) + 1; ++i) { b[i] = arr[left]; left = left - 2; if (left < 0) break; } // Fill the second half int right = 1; for (int i = (n / 2) + 1; i <= n - 1; ++i) { b[i] = arr[right]; right = right + 2; if (right > n - 2) break; } } // Function to find the final array b[] // after n operations of given type static void solve(int n, int arr[]) { // Create the array b int b[] = new int[n]; // If the array size is even if (n % 2 == 0) solveEven(n, arr, b); else solveOdd(n, arr, b); // Print the final array elements for (int i = 0; i <= n - 1; ++i) { System.out.print( b[i] + " "); } } // Driver code public static void main (String[] args) { int []arr = { 1, 2, 3, 4 }; int n = arr.length; solve(n, arr); } } // This code is contributed by anuj_67.. |
Python3
# Python 3 implementation of the approach # Function that generates the # array b[] when n is even def solveEven(n, arr, b): left = n - 1 # Fill the first half of the final array # with reversed sequence for i in range((n // 2)): b[i] = arr[left] left = left - 2 if (left < 0): break # Fill the second half right = 0 for i in range(n // 2, n, 1): b[i] = arr[right] right = right + 2 if (right > n - 2): break # Function that generates the # array b[] when n is odd def solveOdd(n, arr, b): # Fill the first half of the final array # with reversed sequence left = n - 1 for i in range(n // 2 + 1): b[i] = arr[left] left = left - 2 if (left < 0): break # Fill the second half right = 1 for i in range(n // 2 + 1, n, 1): b[i] = arr[right] right = right + 2 if (right > n - 2): break # Function to find the final array b[] # after n operations of given type def solve(n, arr): # Create the array b b = [0 for i in range(n)] # If the array size is even if (n % 2 == 0): solveEven(n, arr, b) else: solveOdd(n, arr, b) # Print the final array elements for i in range(n): print(b[i], end = " ") # Driver code if __name__ == '__main__': arr = [1, 2, 3, 4] n = len(arr) solve(n, arr) # This code is contributed by # Surendra_Gangwar |
C#
// C# implementation of the approach using System; class GFG { // Function that generates the // array b[] when n is even static void solveEven(int n, int []arr, int []b) { int left = n - 1; // Fill the first half of the final array // with reversed sequence for (int i = 0; i < (n / 2); ++i) { b[i] = arr[left]; left = left - 2; if (left < 0) break; } // Fill the second half int right = 0; for (int i = n / 2; i <= n - 1; ++i) { b[i] = arr[right]; right = right + 2; if (right > n - 2) break; } } // Function that generates the // array b[] when n is odd static void solveOdd(int n, int []arr, int []b) { // Fill the first half of the final array // with reversed sequence int left = n - 1; for (int i = 0; i < (n / 2) + 1; ++i) { b[i] = arr[left]; left = left - 2; if (left < 0) break; } // Fill the second half int right = 1; for (int i = (n / 2) + 1; i <= n - 1; ++i) { b[i] = arr[right]; right = right + 2; if (right > n - 2) break; } } // Function to find the final array b[] // after n operations of given type static void solve(int n, int []arr) { // Create the array b int []b = new int[n]; // If the array size is even if (n % 2 == 0) solveEven(n, arr, b); else solveOdd(n, arr, b); // Print the final array elements for (int i = 0; i <= n - 1; ++i) { Console.Write( b[i] + " "); } } // Driver code public static void Main () { int []arr = { 1, 2, 3, 4 }; int n = arr.Length; solve(n, arr); } } // This code is contributed by anuj_67.. |
4 2 1 3
Efficient Approach:
The last Element of Array will be reversed only once. Last but one element will be reversed twice. Hence it goes to the last position in final result array ie b. Hence we can fill b array by iterating the original array from the end and placing elements at the not filled first index and not filled the last index. The same idea is implemented below.
Below is the implementation of the above approach:
Java
// Java implementation of the approach import java.util.*; import java.lang.*; import java.io.*; class GFG { static int[] solve(int[] arr, int n) { int[] b = new int[n]; int p = 0; for (int i = n - 1; i >= 0; i--) { b[p] = arr[i--]; if (i >= 0) b[n - 1 - p] = arr[i]; p++; } return b; } public static void main(String[] args) { int []arr = { 1, 2, 3, 4 }; int n = arr.length; int[] b = solve(arr, n); System.out.println(Arrays.toString(b)); } } // This code is contributed by Pramod Hosahalli |
C#
// C# implementation of the approach using System; class GFG { static int[] solve(int[] arr, int n) { int[] b = new int[n]; int p = 0; for (int i = n - 1; i >= 0; i--) { b[p] = arr[i--]; if (i >= 0) b[n - 1 - p] = arr[i]; p++; } return b; } // Driver Code public static void Main(String[] args) { int []arr = { 1, 2, 3, 4 }; int n = arr.Length; int[] b = solve(arr, n); Console.WriteLine("[" + String.Join(",", b) + "]"); } } // This code is contributed by Princi Singh |
[4, 2, 1, 3]
Recommended Posts:
- Find the modified array after performing k operations of given type
- Maximum Possible Product in Array after performing given Operations
- Maximum sum of all elements of array after performing given operations
- Check if at least half array is reducible to zero by performing some operations
- Number of elements from the array which are reachable after performing given operations on D
- Number of array elements derivable from D after performing certain operations
- Minimum element left from the array after performing given operations
- Maximum count of equal numbers in an array after performing given operations
- Find final value if we double after every successful search in array
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
- Find maximum value of the last element after reducing the array with given operations
- Find minimum length sub-array which has given sub-sequence in it
- Find minimum operations needed to make an Array beautiful
- Find minimum number of merge operations to make an array palindrome
- Find the number of operations required to make all array elements Equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



