The puzzle:
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
Here's the code:
public class Solution {
public int totalNQueens(int n) {
boolean[][] board = new boolean[n][n];
return totalNQueens(board, 0, n);
}
private int totalNQueens(boolean[][] board, int cur, int n) {
int res = 0;
if(cur == n) {
return 1;
}
for(int i = 0; i < n; i++) {
if(!board[cur][i]) {
boolean[][] subboard = board.clone();
for(int r = cur + 1; r < n; r++) {
subboard[r][i] = true;
}
for(int r = cur + 1, c = i - 1; r < n && c >= 0; r++, c--) {
subboard[r][c] = true;
}
for(int r = cur + 1, c = i + 1; r < n && c < n; r++, c++) {
subboard[r][c] = true;
}
res += totalNQueens(subboard, cur+1, n);
}
}
return res;
}
}
The result:
Input:
4
Output:
0
Expected:
2
I can't figure out what's wrong with this code, so please help me, thx in advance.
The problem is with this line:
boolean[][] subboard = board.clone();
Java does not have multi-dimensional arrays. So board is actually a one dimensional array of arrays of booleans. The clone only clones the top level array but not the subarrays. So board[0] is actually the same object as subboard[0] and changes made there are never undone.
Related
I encountered a problem that I need to find the mirror of the prime numbers within the range of 1 to N. A mirror is like 13 and 31, 17 and 71, etc. I write the solution provided below,
/*
* find prime numbers within 1 to N that is a mirror to each other
*/
public static List<Integer> solution(int N) {
List<Integer> primes = findPrimes(N);
Set<Integer> set = new LinkedHashSet<>();
for (int i = 0; i < primes.size(); i++) {
int prime = primes.get(i);
int mirror = hasMirror(prime, primes);
if (mirror == 0) {
continue;
}
set.add(prime);
set.add(mirror);
}
return new ArrayList<>(set);
}
/*
* find the mirror of a number
* */
private static int findMirror(int P) {
int R = 0;
while (P != 0) {
int D = P % 10;
R = R * 10 + D;
P /= 10;
}
return R;
}
private static int hasMirror(int P, List<Integer> B) {
Integer[] A = B.toArray(new Integer[0]);
int N = A.length;
int R = findMirror(P);
for (int i = N - 1; i >= 0; i--) {
if (A[i] == R) {
return R;
}
}
return 0;
}
public static List<Integer> findPrimes(int N) {
int[] F = new int[N + 1];
List<Integer> result = new ArrayList<>();
for (int i = 2; i <= N; i++) {
if (F[i] == 0) {
// the prime value need to be 2 digit for the mirror image
if (i < 10 || i == findMirror(i)) {
continue;
}
result.add(i);
for (int k = i * i; k <= N; k += i) {
if (F[k] == 0) {
F[k] = 1;
}
}
}
}
return result;
}
}
The solution works, but, is there an option to improve the performance?
One thing you can do to improve performance is, not invoking the hasMirror() for already identified mirrors.
For example, let's say you checked 17 and identified that it has a mirror 71. Then when you reach 71 later on your loop, you can skip checking its mirrors as you have already identified it.
List<Integer> primes = findPrimes(N);
Set<Integer> set = new LinkedHashSet<>();
for (int i = 0; i < primes.size(); i++) {
int prime = primes.get(i);
if (!set.contains(prime)) {
int mirror = hasMirror(prime, primes);
if (mirror == 0) {
continue;
}
set.add(prime);
set.add(mirror);
}
}
In my "merge" function, in the while loop, the given condition is a!=null && b!=null, but when b is null, it still enters the while loop and then later gives an error.
int[] merge(int[]a, int[]b)
{
int length = a.length+b.length;
int[]c = new int[length];
while (a!=null && b!=null)
{
if (a[0]<b[0])
{
c[length-1]=a[0];
a = tail(a);
}
else
{
c[length-1]=b[0];
b = tail(b);
}
}
return c;
}
int[] mergeSort(int[]a)
{
if (a.length==1)
return a;
int[] q = new int[a.length];
int[] l = new int[a.length/2];
int [] r = new int[a.length-l.length];
for (int i=0; i<l.length; i++)
{
l[i] = a[i];
}
for (int i=l.length; i<r.length; i++)
{
r[i-l.length] = a[i];
}
q = merge(mergeSort(l), mergeSort(r));
return q;
}
I think, you're facing situation when one of array (either a or b) is already empty, and the other one - still has elements. So, in the merge function you should take this into account.
This is full implementation of your mergeSort:
public class Answer {
public static void main(String[] args) {
int[] unsorted = new int[]{4,3,4,2,1,1,3,3,3,3,3,5,6,6,9,9,10,7,7,8};
System.out.println(Arrays.toString(mergeSort(unsorted)));
}
public static int[] mergeSort(int[]a) {
if (a.length == 1) return a;
int[] q = new int[a.length];
int[] l = new int[a.length / 2];
int [] r = new int[a.length - l.length];
for (int i = 0; i < l.length; i++) {
l[i] = a[i];
}
for (int i = 0; i < r.length; i++) {
r[i] = a[l.length + i];
}
q = merge(mergeSort(l), mergeSort(r));
return q;
}
private static int[] merge(int[]a, int[]b) {
int length = a.length + b.length;
int[] c = new int[length];
int p = 0;
while (a.length > 0 && b.length > 0) {
if (a[0] < b[0]) {
c[p++] = a[0];
a = tail(a);
} else {
c[p++] = b[0];
b = tail(b);
}
}
while (a.length > 0) {
c[p++] = a[0];
a = tail(a);
}
while (b.length > 0) {
c[p++] = b[0];
b = tail(b);
}
return c;
}
private static int[] tail(int[] a) {
int[] t = new int[a.length - 1];
for (int i = 0; i < t.length; i++) {
t[i] = a[i + 1];
}
return t;
}
}
Actually you have a problem with the auxiliary method merge().
I strongly recommend you to take this as an opportunity to practice systematic testing of your methods. Consider that we all constantly make mistakes when we write programs, and spotting errors to correct them is a real part of the job.
What make it easier, is to anticipate over this matter of fact : sh*t happens. And take time to write down some test cases, with input data and expected results.
int[] empty = {};
int [] r1 = merge(empty, empty);
System.out.println("Result should be empty\n");
printArray(r1);
int [] oneElement = { 23 };
int [] r3 = merge(oneElement, empty);
System.out.println("Result should be [23]\n");
printArray(r2);
...
So you have automated tests (we're all too lazy to do it by hand every time after the 35th modification of the code°.
This will lead you to "unit tests"....
I got a question about recursion for an entry exam of a job, but I failed to do it within 2 hours. I am very curious about how to do this after the pre-exam but I cannot work out a solution.
You can imagine there is a coin pusher with size n*m (2D array).
Each operation (moving up or down or left or right) will throw away one row or one column of coins
The question requires me to find the shortest possible moves that remains k coins at last. If it is impossible to remain k coins at last, then return -1
I stuck on how to determine the next move when there is more than one operation that having the same maximum number of coins (same value to be thrown away)
I believe that I need to calculate recursively that simulates all future possible moves to determine the current move operation.
But I do not know how to implement this algorithm, can anyone help?
Thank you!
Question :
There is a rectangular chessboard containing N‘M cells. each of
which either has one coin or nothing.
You can move all the coins together in one direction (such as up,
down, left, and right), but each time you can move these coins by
only one cell.
If any coins fall out of the chessboard, they must be thrown away.
If it is required to keep K coins on the board, what is the minimum
moves you have to take?
Output -1 if you can not meet this requirement.
The first line of the input are two positive
integers n, representing the size of the board.
For the next n line(s), each line has m numbers of
characters, with 'o' indicating a coin, '.' indicates an empty grid.
The last line is a positive integer k,
indicating the number of coins to be retained.
30% small input: 1 <= n,m <= 5, 0 < k < 25
40% medium input: 1 <= n,m <= 10, 0 < k < 100
30% large input: 1 <= n,m <= 100, 0 < k < 10000
sample input:
3 4
.o..
oooo
..o.
3
sample output:
2
My temporary answer
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class main {
String[][] inputArray;
int n;
int m;
int k;
int totalCoin = 0;
int step = 0;
public static void main(String[] args) {
main temp = new main();
temp.readData();
}
public void readData() {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
inputArray = new String [n][m];
sc.nextLine(); // skipping
for (int i = 0; i < n; i++) {
String temp = sc.nextLine();
for (int j = 0; j < m; j++) {
if ((temp.charAt(j) + "").equals("o")) totalCoin++;
inputArray[i][j] = temp.charAt(j) + "";
}
}
k = sc.nextInt();
int result = 0;
if (totalCoin >= k) {
result = findMaxAndMove();
System.out.println(result);
}
}
public String findNextMove() {
Map<String,Integer> tempList = new HashMap<String,Integer>();
tempList.put("up", up());
tempList.put("down", down());
tempList.put("left", left());
tempList.put("right", right());
Map.Entry<String, Integer> maxEntry = null;
for (Entry<String,Integer> temp : tempList.entrySet()) {
if (maxEntry == null || temp.getValue() > maxEntry.getValue()) {
maxEntry = temp;
}
}
Map<String,Integer> maxList = new HashMap<String,Integer>();
for (Entry<String,Integer> temp : tempList.entrySet()) {
if (temp.getValue() == maxEntry.getValue()) {
maxList.put(temp.getKey(), temp.getValue());
}
}
// return maxList.entrySet().iterator().next().getKey();
if (maxList.size() > 1) {
// how to handle this case when more than 1 operations has the same max value???????????
return ??????????????
}
else {
return maxList.entrySet().iterator().next().getKey();
}
//
}
public int findMaxAndMove() {
int up = up();
int down = down();
int left = left();
int right = right();
if ((totalCoin - up) == k) {
step++;
return step;
}
if ((totalCoin - down) == k) {
step++;
return step;
}
if ((totalCoin - left) == k) {
step++;
return step;
}
if ((totalCoin - right) == k) {
step++;
return step;
}
if (totalCoin - up < k && totalCoin - down < k && totalCoin - left < k && totalCoin - right < k) return -1;
else {
switch (findNextMove()) {
case "up" :
totalCoin -= up;
this.moveUp();
break;
case "down" :
totalCoin -= down;
this.moveDown();
break;
case "left" :
totalCoin -= left;
this.moveLeft();
break;
case "right" :
totalCoin -= right();
this.moveRight();
break;
}
step++;
return findMaxAndMove(); // going to next move
}
}
public String[] createBlankRow() {
String[] temp = new String[m];
for (int i = 0; i < m; i++) {
temp[i] = ".";
}
return temp;
}
public int up() {
int coinCounter = 0;
for (int i = 0; i < m; i++) {
if (inputArray[0][i].equals("o")) {
coinCounter++;
}
}
return coinCounter;
}
public void moveUp() {
// going up
for (int i = 0; i < n - 1; i++) {
inputArray[i] = inputArray[i + 1];
}
inputArray[n-1] = createBlankRow();
}
public int down() {
int coinCounter = 0;
for (int i = 0; i < m; i++) {
if (inputArray[n-1][i].equals("o")) {
coinCounter++;
}
}
return coinCounter;
}
public void moveDown() {
// going down
for (int i = n-1; i > 1; i--) {
inputArray[i] = inputArray[i - 1];
}
inputArray[0] = createBlankRow();
}
public int left() {
int coinCounter = 0;
for (int i = 0; i < n; i++) {
if (inputArray[i][0].equals("o")) {
coinCounter++;
}
}
return coinCounter;
}
public void moveLeft() {
// going left
for (int i = 0; i < n; i++) {
for (int j = 0; j < m-1; j++) {
inputArray[i][j] = inputArray[i][j+1];
}
inputArray[i][m-1] = ".";
}
}
public int right() {
int coinCounter = 0;
for (int i = 0; i < n; i++) {
if (inputArray[i][m-1].equals("o")) {
coinCounter++;
}
}
return coinCounter;
}
public void moveRight() {
// going right
for (int i = 0; i < n; i++) {
for (int j = m-1; j > 0; j--) {
inputArray[i][j] = inputArray[i][j-1];
}
inputArray[i][0] = ".";
}
}
public void printboard() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(inputArray[i][j]);
}
System.out.println();
}
}
}
I suspect you didn't find the right algorithm to solve the problem. To find a solution, not only the reachable boards with some special coin count are of concern but all. You must build up a tree of reachable boards. Each node in this tree is connected to its child nodes by an operation. That's were recursion enters the scene. You stop
when you reach your goal (mark this branch as possible solution) or
when it got impossible to reach the goal with further operations (too few coins left)
when the next operation would reach a board already visited in this branch.
In this tree all shortest branches marked as possible solution are the actual solutions. If no branch is marked as possible solution there is no solution and you have to output -1.
Here is my solution
public class CoinsMover {
public static List<String> getMinMoves(Character[][] board, int k, List<String> moves) {
if (!movesAreValid(moves, board)) {
return null;
}
int currentAmountOfCoins = getCoinsOnBoard(board);
// All good no need to move any thing
if (currentAmountOfCoins == k) {
moves.add("done");
return moves;
}
// Moved to much wrong way
if (currentAmountOfCoins < k) {
return null;
}
List<String> moveRight = getMinMoves(moveRight(board), k, getArrayWithApendded(moves, "right"));
List<String> moveLeft = getMinMoves(moveLeft(board), k, getArrayWithApendded(moves, "left"));
List<String> moveUp = getMinMoves(moveUp(board), k, getArrayWithApendded(moves, "up"));
List<String> moveDown = getMinMoves(moveDown(board), k, getArrayWithApendded(moves, "down"));
List<List<String>> results = new ArrayList<>();
if (moveRight != null) {
results.add(moveRight);
}
if (moveLeft != null) {
results.add(moveLeft);
}
if (moveUp != null) {
results.add(moveUp);
}
if (moveDown != null) {
results.add(moveDown);
}
if (results.isEmpty()) {
return null;
}
List<String> result = results.stream().sorted(Comparator.comparing(List::size)).findFirst().get();
return result;
}
private static boolean movesAreValid(List<String> moves, Character[][] board) {
long ups = moves.stream().filter(m -> m.equals("up")).count();
long downs = moves.stream().filter(m -> m.equals("down")).count();
long lefts = moves.stream().filter(m -> m.equals("left")).count();
long rights = moves.stream().filter(m -> m.equals("right")).count();
boolean verticalIsFine = ups <= board.length && downs <= board.length;
boolean horizontalIsFine = lefts <= board[0].length && rights <= board[0].length;
return verticalIsFine && horizontalIsFine;
}
private static List<String> getArrayWithApendded(List<String> moves, String move) {
List<String> result = new ArrayList<>(moves);
result.add(move);
return result;
}
private static Character[][] moveRight(Character[][] board) {
Character result[][] = new Character[board.length][board[0].length];
// Cleaning left column
for (int i = 0; i < board.length; i++)
result[i][0] = '.';
for (int row = 0; row < board.length; row++) {
for (int column = 0; column < board[row].length - 1; column++) {
result[row][column + 1] = board[row][column];
}
}
return result;
}
private static Character[][] moveLeft(Character[][] board) {
Character result[][] = new Character[board.length][board[0].length];
// Cleaning right column
for (int i = 0; i < board.length; i++)
result[i][board[i].length - 1] = '.';
for (int row = 0; row < board.length; row++) {
for (int column = 1; column < board[row].length; column++) {
result[row][column - 1] = board[row][column];
}
}
return result;
}
private static Character[][] moveDown(Character[][] board) {
Character result[][] = new Character[board.length][board[0].length];
// Cleaning upper row
for (int i = 0; i < board[board.length - 1].length; i++)
result[0][i] = '.';
for (int row = board.length - 1; row > 0; row--) {
result[row] = board[row - 1];
}
return result;
}
private static Character[][] moveUp(Character[][] board) {
Character result[][] = new Character[board.length][board[0].length];
// Cleaning upper row
for (int i = 0; i < board[board.length - 1].length; i++)
result[board.length - 1][i] = '.';
for (int row = 0; row < board.length - 1; row++) {
result[row] = board[row + 1];
}
return result;
}
private static int getCoinsOnBoard(Character[][] board) {
int result = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == 'o') {
result++;
}
}
}
return result;
}
public static void main(String... args) {
Character[][] mat = {{'.', 'o', '.', '.'}, {'o', 'o', 'o', 'o'}, {'.', '.', 'o', '.'}};
List<String> result = getMinMoves(mat, 3, new ArrayList<>());
if (result == null) {
System.out.println(-1);//output [right, right, done]
}
System.out.println(result);
}
}
I admit it was hard for me to search for duplicates so instead I am using a list of string which wrights the path you need to take to get to the solution. Now let's look at stopping condition first if current moves are invalid return null example of invalid moves is if you have a table with 4 columns in you moved right 5 times same goes for rows and moves up/down. Second, if board holds the neede amount we are done. And the last if board hold less we have failed. So now what algorithm is trying to do is to step in each direction in search of result and from here proceed recursivly.
You can find the solution below. A few points to note.
Whenever you see a problem that mentions moving an array in 1 direction, it's always a good idea to define an array of possible directions and loop through it in the recursive function. This would prevent you from confusing yourself.
The idea is to count coins by row and column so that you have a way to find out the remaining coins after each move in linear time.
The remaining job is to just loop through the possible directions in your recursive functions to find a possible solution.
As the recursive function may run in a circle and come back to one of the previous locations, you should/can improve the recursive function further by maintaining a Map cache of previous partial solutions using the curRowIdx and curColIdx as key.
public static void main(String[] args) {
char[][] board = {{'.', 'o', '.', '.'},
{'o', 'o', 'o', 'o'},
{'.', '.', 'o', '.'}};
CoinMoveSolver solver = new CoinMoveSolver(board);
System.out.println(solver.getMinimumMove(3));
}
static class CoinMoveSolver {
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
char[][] board;
int[] rowCount;
int[] colCount;
int height;
int width;
int totalCoins;
public CoinMoveSolver(char[][] board) {
// Set up the board
this.board = board;
this.height = board.length;
this.width = board[0].length;
// Count how many coins we have per row,
// per column and the total no. of coins
this.rowCount = new int[height];
this.colCount = new int[width];
for (int i = 0 ; i < board.length ; i++) {
for (int j = 0 ; j < board[i].length ; j++) {
if (board[i][j] == 'o') {
this.rowCount[i]++;
this.colCount[j]++;
totalCoins++;
}
}
}
}
// Returns the number of coins if the top left
// corner of the board is at rowIdx and colIdx
private int countCoins(int rowIdx, int colIdx) {
int sumRow = 0;
for (int i = rowIdx ; i < rowIdx + height ; i++) {
if (i >= 0 && i < height)
sumRow += rowCount[i];
}
int sumCol = 0;
for (int j = colIdx ; j < colIdx + width ; j++) {
if (j >= 0 && j < width)
sumCol += colCount[j];
}
return Math.min(sumRow, sumCol);
}
public int getMinimumMove(int targetCoinCount) {
if (totalCoins < targetCoinCount)
return -1;
else if (totalCoins == targetCoinCount)
return 0;
else
return this.recursiveSolve(0, 0, -1, 0, targetCoinCount);
}
private boolean isOppositeDirection(int prevDirectionIdx, int curDirectionIdx) {
if (prevDirectionIdx < 0)
return false;
else {
int[] prevDirection = directions[prevDirectionIdx];
int[] curDirection = directions[curDirectionIdx];
return prevDirection[0] + curDirection[0] + prevDirection[1] + curDirection[1] == 0;
}
}
private int recursiveSolve(int curRowIdx, int curColIdx, int prevDirectionIdx, int moveCount, int targetCoinCount) {
int minMove = -1;
for (int i = 0 ; i < directions.length ; i++) {
if (!this.isOppositeDirection(prevDirectionIdx, i)) {
int[] direction = directions[i];
int nextRowIdx = curRowIdx + direction[0];
int nextColIdx = curColIdx + direction[1];
int coinCount = this.countCoins(nextRowIdx, nextColIdx);
// If this move reduces too many coins, abandon
if (coinCount < targetCoinCount)
continue;
// If this move can get us the exact number of
// coins we're looking for, break the loop
else if (coinCount == targetCoinCount) {
minMove = moveCount + 1;
break;
} else {
// Look for the potential answer by moving the board in 1 of the 4 directions
int potentialMin = this.recursiveSolve(nextRowIdx, nextColIdx, i, moveCount + 1, targetCoinCount);
if (potentialMin > 0 && (minMove < 0 || potentialMin < minMove))
minMove = potentialMin;
}
}
}
// If minMove is still < 0, that means
// there's no solution
if (minMove < 0)
return -1;
else
return minMove;
}
}
Is there a way to parse 2 dimensional array like this into a rectangle object (x,y, width, height)?. I need the array of all possible rectangles...
{0,0,0,0,0}
{0,0,0,0,0}
{0,1,1,0,0}
{0,1,1,0,0}
{0,0,0,0,0}
This would give 4 rectangles (we are looking at 0):
0,0,5,2
0,0,1,5
3,0,2,5
0,5,5,1
I have tried something like this, but it only gives the area of the biggest rectangle...
public static int[] findMaxRectangleArea(int[][] A, int m, int n) {
// m=rows & n=cols according to question
int corX =0, corY = 0;
int[] single = new int[n];
int largeX = 0, largest = 0;
for (int i = 0; i < m; i++) {
single = new int[n]; // one d array used to check line by line &
// it's size will be n
for (int k = i; k < m; k++) { // this is used for to run until i
// contains element
int a = 0;
int y = k - i + 1; // is used for row and col of the comming
// array
int shrt = 0, ii = 0, small = 0;
int mix = 0;
int findX = 0;
for (int j = 0; j < n; j++) {
single[j] = single[j] + A[k][j]; // postions element are
// added
if (single[j] == y) { // element position equals
shrt = (a == 0) ? j : shrt; // shortcut
a = a + 1;
if (a > findX) {
findX = a;
mix = shrt;
}
} else {
a = 0;
}
}
a = findX;
a = (a == y) ? a - 1 : a;
if (a * y > largeX * largest) { // here i am checking the values
// with xy
largeX = a;
largest = y;
ii = i;
small = mix;
}
}
}// end of loop
return largeX * largest;
}
this code is working with 1s, but that is not the point right now
I'm currently writing a small function which should return how many items of an array are exactly on the same position as the other array and how many are there but on different positions.
the function currently looks like this:
public static int[] numCorrects(char[] leftarray, char[] rightarray){
int counter[] = new int[2];
counter[0] = 0;
counter[1] = 0;
for (int i = 0; i < leftarray.length; i++) {
if(leftarray[i]==rightarray[i]) counter[0]++;
else {
for (int n = 0; n < leftarray.length; n++) {
if (leftarray[i] == rightarray[n] && leftarray[n] != rightarray[n]) {
counter[1]++;
break;
}
}
}
}
return counter;
}
This works kinda well if i have lets say the leftarray of (1, 2, 3, 4) and the rightarray of (4, 3, 2, 2)
it the returns (0, 3) (0 exact match, 3 there but on different position)
but now the problem:
if I swap the inputs with leftarray of (4, 3, 2, 2) and rightarray of (1, 2, 3, 4) the output is (0, 4) which is wrong (should be the same as the 1st one)
hope anyone can help me. thx
You have to decide how to handle repeated elements. This is what's causing the discrepancy. The repeated 2 means that the "different positions" counter is incremented twice when the arrays are swapped.
try this:
public static int[] numCorrects(char[] leftarray, char[] rightarray){
int counter[] = new int[2];
counter[0] = 0;
counter[1] = 0;
boolean taken[] = new boolean[rightarray.length];
for (int i=0;i<taken.length;i++)
taken[i] = false;
for (int i = 0; i < leftarray.length; i++) {
if(leftarray[i]==rightarray[i]){ counter[0]++; taken[i] = true; }
else {
for (int n = 0; n < leftarray.length; n++) {
if (leftarray[i] == rightarray[n] && leftarray[n] != rightarray[n] && !taken[n]) {
taken[n] = true;
counter[1]++;
break;
}
}
}
}
return counter;
}
Remove the break and you will get the same result.
public static int[] numCorrects(char[] leftarray, char[] rightarray){
int counter[] = new int[2];
counter[0] = 0;
counter[1] = 0;
boolean taken[] = new boolean[rightarray.length];
for (int i=0;i<taken.length;i++)
taken[i] = false;
for (int i = 0; i < leftarray.length; i++) {
if(leftarray[i]==rightarray[i])
{
counter[0]++;
taken[i]=true;
}
}
for (int i = 0; i < leftarray.length; i++)
{
if (!taken[i])
{
for (int j = 0; j < rightarray.length; j++) {
if (leftarray[i] == rightarray[j] && i!=j && !taken[j]) { // you can remove i!=j because its already don't apply because of !taken[i]
taken[j] = true;
counter[1]++;
break;
}
}
}
}
return counter;
}