Open In App

Check if elements of array can be arranged in AP, GP or HP

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of N integers. The task is to check whether by arranging the elements of the array, is it possible to generate an Arithmetic Progression, Geometric Progression or Harmonic Progression. If possible print “Yes”, with the type of Progression or Else print “No”.
Examples: 
 

Input: arr[] = {2, 16, 4, 8} 
Output: Yes, A GP can be formed 
Explanation: 
Rearrange given array as {2, 4, 8, 16}, forms a Geometric Progression with common ratio 2.
Input: arr[] = {15, 10, 15, 5} 
Output: Yes, An AP can be formed 
Explanation: 
Rearrange given array as {5, 10, 15, 20}, forms Arithmetic Progression with common difference 5.
Input: arr[] = { 1.0/10.0, 1.0/5.0, 1.0/15.0, 1.0/20.0 } 
Output: Yes, A HP can be formed 
Explanation: 
Rearrange given array as { 1.0/5.0, 1.0/10.0, 1.0/15.0, 1.0/20.0 }, forms a Harmonic Progression. 
 

 

Approach: The idea is to observe that elements in any of the three progressions A.P., G.P. or H.P. are somewhat related to sorted order. So, we need to first sort the given array. 
 

  1. For Arithmetic Progression: Check if the difference between the consecutive elements of the sorted array are same or not. If Yes then given array element forms an Arithmetic Progression.
  2. For Geometric Progression: Check if the ratio of the consecutive elements of the sorted array are same or not. If Yes then given array element forms a Geometric Progression.
  3. For Harmonic Progression: Check if the difference between the reciprocal of all the consecutive elements of the sorted array are same or not. If Yes then given array element forms a Harmonic Progression.

Below is the implementation of the above approach: 
 

C++




// C++ program to check if a given
// array form AP, GP or HP
#include <bits/stdc++.h>
using namespace std;
 
// Returns true if arr[0..n-1]
// can form AP
bool checkIsAP(double arr[], int n)
{
    // Base Case
    if (n == 1)
        return true;
 
    // Sort array
    sort(arr, arr + n);
 
    // After sorting, difference
    // between consecutive elements
    // must be same.
    double d = arr[1] - arr[0];
 
    // Traverse the given array and
    // check if the difference
    // between ith element and (i-1)th
    // element is same or not
    for (int i = 2; i < n; i++) {
        if (arr[i] - arr[i - 1] != d) {
            return false;
        }
    }
 
    return true;
}
 
// Returns true if arr[0..n-1]
// can form GP
bool checkIsGP(double arr[], int n)
{
    // Base Case
    if (n == 1)
        return true;
 
    // Sort array
    sort(arr, arr + n);
 
    // After sorting, common ratio
    // between consecutive elements
    // must be same.
    double r = arr[1] / arr[0];
 
    // Traverse the given array and
    // check if the common ratio
    // between ith element and (i-1)th
    // element is same or not
    for (int i = 2; i < n; i++) {
        if (arr[i] / arr[i - 1] != r)
            return false;
    }
 
    return true;
}
 
// Returns true if arr[0..n-1]
// can form HP
bool checkIsHP(double arr[], int n)
{
    // Base Case
    if (n == 1) {
        return true;
    }
 
    double rec[n];
 
    // Find reciprocal of arr[]
    for (int i = 0; i < n; i++) {
        rec[i] = ((1 / arr[i]));
    }
 
    // After finding reciprocal, check if
    // the reciprocal is in A. P.
    // To check for A.P.
    if (checkIsAP(rec, n))
        return true;
    else
        return false;
}
 
// Driver's Code
int main()
{
    double arr[] = { 1.0 / 5.0, 1.0 / 10.0,
                     1.0 / 15.0, 1.0 / 20.0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int flag = 0;
 
    // Function to check AP
    if (checkIsAP(arr, n)) {
        cout << "Yes, An AP can be formed"
             << endl;
        flag = 1;
    }
 
    // Function to check GP
    if (checkIsGP(arr, n)) {
        cout << "Yes, A GP can be formed"
             << endl;
        flag = 1;
    }
 
    // Function to check HP
    if (checkIsHP(arr, n)) {
        cout << "Yes, A HP can be formed"
             << endl;
        flag = 1;
    }
 
    else if (flag == 0) {
        cout << "No";
    }
 
    return 0;
}


Java




// Java program to check if a given
// array form AP, GP or HP
import java.util.*;
 
class GFG{
  
// Returns true if arr[0..n-1]
// can form AP
static boolean checkIsAP(double arr[], int n)
{
    // Base Case
    if (n == 1)
        return true;
  
    // Sort array
    Arrays.sort(arr);
  
    // After sorting, difference
    // between consecutive elements
    // must be same.
    double d = arr[1] - arr[0];
  
    // Traverse the given array and
    // check if the difference
    // between ith element and (i-1)th
    // element is same or not
    for (int i = 2; i < n; i++) {
        if (arr[i] - arr[i - 1] != d) {
            return false;
        }
    }
  
    return true;
}
  
// Returns true if arr[0..n-1]
// can form GP
static boolean checkIsGP(double arr[], int n)
{
    // Base Case
    if (n == 1)
        return true;
  
    // Sort array
    Arrays.sort(arr);
  
    // After sorting, common ratio
    // between consecutive elements
    // must be same.
    double r = arr[1] / arr[0];
  
    // Traverse the given array and
    // check if the common ratio
    // between ith element and (i-1)th
    // element is same or not
    for (int i = 2; i < n; i++) {
        if (arr[i] / arr[i - 1] != r)
            return false;
    }
  
    return true;
}
  
// Returns true if arr[0..n-1]
// can form HP
static boolean checkIsHP(double arr[], int n)
{
    // Base Case
    if (n == 1) {
        return true;
    }
  
    double []rec = new double[n];
  
    // Find reciprocal of arr[]
    for (int i = 0; i < n; i++) {
        rec[i] = ((1 / arr[i]));
    }
  
    // After finding reciprocal, check if
    // the reciprocal is in A. P.
    // To check for A.P.
    if (checkIsAP(rec, n))
        return true;
    else
        return false;
}
  
// Driver's Code
public static void main(String[] args)
{
    double arr[] = { 1.0 / 5.0, 1.0 / 10.0,
                     1.0 / 15.0, 1.0 / 20.0 };
    int n = arr.length;
    int flag = 0;
  
    // Function to check AP
    if (checkIsAP(arr, n)) {
        System.out.print("Yes, An AP can be formed"
             +"\n");
        flag = 1;
    }
  
    // Function to check GP
    if (checkIsGP(arr, n)) {
        System.out.print("Yes, A GP can be formed"
             +"\n");
        flag = 1;
    }
  
    // Function to check HP
    if (checkIsHP(arr, n)) {
        System.out.print("Yes, A HP can be formed"
             +"\n");
        flag = 1;
    }
  
    else if (flag == 0) {
        System.out.print("No");
    }
}
}
 
// This code is contributed by PrinciRaj1992


Python3




# Python3 program to check if a 
# given array form AP, GP or HP
 
# Returns true if arr[0..n-1]
# can form AP
def checkIsAP(arr, n):
 
    # Base Case
    if (n == 1):
        return True
 
    # Sort array
    arr.sort();
 
    # After sorting, difference
    # between consecutive elements
    # must be same.
    d = arr[1] - arr[0]
 
    # Traverse the given array and
    # check if the difference
    # between ith element and (i-1)th
    # element is same or not
    for i in range(2, n):
        if (arr[i] - arr[i - 1] != d):
            return False
 
    return True
 
# Returns true if arr[0..n-1]
# can form GP
def checkIsGP(arr, n):
 
    # Base Case
    if (n == 1):
        return True
 
    # Sort array
    arr.sort()
 
    # After sorting, common ratio
    # between consecutive elements
    # must be same.
    r = arr[1] / arr[0]
 
    # Traverse the given array and
    # check if the common ratio
    # between ith element and (i-1)th
    # element is same or not
    for i in range(2, n):
        if (arr[i] / arr[i - 1] != r):
            return False
 
    return True
 
# Returns true if arr[0..n-1]
# can form HP
def checkIsHP(arr, n):
 
    # Base Case
    if (n == 1):
        return True
 
    rec = []
 
    # Find reciprocal of arr[]
    for i in range(0, n):
        rec.append((1 / arr[i]))
 
    # After finding reciprocal, check
    # if the reciprocal is in A. P.
    # To check for A.P.
    if (checkIsAP(rec, n)):
        return True
    else:
        return False
 
# Driver Code
arr = [ 1.0 / 5.0, 1.0 / 10.0,
        1.0 / 15.0, 1.0 / 20.0 ]
n = len(arr)
flag = 0
 
# Function to check AP
if (checkIsAP(arr, n)):
    print("Yes, An AP can be formed", end = '\n')
    flag = 1
 
# Function to check GP
if (checkIsGP(arr, n)):
    print("Yes, A GP can be formed", end = '\n')
    flag = 1
     
# Function to check HP
if (checkIsHP(arr, n)):
    print("Yes, A HP can be formed", end = '\n')
    flag = 1
 
elif (flag == 0):
    print("No", end = '\n')
     
# This code is contributed by Pratik


C#




// C# program to check if a given
// array form AP, GP or HP
using System;
 
class GFG{
   
// Returns true if arr[0..n-1]
// can form AP
static bool checkIsAP(double []arr, int n)
{
    // Base Case
    if (n == 1)
        return true;
   
    // Sort array
    Array.Sort(arr);
   
    // After sorting, difference
    // between consecutive elements
    // must be same.
    double d = arr[1] - arr[0];
   
    // Traverse the given array and
    // check if the difference
    // between ith element and (i-1)th
    // element is same or not
    for (int i = 2; i < n; i++) {
        if (arr[i] - arr[i - 1] != d) {
            return false;
        }
    }
   
    return true;
}
   
// Returns true if arr[0..n-1]
// can form GP
static bool checkIsGP(double []arr, int n)
{
    // Base Case
    if (n == 1)
        return true;
   
    // Sort array
    Array.Sort(arr);
   
    // After sorting, common ratio
    // between consecutive elements
    // must be same.
    double r = arr[1] / arr[0];
   
    // Traverse the given array and
    // check if the common ratio
    // between ith element and (i-1)th
    // element is same or not
    for (int i = 2; i < n; i++) {
        if (arr[i] / arr[i - 1] != r)
            return false;
    }
   
    return true;
}
   
// Returns true if arr[0..n-1]
// can form HP
static bool checkIsHP(double []arr, int n)
{
    // Base Case
    if (n == 1) {
        return true;
    }
   
    double []rec = new double[n];
   
    // Find reciprocal of []arr
    for (int i = 0; i < n; i++) {
        rec[i] = ((1 / arr[i]));
    }
   
    // After finding reciprocal, check if
    // the reciprocal is in A. P.
    // To check for A.P.
    if (checkIsAP(rec, n))
        return true;
    else
        return false;
}
   
// Driver's Code
public static void Main(String[] args)
{
    double []arr = { 1.0 / 5.0, 1.0 / 10.0,
                     1.0 / 15.0, 1.0 / 20.0 };
    int n = arr.Length;
    int flag = 0;
   
    // Function to check AP
    if (checkIsAP(arr, n)) {
        Console.Write("Yes, An AP can be formed"
             +"\n");
        flag = 1;
    }
   
    // Function to check GP
    if (checkIsGP(arr, n)) {
        Console.Write("Yes, A GP can be formed"
             +"\n");
        flag = 1;
    }
   
    // Function to check HP
    if (checkIsHP(arr, n)) {
        Console.Write("Yes, A HP can be formed"
             +"\n");
        flag = 1;
    }
   
    else if (flag == 0) {
        Console.Write("No");
    }
}
}
 
// This code is contributed by PrinciRaj1992


Javascript




<script>
 
// Javascript program to check if a given
// array form AP, GP or HP
 
// Returns true if arr[0..n-1]
// can form AP
function checkIsAP(arr, n)
{
    // Base Case
    if (n == 1)
        return true;
 
    // Sort array
    arr.sort(function(a,b){return a-b;});
 
    // After sorting, difference
    // between consecutive elements
    // must be same.
    var d = arr[1] - arr[0];
 
    // Traverse the given array and
    // check if the difference
    // between ith element and (i-1)th
    // element is same or not
    for (var i = 2; i < n; i++) {
        if (arr[i] - arr[i - 1] != d) {
            return false;
        }
    }
 
    return true;
}
 
// Returns true if arr[0..n-1]
// can form GP
function checkIsGP(arr, n)
{
    // Base Case
    if (n == 1)
        return true;
 
    // Sort array
    arr.sort();
 
    // After sorting, common ratio
    // between consecutive elements
    // must be same.
    var r = arr[1] / arr[0];
 
    // Traverse the given array and
    // check if the common ratio
    // between ith element and (i-1)th
    // element is same or not
    for (var i = 2; i < n; i++) {
        if (arr[i] / arr[i - 1] != r)
            return false;
    }
 
    return true;
}
 
// Returns true if arr[0..n-1]
// can form HP
function checkIsHP(arr, n)
{
    // Base Case
    if (n == 1) {
        return true;
    }
 
    var rec = Array(n).fill(0);
 
    // Find reciprocal of arr[]
    for (var i = 0; i < n; i++) {
        rec[i] = ((1 / arr[i]));
    }
 
    // After finding reciprocal, check if
    // the reciprocal is in A. P.
    // To check for A.P.
    if (checkIsAP(rec, n))
        return true;
    else
        return false;
}
 
// Driver's Code
var arr = [ 1.0 / 5.0, 1.0 / 10.0,
                 1.0 / 15.0, 1.0 / 20.0 ];
var n = arr.length;
var flag = 0;
 
// Function to check AP
if (checkIsAP(arr, n)) {
    document.write("Yes, An AP can be formed");
    flag = 1;
}
// Function to check GP
if (checkIsGP(arr, n)) {
    document.write("Yes, A GP can be formed");
    flag = 1;
}
// Function to check HP
if (checkIsHP(arr, n)) {
    document.write("Yes, A HP can be formed");
    flag = 1;
}
else if (flag == 0) {
    document.write("No");
}
 
</script>


Output: 

Yes, A HP can be formed

 

Time Complexity: O(N*log N)

Auxiliary Space: O(N)
 



Last Updated : 29 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads