-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion_144_2D_MatchTwo.java
More file actions
102 lines (39 loc) · 1.4 KB
/
Question_144_2D_MatchTwo.java
File metadata and controls
102 lines (39 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package week7_Array_2D_ArrayList;
import java.util.Scanner;
public class Question_144_2D_MatchTwo {
public static void main(String[] args) {
// olcay // Jul 26, 2020
/* Like in a match 3 game but not.
You get a 2d array and you need to find how many two matches there are.
and return the number(int) of matches you found.
For example : 1 and 2 are not a match, 2 and 2 are a match.
a match in this case is two numbers in a row that are equal .
for example:
[2,2,1,3,4,5]
[5,2,3,3,4,5]
[3,2,3,1,4,5]
print
matches: 2
*/
Scanner inp = new Scanner(System.in);
System.out.println("Enter rows and columns number: ");
int rowsLength = inp.nextInt();
int colsLength = inp.nextInt();
int[][] arr = new int[rowsLength][colsLength];
for(int i=0; i<rowsLength; i++) {
for(int j=0; j<colsLength; j++) {
System.out.println("Enter rows " + (i+1) + " columns " + (j+1));
arr[i][j]=inp.nextInt();
}
}
int matches = 0;
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length-1; j++) {
if(arr[i][j]==arr[i][j+1]) {
matches++;
}
}
}
System.out.println("matches: " + matches);
}
}