forked from dheeraj-2000/dsalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissing_no_of_AP.cpp
More file actions
38 lines (31 loc) · 801 Bytes
/
missing_no_of_AP.cpp
File metadata and controls
38 lines (31 loc) · 801 Bytes
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
class Solution {
public:
int missingNumber(vector<int>& arr) {
int one = arr[1] - arr[0];
int two = arr[2] - arr[1];
int cd;
int ans;
//one > two ? return one: return two;
if (abs(one)>abs(two))
cd=two;
else
cd = one;
/* for(int i=0; i<arr.size();i++){
if(arr[i]==0)
ans = arr[0];
if(arr[i] != arr[0]+i*cd)
ans = arr[i-1]+cd;
}*/
for (int i=0; i < arr.size ()-1; i++) {
if(arr[i]==0)
ans = arr[0];
if (arr [i+1] - arr [i] == cd)
continue;
else {
ans = arr [i] + cd;
break;
}
}
return ans;
}
};