Reduce the array to a single integer with the given operation
Given an array arr[] of N integers from 1 to N. The task is to perform the following operations N – 1 times.
- Select two elements X and Y from the array.
- Delete the chosen elements from the array.
- Add X2 + Y2in the array.
After performing above operations N – 1 times only one integer will be left in the array. The task is to print the maximum possible value of that integer.
Examples:
Input: N = 3
Output: 170
Initial array: arr[] = {1, 2, 3}
Choose 2 and 3 and the array becomes arr[] = {1, 13}
Performing the operation again by choosing the only two elements left,
the array becomes arr[] = {170} which is the maximum possible value.Input: N = 4
Output: 395642
Approach: To maximize the value of final integer we have to maximize the value of (X2 + Y2). So each time we have to choose the maximum two values from the array. Store all integers in a priority queue. Each time pop top 2 elements and push the result of (X2 + Y2) in the priority queue. The last remaining element will be the maximum possible value of the required integer.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define ll long long int // Function to return the maximum // integer after performing the operations int reduceOne(int N) { priority_queue<ll> pq; // Initialize priority queue with // 1 to N for (int i = 1; i <= N; i++) pq.push(i); // Perform the operations while // there are at least 2 elements while (pq.size() > 1) { // Get the maximum and // the second maximum ll x = pq.top(); pq.pop(); ll y = pq.top(); pq.pop(); // Push (x^2 + y^2) pq.push(x * x + y * y); } // Return the only element left return pq.top(); } // Driver code int main() { int N = 3; cout << reduceOne(N); return 0; } |
170
Recommended Posts:
- Find single in an array of 2n+1 integer elements
- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Minimizing array sum by applying XOR operation on all elements of the array
- Reduce every element of the array to it's half retaining the sum zero
- Minimum steps required to reduce all the elements of the array to zero
- Minimum number of given operations required to reduce the array to 0 element
- Maximizing array sum with given operation
- Move all zeroes to end of array | Set-2 (Using single traversal)
- Array element moved by k using single moves
- Sum of the updated array after performing the given operation
- Maximum possible array sum after performing the given operation
- Make the array non-decreasing with the given operation
- Constant time range add operation on an array
- Make all elements of an array equal with the given operation
- Minimum possible sum of array elements after performing the given operation
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.


