Search
 
SCRIPT & CODE EXAMPLE
 

CPP

arithmetic progression 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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: find n unique integers sum up to zero 
Cpp :: #include <iostream #include <stdio.h using namespace std; int main() { int a[5]; a[0]=12; a[1]=13; a[2]=14; a[3]=15; 
Cpp :: c++ throe 
Cpp :: c++ take n number from the user and store them in array and get the max, min number of them and also find the average/summation of these numbers 
Cpp :: adding two dates using necessary member function in c++ 
Cpp :: pointer in cpp details 
Cpp :: setFontSize QT 
Cpp :: get range sum 
Cpp :: function and function prototype. 
Cpp :: check .h files syntax c++ 
Cpp :: c++ if 
Cpp :: CodeChef Starters 30 Division 4 (Rated) Swapping Chefs Way 
Cpp :: turn it codechef solution in c++ 
Cpp :: how to install open cv2 in c++ on ubuntu 
Cpp :: cout two dimension array c++ 
Cpp :: bnchch 
Cpp :: how to use run total in C++ 
Cpp :: can you add a bool and an int 
Cpp :: c++ multiple if conditions 
Cpp :: c++ regex to validate indian phone number pattern 
Cpp :: 400 watt hour per kg 
Cpp :: default argument c++ 
Cpp :: c ++ loop 
Cpp :: initialise a vector c++ 
Cpp :: all usefull stls in cpp imports 
Cpp :: c++ CRL multiline string 
Cpp :: # in c++ 
Cpp :: C++ How to insert and print array elements? 
Cpp :: sprintf add two xeroes for a float number 
Cpp :: log like printf c++ 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =