Given two arrays A[ ] and B[ ]. We can perform the following operation multiple times on the arrays:
- Select two integers i and j (i != j, 1 ≤ i, j ≤ n).
- Increase A[i] and B[j] by 1 and decrease A[j] and B[i] by 1.
We need to find whether it is possible to make the two arrays equal by using some number (possible zero) of operations.
Examples:
Input: A[] = [1, 2, 3], B[] = [3, 2, 1]
Output: true
Explanation: By choosing i = 1 and j = 3 and performing the operations, arrays A and B become equal.Input: A[] = [1, 2], B[] = [2, 1]
Output: false
Explanation: It is not possible to make these two arrays equal.
Table of Content
[Naive Approach] - Brute Force Simulation - O(n^3) Time and O(n) Space
The idea is to simulate the operations step by step. For each index i, if A[i] is less than B[i], we search for another index j where A[j] > B[j] and perform the allowed operation. This moves extra value from A[j] to A[i], while also updating B[j] and B[i] accordingly. We continue this process iteratively until no further changes are possible.
//Driver Code Starts
#include <iostream>
#include <vector>
using namespace std;
//Driver Code Ends
bool isPossible(vector<int> A, vector<int> B) {
int n = A.size();
bool changed = true;
// Keep applying the operation until no changes occur
while (changed) {
changed = false;
// For each element in A, check if it is less than B
for (int i = 0; i < n; i++) {
if (A[i] < B[i]) {
for (int j = 0; j < n; j++) {
if (i != j && A[j] > B[j]) {
// Perform the allowed operation
A[i]++; B[j]++;
A[j]--; B[i]--;
changed = true;
}
}
}
}
}
// Check if arrays are now equal
return A == B;
}
//Driver Code Starts
int main() {
vector<int> A = {1, 2, 3}, B = {3, 2, 1};
cout << (isPossible(A, B)?"true":"false");
}
//Driver Code Ends
//Driver Code Starts
import java.util.Arrays;
public class GFG {
//Driver Code Ends
static boolean isPossible(int[] A, int[] B) {
int n = A.length;
boolean changed = true;
// Keep applying the operation until no changes occur
while (changed) {
changed = false;
// For each element in A, check if it is less than B
for (int i = 0; i < n; i++) {
if (A[i] < B[i]) {
for (int j = 0; j < n; j++) {
if (i != j && A[j] > B[j]) {
// Perform the allowed operation
A[i]++; B[j]++;
A[j]--; B[i]--;
changed = true;
}
}
}
}
}
// Check if arrays are now equal
return Arrays.equals(A, B);
}
//Driver Code Starts
public static void main(String[] args) {
int[] A = {1, 2, 3}, B = {3, 2, 1};
System.out.println(isPossible(A, B)?"true":"false");
}
}
//Driver Code Ends
def isPossible(A, B):
n = len(A)
changed = True
# Keep applying the operation until no changes occur
while changed:
changed = False
# For each element in A, check if it is less than B
for i in range(n):
if A[i] < B[i]:
for j in range(n):
if i != j and A[j] > B[j]:
# Perform the allowed operation
A[i] += 1
B[j] += 1
A[j] -= 1
B[i] -= 1
changed = True
# Check if arrays are now equal
return A == B
if __name__ == "__main__":
A = [1, 2, 3]
B = [3, 2, 1]
print(isPossible(A, B))
using System;
class GFG{
static bool isPossible(int[] A, int[] B) {
int n = A.Length;
bool changed = true;
// Keep applying the operation until no changes occur
while (changed) {
changed = false;
// For each element in A, check if it is less than B
for (int i = 0; i < n; i++) {
if (A[i] < B[i]) {
for (int j = 0; j < n; j++) {
if (i != j && A[j] > B[j]) {
// Perform the allowed operation
A[i]++; B[j]++;
A[j]--; B[i]--;
changed = true;
}
}
}
}
}
// Check if arrays are now equal
for (int i = 0; i < n; i++)
if (A[i] != B[i]) return false;
return true;
}
static void Main() {
int[] A = {1, 2, 3}, B = {3, 2, 1};
Console.WriteLine(isPossible(A, B)?"true":"false");
}
}
function isPossible(A, B) {
const n = A.length;
let changed = true;
// Keep applying the operation until no changes occur
while (changed) {
changed = false;
// For each element in A, check if it is less than B
for (let i = 0; i < n; i++) {
if (A[i] < B[i]) {
for (let j = 0; j < n; j++) {
if (i !== j && A[j] > B[j]) {
// Perform the allowed operation
A[i]++; B[j]++;
A[j]--; B[i]--;
changed = true;
}
}
}
}
}
// Check if arrays are now equal
return A.every((val, idx) => val === B[idx]);
}
//Driver Code Starts
// Driver Code
const A = [1, 2, 3], B = [3, 2, 1];
console.log(isPossible(A, B));
//Driver Code Ends
Output
true
[Better Approach] - Using Parity and Sum Check- O(n) Time and O(1) Space
We first check the parity of each element in the two arrays. Since the allowed operation always changes values by ±1, the parity of each element must match for it to be possible to make the arrays equal. Next, we check the sum of both arrays, because the operation preserves the total sum, so the sums must be equal for a solution to exist. If both conditions hold - matching parity for all elements and equal sums—the arrays can be made equal otherwise, it’s impossible.
//Driver Code Starts
#include <iostream>
#include<vector>
using namespace std;
//Driver Code Ends
bool isPossible(vector<int> A, vector<int> B) {
int sumA = 0, sumB = 0;
int n= A.size();
for (int i = 0; i < n; i++) {
// Check parity of each element
if (A[i] % 2 != B[i] % 2)
return false;
sumA += A[i];
sumB += B[i];
}
// Check if total sums are equal
return sumA == sumB;
}
//Driver Code Starts
int main() {
vector<int> A = {1, 2, 3};
vector<int> B = {3, 2, 1};
if (isPossible(A, B))
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
//Driver Code Ends
//Driver Code Starts
import java.util.Arrays;
public class GFG {
//Driver Code Ends
public static boolean isPossible(int[] A, int[] B) {
int sumA = 0, sumB = 0;
int n = A.length;
for (int i = 0; i < n; i++) {
// Check parity of each element
if (A[i] % 2 != B[i] % 2)
return false;
sumA += A[i];
sumB += B[i];
}
// Check if total sums are equal
return sumA == sumB;
}
//Driver Code Starts
public static void main(String[] args) {
int[] A = {1, 2, 3};
int[] B = {3, 2, 1};
if (isPossible(A, B))
System.out.println("true");
else
System.out.println("false");
}
}
//Driver Code Ends
def isPossible(A, B):
sumA = sumB = 0
n = len(A)
for i in range(n):
# Check parity of each element
if A[i] % 2 != B[i] % 2:
return False
sumA += A[i]
sumB += B[i]
# Check if total sums are equal
return sumA == sumB
#Driver Code Starts
if __name__ == "__main__":
A = [1, 2, 3]
B = [3, 2, 1]
print("true" if isPossible(A, B) else "false")
#Driver Code Ends
//Driver Code Starts
using System;
class GFG {
//Driver Code Ends
public static bool isPossible(int[] A, int[] B) {
int sumA = 0, sumB = 0;
int n = A.Length;
for (int i = 0; i < n; i++) {
// Check parity of each element
if (A[i] % 2 != B[i] % 2)
return false;
sumA += A[i];
sumB += B[i];
}
// Check if total sums are equal
return sumA == sumB;
}
//Driver Code Starts
public static void Main() {
int[] A = {1, 2, 3};
int[] B = {3, 2, 1};
if (isPossible(A, B))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
//Driver Code Ends
function isPossible(A, B) {
let sumA = 0, sumB = 0;
let n = A.length;
for (let i = 0; i < n; i++) {
// Check parity of each element
if (A[i] % 2 !== B[i] % 2) return false;
sumA += A[i];
sumB += B[i];
}
// Check if total sums are equal
return sumA === sumB;
}
//Driver Code Starts
//Driver Code
let A = [1, 2, 3];
let B = [3, 2, 1];
console.log(isPossible(A, B) ? "true" : "false");
//Driver Code Ends
Output
true