Program to Check Arithmetic Progression
Last Updated :
13 Apr, 2025
A sequence of numbers is called an Arithmetic progression if the difference between any two consecutive terms is always the same. In simple terms, it means that the next number in the series is calculated by adding a fixed number to the previous number in the series.
Example:
Input : arr[] = {4, 1, 7, 10]
Output : Yes
The given array can become arithmetic progression. After sorting elements, we get [1, 4, 7, 10]
The difference between 2nd and 1st term 4 - 1 = 3
The difference between 3rd and 2nd term 7 - 4 = 3
The difference between 4th and 3rd term10 - 7 = 3

From the above example, we can conclude that there is some common difference between any two consecutive elements.
Input : arr[] = {1, 20, 10]
Output : No
If we arrange elements in increasing order, we get 1, 10 and 20. The common difference between 1 and 10, and 20 and 10 is not same.
Notation in Arithmetic Progression
In AP, there are some main terms that are generally used, which are denoted as:
- Initial term (a): In an arithmetic progression, the first number in the series is called the initial term.
- Common difference (d): The value by which consecutive terms increase or decrease is called the common difference. The behavior of the arithmetic progression depends on the common difference d. If the common difference is: positive, then the members (terms) will grow towards positive infinity or negative, then the members (terms) will grow towards negative infinity.
- nth Term (an): The nth term of the AP series
- Sum of the first n terms (Sn): The sum of the first n terms of the AP series.
How do we check whether a series is an arithmetic progression or not?
1. Naive Approach.
The idea is to sort the given array or series. After sorting, check if the differences between consecutive elements are the same or not. If all differences are the same, Arithmetic Progression is possible.
Example:
Given the array [6, 2, 8, 4], check if it can form an Arithmetic Progression (AP).
Steps:
1. Sort the Array
Sorted Array: [2, 4, 6, 8]
2. Find the Common Difference (d)
The common difference d is the difference between the first two elements of the sorted array.
d = arr[1] - arr[0] = 4 - 2 = 2
3. Check the Differences Between Consecutive Elements
Now, we check if the difference between each consecutive pair of elements in the sorted array is the same as the common difference d.
arr[2] - arr[1] = 6 - 4 = 2 (same as d)
arr[3] - arr[2] = 8 - 6 = 2 (same as d)
Since all the differences are the same, the array [6, 2, 8, 4] can form an Arithmetic Progression (AP) with a common difference of 2.
Below is the implementation of this approach:
C++
// C++ program to check if a given array
// can form arithmetic progression
#include <bits/stdc++.h>
using namespace std;
// Returns true if a permutation of arr[0..n-1]
// can form arithmetic progression
bool checkIsAP(int arr[], int n)
{
if (n == 1)
return true;
// Sort array
sort(arr, arr + n);
// After sorting, difference between
// consecutive elements must be same.
int d = arr[1] - arr[0];
for (int i = 2; i < n; i++)
if (arr[i] - arr[i - 1] != d)
return false;
return true;
}
// Driven Program
int main()
{
int arr[] = { 20, 15, 5, 0, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
(checkIsAP(arr, n)) ? (cout << "Yes" << endl) : (cout << "No" << endl);
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
// Returns true if a permutation of arr[0..n-1]
// can form arithmetic progression
int checkIsAP(int arr[], int n)
{
if (n == 1)
return 1;
// Sort array
qsort(arr, n, sizeof(int), cmp);
// After sorting, difference between
// consecutive elements must be same.
int d = arr[1] - arr[0];
for (int i = 2; i < n; i++)
if (arr[i] - arr[i - 1] != d)
return 0;
return 1;
}
// Comparison function for qsort
int cmp(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
// Driven Program
int main()
{
int arr[] = { 20, 15, 5, 0, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
(checkIsAP(arr, n)) ? (printf("Yes\n")) : (printf("No\n"));
return 0;
}
Java
// Java program to check if a given array
// can form arithmetic progression
import java.util.Arrays;
class GFG {
// Returns true if a permutation of
// arr[0..n-1] can form arithmetic
// progression
static boolean checkIsAP(int arr[], int n)
{
if (n == 1)
return true;
// Sort array
Arrays.sort(arr);
// After sorting, difference between
// consecutive elements must be same.
int d = arr[1] - arr[0];
for (int i = 2; i < n; i++)
if (arr[i] - arr[i - 1] != d)
return false;
return true;
}
// driver code
public static void main(String[] args)
{
int arr[] = { 20, 15, 5, 0, 10 };
int n = arr.length;
if (checkIsAP(arr, n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Anant Agarwal.
Python
# Python3 program to check if a given
# array can form arithmetic progression
# Returns true if a permutation of arr[0..n-1]
# can form arithmetic progression
def checkIsAP(arr, n):
if (n == 1): return True
# Sort array
arr.sort()
# After sorting, difference between
# consecutive elements must be same.
d = arr[1] - arr[0]
for i in range(2, n):
if (arr[i] - arr[i-1] != d):
return False
return True
# Driver code
arr = [ 20, 15, 5, 0, 10 ]
n = len(arr)
print("Yes") if(checkIsAP(arr, n)) else print("No")
# This code is contributed by Anant Agarwal.
C#
// C# program to check if a given array
// can form arithmetic progression
using System;
class GFG {
// Returns true if a permutation of
// arr[0..n-1] can form arithmetic
// progression
static bool checkIsAP(int[] arr, int n)
{
if (n == 1)
return true;
// Sort array
Array.Sort(arr);
// After sorting, difference between
// consecutive elements must be same.
int d = arr[1] - arr[0];
for (int i = 2; i < n; i++)
if (arr[i] - arr[i - 1] != d)
return false;
return true;
}
// Driver Code
public static void Main()
{
int[] arr = { 20, 15, 5, 0, 10 };
int n = arr.Length;
if (checkIsAP(arr, n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by vt_m.
JavaScript
<script>
// Javascript program to check if a given array
// can form arithmetic progression
// Returns true if a permutation of arr[0..n-1]
// can form arithmetic progression
function compare(a, b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}
function checkIsAP( arr, n){
if (n == 1)
return true;
// Sort array
arr.sort(compare);
// After sorting, difference between
// consecutive elements must be same.
let d = arr[1] - arr[0];
for (let i = 2; i < n; i++)
if (arr[i] - arr[i - 1] != d)
return false;
return true;
}
// Driven Program
let arr = [ 20, 15, 5, 0, 10 ];
let n = arr.length;
(checkIsAP(arr, n)) ? document.write("Yes <br>") : document.write("No <br>");
</script>
PHP
<?php
// PHP program to check if
// a given array can form
// arithmetic progression
// Returns true if a permutation
// of arr[0..n-1] can form
// arithmetic progression
function checkIsAP($arr, $n)
{
if ($n == 1)
return true;
// Sort array
sort($arr);
// After sorting, difference
// between consecutive elements
// must be same.
$d = $arr[1] - $arr[0];
for ($i = 2; $i < $n; $i++)
if ($arr[$i] -
$arr[$i - 1] != $d)
return false;
return true;
}
// Driver Code
$arr = array(20, 15, 5, 0, 10);
$n = count($arr);
if(checkIsAP($arr, $n))
echo "Yes";
else
echo "No";
// This code is contributed
// by Sam007
?>
Time Complexity: O(n Log n).
Auxiliary Space: O(1)
2. Efficient Approaches
Efficient Solutions can include approaches like - Hashing, Counting Sort, Hashing with simple pass. For implementation of the efficient approaches, please refer - Check whether Arithmetic Progression can be formed from the given array
Basic Program related to Arithmetic Progression
More problems related to Arithmetic Progression
Recent Articles on Arithmetic Progression!
Similar Reads
Simplification and Approximation
Profit and Loss
Mixtures & Allegations
Simple Interest & Compound Interest
Time and Work - Aptitude Questions and Answers Time and Work is one of the most basic concepts in Quantitative Aptitude, which is tested extensively in government exams. Undoubtedly, having a strong background in this topic can give candidates an upper hand and help them to score well in competitive exams. A candidate needs to be aware of the ba
13 min read
Time,Speed,and Distance
Mensuration and Geometry
Mensuration in MathsMensuration is a branch of mathematics that deals with measuring geometric figures, including their length, area, volume, and surface area. It provides formulas and techniques for calculating these attributes for 2D (plane) shapes and 3D (solid) objects.Types of Mensuration 2D Mensuration- Deals wit
4 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Introduction to GeometryGeometry is the branch of mathematics that deals with the shapes, angles, dimensions, and sizes of various things that we see in everyday life. It is mainly divided into plane and solid geometry. In plane geometry, 2d shapes such as triangles, squares, rectangles, and circles are studied. Whereas, i
9 min read
Coordinate GeometryCoordinate geometry is a branch of mathematics that combines algebra and geometry using a coordinate plane. It helps us represent points, lines, and shapes with numbers and equations, making it easier to analyze their positions, distances, and relationships. From plotting points to finding the short
3 min read
Ratio & Proportion,Percentage
Number System
Sequence & Series
Sequences and SeriesA sequence is an ordered list of numbers following a specific rule. Each number in a sequence is called a "term." The order in which terms are arranged is crucial, as each term has a specific position, often denoted as anâ, where n indicates the position in the sequence.For example:2, 5, 8, 11, 14,
10 min read
Sequences and Series FormulasSequences and Series Formulas: In mathematics, sequence and series are the fundamental concepts of arithmetic. A sequence is also referred to as a progression, which is defined as a successive arrangement of numbers in an order according to some specific rules. A series is formed by adding the eleme
10 min read
Special Series in Maths - Sequences and Series | Class 11 MathsSpecial Series: A series can be defined as the sum of all the numbers of the given sequence. The sequences are finite as well as infinite. In the same way, the series can also be finite or infinite. For example, consider a sequence as 1, 3, 5, 7, ⦠Then the series of these terms will be 1 + 3 + 5 +
10 min read