0% found this document useful (0 votes)
17 views4 pages

Nptel Work Shop Week 1

Uploaded by

717822y141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Nptel Work Shop Week 1

Uploaded by

717822y141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Given a sequence of ( and ) characters, your task is to


give an algorithm to check whether the sequence is a
balanced parenthesis sequence. For example, ((()()())) is
a balanced sequence whereas (()))(() is not a balanced
sequence.
Professor Alok suggests the following method for the problem.
Just count the number of (s (open parentheses) and the number
of )s (closed parentheses) and the sequence is balanced if both
the numbers are the same, and is not otherwise. Is he correct?
Why or why not?
The idea suggested by the professor may be correct for few cases such as:
1. ()()()
2. (())()
3. (()
The above examples gives true if no of ‘(‘ is equal to no of ‘)’ and false if they are
not equal. Here the number of brackets are the only concern and the sequence
of it doesn’t matters or checked.
The same idea suggested by the professor will fail for the cases such as:
1. (())()
2. )(
3. )(())(
The following example’s has same number of ‘(‘ and ‘)’ brackets but the
sequence of it is different which makes it as an not properly balanced sequence.

Give an algorithm to check whether the given input parenthesis


sequence is balanced using a stack.
Create an function named as check_parenthesis
Create an new empty stack
Iterate over the complete string of parenthesis:
If charAt[i] == ‘(‘ :
Push it into stack
Else if charAt[i] == ‘)’ :
Pop the last char from stack
If the poped char != ‘(‘:
Return false
If stack is empty:
Return true
Else:
Return false
End the function
2. Given two positive integers a and n, to compute
an using only multiplications, the straight forward
method uses n − 1 multiplications (start with a, and
multiply itself n − 1 times keeping the answer in some
variable).
• Describe a method to compute an using only O(log n)
multiplications.
The above problem can be solved in O(log n) by using the method of
divide and conquer. Which divides the n value half each time and then it
multiplies the same value again to get the needed result.
For example:
If n = 4 and a = 2 then,
2 4 = 2 2 * 22
Which is better than 2*2*2*2 according to time complexity.

The above concept can be implemented using a simple recursive call and
keeping track of the computed result value at each recursion call.

Algorithm for the problem:

Create a function having a and n as parameters

//base condition for the recursive calls


If n==0 return 1
If n==1 return a

//main recursion call


If n%2==0:
Call the function by passing the parameters as a and n/2
Else:
Call the function by passing the parameters as a and n-1/2

End the function by passing the computed result

• How many multiplications does your algorithm use to compute


a 100? Describe completely the set of multiplications and the
number of multiplications.
Figure 1 Reference : chatGPT

Total of 8 multiplication is needed.

3. Given an array of N integers in which the first n of


them are positive and the rest are negative. You don’t
know n and the goal is to find n.
• Give an O(log N) algorithm to find n.

The problem can be solved in O(log n) by using the method of binary


search which divides the complete array into half at each recursion.

Algorithm:

Create an function search with parameters low , high , arr , n


Calculate mid = high + low / 2

If mid == n
Return low
Else if mid < n
Search (low , mid -1 , arr ,n)
Else mid > n
Search (mid , high , arr , n)

End

• Give an O(log n) algorithm to find n.(Reference chatGpt)


If n is much smaller than N, first estimate an upper bound for n using exponential search, then use
binary search within this reduced range.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy