5101 - Design & Analysis of Algo
← 5101 - Design & Analysis of Algo
session: "5101-design-analysis-of-algo/2026-08-23 0000 DAA 23 Aug Lab"
title: "DAA 23 Aug Lab"
date: "2026-08-23 00:00"
duration: "1h 57m"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 4
transcript_chars: 62950
generated: "2026-11-23T23:27:59+05:30"
This lab session introduces pseudo-code-first problem solving through two array/stack problems: finding the first greater element to the right of every element (Next Greater Element), and finding the length of the longest balanced sub-array in an array of only 1 and -1. The class develops the brute-force solution for the first problem, analyzes its O(n²) time and O(n) space complexity, and then builds the optimal stack-based solution traversing right-to-left. The second problem is solved by brute force with a full dry run, with prefix sum flagged as the optimal approach. The session also covers Python range/reversed, monotonic stacks, the time–space trade-off, and lab/exam logistics.
-1.range(start, stop, step) with a negative step and the reversed function for right-to-left iteration.for versus while.The instructor's framing: before writing actual code, first write out the solution in pseudo-code. This matters because the logic is validated independently of language syntax; the class deliberately starts with pseudo-code rather than jumping into code.
Given an integer array of size n, for every element find the first greater element to its right; return -1 if none exists. The solution should use a stack to efficiently find the answer. Input format: first line n, second line n space-separated integers; output is space-separated integers where the ith value is the first element to the right of a[i] that is greater than a[i]. This is the running example for both brute force and the stack solution.
For each index i, scan j from i+1 to n-1; as soon as a[j] > a[i], store a[j] as the answer and break. If no greater element is found, the answer stays at its default -1. The answer is initialized to -1 by default because the last element always gets -1 (no right-side element). Indices start from 0; the inner loop starts at i+1 because we compare with the right element. This is the baseline against which the stack solution is measured.
Time complexity is counted by how many times loops are applied, especially nested loops: two nested loops each running up to about n give n × n, i.e. O(n²). Constants like -1 are dropped because for large values (e.g. 10 million) they don't matter in the long run. Space complexity is extra memory beyond the input array; here an extra result array of length n is used, so space is O(n).
Brute force repeatedly scans the same elements. Example given: with an array like 9 8 7 6 5 4 3 ... 20, the element 20 is re-checked for 10, for 9, for 8, etc. — repeated checking of the same element, which is the worst case. This motivates the stack solution.
A stack is like a stack of cloths/books — the first item put in goes to the bottom, later items go on top; the last item placed is on top. Stack follows the simple rule last in, first out (LIFO); queue is first in, first out. A monotonic stack keeps elements in a particular order, either increasing or decreasing. This is the data structure underlying the optimal solution.
Process from right to left. If the current element is greater than or equal to the element on top of the stack, that top element is "useless" (it can never be the next greater element for anything further left), so pop it. The top of the stack then represents the first useful greater element. If the stack is empty, the answer is -1. Use for when you know where the loop ends; use while when you don't know where it can go (e.g. popping until the stack condition fails). The stack solution uses both an array and a stack.
You must compromise between time complexity and space complexity — either increase time complexity or decrease space complexity, or increase space complexity. Taking the stack as another element means storing extra space, so space complexity is increased while time is reduced. The stack approach reduces time complexity but you compromise on space (you maintain both a stack and an answer array).
Given an integer array a containing only 1 and -1, a sub-array is balanced if the sum of its elements is zero. Find the length of the longest balanced sub-array; the stack must be used to help identify matching positions. The full array can be a sub-array, and a part of the array is also a sub-array. Brute force is two nested loops; prefix sum is the optimal approach (given in the comments).
When scanning to the right of the current element, if the value found is equal to the current element (not greater, not less), the code simply rejects it — it says "no." Equality is not accepted; the comparison is strictly less than, which is why an equal value to the right is rejected.
Theoretical questions (e.g., "what is the time complexity of bubble sort") and practical questions (a code block with a missing part, with the missing code given in the options). Formats: MCQs, MSQs, or match the following.
for i = 0 to n - 1 — iterates over every element index; used to compute the answer for each element.i + 1, going up to n - 1 — scans only the right side of element i; used in the brute-force comparison.-1 — default answer; used because the last element always gets -1.n × n = O(n²) — n is the array size; used to characterize the nested-loop solution.n; used to characterize memory beyond the input.n = 6, the loop runs 36 times (36 times / 36 milliseconds, as stated) — n is the array length; illustrates the n² count.range(n) goes from 0 to n-1 by default; range(i+1, n) starts at i+1 and goes up to n-1 — Python iteration bounds; used to implement the loops.range can take three values: start, stop, and step (e.g. start at n-1, stop at -1, step -1 to go right-to-left) — used for reverse iteration.i to index j: j - i + 1 — i and j are sub-array endpoints; used to compute candidate lengths in the balanced sub-array problem.max_length = max(max_length, current_length) — tracks the best length seen; used whenever a balanced sub-array is found.4 5 2 10 8 7 (indices 0 1 2 3 4 5)i = 0 (element 4): compare with 5 → 5 > 4, answer = 5, break, print 5.i = 1 (element 5): 2 is not greater; next 10 > 5 → answer = 10, break, print 10.i = 2 (element 2): 10 > 2 → answer = 10, break, print 10.i = 3 (element 10): 8 not greater, 7 not greater → answer stays -1, print -1.i = 4 (element 8): 7 not greater → answer -1.5, 10, 10, -1, ... (and -1 for 8).Full per-element table as discussed:
| Element | Right side elements | First greater element to the right |
|---|---|---|
| 4 | 5, 2, 10, 8, 7 | 5 |
| 5 | 2, 10, 8, 7 | 10 (2 is not greater than 5, so continue to 10) |
| 2 | 10, 8, 7 | 10 |
| 10 | 8, 7 | -1 (no greater element) |
| 8 | 7 | -1 (no greater element) |
| 7 | (none) | -1 (no right-side element) |
Resulting answer list: 5, 10, 10, -1, -1, -1.
4 5 2 10 8 7 (processing right to left)-1; push 7. Stack: [7].-1; push 8. Stack: [8].-1; push 10. Stack: [10].[10, 2] (10 bottom, 2 top).[10, 5].[10, 5, 4].5, 10, 10, -1, -1, -1.Hypothetical element 6 arriving: check top 4 → not greater, pop; check 5 → not greater, pop; check 10 → greater, answer = 10; push 6.
range demonstrationStarting at index 5 and stepping by -1 gives 5, 4, 3, 2, 1, 0.
Example array discussed: 4 5 2 10 8 7 on the screen. For the current element 5, looking to the right, the code finds 2, which is lesser than 5. Modified example: replacing 2 with 5, giving 4 5 5 10 8 7 (a duplicate value). In this case, the equal value to the right is not a "right" element — the code says no, because the comparison is strictly less than.
8 with elements 1, -1, 1, ... — if you add them you get 0, so the output is 8.1 + 1 - 1 up to the last element gives output 6.1, 1, -1, -1, 1, -1 (six elements).max_length = 0.for i from 0 to n-1; inner loop for j from i to n-1; maintain a sum variable; sum = sum + array[j]; if sum == 0, compute length j - i + 1 and update max_length.i = 0: j = 0, sum = 0 + array[0] = 0 + 1 = 1 → not zero, so the if is not entered (no pop; it simply does not go through the if statement). No break statement is applied, so the inner loop continues.j = 1: add another 1 → sum = 1 + 1 = 2.j = 2 (element -1): sum = 2 + (-1) = 1.j = 3 (element -1): sum = 1 + (-1) = 0 → balanced sub-array 1, 1, -1, -1; length = j - i + 1 = 3 - 0 + 1 = 4; max_length = 4.j = 4 (element 1): sum = 0 + 1 = 1 → not balanced.j = 5 (element -1): sum = 0; length = 5 - 0 + 1 = 6 → returns 6.i = 1, i = 2, … each run to j = n-1, giving O(n²).Stack shown with 10 pushed first and 2 pushed after (10 at the bottom, 2 on top); later example: stack is 6 with 10 at the bottom. When the current element comes, first check whether it is greater — if it is greater, pop; it first checks 6 and then 10.
Can we use reversed? (Sanyam)
Yes, absolutely; it is a predefined function and in many cases easier to read. Instead of for i in range(n-1, -1, -1) you can write for i in reversed(...) over the array and print the value.
Question on time vs space complexity (Robin)
The stack is used as another element, i.e., stored as space, so space complexity is increased while time is reduced — a trade-off.
"Explain this stack one more time" / "what type of stack is it"
It is a monotonic stack — a stack where elements are kept in a particular order, increasing or decreasing.
Request for a real-life example / dry run of the second problem (Sapna)
The instructor gives the dry run above, noting it is the brute force approach, not the optimal one.
"If sum is not zero, will it pop it out?" (interruption during the dry run)
No; it will not go through the if statement.
The second problem has time complexity O(n²) because of two loops — correct? (Shraddha Kamath)
Confirmed correct; that is why the optimal approach should be found and submitted. Also asked how LIFO is applicable in the first question — answered with the next greater element stack illustration (check if the current element is greater, then pop; check 6, then 10).
Assignment grading / email
Assignments are graded. The instructor has no admin access; an Excel sheet with lab links, dates and deadlines will be provided, and it will be forwarded every Sunday; it can be pinned in the WhatsApp group.
If a session is missed, can the code be done ourselves? (Ravi Ramjan Singh)
Yes. Assignment end date: one week. Any language can be used; there is no language restriction (Python is taught because of the course).
Missed first lab class / cannot submit
The instructor can extend the deadline by up to one week for those who could not submit, using the same link (or a new one). A student confirmed the "submit code" button is still enabled on MTech Test Lab 1.
Map-based approach question
A student describes using a map and storing 1 / -1 / 0 to find the longest stretch of zeros. Instructor: it is fine, but it is the brute force approach; brute force will always be greater than the optimized approach (O(n²) here). The instructor solves it brute force first because that is the first approach that comes to mind.
The assessment link routes to HackerRank? (Aditya)
Confirmed.
Asked about the nested for loop (first/second question) (Abhishek Upadhyay)
The instructor notes he did not attend the dry run and refers him to the CPT.
Share the lab link through the Moodle assignment since it is graded
The instructor says he will tell sir.
Last year's assignment/paper and final exam question types (Gotham Bharati)
The instructor does not have last year's paper or a sample paper. Mid-semester has both theoretical (MCQs/MSQs, e.g., time complexity of bubble sort) and practical (fill the missing code from options) questions; formats are MCQ, MSQ, or match the following.
For Lab 2, brute force or the discussed approach? (Sujit Kumar)
Either can be submitted (optimal or brute force); the language can be anything.
What happens if the value to the right is the same digit (duplicate), e.g., 4 5 5 10 8 7 with current element 5? (Rizwan)
It is simply rejected — the code says no; equality is not applied, which is why it is strictly less than.
What topic to prepare for the next class? (Rizwan)
The instructor does not
One line per segment, [hh:mm:ss] timestamp first.
loading…