forked from krishnamanojpvr/DSCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
31_Longest_Arith_Sub_New.cpp
49 lines (43 loc) · 1.74 KB
/
31_Longest_Arith_Sub_New.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Write a C++ program to find the length of a
longest arithmetic subarray
SAMPLE INPUT/OUTPUT:
Number of elements:5
Array elements:
3 4 5 6 7
longest arithmetic subarray length:4
Number of elements:5
Array elements:
6 8 10 15 16
longest arithmetic subarray length:2
*/
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int noOfElem;
cout << "Number of elements:";
cin >> noOfElem;
int currDiff = 0;
int *p = new int[noOfElem];
int length = 1;
int maxN = 0;
currDiff = abs(p[1]-p[0]);
cout << "Array elements:"<<endl;
for(int i = 0; i<noOfElem; i++){
cin >> p[i];
}
for(int i = 1; i<noOfElem; i++){
if(abs(p[i]-p[i-1]) == currDiff){
length++;
}
else{
currDiff = abs(p[i] - p[i-1]);
length = 1;
}
maxN = max(maxN,length);
}
cout << "longest arithmetic subarray length:"<<maxN;
delete[] p;
return 0;
}