🚀 Day 92/100 of #100DaysOfLeetCode 🚀 🎯 Problem: 2169. Count Operations to Obtain Zero You are given two non-negative integers num1 and num2. In one operation: If num1 >= num2, subtract num2 from num1. Otherwise, subtract num1 from num2. Repeat until either num1 or num2 becomes 0. 🔍 Goal: Find the total number of operations required to make one of them zero. 💡 Brute Force Approach: Simply simulate the subtraction steps using a while loop. int countOperations(int num1, int num2) { int count = 0; while(num1 != 0 && num2 != 0) { if(num1 >= num2) num1 -= num2; else num2 -= num1; count++; } return count; } ⏱️ Time Complexity: O(max(num1, num2)) 💾 Space Complexity: O(1) ⚡ Optimized Approach (Using Modulo): Instead of repeatedly subtracting, we can jump ahead using division & modulo — similar to the Euclidean algorithm for GCD. int countOperations(int num1, int num2) { if (num1 == 0 || num2 == 0) return 0; if (num1 < num2) swap(num1, num2); return num1 / num2 + countOperations(num1 % num2, num2); } 🔹 This optimization uses mathematical insight to skip redundant subtractions. 🔹 Works recursively, reducing both time and iterations drastically. ⏱️ Time Complexity: O(log(max(num1, num2))) 💾 Space Complexity: O(log(max(num1, num2))) (recursive stack) 🧠 Key Learnings: Recognized the pattern resembling the GCD algorithm. Reduced repeated subtraction using division and modulo. Understood base cases where either number becomes zero. ⚠️ Edge Cases to Keep in Mind: If either number starts as 0, return 0. If both numbers are equal, only one operation is needed. Large numbers — ensure no overflow issues in recursion. 🔥 Outcome: Problem Solved ✅ — Efficient, Clean, and Recursive! #100DaysOfCode #LeetCode #Day92 #Cplusplus #CodingChallenge #ProblemSolving #DSA #Algorithms #CodingJourney #LearnWithMe #TechCommunity #SoftwareEngineering #Optimization #CodeNewbie
keep grinding champ :)
👏