Skip to content

Commit cf667ba

Browse files
solved on 12/12/23
1 parent 8c99e05 commit cf667ba

5 files changed

+240
-0
lines changed

CS_34_ReverseFirstK_elements.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
queue<int> reverseElements(queue<int> q, int k)
5+
{
6+
stack<int> s;
7+
int n = q.size();
8+
// push first k element in stack
9+
for (int i = 0; i < k; i++)
10+
{
11+
s.push(q.front());
12+
q.pop();
13+
}
14+
15+
// push all k elements in queue
16+
while (!s.empty())
17+
{
18+
q.push(s.top());
19+
s.pop();
20+
}
21+
22+
// pop and push first n-k elements into queue again
23+
for (int i = 0; i < n - k; i++)
24+
{
25+
q.push(q.front());
26+
q.pop();
27+
}
28+
return q;
29+
}
30+
31+
int main()
32+
{
33+
queue<int> q;
34+
q.push(10);
35+
q.push(20);
36+
q.push(30);
37+
q.push(40);
38+
q.push(50);
39+
40+
int k = 3;
41+
q = reverseElements(q, k);
42+
43+
cout << "Queue after reversing first " << k << " elements: ";
44+
while (!q.empty())
45+
{
46+
cout << q.front() << " ";
47+
q.pop();
48+
}
49+
return 0;
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void interLeaveQueue(queue<int> &first)
5+
{
6+
queue<int> second;
7+
// push 1st half of first queue in 2nd queue
8+
int size = first.size();
9+
for (int i = 0; i < size / 2; i++)
10+
{
11+
second.push(first.front());
12+
first.pop();
13+
}
14+
15+
// merge both halves into the og queue named as first
16+
for (int i = 0; i < size / 2; i++)
17+
{
18+
first.push(second.front());
19+
second.pop();
20+
21+
first.push(first.front());
22+
first.pop();
23+
}
24+
}
25+
26+
int main()
27+
{
28+
queue<int> q;
29+
q.push(11);
30+
q.push(12);
31+
q.push(13);
32+
q.push(14);
33+
q.push(15);
34+
q.push(16);
35+
q.push(17);
36+
q.push(18);
37+
q.push(19);
38+
q.push(20);
39+
40+
interLeaveQueue(q);
41+
42+
while (!q.empty())
43+
{
44+
cout << q.front() << " ";
45+
q.pop();
46+
}
47+
cout << endl;
48+
return 0;
49+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// tc
5+
vector<int> firstNegativeInteger(vector<int> &arr, int k, int n)
6+
{
7+
queue<int> q; // store the index of the negative elements in the window
8+
vector<int> ans; // store the answer elements
9+
// process first k elements - 1st window
10+
for (int i = 0; i < k; i++)
11+
{
12+
if (arr[i] < 0)
13+
q.push(i);
14+
}
15+
16+
// process remaining windows -> remove the element which is out of the window and add the new e lement
17+
for (int i = k; i < n; i++)
18+
{
19+
// before further processing, add the negative element of the previous window in the answer arra if it exists in the queue
20+
if (!q.empty())
21+
ans.push_back(arr[q.front()]);
22+
else
23+
ans.push_back(0);
24+
// remove the front element if it is out of the window
25+
if (i - q.front() >= k)
26+
q.pop();
27+
// add the new element if it is negative, i.e. consider it for the next window if is negative
28+
if (arr[i] < 0)
29+
q.push(i);
30+
}
31+
32+
// process the last window
33+
if (!q.empty())
34+
ans.push_back(arr[q.front()]);
35+
else
36+
ans.push_back(0);
37+
38+
return ans;
39+
}
40+
41+
int main()
42+
{
43+
vector<int> arr = {2, -5, 4, -1, -2, 0, 5};
44+
int k = 3;
45+
int n = arr.size();
46+
vector<int> ans = firstNegativeInteger(arr, k, n);
47+
for (auto x : ans)
48+
cout << x << " ";
49+
cout << endl;
50+
return 0;
51+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<char> firstNonRepeating(string stream)
5+
{
6+
vector<char> ans;
7+
queue<char> q;
8+
int freq[26] = {0};
9+
10+
// traverse input stream, increase freq count of current char, add it to queue
11+
// remove it from queue if it is repeating (freq more than 1), this will help
12+
// to constrain the comparisons as we remove the repeating char, we need not to
13+
// worry about it in the next iteration.
14+
// if the current char isn't repeating add it to ans and stop
15+
for (int i = 0; i < stream.length(); i++)
16+
{
17+
char ch = stream[i];
18+
freq[ch - 'a']++;
19+
q.push(ch);
20+
21+
// finding answer
22+
while (!q.empty())
23+
{
24+
char frontChar = q.front();
25+
if (freq[frontChar - 'a'] > 1)
26+
{
27+
q.pop();
28+
}
29+
else
30+
{
31+
// freq = 1, this is the answer
32+
ans.push_back(frontChar);
33+
break;
34+
}
35+
}
36+
// here either the entire while loop has exhausted indicating that all chars
37+
// were repeated, or the 1st non-repeating char was encountered and loop broke
38+
}
39+
return ans;
40+
}
41+
42+
int main()
43+
{
44+
string stream = "bbabcbcab ";
45+
vector<char> ans = firstNonRepeating(stream);
46+
for (auto ch : ans)
47+
cout << ch << " ";
48+
cout << endl;
49+
return 0;
50+
}

Queue/ReverseQueue.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// tc: O(n) sc: O(n) - iterative
5+
// void reverseQueue(queue<int> &q){
6+
// stack<int> s;
7+
// while(!q.empty()){
8+
// s.push(q.front());
9+
// q.pop();
10+
// }
11+
// while(!s.empty()){
12+
// q.push(s.top());
13+
// s.pop();
14+
// }
15+
// }
16+
17+
// tc: O(n) sc: O(n) - recursive
18+
void reverseQueue(queue<int> &q){
19+
if(q.empty()) return; // base case
20+
int x = q.front();
21+
q.pop();
22+
reverseQueue(q);
23+
q.push(x);
24+
}
25+
26+
int main(){
27+
queue<int> q;
28+
q.push(10);
29+
q.push(20);
30+
q.push(30);
31+
q.push(40);
32+
q.push(50);
33+
reverseQueue(q);
34+
cout << "Queue after reversing: ";
35+
while(!q.empty()){
36+
cout << q.front() << " ";
37+
q.pop();
38+
}
39+
return 0;
40+
}

0 commit comments

Comments
 (0)