commit 5c96deee302a46f95974ff81b97774a243fa46b7 parent b2a0edc84d24edc9f0e722dbb8d926a14d1efdc4 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Mon, 5 Jun 2023 21:47:38 -0500 Completed arithmetic progression problem using dart Diffstat:
| A | can-make-arithmetic-progression-from-sequence/can-make-arithmetic-progression-from-sequence.dart | | | 23 | +++++++++++++++++++++++ |
1 file changed, 23 insertions(+), 0 deletions(-)
diff --git a/can-make-arithmetic-progression-from-sequence/can-make-arithmetic-progression-from-sequence.dart b/can-make-arithmetic-progression-from-sequence/can-make-arithmetic-progression-from-sequence.dart @@ -0,0 +1,23 @@ +//Given a list of arrays return true +//if there is a possible configuration of the list +//such that the difference between i and i-1 is always the same for +//i < arr.length. + +//To solve I ordered the list and checked to see if the differences were all the same. +//If they are not return false if they are return true. + +//Time: 266ms Beats: 100% +//Memory: 145.2MB Beats: 100% + +class Solution { + bool canMakeArithmeticProgression(List<int> arr) { + arr.sort(); + int diff = arr[1] - arr[0]; + for(int i = 1 ; i < arr.length ; ++i){ + if(arr[i] - arr[i - 1] != diff){ + return false; + } + } + return true; + } +}