You are on page 1of 9

BRACT’s

Vishwakarma Institute of Information Technology, Kondhwa(BK),


Pune-48

Department of Computer Engineering

Fundamentals of Data Structure

SY BTech Computer Engineering

Year: 2021-22

Group No:

Group Members:
GrNo. RollNo. Name of Student

Faculty Mentor:
n
m 1|Page
Index

Sr. Title Page


No.
1. Problem Statement 1 3

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).

There are many real-life examples of a stack. Consider an example of plates


stacked over one another in the canteen. The plate which is at the top is the first
one to be removed, i.e., the plate which has been placed at the bottommost
position remains in the stack for the longest period of time. So, it can be simply
seen to follow LIFO (Last In First Out)/FILO (First In Last Out) order.

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;
 

bool isBalanced(string expr)


{
    stack<char> s;
    char x;

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.

Theory: An array is a collection of items stored at contiguous memory


locations. The idea is to store multiple items of the same type together. This
makes it easier to calculate the position of each element by simply adding an
offset to a base value, i.e., the memory location of the first element of the array
(generally denoted by the name of the array). The base value is index 0 and the
difference between the two indexes is the offset.

Algorithm:
leftRotate(arr[], d, n)
start
For i = 0 to i < d
Left rotate all elements of arr[] by one
end

To rotate by one, store arr[0] in a temporary variable temp, move arr[1] to


arr[0], arr[2] to arr[1] …and finally temp to arr[n-1]. Let us take the same
example arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2 . Rotate arr[] by one 2 times . We get
[2, 3, 4, 5, 6, 7, 1] after first rotation and [ 3, 4, 5, 6, 7, 1, 2] after second
rotation.

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:

Time complexity: O (n * d) 


Auxiliary Space: O (1)

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

You might also like