-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathadjacent.cpp
57 lines (48 loc) · 1.48 KB
/
adjacent.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
51
52
53
54
55
56
57
/*
The adjacent_difference algo produces a new sequence
in which each element is difference between adjacent
elements in original sequence.
*/
#include<bits/stdc++.h>
using namespace std;
void adjacent_difference()
{
int num;
cout << "Enter the no. of values :" << endl;
cin >> num;
vector<int> v(num), r(num);
for(int i=0; i<num; i++)
{
v[i] = i*2;
}
cout << "Original sequence :" << endl;
for(int i=0; i<num; i++)
{
cout << v[i] << " ";
}
cout << endl;
adjacent_difference(v.begin(), v.end(), r.begin());
cout<<"Resulting sequence :" << endl;
for(int i=0; i<num; i++)
{
cout<< r[i] <<" ";
}
}
int main()
{
adjacent_difference();
return 0;
}
/*
Sample Output:
Enter the no. of values :
5
Original sequence :
0 2 4 6 8
Resulting sequence :
0 2 2 2 2
*/
/*
Time-Complexity = O(N)
Space-Complexity = O(N)
*/