-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsucessive_pair-consecutive.cpp
More file actions
102 lines (89 loc) · 2.56 KB
/
sucessive_pair-consecutive.cpp
File metadata and controls
102 lines (89 loc) · 2.56 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Submitted By : DEEPAK YADAV
Question 2:
(QUEUE): Given a stack of integers, how do you check whether each successive pair of
numbers in the stack is consecutive or not. The pairs can be increasing or decreasing, and if
the stack has an odd number of elements, the element at the top is left out of a pair. For
example , if the stack of elements are [ 4,5,-2,-3,11,10,5,6,20] , then the output should be
true because each of the pairs (4,5), (-2,-3),(11,10) and (5,6) consist of consecutive numbers.
*/
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
int consecutive_pairs(queue<int> q)
/*
objective: returns whether each successive pair of numbers in the queue is consecutive or not
input parameters: none
output value: none
approach: check whether the successive pairs at the front are consecutive or not recursively
*/
{
int num1,num2;
if(!q.empty())
{
num1=q.front();
q.pop();
num2=q.front();
q.pop();
if((abs(num1)==abs(num2)+1)||(abs(num1)==abs(num2)-1))
return consecutive_pairs(q);
else
return 0;
}
else
return 1;
}
int main()
/*
objective: to check whether each successive pair of numbers in the stack is consecutive or not
input parameters:
num: integers that are to be pushed into the stack
output value: none
approach: a recursive function is called which returns a value based on the input
*/
{
stack<int> st;
queue<int> q;
int num,response;
char ch;
do{
cout<<"\nEnter a number into the stack:";
cin>>num;
st.push(num);
cout<<"\nDo you wish to enter more?(Y/N)\t ";
cin>>ch;
}while(ch=='Y'||ch=='y');
//determines the size of the stack
int length=st.size();
//if the number of integers in the stack are odd then the last integer that is pushed is popped out
// and the elements of the stack are transferred into a queue in the reverse (the order doesn't matter)
if(length%2!=0)
{
st.pop();
for(int i=0;i<length-1;i++)
{
int temp=st.top();
q.push(temp);
st.pop();
}
}
//else the elements of the stack are transferred into a queue in the reverse
else
{
for(int i=0;i<length;i++)
{
int temp=st.top();
q.push(temp);
st.pop();
}
}
response=consecutive_pairs(q);
if(response==1)
cout<<"\nEach successive pair of numbers in the stack is consecutive!";
else if(response==0)
cout<<"\nEach successive pair of numbers in the stack is not consecutive!";
else
cout<<"Error!";
}