 Open in App
Not now

# Check if A[] can be made equal to B[] by choosing X indices in each operation

• Last Updated : 17 Feb, 2023

Given two arrays A[] and B[] of length N along with an integer X. Initially all the elements of A[] are equal to zero, the task is to check if it is possible to make all elements of A[] equal to elements of B[] (Order of elements doesn’t matters) by selecting any X distinct indices of A[] and incrementing elements at those indices by 1 any number of times.

Examples:

Input: N = 5, X = 3, B[] = {3, 2, 1, 4, 5}
Output: YES
Explanation: Array A[] initially: {0, 0, 0, 0, 0}

• First operation: choose indices 1, 2, and 3 of A[], Then after operation A[] = {1, 1, 1, 0, 0}
• Second operation: choose indices 2, 3 and 4 of A[], Then after operation A[] = {1, 2, 2, 1, 0}
• Third operation: choose indices 2, 3 and 5 of A[], Then after operation A[] = {1, 3, 3, 1, 1}
• Fourth operation: choose indices 1, 4 and 3 of A[], Then after operation A[] = {2, 3, 4, 2, 1}
• Fifth operation: choose indices 2, 4 and 3 of A[], Then after operation A[] = {2, 4, 5, 3, 1}

It can be verified that now A[] only contains elements of B[]. Therefore the output is YES.

Input: N = 3, X = 2, B[] = {4, 1, 2}
Output: NO
Explanation: It can be verified that it is not possible to make all the elements of A[] equal to B[] by using the given operation.

Approach: Implement the idea below to solve the problem:

The problem is observation based and can be solved by using some mathematics. It should be noted that the conversion from A[] to B[] only and only if condition (sum % k == 0) &&
( max ≤ (sum/k)) satisfies. Where sum and max are equal to the sum of all elements of B[] and the maximum element of B[] respectively.

Steps were taken to solve the problem:

• Create two variables sum and max of the long data type.
• Initialize sum and max with the sum of all elements of B[] and max element of B[] by traversing B[].
• If condition (sum % k == 0) && (max ≤ (sum / k) is true, Then return YES else return NO.

Code to implement the approach:

## C++

 `// C++ code to implement the approach` `#include ` `using` `namespace` `std;`   `// Function for checking if conversion` `// is possible or not` `string Is_conversion_possible(``long` `n, ``long` `k, ``long` `b[])` `    ``{`   `        ``// variable for holding sum of B[]` `        ``long` `sum = 0;`   `        ``// Variable for holding max element` `        ``// of B[]` `        ``long` `max = -1;`   `        ``// Loop for traversing over B[]` `        ``for` `(``int` `j = 0; j < n; j++) {` `            ``long` `d = b[j];` `            ``if` `(max < d) {` `                ``max = d;` `            ``}` `            ``sum += d;` `        ``}`   `        ``// Checking for the conversion` `        ``// condition` `        ``if` `((sum % k == 0) && (max <= (sum / k))) {` `            ``return` `"YES"``;` `        ``}` `        ``else` `{` `            ``return` `"NO"``;` `        ``}` `    ``}`   `int` `main() {`   `     ``// Input values of N and K` `     ``long` `n = 5;` `     ``long` `k = 3;`   `     ``// Input array B[]` `     ``long` `b[] = { 5, 2, 3, 4, 1 };`   `     ``// Function call` `     ``cout << Is_conversion_possible(n, k, b) << endl;` `}`

## Java

 `// Java codevto 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` `    ``{`   `        ``// Input values of N and K` `        ``long` `n = ``5``;` `        ``long` `k = ``3``;`   `        ``// Input array B[]` `        ``long` `b[] = { ``5``, ``2``, ``3``, ``4``, ``1` `};`   `        ``// Function call` `        ``System.out.println(Is_conversion_possible(n, k, b));` `    ``}`   `    ``// Function for checking if conversion` `    ``// is possible or not` `    ``static` `String Is_conversion_possible(``long` `n, ``long` `k,` `                                         ``long` `b[])` `    ``{`   `        ``// variable for holding sum of B[]` `        ``long` `sum = ``0``;`   `        ``// Variable for holding max element` `        ``// of B[]` `        ``long` `max = -``1``;`   `        ``// Loop for traversing over B[]` `        ``for` `(``int` `j = ``0``; j < n; j++) {` `            ``long` `d = b[j];` `            ``if` `(max < d) {` `                ``max = d;` `            ``}` `            ``sum = sum + d;` `        ``}`   `        ``// Checking for the conversion` `        ``// condition` `        ``if` `((sum % k == ``0``) && (max <= (sum / k))) {` `            ``return` `"YES"``;` `        ``}` `        ``else` `{` `            ``return` `"NO"``;` `        ``}` `    ``}` `}`

## Python3

 `# Python code to implement the approach`   `def` `is_conversion_possible(n, k, b):` `    ``# variable for holding sum of B[]` `    ``sum` `=` `0` `    ``# Variable for holding max element` `    ``# of B[]` `    ``max` `=` `-``1` `  `  `    ``# Loop for traversing over B[]` `    ``for` `d ``in` `b:` `        ``if` `max` `< d:` `            ``max` `=` `d` `        ``sum` `=` `sum` `+` `d` `  `  `    ``# Checking for the conversion condition` `    ``if` `(``sum` `%` `k ``=``=` `0``) ``and` `(``max` `<``=` `(``sum` `/` `k)):` `        ``return` `"YES"` `    ``else``:` `        ``return` `"NO"` `  `  `if` `__name__ ``=``=` `'__main__'``:` `    ``# Input values of N and K` `    ``n ``=` `5` `    ``k ``=` `3`   `    ``# Input array B[]` `    ``b ``=` `[``5``, ``2``, ``3``, ``4``, ``1``]`   `    ``# Function call` `    ``print``(is_conversion_possible(n, k, b))`   `# This code is contributed by lokesh.`

## C#

 `// C# code to implement the approach` `using` `System;` `public` `class` `GFG {`   `  ``static` `public` `void` `Main()` `  ``{`   `    ``// Code` `    ``// Input values of N and K` `    ``long` `n = 5;` `    ``long` `k = 3;`   `    ``// Input array B[]` `    ``long``[] b = { 5, 2, 3, 4, 1 };`   `    ``// Function call` `    ``Console.WriteLine(Is_conversion_possible(n, k, b));` `  ``}`   `  ``// Function for checking if conversion is possible or` `  ``// not` `  ``static` `String Is_conversion_possible(``long` `n, ``long` `k,` `                                       ``long``[] b)` `  ``{`   `    ``// variable for holding sum of B[]` `    ``long` `sum = 0;`   `    ``// Variable for holding max element of B[]` `    ``long` `max = -1;`   `    ``// Loop for traversing over B[]` `    ``for` `(``int` `j = 0; j < n; j++) {` `      ``long` `d = b[j];` `      ``if` `(max < d) {` `        ``max = d;` `      ``}` `      ``sum = sum + d;` `    ``}`   `    ``// Checking for the conversion condition` `    ``if` `((sum % k == 0) && (max <= (sum / k))) {` `      ``return` `"YES"``;` `    ``}` `    ``else` `{` `      ``return` `"NO"``;` `    ``}` `  ``}` `}`   `// This code is contributed by karthik.`

## Javascript

 `// Javascript code to implement the approach`   `// Function for checking if conversion` `// is possible or not` `function` `Is_conversion_possible(n, k, b) {` `// variable for holding sum of B[]` `    ``let sum = 0;` `    `  `     ``// Variable for holding max element` `    ``// of B[]` `    ``let max = -1;`   `// Loop for traversing over B[]` `    ``for` `(let j = 0; j < n; j++) {` `        ``let d = b[j];` `        ``if` `(max < d) {` `            ``max = d;` `        ``}` `        ``sum += d;` `    ``}` `// Checking for the conversion` `// condition` `    ``if` `((sum % k == 0) && (max <= (sum / k))) {` `        ``return` `"YES"``;` `    ``}` `    ``else` `{` `        ``return` `"NO"``;` `    ``}` `}`   `let n = 5;` `let k = 3;` `let b = [5, 2, 3, 4, 1];`   `console.log(Is_conversion_possible(n, k, b));`

Output

`YES`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles