Professional Documents
Culture Documents
Year: 2021-22
Group No:
Group Members:
GrNo. RollNo. Name of Student
Faculty Mentor:
n
m 1|Page
Index
2. Theory 3
3. Algorithm 3
4. Code 4
5. Output 6
6. Problem Statement 2 7
7. Theory 7
8. Algorithm 7
9. Code 7
10. Output 8
11. Conclusion 9
2|Page
Problem Statement 1: Given n strings of brackets, determine whether each
sequence of brackets is balanced. If a string is balanced, return YES. Otherwise,
return NO.
INPUT: The first line contains a single integer n, the number of strings. Each of
the next n lines contains a single string s, a sequence of brackets.
OUTPUT: For each string, return YES or NO
Theory:
Stack is a linear data structure which follows a particular order in which the
operations are performed. The order may be LIFO (Last In First Out) or FILO
(First In Last Out).
Algorithm:
Declare a character stack S.
Now traverse the expression string exp.
1. If the current character is a starting bracket (‘(‘or ‘{‘or ‘[‘) then
push it to stack.
2. If the current character is a closing bracket (‘)’ or ‘}’ or ‘]’) then
pop from stack and if the popped character is the matching starting
bracket, then fine else brackets are not balanced.
3|Page
After complete traversal, if there is some starting bracket left in stack
then “not balanced”
Below image is a dry run of the above approach:
Code:
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
4|Page
for (int i = 0; i < expr.length(); i++)
{
if (expr[i] == '(' || expr[i] == '[' || expr[i] == '{')
{
s.push(expr[i]);
continue;
}
if (s.empty())
{
return false;
break;
}
switch (expr[i]) {
case ')':
x = s.top();
s.pop();
if (x == '{' || x == '[')
{
return false;
}
break;
case '}':
x = s.top();
s.pop();
if (x == '(' || x == '[')
{
return false;
}
break;
case ']':
x = s.top();
s.pop();
if (x == '(' || x == '{')
{
return false;
}
break;
}
}
return (s.empty());
}
int main()
{
int n;
cin>>n;
5|Page
string arr[n];
for(int i=0;i<n;i++)
{
string str;
cin>>str;
if (isBalanced(str))
{
arr[i]="YES";
}
else
{
arr[i]="NO";
}
}
for(int i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
Output:
Time Complexity: O(n)
Auxiliary Space: O(n) for stack.
6|Page
Problem Statement 2: Given an array of integers and a number, perform left
rotations on the array. Then print the updated array as a single line of space-
separated integers.
Input Format: The first line contains two space-separated integers denoting the
respective values of (the number of integers) and (the number of left rotations
you must perform). The second line contains space-separated integers
describing the respective elements of the array's initial state.
Output Format: Print a single line of space-separated integers denoting the final
state of the array after performing left rotations.
Algorithm:
leftRotate(arr[], d, n)
start
For i = 0 to i < d
Left rotate all elements of arr[] by one
end
Code:
#include <iostream>
using namespace std;
int main()
{
int a,d;
7|Page
cin>>a;
cin>>d;
int arr[a];
for(int i=0;i<a;i++)
{
cin>>arr[i];
}
for(int i=0;i<d;i++)
{
int k;
k=arr[0];
for(int i=0;i<a-1;i++)
{
arr[i]=arr[i+1];
}
arr[a-1]=k;
}
for(int i=0;i<a;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
Output:
8|Page
Conclusion: Thus by using the concept of stack in problem statement 1 and the
concept of array and loops in problem statement 2 we have successfully
demonstrated the working of both the code with expected and outputs and the
logic for the same.
9|Page