Given an array A[] of size N / 2, the task is to construct the array B[] of size N such that:
- B[] is sorted in non-decreasing order.
- A[i] = B[i] + B[n – i + 1].
Note: Array A[] is given in such a way that the answer is always possible.
Examples:
Input: A[] = {3, 4}
Output: 0 1 3 3Input: A[] = {4, 1}
Output: 0 0 1 4
Approach: Let’s present the following greedy approach. The numbers will be restored in pairs (B[0], B[n – 1]), (B[1], B[n – 2]) and so on. Thus, we can have some limits on the values of the current pair (satisfying the criteria about sorted result).
Initially, l = 0 and r = 109, they are updated with l = a[i] and r = a[n – i + 1]. Let l be minimal possible in the answer. Take a[i] = max(l, b[i] – r) and r = b[i] – l, that way l was chosen in such a way that both l and r are within the restrictions and l is also minimal possible.
If l was any greater than we would move both l limit up and r limit down leaving less freedom for later choices.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Utility function to print the contents of the array void printArr(int b[], int n) { for (int i = 0; i < n; i++) cout << b[i] << " "; } // Function to build array B[] void ModifiedArray(int a[], int n) { // Lower and upper limits int l = 0, r = INT_MAX; // To store the required array int b[n] = { 0 }; // Apply greedy approach for (int i = 0; i < n / 2; i++) { b[i] = max(l, a[i] - r); b[n - i - 1] = a[i] - b[i]; l = b[i]; r = b[n - i - 1]; } // Print the built array b[] printArr(b, n); } // Driver code int main() { int a[] = { 5, 6 }; int n = sizeof(a) / sizeof(a[0]); ModifiedArray(a, 2 * n); return 0; } |
Python3
# Python 3 implementation of the approach import sys # Utility function to print the # contents of the array def printArr(b, n): for i in range(0, n, 1): print(b[i], end = " ") # Function to build array B[] def ModifiedArray(a, n): # Lower and upper limits l = 0 r = sys.maxsize # To store the required array b = [0 for i in range(n)] # Apply greedy approach for i in range(0, int(n / 2), 1): b[i] = max(l, a[i] - r) b[n - i - 1] = a[i] - b[i] l = b[i] r = b[n - i - 1] # Print the built array b[] printArr(b, n) # Driver code if __name__ == '__main__': a = [5, 6] n = len(a) ModifiedArray(a, 2 * n) # This code is contributed by # Shashank_Sharma |
PHP
0 1 5 5
Recommended Posts:
- Minimum in an array which is first decreasing then increasing
- Sum of array elements that is first continuously increasing then decreasing
- Tidy Number (Digits in non-decreasing Order)
- Largest number smaller than or equal to n and digits in non-decreasing order
- Find minimum value to assign all array elements so that array product becomes greater
- Find element in array that divides all array elements
- Sort prime numbers of an array in descending order
- Minimum increment operations to make the array in increasing order
- Sort the array of strings according to alphabetical order defined by another string
- Find the count of Strictly decreasing Subarrays
- Find pair with maximum GCD in an array
- Find triplets in an array whose AND is maximum
- Find all good indices in the given Array
- Program to find sum of elements in a given array
- Find the most frequent digit without using array/string
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.
Improved By : Shashank_Sharma



