Given two arrays a[] and b[] of the same size n, in one swap you can swap a[i] with b[i]. Find the minimum number of swaps required to make all elements of a[] equal or all elements of b[] equal. If it is not possible return -1.
Examples:
Input: a[] = [2, 1, 2, 2], b[] = [3, 2, 4, 4]
Output: 1
Explanation: Swap a[1] with b[1]. a[] becomes [2, 2, 2, 2] and b[] becomes [3, 1, 4, 4]. All elements of a[] are equal.Input: a[] = [1, 1, 2], b[] = [1, 1, 1]
Output: 0
Explanation: All elements of b[] are already equal, no swaps needed.
[Naive Approach] Trying All Possible Target Values - O(n) Time and O(n) Space
The idea is to try every unique value present in both arrays as a potential target. For each candidate target, count how many swaps are needed to make all elements of a[] equal to that target. If at any index neither a[i] nor b[i] equals the target, it is impossible for that candidate. Return the minimum swaps across all valid candidates or -1 if none work.
#include <iostream>
#include <vector>
#include <climits>
#include <unordered_set>
using namespace std;
int minSwaps(vector<int>& a, vector<int>& b) {
int res = INT_MAX;
// Collect all unique values from both arrays as candidates
unordered_set<int> candidates;
for (int x : a) candidates.insert(x);
for (int x : b) candidates.insert(x);
// Try each candidate as target for a[]
for (int target : candidates) {
int cnt = 0;
// Count swaps needed to make a[] all equal to target
for (int i = 0; i < (int)a.size(); i++) {
// Current element matches no swap needed
if (a[i] == target) continue;
// Swap brings target value count it
if (b[i] == target) {
cnt++;
continue;
}
// Neither matches impossible for this target
cnt = INT_MAX;
break;
}
res = min(res, cnt);
}
// Try each candidate as target for b[]
for (int target : candidates) {
int cnt = 0;
// Count swaps needed to make b[] all equal to target
for (int i = 0; i < (int)b.size(); i++) {
// Current element matches no swap needed
if (b[i] == target) continue;
// Swap brings target value count it
if (a[i] == target) {
cnt++;
continue;
}
// Neither matches impossible for this target
cnt = INT_MAX;
break;
}
res = min(res, cnt);
}
return res == INT_MAX ? -1 : res;
}
int main() {
vector<int> a = {2, 1, 2, 2};
vector<int> b = {3, 2, 4, 4};
cout << minSwaps(a, b) << endl;
return 0;
}
import java.util.*;
class GFG {
static int minSwaps(int[] a, int[] b) {
int res = Integer.MAX_VALUE;
// Collect all unique values from both arrays as candidates
Set<Integer> candidates = new HashSet<>();
for (int x : a) candidates.add(x);
for (int x : b) candidates.add(x);
// Try each candidate as target for a[]
for (int target : candidates) {
int cnt = 0;
// Count swaps needed to make a[] all equal to target
for (int i = 0; i < a.length; i++) {
// Current element matches no swap needed
if (a[i] == target) continue;
// Swap brings target value count it
if (b[i] == target) {
cnt++;
continue;
}
// Neither matches impossible for this target
cnt = Integer.MAX_VALUE;
break;
}
res = Math.min(res, cnt);
}
// Try each candidate as target for b[]
for (int target : candidates) {
int cnt = 0;
// Count swaps needed to make b[] all equal to target
for (int i = 0; i < b.length; i++) {
// Current element matches no swap needed
if (b[i] == target) continue;
// Swap brings target value count it
if (a[i] == target) {
cnt++;
continue;
}
// Neither matches impossible for this target
cnt = Integer.MAX_VALUE;
break;
}
res = Math.min(res, cnt);
}
return res == Integer.MAX_VALUE ? -1 : res;
}
public static void main(String[] args) {
int[] a = {2, 1, 2, 2};
int[] b = {3, 2, 4, 4};
System.out.println(minSwaps(a, b));
}
}
def minSwaps(a, b):
res = float('inf')
# Collect all unique values from both arrays as candidates
candidates = set(a) | set(b)
# Try each candidate as target for a[]
for target in candidates:
cnt = 0
# Count swaps needed to make a[] all equal to target
for i in range(len(a)):
# Current element matches no swap needed
if a[i] == target: continue
# Swap brings target value count it
if b[i] == target:
cnt += 1
continue
# Neither matches impossible for this target
cnt = float('inf')
break
res = min(res, cnt)
# Try each candidate as target for b[]
for target in candidates:
cnt = 0
# Count swaps needed to make b[] all equal to target
for i in range(len(b)):
# Current element matches no swap needed
if b[i] == target: continue
# Swap brings target value count it
if a[i] == target:
cnt += 1
continue
# Neither matches impossible for this target
cnt = float('inf')
break
res = min(res, cnt)
return -1 if res == float('inf') else res
if __name__ == "__main__":
a = [2, 1, 2, 2]
b = [3, 2, 4, 4]
print(minSwaps(a, b))
using System;
using System.Collections.Generic;
class GFG {
static int minSwaps(int[] a, int[] b) {
int res = int.MaxValue;
// Collect all unique values from both arrays as candidates
HashSet<int> candidates = new HashSet<int>();
foreach (int x in a) candidates.Add(x);
foreach (int x in b) candidates.Add(x);
// Try each candidate as target for a[]
foreach (int target in candidates) {
int cnt = 0;
// Count swaps needed to make a[] all equal to target
for (int i = 0; i < a.Length; i++) {
// Current element matches no swap needed
if (a[i] == target) continue;
// Swap brings target value count it
if (b[i] == target) {
cnt++;
continue;
}
// Neither matches impossible for this target
cnt = int.MaxValue;
break;
}
res = Math.Min(res, cnt);
}
// Try each candidate as target for b[]
foreach (int target in candidates) {
int cnt = 0;
// Count swaps needed to make b[] all equal to target
for (int i = 0; i < b.Length; i++) {
// Current element matches no swap needed
if (b[i] == target) continue;
// Swap brings target value count it
if (a[i] == target) {
cnt++;
continue;
}
// Neither matches impossible for this target
cnt = int.MaxValue;
break;
}
res = Math.Min(res, cnt);
}
return res == int.MaxValue ? -1 : res;
}
static void Main() {
int[] a = {2, 1, 2, 2};
int[] b = {3, 2, 4, 4};
Console.WriteLine(minSwaps(a, b));
}
}
function minSwaps(a, b) {
let res = Infinity;
// Collect all unique values from both arrays as candidates
let candidates = new Set([...a, ...b]);
// Try each candidate as target for a[]
for (const target of candidates) {
let cnt = 0;
// Count swaps needed to make a[] all equal to target
for (let i = 0; i < a.length; i++) {
// Current element matches no swap needed
if (a[i] === target) continue;
// Swap brings target value count it
if (b[i] === target) {
cnt++;
continue;
}
// Neither matches impossible for this target
cnt = Infinity;
break;
}
res = Math.min(res, cnt);
}
// Try each candidate as target for b[]
for (const target of candidates) {
let cnt = 0;
// Count swaps needed to make b[] all equal to target
for (let i = 0; i < b.length; i++) {
// Current element matches no swap needed
if (b[i] === target) continue;
// Swap brings target value count it
if (a[i] === target) {
cnt++;
continue;
}
// Neither matches impossible for this target
cnt = Infinity;
break;
}
res = Math.min(res, cnt);
}
return res === Infinity ? -1 : res;
}
// Driver code
let a = [2, 1, 2, 2];
let b = [3, 2, 4, 4];
console.log(minSwaps(a, b));
Output
1
[Expected Approach] Using First Element as Candidate - O(n) Time and O(1) Space
The key insight is that the final equal element must be either a[0] or b[0] since index 0 can never be swapped with any other index - only a[0] and b[0] can occupy position 0. So we only need to check these two values as candidates for both arrays giving us at most 4 checks total.
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int minSwaps(vector<int>& a, vector<int>& b) {
int res = INT_MAX;
// Only a[0] and b[0] are candidates for target equal element
vector<int> candidates = {a[0], b[0]};
// Try making a[] equal then b[] equal by swapping arrays
for (int j = 0; j < 2; j++) {
for (int target : candidates) {
int cnt = 0;
// Count swaps needed to make current a[] all equal to target
for (int i = 0; i < (int)a.size(); i++) {
// Current element matches target no swap needed
if (a[i] == target) continue;
// Swap brings target value count the swap
if (b[i] == target) {
cnt++;
continue;
}
// Neither matches target impossible
cnt = INT_MAX;
break;
}
res = min(res, cnt);
}
// Swap arrays to check making b[] equal
swap(a, b);
}
return res == INT_MAX ? -1 : res;
}
int main() {
vector<int> a = {2, 1, 2, 2};
vector<int> b = {3, 2, 4, 4};
cout << minSwaps(a, b) << endl;
return 0;
}
class GFG {
static int minSwaps(int[] a, int[] b) {
int res = Integer.MAX_VALUE;
// Only a[0] and b[0] are candidates for target equal element
int[] candidates = {a[0], b[0]};
// Try making a[] equal then b[] equal by swapping arrays
for (int j = 0; j < 2; j++) {
for (int target : candidates) {
int cnt = 0;
// Count swaps needed to make current a[] all equal to target
for (int i = 0; i < a.length; i++) {
// Current element matches target no swap needed
if (a[i] == target) continue;
// Swap brings target value count the swap
if (b[i] == target) {
cnt++;
continue;
}
// Neither matches target impossible
cnt = Integer.MAX_VALUE;
break;
}
res = Math.min(res, cnt);
}
// Swap arrays to check making b[] equal
int[] temp = a;
a = b;
b = temp;
}
return res == Integer.MAX_VALUE ? -1 : res;
}
public static void main(String[] args) {
int[] a = {2, 1, 2, 2};
int[] b = {3, 2, 4, 4};
System.out.println(minSwaps(a, b));
}
}
def minSwaps(a, b):
res = float('inf')
# Only a[0] and b[0] are candidates for target equal element
candidates = [a[0], b[0]]
# Try making a[] equal then b[] equal by swapping arrays
for j in range(2):
for target in candidates:
cnt = 0
# Count swaps needed to make current a[] all equal to target
for i in range(len(a)):
# Current element matches target no swap needed
if a[i] == target: continue
# Swap brings target value count the swap
if b[i] == target:
cnt += 1
continue
# Neither matches target impossible
cnt = float('inf')
break
res = min(res, cnt)
# Swap arrays to check making b[] equal
a, b = b, a
return -1 if res == float('inf') else res
if __name__ == "__main__":
a = [2, 1, 2, 2]
b = [3, 2, 4, 4]
print(minSwaps(a, b))
using System;
class GFG {
static int minSwaps(int[] a, int[] b) {
int res = int.MaxValue;
// Only a[0] and b[0] are candidates for target equal element
int[] candidates = {a[0], b[0]};
// Try making a[] equal then b[] equal by swapping arrays
for (int j = 0; j < 2; j++) {
foreach (int target in candidates) {
int cnt = 0;
// Count swaps needed to make current a[] all equal to target
for (int i = 0; i < a.Length; i++) {
// Current element matches target no swap needed
if (a[i] == target) continue;
// Swap brings target value count the swap
if (b[i] == target) {
cnt++;
continue;
}
// Neither matches target impossible
cnt = int.MaxValue;
break;
}
res = Math.Min(res, cnt);
}
// Swap arrays to check making b[] equal
int[] temp = a;
a = b;
b = temp;
}
return res == int.MaxValue ? -1 : res;
}
static void Main() {
int[] a = {2, 1, 2, 2};
int[] b = {3, 2, 4, 4};
Console.WriteLine(minSwaps(a, b));
}
}
function minSwaps(a, b) {
let res = Infinity;
// Only a[0] and b[0] are candidates for target equal element
const candidates = [a[0], b[0]];
// Try making a[] equal then b[] equal by swapping arrays
for (let j = 0; j < 2; j++) {
for (const target of candidates) {
let cnt = 0;
// Count swaps needed to make current a[] all equal to target
for (let i = 0; i < a.length; i++) {
// Current element matches target no swap needed
if (a[i] === target) continue;
// Swap brings target value count the swap
if (b[i] === target) {
cnt++;
continue;
}
// Neither matches target impossible
cnt = Infinity;
break;
}
res = Math.min(res, cnt);
}
// Swap arrays to check making b[] equal
[a, b] = [b, a];
}
return res === Infinity ? -1 : res;
}
// Driver code
let a = [2, 1, 2, 2];
let b = [3, 2, 4, 4];
console.log(minSwaps(a, b));
Output
1