# 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
**X**in the array.^{2}+ Y^{2}

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 = 3Output: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 = 4Output:395642

**Approach:** To maximize the value of final integer we have to maximize the value of **(X ^{2} + Y^{2})**. 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

**(X**in the priority queue. The last remaining element will be the maximum possible value of the required integer.

^{2}+ Y^{2})Below is the implementation of the above approach:

## C++

`// 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;` `}` |

## Java

`// Java implementation of the approach` `import` `java.util.*;` `class` `GFG` `{` ` ` `// Function to return the maximum` ` ` `// integer after performing the operations` ` ` `static` `long` `reduceOne(` `int` `N)` ` ` `{` ` ` `// To create Max-Heap` ` ` `PriorityQueue<Long> pq = ` `new` ` ` `PriorityQueue<Long>(Collections.reverseOrder());` ` ` `// Initialize priority queue with` ` ` `// 1 to N` ` ` `for` `(` `long` `i = ` `1` `; i <= N; i++)` ` ` `pq.add(i);` ` ` `// Perform the operations while` ` ` `// there are at least 2 elements` ` ` `while` `(pq.size() > ` `1` `)` ` ` `{` ` ` `// Get the maximum and` ` ` `// the second maximum` ` ` `long` `x = pq.poll();` ` ` `long` `y = pq.poll();` ` ` `// Push (x^2 + y^2)` ` ` `pq.add(x * x + y * y);` ` ` `}` ` ` `// Return the only element left` ` ` `return` `pq.peek();` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `N = ` `3` `;` ` ` `System.out.println(reduceOne(N));` ` ` `}` `}` `// This code is contributed by` `// sanjeev2552` |

## Python3

`# Python3 implementation of the approach` `# Function to return the maximum` `# integer after performing the operations` `def` `reduceOne(N) :` ` ` `pq ` `=` `[]` ` ` ` ` `# Initialize priority queue with` ` ` `# 1 to N` ` ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `) :` ` ` `pq.append(i)` ` ` `pq.sort()` ` ` `pq.reverse()` ` ` ` ` `# Perform the operations while` ` ` `# there are at least 2 elements` ` ` `while` `(` `len` `(pq) > ` `1` `) :` ` ` ` ` `# Get the maximum and` ` ` `# the second maximum` ` ` `x ` `=` `pq[` `0` `]` ` ` `pq.pop(` `0` `)` ` ` `y ` `=` `pq[` `0` `]` ` ` `pq.pop(` `0` `)` ` ` ` ` `# Push (x^2 + y^2)` ` ` `pq.append(x ` `*` `x ` `+` `y ` `*` `y)` ` ` `pq.sort()` ` ` `pq.reverse()` ` ` ` ` `# Return the only element left` ` ` `return` `pq[` `0` `]` ` ` ` ` `# Driver code` `N ` `=` `3` `print` `(reduceOne(N))` `# This code is contributed by divyeshrabadiya07.` |

## C#

`// C# implementation of the approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG {` ` ` `// Function to return the maximum` ` ` `// integer after performing the operations` ` ` `static` `int` `reduceOne(` `int` `N)` ` ` `{` ` ` `List<` `int` `> pq = ` `new` `List<` `int` `>();` ` ` `// Initialize priority queue with` ` ` `// 1 to N` ` ` `for` `(` `int` `i = 1; i <= N; i++)` ` ` `pq.Add(i);` ` ` `pq.Sort();` ` ` `pq.Reverse();` ` ` `// Perform the operations while` ` ` `// there are at least 2 elements` ` ` `while` `(pq.Count > 1) {` ` ` `// Get the maximum and` ` ` `// the second maximum` ` ` `int` `x = pq[0];` ` ` `pq.RemoveAt(0);` ` ` `int` `y = pq[0];` ` ` `pq.RemoveAt(0);` ` ` `// Push (x^2 + y^2)` ` ` `pq.Add(x * x + y * y);` ` ` `pq.Sort();` ` ` `pq.Reverse();` ` ` `}` ` ` `// Return the only element left` ` ` `return` `pq[0];` ` ` `}` ` ` `// Driver code` ` ` `static` `void` `Main()` ` ` `{` ` ` `int` `N = 3;` ` ` `Console.WriteLine(reduceOne(N));` ` ` `}` `}` `// This code is contributed by divyesh072019.` |

## Javascript

`<script>` ` ` `// Javascript implementation of the approach` ` ` ` ` `// Function to return the maximum` ` ` `// integer after performing the operations` ` ` `function` `reduceOne(N)` ` ` `{` ` ` `let pq = [];` ` ` `// Initialize priority queue with` ` ` `// 1 to N` ` ` `for` `(let i = 1; i <= N; i++)` ` ` `pq.push(i);` ` ` `pq.sort(` `function` `(a, b){` `return` `a - b});` ` ` `pq.reverse();` ` ` `// Perform the operations while` ` ` `// there are at least 2 elements` ` ` `while` `(pq.length > 1) {` ` ` `// Get the maximum and` ` ` `// the second maximum` ` ` `let x = pq[0];` ` ` `pq.shift();` ` ` `let y = pq[0];` ` ` `pq.shift();` ` ` `// Push (x^2 + y^2)` ` ` `pq.push(x * x + y * y);` ` ` `pq.sort(` `function` `(a, b){` `return` `a - b});` ` ` `pq.reverse();` ` ` `}` ` ` `// Return the only element left` ` ` `return` `pq[0];` ` ` `}` ` ` ` ` `let N = 3;` ` ` `document.write(reduceOne(N));` ` ` ` ` `// This code is contributed by suresh07.` `</script>` |

**Output:**

170