Count Good Numbers with Binary Exponentiation DSA Challenge

🔥 DSA Challenge – Day 120/360 🚀 📌 Topic: Math + Recursion 🧩 Problem: Count Good Numbers Problem Statement: Count total valid numbers of length n where even indices have 5 choices and odd indices have 4 choices. 🔍 Example: Input: n = 4 Output: 400 💡 Approach: Optimized (Binary Exponentiation) 1️⃣ Step 1 – Count even positions → use (n + 1) / 2 2️⃣ Step 2 – Count odd positions → use n / 2 3️⃣ Step 3 – Compute power using fast exponentiation and multiply ✔ Use formula: 5^(ceil(n/2)) × 4^(floor(n/2)) ✔ Apply modulo to handle large numbers ✔ Use recursion to reduce time complexity ⏱ Complexity: Time: O(log n) Space: O(log n) 📚 Key Learning: Binary Exponentiation helps reduce power calculation from O(n) to O(log n), which is very useful in large constraints problems. #DSA #Java #Coding #InterviewPrep #ProblemSolving #TechJourney #360DaysOfCode #LeetCode

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories