Перейти к содержимому

Count Indices That Escape Array | DFS + Cycle Detection | GFG POTD | C++

Aaj Ka Code

0:00 / 0:00

Count Indices That Escape Array | DFS + Cycle Detection | GFG POTD | C++

273 просмотра · 6 дней назад
Aaj Ka Code
121 подписчик
273 просмотра · 6 дней назад
🔥 GFG POTD — Count Indices That Escape Array | C++ Solution Given an array where arr[i] represents the exact jump from index i, count how many indices eventually move outside the array. Positive value → move right Negative value → move left Zero → no movement, so the index is bad A cycle means the index can never escape 🧠 APPROACH — DFS + CYCLE DETECTION We use DFS to follow the jump sequence from each index. `vis[i] == 1`: The index is currently in the DFS path. Revisiting it means a cycle. `vis[i] == 2`: The index has already been processed. `good[i]`: Stores whether the index eventually escapes. If a jump goes outside the array, that path is good. If it enters a cycle or reaches a bad index, it is bad. We reuse previously computed results to avoid repeating work. ⏱️ COMPLEXITY Time: O(n) Auxiliary Space: O(n) 💻 C++ Code: https://github.com/PiyushKumar23-12/A... 📌 Problem: Count Indices That Escape Array Platform: GeeksforGeeks Difficulty: Medium Link: https://www.geeksforgeeks.org/problem... If you found this explanation helpful, Like 👍, Comment 💬, and Subscribe ❤️ for daily DSA solutions. Code se zyada, Soch Samjho. #GFGPOTD #DSA #DFS #CycleDetection #GraphAlgorithms #Cpp #GeeksforGeeks