GFG App
Open App
Browser
Continue

# Check possibility to make sum atleast X by making two symmetric Arrays

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

 `// Java code to implement the approach`   `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;`   `class` `GFG {`   `    ``// Driver Function` `    ``public` `static` `void` `main(String[] args)` `        ``throws` `java.lang.Exception` `    ``{`   `        ``// Inputs` `        ``long` `N = ``6``;` `        ``long` `X = ``10``;` `        ``long` `Y = ``2``;` `        ``long` `Z[] = { ``1``, ``3``, ``2``, ``2``, ``1``, ``3` `};`   `        ``// Function call` `        ``Can_Maximize_to_X(N, X, Y, Z);` `    ``}`   `    ``// Method for checking is it possible` `    ``// to maximize Y to >= X` `    ``static` `void` `Can_Maximize_to_X(``long` `N, ``long` `X, ``long` `Y,` `                                  ``long``[] Z)` `    ``{`   `        ``// Map initialized for counting` `        ``// frequencies of elements in Z[]` `        ``Map Map = ``new` `HashMap<>();`   `        ``// Loop for traversing Z[] and` `        ``// initialize map with frequencies` `        ``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);` `        ``}`   `        ``// Condition when already Y >= X,` `        ``// Formally no need of symmetrical` `        ``// arrays to maximize Y` `        ``if` `(Y >= X) {` `            ``System.out.println(``"YES"``);` `        ``}` `        ``else` `{`   `            ``// Temporary variable to` `            ``// store value of Y` `            ``long` `temp = Y;`   `            ``// Loop for traversing` `            ``// HashMap` `            ``for` `(Map.Entry p : Map.entrySet()) {`   `                ``// Condition when Y's sum` `                ``// is >=X after adding sum` `                ``// of both symmetrical arrays` `                ``if` `(temp >= X)` `                    ``break``;`   `                ``// If an element found such` `                ``// that its frequency is` `                ``// greater than 1` `                ``if` `(p.getValue() > ``1``) {`   `                    ``// If frequency is even` `                    ``// then we are taking` `                    ``// even contribution` `                    ``// in Y else contributing` `                    ``// (frequency-1)*element` `                    ``temp += (p.getValue() % ``2` `== ``0``)` `                                ``? p.getKey() * p.getValue()` `                                ``: p.getKey()` `                                      ``* (p.getValue() - ``1``);` `                ``}` `            ``}`   `            ``// Checking after adding sum` `            ``// of symmetrical arrays in Y,` `            ``// Y is >=X or not` `            ``if` `(temp >= X)` `                ``System.out.println(``"YES"``);` `            ``else` `                ``System.out.println(``"NO"``);` `        ``}` `    ``}` `}`

## C++

 `// C++ code to implement the approach` `#include ` `using` `namespace` `std;`   `// Method for checking is it possible to maximize Y to >= X` `void` `Can_Maximize_to_X(``long` `N, ``long` `X, ``long` `Y, ``long` `z[])` `    ``{` `        ``// Map initialized for counting` `        ``// frequencies of elements in Z[]` `        ``map<``long``, ``long``> Map;`   `        ``// Loop for traversing Z[] and` `        ``// initialize map with frequencies` `        ``for` `(``int` `j = 0; j < N; j++) {` `            ``Map[z[j]]++;` `        ``}`   `        ``// Condition when already Y >= X,` `        ``// Formally no need of symmetrical` `        ``// arrays to maximize Y` `        ``if` `(Y >= X) {` `            ``cout << ``"YES"` `<< endl;` `        ``}` `        ``else` `{`   `            ``// Temporary variable to` `            ``// store value of Y` `            ``long` `temp = Y;`   `            ``// Loop for traversing Map` `            ``for` `(``auto``& i : Map) {` `                `  `                  ``int` `key = i.first;` `                  ``int` `value = i.second;` `                ``// Condition when Y's sum` `                ``// is >=X after adding sum` `                ``// of both symmetrical arrays` `                ``if` `(temp >= X)` `                    ``break``;`   `                ``// If an element found such` `                ``// that its frequency is` `                ``// greater than 1` `                ``if` `(value > 1) {`   `                    ``// If frequency is even` `                    ``// then we are taking` `                    ``// even contribution` `                    ``// in Y else contributing` `                    ``// (frequency-1)*element` `                    ``temp += (value % 2 == 0) ? key * value : key * (value - 1);` `                ``}` `            ``}`   `            ``// Checking after adding sum` `            ``// of symmetrical arrays in Y,` `            ``// Y is >=X or not` `            ``if` `(temp >= X)` `                ``cout << ``"YES"` `<< endl;` `            ``else` `                ``cout << ``"NO"` `<< endl;` `        ``}` `    ``}`   `int` `main() {`   `    ``// Inputs` `    ``long` `N = 6;` `    ``long` `X = 10;` `    ``long` `Y = 2;` `    ``long` `Z[] = { 1, 3, 2, 2, 1, 3 };`   `    ``// Function call` `    ``Can_Maximize_to_X(N, X, Y, Z);` `}`   `// This code is contributed by Rahul Bhardwaj 🙂`

## Javascript

 `// JavaScript code to implement the approach`   `// Method for checking is it possible to maximize Y to >= X` `function` `Can_Maximize_to_X( N, X, Y, z)` `{` `    ``// Map initialized for counting` `    ``// frequencies of elements in Z[]` `    ``let map=``new` `Map();`   `    ``// Loop for traversing Z[] and` `    ``// initialize map with frequencies` `    ``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);` `    ``}`   `    ``// Condition when already Y >= X,` `    ``// Formally no need of symmetrical` `    ``// arrays to maximize Y` `    ``if` `(Y >= X) {` `        ``console.log(``"YES"``);` `    ``}` `    ``else` `{`   `        ``// Temporary variable to` `        ``// store value of Y` `        ``let temp = Y;`   `        ``// Loop for traversing Map` `        ``for` `(const [key, value] of map.entries()) {` `            `  `            ``// Condition when Y's sum` `            ``// is >=X after adding sum` `            ``// of both symmetrical arrays` `            ``if` `(temp >= X)` `                ``break``;`   `            ``// If an element found such` `            ``// that its frequency is` `            ``// greater than 1` `            ``if` `(value > 1) {`   `                ``// If frequency is even` `                ``// then we are taking` `                ``// even contribution` `                ``// in Y else contributing` `                ``// (frequency-1)*element` `                ``temp += (value % 2 == 0) ? key * value : key * (value - 1);` `            ``}` `        ``}`   `        ``// Checking after adding sum` `        ``// of symmetrical arrays in Y,` `        ``// Y is >=X or not` `        ``if` `(temp >= X)` `            ``console.log(``"YES"``);` `        ``else` `            ``console.log(``"NO"``);` `    ``}` `}`   `// Inputs` `let N = 6;` `let X = 10;` `let Y = 2;` `let Z = [ 1, 3, 2, 2, 1, 3 ];`   `// Function call` `Can_Maximize_to_X(N, X, Y, Z);`

## Python3

 `# Python code to implement the approach`   `def` `Can_Maximize_to_X(N, X, Y, Z):` `    ``# Map initialized for counting` `    ``# frequencies of elements in Z[]` `    ``Map` `=` `{}`   `    ``# Loop for traversing Z[] and` `    ``# initialize map with frequencies` `    ``for` `j ``in` `range``(N):` `        ``temp ``=` `Z[j]` `        ``if` `temp ``in` `Map``:` `            ``Map``[temp] ``+``=` `1` `        ``else``:` `            ``Map``[temp] ``=` `1`   `    ``# Condition when already Y >= X,` `    ``# Formally no need of symmetrical` `    ``# arrays to maximize Y` `    ``if` `Y >``=` `X:` `        ``print``(``"YES"``)` `    ``else``:` `        ``# Temporary variable to` `        ``# store value of Y` `        ``temp ``=` `Y`   `        ``# Loop for traversing` `        ``# HashMap` `        ``for` `p ``in` `Map``.items():` `            ``# Condition when Y's sum` `            ``# is >=X after adding sum` `            ``# of both symmetrical arrays` `            ``if` `temp >``=` `X:` `                ``break`   `            ``# If an element found such` `            ``# that its frequency is` `            ``# greater than 1` `            ``if` `p[``1``] > ``1``:` `                ``# If frequency is even` `                ``# then we are taking` `                ``# even contribution` `                ``# in Y else contributing` `                ``# (frequency-1)*element` `                ``temp ``+``=` `p[``0``] ``*` `p[``1``] ``if` `p[``1``] ``%` `2` `=``=` `0` `else` `p[``0``] ``*` `(p[``1``] ``-` `1``)`   `        ``# Checking after adding sum` `        ``# of symmetrical arrays in Y,` `        ``# Y is >=X or not` `        ``if` `temp >``=` `X:` `            ``print``(``"YES"``)` `        ``else``:` `            ``print``(``"NO"``)`   `# Driver Function` `if` `__name__ ``=``=` `'__main__'``:` `    ``# Inputs` `    ``N ``=` `6` `    ``X ``=` `10` `    ``Y ``=` `2` `    ``Z ``=` `[``1``, ``3``, ``2``, ``2``, ``1``, ``3``]`   `    ``# Function call` `    ``Can_Maximize_to_X(N, X, Y, Z)` `    `  `#This code is contributed by shivamsharma215`

## C#

 `// C# code to implement the approach`   `using` `System;` `using` `System.Collections.Generic;`   `public` `class` `GFG {` `    ``// Method for checking is it possible to maximize Y to` `    ``// >= X` `    ``public` `static` `void` `Can_Maximize_to_X(``int` `N, ``int` `X,` `                                         ``int` `Y, ``int``[] Z)` `    ``{` `        ``// Dictionary initialized for counting` `        ``// frequencies of elements in Z[]` `        ``Dictionary<``int``, ``int``> dict` `            ``= ``new` `Dictionary<``int``, ``int``>();`   `        ``// Loop for traversing Z[] and` `        ``// initialize dictionary with frequencies` `        ``for` `(``int` `i = 0; i < N; i++) {` `            ``if` `(dict.ContainsKey(Z[i]))` `                ``dict[Z[i]]++;` `            ``else` `                ``dict.Add(Z[i], 1);` `        ``}`   `        ``// Condition when already Y >= X,` `        ``// Formally no need of symmetrical` `        ``// arrays to maximize Y` `        ``if` `(Y >= X)` `            ``Console.WriteLine(``"YES"``);` `        ``else` `{` `            ``// Temporary variable to` `            ``// store value of Y` `            ``int` `temp = Y;`   `            ``// Loop for traversing Dictionary` `            ``foreach``(KeyValuePair<``int``, ``int``> entry ``in` `dict)` `            ``{` `                ``int` `key = entry.Key;` `                ``int` `value = entry.Value;`   `                ``// Condition when Y's sum` `                ``// is >=X after adding sum` `                ``// of both symmetrical arrays` `                ``if` `(temp >= X)` `                    ``break``;`   `                ``// If an element found such` `                ``// that its frequency is` `                ``// greater than 1` `                ``if` `(value > 1) {` `                    ``// If frequency is even` `                    ``// then we are taking` `                    ``// even contribution` `                    ``// in Y else contributing` `                    ``// (frequency-1)*element` `                    ``if` `(value % 2 == 0)` `                        ``temp += key * value;` `                    ``else` `                        ``temp += key * (value - 1);` `                ``}` `            ``}`   `            ``// Checking after adding sum` `            ``// of symmetrical arrays in Y,` `            ``// Y is >=X or not` `            ``if` `(temp >= X)` `                ``Console.WriteLine(``"YES"``);` `            ``else` `                ``Console.WriteLine(``"NO"``);` `        ``}` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main()` `    ``{` `        ``// Inputs` `        ``int` `N = 6;` `        ``int` `X = 10;` `        ``int` `Y = 2;` `        ``int``[] Z = ``new` `int``[] { 1, 3, 2, 2, 1, 3 };`   `        ``// Function call` `        ``Can_Maximize_to_X(N, X, Y, Z);` `    ``}` `}`

Output

`YES`

Time Complexity: O(N)
Auxiliary Space: O(N), As HashMap is used for storing

My Personal Notes arrow_drop_up