A sequence of numbers is called a Harmonic progression if the reciprocal of the terms are in AP. In simple terms, a, b, c, d, e, f are in HP if 1/a, 1/b, 1/c, 1/d, 1/e, 1/f are in AP. For example, 1/a, 1/(a+d), 1/(a+2d), and so on are in HP because a, a + d, a + 2d are in AP.

Fact about Harmonic Progression :
- In order to solve a problem on Harmonic Progression, one should make the corresponding AP series and then solve the problem.
- As the nth term of an A.P is given by an = a + (n-1)d, So the nth term of an H.P is given by 1/ [a + (n -1) d].
- For two numbers, if A, G and H are respectively the arithmetic, geometric and harmonic means, then
- A ≥ G ≥ H
- A H = G2, i.e., A, G, H are in GP
- If we need to find three numbers in a H.P. then they should be assumed as 1/a–d, 1/a, 1/a+d
- Majority of the questions of H.P. are solved by first converting them into A.P
Formula of Harmonic Progression:

How we check whether a series is harmonic progression or not?
The idea is to reciprocal the given array or series. After reciprocal, check if differences between consecutive elements are same or not. If all differences are same, Arithmetic Progression is possible. So as we know if the reciprocal of the terms are in AP then given a sequence of series is in H.P. Let’s take a series 1/5, 1/10, 1/15, 1/20, 1/25 and check whether it is a harmonic progression or not. Below is the implementation:
C++
#include<bits/stdc++.h>
using namespace std;
bool checkIsHP(vector< double > &arr)
{
int n = arr.size();
if (n == 1)
{
return true ;
}
vector< int > rec;
for ( int i = 0; i < n; i++)
{
rec.push_back((1 / arr[i]));
}
sort(rec.begin(), rec.end());
int d = (rec[1]) - (rec[0]);
for ( int i = 2; i < n; i++)
{
if (rec[i] - rec[i - 1] != d)
{
return false ;
}
}
return true ;
}
int main()
{
vector< double > arr = {1 / 5, 1 / 10, 1 / 15, 1 / 20, 1 / 25};
if (checkIsHP(arr))
{
cout << "Yes" << std::endl;
}
else
{
cout << "No" <<endl;
}
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean checkIsHP( double []arr)
{
int n = arr.length;
if (n == 1 )
return true ;
ArrayList<Integer> rec = new ArrayList<Integer>();
for ( int i = 0 ; i < n; i++)
rec.add(( int )( 1 / arr[i]));
Collections.sort(rec);
int d = ( int )rec.get( 1 ) - ( int )rec.get( 0 );
for ( int i = 2 ; i < n; i++)
if (rec.get(i) - rec.get(i - 1 ) != d)
return false ;
return true ;
}
public static void main(String[] args)
{
double arr[] = { 1 / 5 , 1 / 10 , 1 / 15 ,
1 / 20 , 1 / 25 };
if (checkIsHP(arr))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkIsHP(arr):
n = len (arr)
if (n = = 1 ):
return True
rec = []
for i in range ( 0 , len (arr)):
a = 1 / arr[i]
rec.append(a)
return (rec)
rec.sort()
d = rec[ 1 ] - rec[ 0 ]
for i in range ( 2 , n):
if (rec[i] - rec[i - 1 ] ! = d):
return False
return True
if __name__ = = '__main__' :
arr = [ 1 / 5 , 1 / 10 , 1 / 15 , 1 / 20 , 1 / 25 ]
if (checkIsHP(arr)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections;
class GFG
{
static bool checkIsHP( double [] arr)
{
int n = arr.Length;
if (n == 1)
return true ;
ArrayList rec = new ArrayList();
for ( int i = 0; i < n; i++)
rec.Add(( int )(1 / arr[i]));
rec.Sort();
int d = ( int )rec[1] - ( int )rec[0];
for ( int i = 2; i < n; i++)
if (( int )rec[i] - ( int )rec[i - 1] != d)
return false ;
return true ;
}
public static void Main()
{
double [] arr = { 1/5, 1/10, 1/15,
1/20, 1/25 };
if (checkIsHP(arr))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function checkIsHP( $arr )
{
$n = count ( $arr );
if ( $n == 1)
return true;
$rec = array ();
for ( $i =0; $i < count ( $arr ); $i ++)
{
$a = 1 / $arr [ $i ];
array_push ( $rec , $a );
}
return ( $rec );
sort( $rec );
$d = $rec [1] - $rec [0];
for ( $i =2; $i < $n ; $i ++)
if ( $rec [ $i ] - $rec [ $i -1] != $d )
return false;
return true;
}
$arr = array ( 1/5, 1/10, 1/15, 1/20, 1/25 );
if (checkIsHP( $arr ))
print ( "Yes" );
else
print ( "No" );
?>
|
Javascript
<script>
function checkIsHP(arr)
{
let n = arr.length;
if (n == 1)
{
return true ;
}
let rec = [];
for (let i = 0; i < n; i++)
{
rec.push((1 / arr[i]));
}
rec.sort((a,b) => a - b);
let d = (rec[1]) - (rec[0]);
for (let i = 2; i < n; i++)
{
if (rec[i] - rec[i - 1] != d)
{
return false ;
}
}
return true ;
}
let arr = [1 / 5, 1 / 10, 1 / 15, 1 / 20, 1 / 25];
if (checkIsHP(arr))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Output:
Yes
Time Complexity: O(n Log n).
Auxiliary Space: O(n)
Basic Program related to Harmonic Progression
Recent Articles on Harmonic Progression!
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 Jul, 2022
Like Article
Save Article