Check if A[] can be made equal to B[] by choosing X indices in each operation
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++
#include <bits/stdc++.h>
using namespace std;
string Is_conversion_possible( long n, long k, long b[])
{
long sum = 0;
long max = -1;
for ( int j = 0; j < n; j++) {
long d = b[j];
if (max < d) {
max = d;
}
sum += d;
}
if ((sum % k == 0) && (max <= (sum / k))) {
return "YES" ;
}
else {
return "NO" ;
}
}
int main() {
long n = 5;
long k = 3;
long b[] = { 5, 2, 3, 4, 1 };
cout << Is_conversion_possible(n, k, b) << endl;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
long n = 5 ;
long k = 3 ;
long b[] = { 5 , 2 , 3 , 4 , 1 };
System.out.println(Is_conversion_possible(n, k, b));
}
static String Is_conversion_possible( long n, long k,
long b[])
{
long sum = 0 ;
long max = - 1 ;
for ( int j = 0 ; j < n; j++) {
long d = b[j];
if (max < d) {
max = d;
}
sum = sum + d;
}
if ((sum % k == 0 ) && (max <= (sum / k))) {
return "YES" ;
}
else {
return "NO" ;
}
}
}
|
Python3
def is_conversion_possible(n, k, b):
sum = 0
max = - 1
for d in b:
if max < d:
max = d
sum = sum + d
if ( sum % k = = 0 ) and ( max < = ( sum / k)):
return "YES"
else :
return "NO"
if __name__ = = '__main__' :
n = 5
k = 3
b = [ 5 , 2 , 3 , 4 , 1 ]
print (is_conversion_possible(n, k, b))
|
C#
using System;
public class GFG {
static public void Main()
{
long n = 5;
long k = 3;
long [] b = { 5, 2, 3, 4, 1 };
Console.WriteLine(Is_conversion_possible(n, k, b));
}
static String Is_conversion_possible( long n, long k,
long [] b)
{
long sum = 0;
long max = -1;
for ( int j = 0; j < n; j++) {
long d = b[j];
if (max < d) {
max = d;
}
sum = sum + d;
}
if ((sum % k == 0) && (max <= (sum / k))) {
return "YES" ;
}
else {
return "NO" ;
}
}
}
|
Javascript
function Is_conversion_possible(n, k, b) {
let sum = 0;
let max = -1;
for (let j = 0; j < n; j++) {
let d = b[j];
if (max < d) {
max = d;
}
sum += d;
}
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));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
17 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...