Skip to content

Commit b958ab1

Browse files
committed
leetcode
1 parent 754a4ec commit b958ab1

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
3+
-* 1502. Can Make Arithmetic Progression From Sequence *-
4+
5+
6+
A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.
7+
8+
Given an array of numbers arr, return true if the array can be rearranged to form an arithmetic progression. Otherwise, return false.
9+
10+
11+
12+
Example 1:
13+
14+
Input: arr = [3,5,1]
15+
Output: true
16+
Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.
17+
Example 2:
18+
19+
Input: arr = [1,2,4]
20+
Output: false
21+
Explanation: There is no way to reorder the elements to obtain an arithmetic progression.
22+
23+
24+
Constraints:
25+
26+
2 <= arr.length <= 1000
27+
-106 <= arr[i] <= 106
28+
29+
*/
30+
31+
class A {
32+
bool canMakeArithmeticProgression(List<int> arr) {
33+
if (arr.length < 2) return false;
34+
35+
arr.sort();
36+
37+
int difference = arr[1] - arr[0];
38+
for (int index = 2; index < arr.length; index++) {
39+
if ((arr[index] - arr[arr.length - 1]) != difference) {
40+
return false;
41+
}
42+
}
43+
return true;
44+
}
45+
}
46+
47+
class B {
48+
bool canMakeArithmeticProgression(List<int> arr) {
49+
int length = arr.length;
50+
int minVal = arr[0];
51+
int maxVal = arr[0];
52+
53+
for (int number in arr) {
54+
if (number < minVal) {
55+
minVal = number;
56+
}
57+
if (number > maxVal) {
58+
maxVal = number;
59+
}
60+
}
61+
62+
int diff = (maxVal - minVal) ~/ (length - 1);
63+
64+
for (int i = 0; i < length; i++) {
65+
int expected = minVal + i * diff;
66+
bool found = false;
67+
for (int number in arr) {
68+
if ((number - expected).abs() < 1e-9) {
69+
found = true;
70+
break;
71+
}
72+
}
73+
if (!found) {
74+
return false;
75+
}
76+
}
77+
78+
return true;
79+
}
80+
}
81+
82+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
// func canMakeArithmeticProgression(arr []int) bool {
4+
// length := len(arr)
5+
// minVal := arr[0]
6+
// maxVal := arr[0]
7+
8+
// for _, num := range arr {
9+
// if num < minVal {
10+
// minVal = num
11+
// }
12+
// if num > maxVal {
13+
// maxVal = num
14+
// }
15+
// }
16+
17+
// diff := float64(maxVal-minVal) / float64(length-1)
18+
19+
// for i := 0; i < length; i++ {
20+
// expected := float64(minVal) + float64(i)*diff
21+
// found := false
22+
// for _, num := range arr {
23+
// if math.Abs(float64(num)-expected) < 1e-9 {
24+
// found = true
25+
// break
26+
// }
27+
// }
28+
// if !found {
29+
// return false
30+
// }
31+
// }
32+
33+
// return true
34+
// }
35+
36+
func canMakeArithmeticProgression(arr []int) bool {
37+
seed := 1000001
38+
last := -seed
39+
40+
for _, x := range arr {
41+
if x < seed {
42+
seed = x
43+
}
44+
if x > last {
45+
last = x
46+
}
47+
}
48+
49+
diff := last - seed
50+
if diff == 0 {
51+
return true
52+
}
53+
if diff%(len(arr)-1) != 0 {
54+
return false
55+
}
56+
diff /= len(arr) - 1
57+
58+
res := 0
59+
for i, x := range arr {
60+
index := x - seed
61+
if index%diff != 0 {
62+
return false
63+
}
64+
index /= diff
65+
res ^= i ^ index
66+
}
67+
68+
return res == 0
69+
}

CanMakeArithmeticProgressionFromSequence/can_make_arithmetic_progression_from_sequence.md

Whitespace-only changes.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
222222
- [**404.** Sum of Left Leaves](SumOfLeftLeaves/sum_of_left_leaves.dart)
223223
- [**1232.** Check If It Is a Straight Line](CheckIfItIsAStraightLine/check_if_it_is_a_straight_line.dart)
224224
- [**405.** Convert a Number to Hexadecimal](ConvertANumberToHexadecimal/convert_a_number_to_hexadecimal.dart)
225+
- [**1502.** Can Make Arithmetic Progression From Sequence](CanMakeArithmeticProgressionFromSequence/can_make_arithmetic_progression_from_sequence.dart)
225226

226227
## Reach me via
227228

0 commit comments

Comments
 (0)