Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Arithmetic Sequence Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Arithmetic Sequence | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

An Arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant. Given an unsorted array, find if it can be reordered to form an arithmetic sequence.

Naive Approach

A simple solution is to find the smallest and the second smallest element in the array and calculate the common difference (D). Then find the i-th smallest element in the array. If at any point, the difference between two consecutive elements is not equal to D return false otherwise return true.

Analysis

  • Time Complexity: O(n2)
  • Space Complexity: O(1)

Implementation

C++
bool isArithmeticSequence(vector<int> &arr) {
   int n = arr.size();
   if(n == 1) {
       return true;
   }
   int smallest = INT_MAX, secondSmallest = INT_MAX, commonDiff;
   int smallestIndx;
   for (int i = 0; i < n; i++) {
       if (arr[i] < smallest) {
           secondSmallest = smallest;
           smallest = arr[i];
           smallestIndx = i;
       }
       else if(arr[i] < secondSmallest) {
           secondSmallest = arr[i];
       }
   }
   commonDiff = secondSmallest - smallest;
   arr[smallestIndx] = INT_MAX;
   int prevSmallest = smallest;
   for (int i = 1; i < n; i++) {
       int ithSmallest = INT_MAX, ithIndex;
       for (int j = 0; j < n; j++) {
           if(arr[j] < ithSmallest) {
               ithSmallest = arr[j];
               ithIndex = j;
           }
       }
       arr[ithIndex] = INT_MAX;
       if (commonDiff != ithSmallest - prevSmallest) {
           return false;
       }
       prevSmallest = ithSmallest;
   }
   return true;
}
Java
class Solution {
   boolean isArithmeticSequence (int[] arr) {
       int n = arr.length;
       if(n == 1) {
           return true;
       }
       int smallest = Integer.MAX_VALUE; 
       int secondSmallest = Integer.MAX_VALUE, commonDiff;
       int smallestIndx = 0;
       for (int i = 0; i < n; i++) {
           if (arr[i] < smallest) {
               secondSmallest = smallest;
               smallest = arr[i];
               smallestIndx = i;
           }
           else if(arr[i] < secondSmallest) {
               secondSmallest = arr[i];
           }
       }
       commonDiff = secondSmallest - smallest;
       arr[smallestIndx] = Integer.MAX_VALUE;
       int prevSmallest = smallest;
       for (int i = 1; i < n; i++) {
           int ithSmallest = Integer.MAX_VALUE;
           int ithIndex = 0;
           for (int j = 0; j < n; j++) {
               if(arr[j] < ithSmallest) {
                   ithSmallest = arr[j];
                   ithIndex = j;
               }
           }
           arr[ithIndex] = Integer.MAX_VALUE;
           if (commonDiff != ithSmallest - prevSmallest) {
               return false;
           }
           prevSmallest = ithSmallest;
       }
       return true;
   }
}

Optimal Approach

The problem will be much easier after sorting the array as now only the differences between two consecutive elements of the sorted array need to be checked. If at any point two different common difference (D) is encountered return false otherwise return true.

Analysis

  • Time Complexity: O(n * log(n))
  • Space Complexity: O(1)

Implementation

C++
bool isArithmeticSequence(vector<int> &arr) {
   int n = arr.size();
   if (n == 1) {
       return true;
   }
   sort(arr.begin(), arr.end());
   int commonDiff = arr[1] - arr[0];
   for (int i = 1; i < n; i++) {
       if (arr[i] - arr[i - 1] != commonDiff) {
           return false;
       }
   }
   return true;
}
Java
class Solution {
   boolean isArithmeticSequence (int[] arr) {
       int n = arr.length;
       if (n == 1) {
           return true;
       }
       Arrays.sort(arr);
       int commonDiff = arr[1] - arr[0];
       for (int i = 1; i < n; i++) {
           if (arr[i] - arr[i - 1] != commonDiff) {
               return false;
           }
       }
       return true;
   }
}
Related Content
Implement Counting Sort
Implement Insertion Sort
Implement Merge Sort
Implement Quicksort
Inversion Count
Kth Largest Element
Merge Overlapping Intervals
Merge Sorted Subarrays
Square sorted array
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet