HackerRank Beautiful Pairs problem solution YASH PAL, 31 July 2024 In this HackerRank Beautiful Pairs problem solution, we have given two arrays A and B, and both containing N integers and we need to change exactly one element in B so that the size of the pairwise disjoint is beautiful set is maximum. Problem solution in Python. n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) res = 0 for i in range(n): for j in range (n): if a[i] == b[j] and a[i] > 0: res += 1 a[i] = 0 #just set to 0 to be not used anymore b[j] = 0 if res < n: res += 1 else: res -= 1 print (res) {“mode”:”full”,”isActive”:false} Problem solution in Java. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int[] A = readArray(in, N); int[] B = readArray(in, N); Arrays.sort(A); Arrays.sort(B); int i = 0; int j = 0; int count = 0; while (i < N && j < N) { if (A[i] < B[j]) i++; else if (A[i] > B[j]) j++; else { i++; j++; count++; } } if (count < N) count++; else count--; System.out.println(count); } public static int[] readArray(Scanner in, int N) { int[] ar = new int[N]; for (int i = 0; i < N; i++) { ar[i] = in.nextInt(); } return ar; } } {“mode”:”full”,”isActive”:false} Problem solution in C++. #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int n; cin>>n; vector<int> a(1000, 0),b(1000, 0); for(int i = 0;i < n;i++) { int c; cin>>c; a[c]++; } for(int i = 0;i < n;i++) { int c; cin>>c; b[c]++; } int r = 0; for(int i = 1;i <= 1000;i++) { r += min(a[i], b[i]); } cout<<(r==n?n-1:r+1); return 0; } {“mode”:”full”,”isActive”:false} Problem solution in C. #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int n,i,j,l,k,count=0,check=0; scanf("%d",&n); int a[n],b[n]; int c[n]; //t ad[n],bd[n]; for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) scanf("%d",&b[i]); for(k=0,i=0;i<n;i++) for(j=0;j<n;j++){ //printf("i=%d j=%d %d %dn",i,j,a[i],b[j]); if(a[i]==b[j]) { //printf("j===%dn",j); for(check=0,l=0;l<k;l++) { // printf("c[%d]==%d----j=%dn",l,c[k],j); if(j==c[l]) check=1; } //printf("check---%dn",check); if(check!=1) { c[k]=j; // printf("%d-vvv--n",c[k]); k++; // printf("entern"); count++; break; } } } if(count<n) count++; else if(count==n) count--; printf("%d",count); return 0; } {“mode”:”full”,”isActive”:false} algorithm coding problems