classnotes RTX 4070 Ti SUPER · faster-whisper · DeepSeek

5101 - Design & Analysis of Algo

← 5101 - Design & Analysis of Algo

DAA_23 Aug_Lab

Date 2026-08-23 Length 1h 57m Words 13,017 Segments 855
⤓ Download transcript (.txt with timestamps) ⤓ Plain transcript (.txt) ⤓ Transcript (.md)

Summary


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"


DAA 23 Aug Lab

Overview

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.

Learning outcomes

Core concepts

Pseudo-code first

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.

Next Greater Element problem

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.

Brute force (nested loops)

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.

Complexity analysis

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

Why brute force is wasteful

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.

Stack (LIFO) and the monotonic stack

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.

Stack-based solution (right-to-left)

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.

Time–space trade-off

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

Balanced sub-array problem

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

Duplicate value handling

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.

Exam question types

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.

Formulas and maths

Worked examples

Next Greater Element — brute-force trace on 4 5 2 10 8 7 (indices 0 1 2 3 4 5)

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.

Next Greater Element — stack approach trace on 4 5 2 10 8 7 (processing right to left)

Hypothetical element 6 arriving: check top 4 → not greater, pop; check 5 → not greater, pop; check 10 → greater, answer = 10; push 6.

range demonstration

Starting at index 5 and stepping by -1 gives 5, 4, 3, 2, 1, 0.

Duplicate value example

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.

Balanced sub-array — sample input/output

Balanced sub-array — dry run of the brute force (pseudo-code, not Python)

Stack illustration (LIFO)

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.

Questions asked in class

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

Transcript

One line per segment, [hh:mm:ss] timestamp first.

loading…