给定一个包含从1到N的N 个整数的数组arr[] 。任务是执行以下操作N – 1次。
- 从数组中选择两个元素X和Y。
- 从数组中删除所选元素。
- 在数组中添加X 2 + Y 2 。
执行上述操作N – 1次后,数组中将只剩下一个整数。任务是打印该整数的最大可能值。
例子:
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
方法:为了最大化最终整数的值,我们必须最大化(X 2 + Y 2 ) 的值。所以每次我们必须从数组中选择最大的两个值。将所有整数存储在优先级队列中。每次弹出前 2 个元素并将(X 2 + Y 2 )的结果推送到优先级队列中。最后剩余的元素将是所需整数的最大可能值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define ll long long int
// Function to return the maximum
// integer after performing the operations
int reduceOne(int N)
{
priority_queue 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 pq = new
PriorityQueue(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 pq = new List();
// 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
输出:
170
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。