problem no 70: check if the given array forms Arithematic Progression or not.

 problem:

Given an array of N integers. Write a program to check whether an arithmetic progression can be formed using all the given elements. 
 

Example 1:

Input:
N=4
arr[] = { 0,12,4,8 }
Output: YES
Explanation: Rearrange given array as
{0, 4, 8, 12}  which forms an
arithmetic progression.

Example 2:

Input:
N=4
arr[] = {12, 40, 11, 20}
Output: NO

 

Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function checkIsAP() that takes array arr and integer N as parameters and return true for "Yes" and false for "No".



code:

bool checkIsAP(int arr[], int n)

    {

        sort(arr,arr+n);

        int diff=arr[1]-arr[0];

        int flagforap=1;

        for(int i=2;i<n-1;i++)

        {

          if(arr[i+1]-arr[i]!=diff)

          {

              flagforap=0;

          }

        }

        if(flagforap==0)

        {

          return false;  

        }

        else

        {

            return true;

        }

    }

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.