Reduce the array to a single integer with the given operation in C++


Given an integer variable Number as input. Let us consider an array containing elements in the range 1 to Number in any order. If we perform an operation Number-1 times on an array such that

  • We select two elements A and B from the array

  • Remove A and B from array

  • Add sum of squares of A and B to the array

We will get a single integer value in the end; the goal is to find the maximum possible value for that element.

Using Priority Queue

  • In order to maximize the end result, we will have to choose A and B such that they are maximum.

  • To find maximum A and B, we will use a priority queue to store values of elements inside it.

  • A priority queue stores elements in decreasing order.

  • Topmost element is the greatest in value and so on. So after popping two elements we will push their squares to the queue again.

  • Will pop and push Number - 1 times to get the desired result.

Examples

Input − Number=2

Output − Single element after array reduction: 5

Explanation − Suppose we have elements in array as [ 1 2]

After inserting in Priority Queue-: 2 1

A=5, B=4 : A2+B2=1+4=5

Last element: 5

Input − Number=5

Output − Single element after array reduction: 5

Explanation − Suppose we have elements in array as [ 5 1 2 4 3]

After inserting in Priority Queue-: 5 4 3 2 1

A=5, B=4 : A2+B2=25+16=41 : 41 3 2 1

A=41, B=3 : A2+B2=1681+9=1690 : 1690 2 1

A=1690, B=2 : A2+B2=1681+4=2856104 : 2856104 1

A=2856104 , B=1 : A2+B2=1187163712+1=1187163713 : 1187163713

Last element : 1187163713

Approach used in the below program is as follows

In this approach we will prioritize the queue to store elements of the array in decreasing order. Pop top two greatest elements and push sum of squares of both to that queue again. Do this until only a single value is left.

  • Take the input variable Number.

  • Take data type for results as long long integers - lli

  • Function reduceArray(int Num) takes the input number and returns the maximum single integer calculated using above operations.

  • Take a priority queue as pQueue.

  • Populate pQueue with numbers 1 to N using a while loop.

  • While i<=Num, push i to pQueue

  • Now pQueue has integers 1 to N in decreasing order and size is N.

  • Now traverse pQueue using a while loop till its size is >=1.

  • Take the maximum as var1=pQueue.top() and pop it.

  • Take the next maximum as var2=pQueue.top() and pop it.

  • Set var1 as its square and var2 as its square.

  • Push var1+var2 to pQueue again.

  • At the end of the while loop, return the top element.

  • Print the result inside main.

Example

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
int reduceArray(int Num){
   priority_queue<lli> pQueue;
   int i=1;
   while(i<=Num){
      pQueue.push(i);
      i=i+1;
   }
   while (pQueue.size() > 1) {
      lli var1 = pQueue.top();
      pQueue.pop();
      lli var2 = pQueue.top();
      pQueue.pop();
      var1=var1*var1;
      var2=var2*var2;
      pQueue.push(var1+var2);
   }
   return pQueue.top();
}
int main(){
   int Number = 5;
   cout<<"Single element after array reduction: "<<reduceArray(Number);
   return 0;
}

Output

If we run the above code it will generate the following Output

Single element after array reduction: 1187163713

Updated on: 03-Nov-2021

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements