-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1502_Can Make Arithmetic Progression From Sequence
68 lines (46 loc) · 1.61 KB
/
1502_Can Make Arithmetic Progression From Sequence
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
/*1502. Can Make Arithmetic Progression From Sequence
Runtime: 1 ms, faster than 96.36% of Java online submissions for Can Make Arithmetic Progression From Sequence.
Memory Usage: 38.9 MB, less than 85.33% of Java online submissions for Can Make Arithmetic Progression From Sequence.
*/
class Solution {
public boolean canMakeArithmeticProgression(int[] arr) {
int len= arr.length;
if(len<2)
return true;
int min=Integer.MAX_VALUE, max=Integer.MIN_VALUE;
Set<Integer> li= new HashSet();
for(int i:arr)
{
min=Math.min(min,i);
max=Math.max(max,i);
li.add(i);
}
int d=max-min;
if(d%(len -1)!=0)
return false;
d=d/(len-1);
for(int i=0;i<len-1;i++)
{if(li.contains(min+d))
min=min+d;
else
return false;
}
return true;
}
}
/*
Runtime: 1 ms, faster than 96.36% of Java online submissions for Can Make Arithmetic Progression From Sequence.
Memory Usage: 39.2 MB, less than 57.01% of Java online submissions for Can Make Arithmetic Progression From Sequence.
class Solution {
public boolean canMakeArithmeticProgression(int[] arr) {
if(arr.length<2)
return true;
Arrays.sort(arr);
int diff=arr[1]-arr[0];
for(int i=2;i<arr.length;i++)
if(diff!=(arr[i]-arr[i-1]))
return false;
return true;
}
}
*/