forked from SauravP97/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
arithmetic-subarrays.java
69 lines (61 loc) · 2.17 KB
/
arithmetic-subarrays.java
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
/**
* Problem Name : Arithmetic Subarrays
* Concept Involved : Maths, Interval Count, Array
*
* Execution Time : 500 ms
* Memory Consumed : 40.6 mb
*
* Solution : We have been asked Q queries. Every query has a range l and r
* and we have to tell whether the array elements from l to r form an
* Arithmetic Sequence.
* The idea is to find the maximum and minimum element of the sub array for
* every query and divide the difference of maximum and minimum element into
* (maximum-minimum)/x equal parts, where x is the size of the sub array.
* Now we jump to every part and check whether the given part is present in the
* sub array. For this we can use a hash set, I have used index based hashing
* in the array to save some time.
*/
class Solution {
public int getValue(int ele){
if(ele < 0){
return 100000 - ele;
}
return ele;
}
public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
int m = l.length;
ArrayList<Boolean> res = new ArrayList<>();
for(int i=0; i<m; i++){
int li = l[i];
int ri = r[i];
int maxel = Integer.MIN_VALUE;
int minel = Integer.MAX_VALUE;
int[] index = new int[200001];
for(int j=li; j<=ri; j++){
maxel = Math.max(maxel, nums[j]);
minel = Math.min(minel, nums[j]);
int ele = (nums[j] < 0) ? 100000 - nums[j] : nums[j];
index[ele]++;
}
int diff = maxel - minel;
int subs = ri-li;
if(diff%subs == 0){
int jump = diff/subs;
int start = minel;
while((start+jump) <= maxel && index[getValue(start+jump)] == 1){
start += jump;
}
if(start < maxel){
res.add(false);
}
else{
res.add(true);
}
}
else{
res.add(false);
}
}
return res;
}
}