📌  相关文章
📜  检查数组元素是否可以在AP、GP或HP中排列

📅  最后修改于: 2021-10-26 06:47:37             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] 。任务是检查是否可以通过排列数组的元素来生成算术级数、几何级数或谐波级数。如果可能,打印“是”,使用进度类型,否则打印“否”。
例子:

方法:这个想法是观察三个进程 AP、GP 或 HP 中任何一个中的元素都与排序顺序有些相关。所以,我们需要先对给定的数组进行排序。

  1. 对于算术级数检查已排序数组的连续元素之间的差异是否相同。如果是,则给定的数组元素形成算术级数
  2. 对于几何级数检查已排序数组的连续元素的比率是否相同。如果是,则给定的数组元素形成Geometric Progression
  3. 对于 Harmonic Progression检查已排序数组的所有连续元素的倒数之间的差异是否相同。如果是,则给定的数组元素形成Harmonic Progression

下面是上述方法的实现:

C++
// C++ program to check if a given
// array form AP, GP or HP
#include 
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


输出:
Yes, A HP can be formed

时间复杂度: O(N*log N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程