Count Good Digit Strings of Length n

Day - 57 Count Good Numbers The problem - A digit string is good if the digits at even indices are even (0,2,4,6,8) and digits at odd indices are prime (2,3,5,7). Return count of good digit strings of length n. Example : n = 1 → 5, n = 4 → 400 Brute Force - Generate all strings, but complexity would be exponential, and it is impractical to do for large numbers. Approach Used - •) Calculate even_places = (n+1)/2, odd_places = n/2. •)Use binary exponentiation, result = (5^even_places × 4^odd_places) % MOD10⁹+7. •) Binary exponentiation helper - squares base repeatedly, multiplies result when exp bit is 1. Complexity - Time - O(log n),binary exponential. Space - O(1), no extra space. Note - Even positions have 5 choices (0,2,4,6,8), odd positions have 4 choices (2,3,5,7). Answer = 5^(even_positions) × 4^(odd_positions) mod 10⁹+7. Use fast exponentiation. #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving

  • text

To view or add a comment, sign in

Explore content categories