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
Count Good Digit Strings of Length n
More Relevant Posts
-
Day - 61 Subsets II The problem - Given array that may contain duplicates, return all possible unique subsets. Example : candidates = [10,1,2,7,6,1,5], target = 8 → [[1,1,6],[1,2,5],[1,7],[2,6]] Brute Force - Generate all combinations iteratively, still exponential, but less elegant than recursion. Approach Used - •) Sort the nums array.nums.lengthcktrack(0, nums, subset, res). •) Base case, if i == nums.length, reached end, add new ArrayList(subset) to res and return. •) Recursive First case, include nums[i], add to subset, recurse with i+1, backtrack (remove). •) Recursive Second case, exclude nums[i], recurse with i+1 (don't add). Complexity - Time - O(2^n), each element include/exclude. Space - O(n), recursion depth. Note - Sort array. After excluding an element, skip all duplicates before next recursion to avoid duplicate subsets. #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
-
Day - 63 Combination Sum III The problem - Find all combinations of k numbers that sum to n, using only numbers 1-9, each number used at most once. Example : k = 3, n = 7 → [[1,2,4]] Brute Force - Generate all C(9, k) combinations, check each sum, still needs to enumerate all combinations. Approach Used - •) Initialize: Call findCombination(k, 1, n, new ArrayList<>(), ans) (start with number 1). •) findCombination(k, num, target, lst, ans), •) If (target == 0 && k == 0), found valid combination, add new ArrayList(lst) to ans and return. •) Recursive case, for num to 9, 1 - If i > target || k <= 0, break. 2 - Add i to lst. 3 - Recurse with k - 1 (need one less number), i + 1 (next number to try), target - i (remaining sum). 4 - Backtrack: remove i from lst. Complexity - Time - O(C(9, k) × k), C(9,k) combinations, k time to copy each. Space - O(k), recursion depth. Note - Loop from 1-9, add number, recurse with k-1 and target-num. Backtrack when target == 0 && k == 0! #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
-
🚀 DSA Tip: Two Pointer Approach for Target Sum If your array is sorted, you don’t need nested loops to find a pair with a given target sum. 👉 Use the Two Pointer Technique — simple and O(n)! 💡 How it works: • Place one pointer at the start (left) • Place another at the end (right) • Compare their sum with the target ✅ If sum is small → move left++ ✅ If sum is large → move right-- ✅ If equal → 🎯 pair found ⏱️ Complexity: Time → O(n) Space → O(1) 🔥 When to use: ✔️ Target sum in sorted array ✔️ Pair problems ✔️ Remove duplicates ✔️ Reverse problems Mastering two pointers can save you from unnecessary O(n²) solutions! #DSA #CodingInterview #Java #TwoPointers #ProblemSolving #TechLearning
To view or add a comment, sign in
-
-
Day - 62 Generate Parentheses The problem - Given n pairs of parentheses, generate all combinations of well formed parentheses. Example : n = 3 → ["((()))","(()())","(())()","()(())","()()()"] Brute Force - Generate all 2^(2n) combinations, then filter for valid ones, still exponential. Approach Used - •) Initialize: Call DFS(0, 0, "", n, res) (start with 0 open, 0 close, empty string). •) DFS(openP, closeP, s, n, res), 1 - Base case, if openP == closeP == n: add s to res (valid combination) and return. 2 - Recursive First case, if openP < n: add '(', recurse with openP+1. 3 - Recursive Second case, if closeP < openP: add ')', recurse with closeP+1. Complexity - Time - O(4^n / √n), the number of valid combinations. Space - O(n), recursion depth. Note - Track open and close parentheses count. Add '(' if open < n, add ')' if close < open. Recurse until both equal n. #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
-
Day 23 - Find Minimum in Rotated Sorted Array Technique Used: Modified Binary Search Key Idea: Since the array is rotated but originally sorted, used binary search to identify the pivot (minimum element). Compared mid with end to determine which half is unsorted and adjusted boundaries accordingly. Time Complexity: O(log n) #Day23 #LeetCode #Java #BinarySearch #DSA #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 8/30 – DSA Consistency Journey 🔎 Problem 50: Greatest Character Less Than Target (Binary Search Variation) Today I practiced another powerful variation of Binary Search 🔥 🧠 Problem Statement Given: A sorted character array (ascending order) A target character (not present in array) 👉 Find the greatest character strictly smaller than the target 👉 If no such character exists, return 'a' 💡 Example Array: {'c','e','g','i','k','m'} Target: 'f' Output: 'e' 🛠 Approach Used This is a Floor Concept in Binary Search. After the loop ends: high points to the greatest character < target If high < 0 → no smaller element exists Time Complexity: O(log n) Space Complexity: O(1) 📌 What I Learned Today ✔ Binary Search Variations ✔ Floor Logic ✔ Boundary Handling ✔ Edge Case Thinking 🎯 50 Problems Completed 📅 Day 8 of 30 Small improvements daily → Big results over time 💪 #Day8 #DSA #Java #BinarySearch #ProblemSolving #CodingJourney #InterviewPrep #Consistency
To view or add a comment, sign in
-
-
LeetCode Problem Solved | #739 : Daily Temperatures Today I solved 739. Daily Temperatures - a classic problem that beautifully demonstrates the power of the Monotonic Stack pattern. 🧩 Problem Summary Given an array of daily temperatures, return an array such that for each day, you tell how many days you have to wait until a warmer temperature. If there is no future warmer day, return 0 for that day. Example: Input: [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0] 💡 Solution approach : ✔ Store indices (not temperatures). ✔ Maintain decreasing order of temperatures in the stack. ✔ When a warmer temperature appears, pop elements and calculate the difference in indices. 💻 I’ve added my Java solution in the comments below. #LeetCode #DataStructures #Java #MonotonicStack #DSA #ProblemSolving #Stack
To view or add a comment, sign in
-
-
Day - 59 Subsets The problem - Given array of unique integers, return all possible subsets (power set). Solution must not contain duplicate subsets. Example : nums = [1,2,3] → [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] Brute Force - Generate all combinations iteratively, still exponential, but less elegant than recursion. Approach Used - •) Call createSubset(nums, 0, res, subset). •) Base case, if index == nums.length, add current subset to result. •) Recursive First case, include nums[index], add to subset, recurse with index+1, backtrack (remove). •) Recursive Second case, exclude nums[index]: recurse with index+1 (don't add). Complexity - Time - O(n × 2^n),each element has 2 choices. Space - O(n), recursion depth. Note - For each element, make two choices, include it or exclude it. Recursively build all combinations #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
-
💡 Detecting Duplicates using Insertion Sort Logic (No Extra Space!) Instead of using HashSet or extra memory, I tried a different approach — modifying Insertion Sort. 👉 Idea: Assume the left part of the array is already sorted. When inserting the next element (key), we shift bigger elements right. During shifting: If we meet an element equal to the key → Duplicate found immediately If smaller → insert at correct position In simple words: Keep shifting while elements are bigger If equal → duplicate If smaller → insert 📊 Complexity: • Worst Case: O(n²) • Space: O(1) • Early exit if duplicate appears Nice reminder that sometimes classic sorting logic can double as a searching technique! #DSA #Java #Algorithms #ProblemSolving #CodingInterview
To view or add a comment, sign in
-
-
🔍 Day 45/100: Find the Index - The Substring Search Day 45. Implementing what .indexOf() does under the hood. 🔎 📌 Problem: Find the Index of the First Occurrence - EASY Find the first occurrence of needle in haystack. Return its index, or -1 if not found. Example: haystack = "sadbutsad", needle = "sad" → 0 💡 The Brute Force Approach: Check every possible starting position in haystack: 🎯 The Key Optimization: Only check positions where needle CAN fit: i <= haystack.length - needle.length No point checking position 8 if needle is 3 characters and haystack ends at 9. 📊 Complexity: O(n × m) where n = haystack length, m = needle length Not optimal (KMP is O(n+m)), but good enough for EASY. 🌱 Lesson: Sometimes brute force is the right answer. Don't over-optimize EASY problems. #100DaysOfCode #DSA #LeetCode #Day45 #Java #StringSearch #SubstringMatch #BruteForceWorks
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development