Given two arrays A and B of equal size n, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain same set of elements, arrangements (or permutation) of elements may be different though.
Note : If there are repetitions, then counts of repeated elements must also be same for two array to be equal.
Input
line 1: contains an integer n denoting size of array.
line 2: contains n spaced integers denoting elements of array A.
line 3: contains n spaced integers denoting elements of array B.
Output
Print true
if the two arrays are equal otherwise print false
.
Example 1
Input
5 1 2 5 4 0 2 4 5 0 1
Output
true
Explanation
Both the arrays contain the same set of elements same number of times.
Example 2
Input
5 1 2 5 0 0 2 5 5 0 1
Output
false
Explanation
Both the arrays contain the same set of elements same but the frequency is different.
Constraints
1<=n<=10^6
1<=A[i],B[i]<=10^6
Solution of CHECK IF TWO ARRAYS ARE EQUAL OR NOT in java:–
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { //your code here Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int A[] = new int[n]; int B[] = new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } for(int i=0;i<n;i++) { B[i]=sc.nextInt(); } Arrays.sort(A); Arrays.sort(B); int count=1; for(int i=0;i<n;i++) { if(A[i]!=B[i]) { count=0; } } if(count>0) { System.out.println(1); } else{ System.out.println(0); } } }
Add a Comment