Check possibility to make sum atleast X by making two symmetric Arrays
Last Updated :
15 Mar, 2023
Given two integers X and Y and an array Z[] of length N such that (X, Y, Z[], N ≥ 1). Two symmetrical arrays can be formed by using elements of Z[], then the task is to check if the total sum of both symmetrical arrays after adding with Y can be made greater than or equal to X or not.
Note: Two arrays are called symmetrical if they contain elements in the same frequency and the sum of the individual arrays is equal to each other. For example, A[] = {2, 2, 1, 3} and B[] = {3, 1, 2, 2} are symmetrical whereas X[] = {1, 1, 2} and Y[] = {2, 2} are not, also it is not necessary that both the arrays are strictly needed always. Formally, In some cases, no symmetrical arrays are needed. For example: If Y is already greater than X, then there is no need to form any two arrays to maximize Y, Just output in such cases YES.
Examples:
Input: N = 6, X = 10, Y = 2, Z[] = {1, 3, 2, 3, 1, 2}
Output: YES
Explanation:
- First Symmetrical Array A[] = {2, 3, 1}, Sum(A[]) = 6
- Second Symmetrical Array B[] = {3, 1, 2}, Sum(A[]) = 6
- Sum of Both arrays added with Y = 6 + 6 + 2 = 14
Total sum = 14, Which is greater than or equal to 10(Value of X). Therefore, output is YES.
Input: N = 7, X = 100, Y = 50, Z[] = {100, 10, 10, 10, 10, 10, 90}
Output: NO
Explanation: Two Optimal symmetrical arrays from Z[] can be A[] = {10, 10} and B[] = {10, 10}. Both gives total sum with Y as: 20 + 20 + 50 = 90. Which is less than X. Hence output is NO.
Approach: Implement the idea below to solve the problem
The problem can be solved by using the HashMap data structure and based on the frequency of elements in Z[].
Steps were taken to solve the problem:
- Create a HashMap let’s say map.
- Initialize the map with elements and their frequencies by traversing Z[].
- Create a temp variable and initialize it equal to Y, also run a loop for traversing on HashMap and follow the below-mentioned steps under the scope of loop:
- If (temp ≥ X), then break the loop.
- Else, If the frequency of the current element in map is greater than one and if the frequency is even then add (element*frequency) in the temp variable else add (frequency – 1)*element.
- If the value of temp is greater than or X then output YES else NO.
Below is the code to implement the approach:
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
long N = 6 ;
long X = 10 ;
long Y = 2 ;
long Z[] = { 1 , 3 , 2 , 2 , 1 , 3 };
Can_Maximize_to_X(N, X, Y, Z);
}
static void Can_Maximize_to_X( long N, long X, long Y,
long [] Z)
{
Map<Long, Long> Map = new HashMap<>();
for ( int j = 0 ; j < N; j++) {
long temp = Z[j];
if (Map.containsKey(temp))
Map.put(temp, Map.get(temp) + 1L);
else
Map.put(temp, 1L);
}
if (Y >= X) {
System.out.println( "YES" );
}
else {
long temp = Y;
for (Map.Entry<Long, Long> p : Map.entrySet()) {
if (temp >= X)
break ;
if (p.getValue() > 1 ) {
temp += (p.getValue() % 2 == 0 )
? p.getKey() * p.getValue()
: p.getKey()
* (p.getValue() - 1 );
}
}
if (temp >= X)
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
}
|
C++
#include <bits/stdc++.h>
using namespace std;
void Can_Maximize_to_X( long N, long X, long Y, long z[])
{
map< long , long > Map;
for ( int j = 0; j < N; j++) {
Map[z[j]]++;
}
if (Y >= X) {
cout << "YES" << endl;
}
else {
long temp = Y;
for ( auto & i : Map) {
int key = i.first;
int value = i.second;
if (temp >= X)
break ;
if (value > 1) {
temp += (value % 2 == 0) ? key * value : key * (value - 1);
}
}
if (temp >= X)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
int main() {
long N = 6;
long X = 10;
long Y = 2;
long Z[] = { 1, 3, 2, 2, 1, 3 };
Can_Maximize_to_X(N, X, Y, Z);
}
|
Javascript
function Can_Maximize_to_X( N, X, Y, z)
{
let map= new Map();
for (let j = 0; j < N; j++) {
if (map.has(z[j]))
map.set(z[j], map.get(z[j])+1);
else
map.set(z[j],1);
}
if (Y >= X) {
console.log( "YES" );
}
else {
let temp = Y;
for (const [key, value] of map.entries()) {
if (temp >= X)
break ;
if (value > 1) {
temp += (value % 2 == 0) ? key * value : key * (value - 1);
}
}
if (temp >= X)
console.log( "YES" );
else
console.log( "NO" );
}
}
let N = 6;
let X = 10;
let Y = 2;
let Z = [ 1, 3, 2, 2, 1, 3 ];
Can_Maximize_to_X(N, X, Y, Z);
|
Python3
def Can_Maximize_to_X(N, X, Y, Z):
Map = {}
for j in range (N):
temp = Z[j]
if temp in Map :
Map [temp] + = 1
else :
Map [temp] = 1
if Y > = X:
print ( "YES" )
else :
temp = Y
for p in Map .items():
if temp > = X:
break
if p[ 1 ] > 1 :
temp + = p[ 0 ] * p[ 1 ] if p[ 1 ] % 2 = = 0 else p[ 0 ] * (p[ 1 ] - 1 )
if temp > = X:
print ( "YES" )
else :
print ( "NO" )
if __name__ = = '__main__' :
N = 6
X = 10
Y = 2
Z = [ 1 , 3 , 2 , 2 , 1 , 3 ]
Can_Maximize_to_X(N, X, Y, Z)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void Can_Maximize_to_X( int N, int X,
int Y, int [] Z)
{
Dictionary< int , int > dict
= new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (dict.ContainsKey(Z[i]))
dict[Z[i]]++;
else
dict.Add(Z[i], 1);
}
if (Y >= X)
Console.WriteLine( "YES" );
else {
int temp = Y;
foreach (KeyValuePair< int , int > entry in dict)
{
int key = entry.Key;
int value = entry.Value;
if (temp >= X)
break ;
if (value > 1) {
if (value % 2 == 0)
temp += key * value;
else
temp += key * (value - 1);
}
}
if (temp >= X)
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
public static void Main()
{
int N = 6;
int X = 10;
int Y = 2;
int [] Z = new int [] { 1, 3, 2, 2, 1, 3 };
Can_Maximize_to_X(N, X, Y, Z);
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(N), As HashMap is used for storing
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...