leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

can-make-arithmetic-progression-from-sequence.dart (659B)


      1 //Given a list of arrays return true
      2 //if there is a possible configuration of the list
      3 //such that the difference between i and i-1 is always the same for
      4 //i < arr.length.
      5 
      6 //To solve I ordered the list and checked to see if the differences were all the same.
      7 //If they are not return false if they are return true.
      8 
      9 //Time: 266ms Beats: 100%
     10 //Memory: 145.2MB Beats: 100%
     11 
     12 class Solution {
     13   bool canMakeArithmeticProgression(List<int> arr) {
     14       arr.sort();
     15       int diff = arr[1] - arr[0];
     16       for(int i = 1 ; i < arr.length ; ++i){
     17           if(arr[i] - arr[i - 1] != diff){
     18               return false;
     19           }
     20       }
     21       return true;
     22   }
     23 }