0% found this document useful (0 votes)
2 views2 pages

Stack Problems

Uploaded by

iamaishwarya2005
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)
2 views2 pages

Stack Problems

Uploaded by

iamaishwarya2005
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

Previous Smallest Element:

Given an array arr[], find the Previous Smaller Element (PSE) for every element in the
array.
 The Previous Smaller Element of an element x is defined as the first element to its left in
the array that is smaller than x.
 If no such element exists for a particular position, the PSE should be considered as -1.

Examples:
Input: arr[] = [1, 6, 2]
Output: [-1, 1, 1]
Explanation: For the first element 1, there is no element to its left, so the result is -1. For 6,
the previous smaller element is 1. For 2, the previous smaller element is also 1, since it is the
closest smaller number when looking left.

Input: arr[] = [1, 5, 0, 3, 4, 5]


Output: [-1, 1, -1, 0, 3, 4]
Explanation:
For 1, no element on the left → -1
For 5, the previous smaller element is 1
For 0, no smaller element on the left → -1
For 3, the previous smaller element is 0
For 4, the previous smaller element is 3
For the last 5, the previous smaller element is 4

2) Reverse a Individual Words


Given string str, we need to print the reverse of individual words.
Examples:
Input: Hello World
Output: olleH dlroW
Explanation: Each word in "Hello World" is reversed individually, preserving the original
order, resulting in "olleH dlroW".
Input: Geeks for Geeks
Output: skeeG rof skeeG

3) Given a string with brackets. If the start index of the open bracket is given, find the index
of the closing bracket. Examples:
Input : string = [ABC[23]][89]
index = 0
Output : 8
The opening bracket at index 0 corresponds
to closing bracket at index 8.
4) Given an array of n strings arr[]. The task is to determine the number of words remaining
after pairwise destruction.
If two consecutive words in the array are identical, they cancel each other out. This
process continues until no more eliminations are possible.
Examples:
Input: arr[] = ["gfg", "for", "geeks", "geeks", "for"]
Output: 1
Explanation: After the first iteration, we'll have: [gfg, for, for]. Then after the second
iteration, we'll have: [gfg]. No more eliminations are possible. Hence, the result is 1.
Input: arr[] = ["ab", "aa", "aa", "bcd", "ab"]
Output: 3
Explanation: After the first iteration, we'll have: [ab, bcd, ab]. We can't further destroy more
strings and hence we stop and the result is 3.
Input: arr[] = ["tom", "jerry", "jerry", "tom"]
Output: 0
Explanation: After the first iteration, we'll have: [tom, tom]. After the second iteration:
'empty-array' . Hence, the result is 0.

You might also like