-
Notifications
You must be signed in to change notification settings - Fork 4
/
nextTwo.cpp
50 lines (42 loc) · 1007 Bytes
/
nextTwo.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
50
// Given an array arr[] of size n, The task is to replace every element of the array by the sum of
// next two consecutive elements in a circular manner
// i.e. arr[0] = arr[1] + arr[2], arr[1] = arr[2] + arr[3], … arr[n – 1] = arr[0] + arr[1].
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
void nextTwoSum( vector<int> &a, int n){
int zero,one;
for(int i=0;i<n;i++){
if(i == 0){
zero = a[i+1] + a[i+2];
}
else if(i == 1){
one = a[i+1] + a[i+2];
}
else{
if(i == (n-2) ){
a[i] = a[i+1] + a[ (i+2) -n ];
}
else if(i == (n-1) ){
a[i] = a[(i+1) -n] + a[(i+2) -n];
}
else{
a[i] = a[i+1] + a[i+2];
}
}
}
a[0] = zero; a[1] = one;
cout<<"Output : "<<endl;
for(int j=0;j<n;j++){
cout<<a[j]<<" ";
}
} // end nextTwoSum
int main(){
int n; cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
nextTwoSum(a,n);
} // end main