-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC3152.java
More file actions
66 lines (54 loc) · 1.83 KB
/
LC3152.java
File metadata and controls
66 lines (54 loc) · 1.83 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
/*
* LC3152
*/
import java.util.*;
public class LC3152 {
public static boolean[] isArraySpecial(int[] nums, int[][] queries) {
int n = nums.length;
int[] count = new int[n];
count[0] = 0;
for (int i = 1; i < n; i++) {
int parity = (nums[i] % 2 == nums[i - 1] % 2) ? 1 : 0;
count[i] = count[i - 1] + parity;
}
int m = queries.length;
boolean[] res = new boolean[m];
for (int i = 0; i < m; i++) {
int start = queries[i][0];
int end = queries[i][1];
res[i] = ((count[end] - count[start]) == 0);
}
return res;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter The Nums Array Size : ");
int n = sc.nextInt();
System.out.println();
int[] nums = new int[n];
System.out.println("Enter The Nums Array Elements : ");
for (int i = 0; i < nums.length; i++) {
System.out.printf("[%d] : ", i);
nums[i] = sc.nextInt();
}
System.out.println();
System.out.print("Enter The Queries Array Length : ");
int size = sc.nextInt();
System.out.println();
int[][] queries = new int[size][size];
System.out.println("Enter The Queries Array Elements : ");
for (int i = 0; i < queries.length; i++) {
for (int j = 0; j < queries.length; j++) {
System.out.printf("[%d][%d] : ", i, j);
queries[i][j] = sc.nextInt();
}
}
System.out.println();
boolean[] ans = isArraySpecial(nums, queries);
System.out.println("Answers : ");
for (int i = 0; i < ans.length; i++) {
System.out.printf("%d, ", ans[i]);
}
sc.close();
}
}