-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
25 lines (22 loc) · 853 Bytes
/
Solution.cs
File metadata and controls
25 lines (22 loc) · 853 Bytes
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
namespace LeetCode.Problem1502{
//1502. Can Make Arithmetic Progression From Sequence
//https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/
/*
A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.
Given an array of numbers arr, return true if the array can be rearranged to form an arithmetic progression. Otherwise, return false.
*/
public class Solution {
public bool CanMakeArithmeticProgression(int[] arr) {
var nums = arr.OrderBy(p => p).ToArray();
var diff = Math.Abs(nums[1] - nums[0]);
for (int i = 1; i < nums.Length; i++)
{
if (nums[i] - nums[i - 1] == diff)
diff = nums[i] - nums[i - 1];
else
return false;
}
return true;
}
}
}