Question -
Given an progression , you have to check that progression is Arithmetic or not ?
Example -
Input : arr[] = {1,2,3,4,5}
Output : Arithmetic Progression
Input : arr[] = {2,4,6,8,9}
Output : Not Arithmetic progression
Approach -
first we calculate d and check d with every element.
Implementation -
//check that given progression is arithmetic or not
#include<iostream>
using namespace std;
void check(int arr[],int n)
{
int a=arr[0];
int d=arr[1]-arr[0];
int check=1;
for(int i=1;i<n-1;i++){
if((arr[i+1]-arr[i])!=d){
check=0;
break;
}
}
if(check==1)
cout<<"-->>Arithmetic progression"<<endl;
else
cout<<"Not Arithmetic progression"<<endl;
}
int main(int argc, char *argv[])
{
int n;
cout<<"Enter the number of element of Arithmetic progression : ";cin>>n;
int arr[n];
cout<<"Enter elements : "<<endl;
for(int i=0;i<n;i++)
cin>>arr[i];
check(arr,n);
return 0;
}
Time complixity - O(n)
Comments
Post a Comment