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
Explanation: 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.
Algorithm:
Step 1: Start
Step 2: Create a static function names reduceOne of long return type which take an integer N as input.
Step 3: Create a priority_queue of type long long int
Step 4: Now with the help of for loop initialize the priority queue from 1 to n.
Step 5: Loop while the size of priority_queue is greater than 1
Get the maximum element x from priority_queue and remove it
Get the second top element y from priority_queue and remove it
Step 6: Now calculate the value of x^2 + y^2 and add it to the priority queue
Step 7: Return the element left in the priority queue
Step 8: End
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int reduceOne( int N)
{
priority_queue<ll> pq;
for ( int i = 1; i <= N; i++)
pq.push(i);
while (pq.size() > 1) {
ll x = pq.top();
pq.pop();
ll y = pq.top();
pq.pop();
pq.push(x * x + y * y);
}
return pq.top();
}
int main()
{
int N = 3;
cout << reduceOne(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static long reduceOne( int N)
{
PriorityQueue<Long> pq = new
PriorityQueue<Long>(Collections.reverseOrder());
for ( long i = 1 ; i <= N; i++)
pq.add(i);
while (pq.size() > 1 )
{
long x = pq.poll();
long y = pq.poll();
pq.add(x * x + y * y);
}
return pq.peek();
}
public static void main(String[] args)
{
int N = 3 ;
System.out.println(reduceOne(N));
}
}
|
Python3
from heapq import heappop, heappush, heapify
def reduceOne(N) :
pq = []
for i in range ( 1 , N + 1 ):
pq.append(i)
for i in range ( 0 , N):
pq[i] = - 1 * pq[i];
heapify(pq)
while ( len (pq) > 1 ) :
x = heappop(pq)
y = heappop(pq)
heappush(pq, - 1 * (x * x + y * y))
return - 1 * heappop(pq)
if __name__ = = '__main__' :
N = 3
print (reduceOne(N))
|
Javascript
function reduceOne(N)
{
let pq = [];
for (let i = 1; i <= N; i++){
pq.push(i);
}
pq.sort();
while (pq.length > 1)
{
let x = pq.pop();
let y = pq.pop();
pq.push((x * x) +(y * y));
}
return pq.pop();
}
let N = 3;
console.log(reduceOne(N));
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int reduceOne( int N)
{
var pq = new List< int >();
for ( int i = 1; i <= N; i++)
{
pq.Add(i);
}
pq.Sort();
while (pq.Count > 1)
{
int x = pq[pq.Count - 1];
pq.RemoveAt(pq.Count - 1);
int y = pq[pq.Count - 1];
pq.RemoveAt(pq.Count - 1);
pq.Add(x * x + y * y);
pq.Sort();
}
return pq[0];
}
static void Main( string [] args)
{
int N = 3;
Console.WriteLine(reduceOne(N));
}
}
|
Time Complexity: O(N * logN)
Auxiliary Space: O(N)