Loops Time Complexity - 7

Duration: 14 min

This video lesson is available to enrolled students.

Return to /learn/GATE-GUIDANCE-BY-SANCHIT-SIR/algorithms/time-complexity-analysis/advance-iterative-loops-code/asset-loops-time-complexity-7-19899 after enrolling

Inside: a video lesson and guided study material.

Module outline

  1. Discrete Mathematics: Set Theory, Relations, Functions, Graph Theory, Group Theory, Propositional and Predicate Logic
  2. DataBase Management System/DBMS: Basics of DBMS, ER Diagram, Relational Model & Functional Dependencies, Keys & Integrity Constraints, Normalization (1NF - BCNF), Decomposition Properties & 4NF, File Organization & Indexing, Relational Algebra, SQL, Relational Calculus, Transaction Management, Concurrency Control
  3. Digital Electronics: Digital Systems & Boolean Basics, Logic Gates & Hardware, Boolean Expression, Boolean Minimization, Combinational Circuit, Sequential Circuits, Number System, Number Representation
  4. Computer Architecture: Floating Point Rep, Cache Memory Organization, Input Output Organisation, Pipelining, Instr Formats & Modes, Control Unit Design
  5. Operating System: Introduction to OS, Process Management, CPU Scheduling, Process Synchronization, Threads & Process Creation, Deadlock, Memory Management, Virtual Memory, Disc Scheduling, File Management
  6. C Language: C Fundamentals, Control Flow, Functions, Arrays & Pointers, Storage Classes, Structures & Enums, DMA, Macros, Scoping & File Handling
  7. Data Structures: Introduction to DS, Array, Stack, Queue, Linked List, Tree, Graphs, Hashing
  8. Algorithms: Algorithm Analysis, Time Complexity Analysis, Sorting Algorithms, Greedy Algorithms, Dynamic Programming, Minimum Spanning Trees, Shortest Path Algos
  9. Computer Networks: Introduction to CN, DLL: Access Control, DLL: Flow Control, DLL: Error Control, DLL: Framing, Data Link Layer - Ethernet, Net Layer: IPv4 & Proto, Net Layer: IP Addressing, Net Layer:Routing Protocol, Transport Layer Services, TL: Congestion & UDP, Application Layer, Hardware Basics
  10. Theory Of Computation/Automata Theory: Introduction to TOC, Deterministic FA (DFA), Non-Deterministic FA, Regular Expressions, Grammar, Regular Language Properties, Moore & Mealy Machines, Pushdown Automata & CFG, Turing Machines, Complexity Theory
  11. Compiler Design: Intro to Compilers, Lexical Analysis, Grammar & CFG, Syntax Analysis: Top-Down, Syntax Analysis: Bottom-Up, Semantic Analysis & SDT, Intermediate Code Gen, Code Optimization, Run Time Environment
  12. Engineering Mathematics: Permutation and Combination, Linear Algebra, Calculus, Probability, Statistics
  13. General Aptitude: Ratio and Proportion (Ratios), Divisibility Rules, Data Interpretation, Logarithm, Number System, HCF LCM, Sequence and Series (Series), Speed Time and Distance, Series (Number and Letter Series) (Numerical Relations and Reasoning), Coding Decoding, Data Sufficiency, Non Verbal Reasoning (Spatial Aptitude) (Spatial Reasoning) (Visual Reasoning), Percentage, Mensuration and Geometry, Mental Ability, Arithmetic, Profit and Loss, Powers and Exponents (Surds and Indices), Average, Deductive and Inductive Reasoning (Logical Deduction and Induction) (Prepositional Reasoning), Syllogisms, Venn Diagram, Seating Arrangements, Blood Relations, Directions (Direction Test), Analogy, Algebra, Time and Work, Analytical Reasoning (Counting Figures Reasoning), Puzzle Solving (Puzzles), Cubes & Dices, Ranking, Order and Sequence, Mixture and Alligation, Age Problems, Clock, Selection Decision Table (Decision Making), Data Arrangement
  14. English (Verbal Aptitude): Vocabulary, Noun, Subject Verb Agreement (Verb Noun Agreement), Adjectives, Tenses, Pronoun, Preposition, Direct and Indirect Speech, Sentence Re-arrangements (Para Jumbles) (Narrative Sequencing), Sentence Completion (Fill in the blanks), Comprehension / Reading Comprehension / Unseen Passages (Critical Reasoning) (Paragraph Questions), Sentence Correction (Error Correction), Verbal Analogy (Word Based Analogy), Conjunction, Interjection, Verb, Articles, Adverb, Modals, Sentence Construction
  15. Live Classes Recordings(Earlier Batch): GATE 2026 Live Class
  16. Full Mock Test:
  17. Previous Year Papers:
  18. GATE 2026 Counselling: Counselling and Guidance Sessions
AI summary & chapters

AI Summary

An AI-generated summary of this video lecture.

This lecture focuses on determining the time complexity of various loop structures, emphasizing step-by-step tracing and mathematical derivation. The instructor begins by introducing a problem involving a loop where an integer variable `i` is initialized to `n` and modified based on a boolean flag. The core concept involves analyzing how the value of `i` changes across iterations to determine the total number of operations. The lecture progresses from simple decrementing loops to more complex structures involving conditional logic and nested loops with exponential growth. Key methods include writing down the sequence of variable values, setting up inequalities for termination conditions, and identifying dominant operations like division or squaring. The instructor demonstrates how to derive logarithmic complexities by relating the input size `n` to powers of 2, specifically showing how nested loops can result in O(log log n) complexity. The session concludes with a synthesis of these techniques, reinforcing the importance of tracing variable states to accurately assess algorithmic efficiency.

Chapters

  1. 0:00 – 2:00 00:00-02:00

    The instructor introduces the problem of determining time complexity for a specific loop structure. He initializes variables on the whiteboard, setting `i` to `n` and `flag` to 0. The code on screen shows a while loop with the condition `i >= 1` and an if statement checking `flag == 0`. The instructor points to the initialization section where `int i = n;` and `flag = 0;` are defined. He begins tracing the execution flow, noting that initially `flag` is 0, causing `i` to decrement by 1 in the first iteration. The visible text includes '11. Determine the Time Complexity of the Following Loop' and the code snippet `if(flag == 0) i = i - 1; flag = 1;`. This establishes the baseline for analyzing how variable states evolve.

  2. 2:00 – 5:00 02:00-05:00

    The instructor continues analyzing the loop by writing down the sequence of values for `i`. He notes that after the first iteration, `i` becomes `n-1`, and then roughly `n/2`. He sets up an inequality to determine the number of iterations T, writing `i = n/2 - 1 | n/2 - 1 >= 1 ? T - ④`. The derivation shows that `i` reduces from `n` to `n-1`, then roughly `n/2`, and continues this halving process. The instructor calculates the approximate number of iterations based on the condition `i >= 1`. Key visible events include writing the sequence `n, n-1, (n-1)/2` and deriving a formula for the loop's termination condition. The text on screen shows `i = (n-1)/2 ≈ n/2 | n/2 >= 1? T - ③`, indicating the transition from linear decrement to logarithmic reduction.

  3. 5:00 – 10:00 05:00-10:00

    The instructor breaks down the execution into steps based on a flag variable, showing that `i` is decremented by 1 once and then divided by 2 repeatedly. He tracks the value of `i` through iterations (n, n-1, n/2, n/4...) to determine the total number of operations. The visual derivation labels steps 1, 2, 3, 4 and writes the final time complexity as O(log n). The instructor points to the sequence of values for `i` and analyzes the condition `i >= 1`. The code snippet on screen includes `else { i = i / 2; flag = 0; }`, which is crucial for the halving behavior. This section solidifies the method of identifying dominant operations and summing up iterations to find complexity.

  4. 10:00 – 14:17 10:00-14:17

    The lecture shifts to a nested loop structure where an outer for-loop runs from 1 to n and an inner while-loop squares the variable `j` until it exceeds `n`. The instructor breaks down the execution of the inner loop by showing how `j` evolves: starting at 2, then squaring to 4 (2^2), then 16 (2^4), and so on. He writes out the sequence of values for `j` as powers of 2 to demonstrate that the inner loop runs logarithmically with respect to `n`, specifically O(log log n). The instructor draws a bracket grouping the inner loop iterations and writes (log log n) underneath. He points to the outer for-loop initialization `i = 1` and combines it with the inner loop complexity. The final calculation shows T(n) = (m * log(log n)), reinforcing the concept of nested loop analysis.

The lecture systematically builds understanding of time complexity analysis through concrete examples. The first problem demonstrates how conditional logic within a loop can create mixed behavior (decrement then halve), leading to logarithmic complexity O(log n). The instructor emphasizes tracing variable values step-by-step and setting up inequalities to find termination conditions. The second problem introduces nested loops with exponential growth, showing how squaring a variable leads to O(log log n) complexity for the inner loop. Key takeaways include recognizing patterns in variable updates, relating input size to powers of 2 for logarithmic analysis, and combining complexities from nested structures. The visual derivations on the whiteboard serve as a guide for students to replicate the analysis process, ensuring they can identify dominant operations and calculate total iterations accurately.

Loading lesson…