You can only swapping at most one pair of elements in either of the arrays.
My code is working but I don't know how to make my code faster. I need it to be executed in 3s.
boolean checkSimilarArray(int[] a, int[] b) { int sml = 0; boolean result = false; for (int i = 0; i<a.length; i++){ if (a[i]!= b[i]){ sml++; } } Arrays.sort(a); Arrays.sort(b); if(Arrays.equals(a,b)){ if (sml <= 2){ result = true; }else{ result = false; } } return result; } Expected input and output images



