-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZeroArrayTrans1.java
More file actions
47 lines (32 loc) · 1.05 KB
/
Copy pathZeroArrayTrans1.java
File metadata and controls
47 lines (32 loc) · 1.05 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
package DifferenceArrayTech;
import java.util.Arrays;
public class ZeroArrayTrans1 {
public static void main(String[] args) {
ZeroArrayTrans1 obj = new ZeroArrayTrans1();
int[] nums = {1, 2, 3, 4, 5};
int[][] queries = {{0, 2}, {1, 3}};
System.out.println(obj.isZeroArray(nums, queries));
}
public boolean isZeroArray(int[] nums, int[][] queries) {
int n = nums.length;
//Step 1 create difference array
int[] diffArray = new int[n];
Arrays.fill(diffArray, 0);
for(int [] querie: queries){
int start = querie[0];
int end = querie[1];
diffArray[start] += 1;
if(end + 1 < n ){
diffArray[end + 1] -= 1;
}
}
//commulative sum
for(int i = 1; i < n; i++){
diffArray[i] += diffArray[i - 1];
}
for(int i = 0; i < n; i++){
if(diffArray[i] < nums[i]) return false;
}
return true;
}
}