📘 DSA Journey — Day 34 Today’s focus: Binary Search on answer space. Problem solved: • Arranging Coins (LeetCode 441) Concepts used: • Binary Search • Mathematical observation • Search space reduction Key takeaway: The goal is to find how many complete rows of coins can be formed, where the i-th row requires i coins. This forms a sequence: 1 + 2 + 3 + ... + k ≤ n Instead of iterating linearly, we use binary search on k: Check if: k * (k + 1) / 2 ≤ n • If true → try larger k • Else → reduce k This allows us to find the maximum valid k in O(log n) time. Continuing to strengthen fundamentals and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
Binary Search on Answer Space for Coin Problem
More Relevant Posts
-
📘 DSA Journey — Day 29 Today’s focus: Binary Search on answer space. Problem solved: • Find Peak Element (LeetCode 162) Concepts used: • Binary Search • Observing slope / trend • Search space reduction Key takeaway: The goal is to find a peak element (an element greater than its neighbors). Instead of checking all elements, we use binary search by observing the slope: • If nums[mid] < nums[mid + 1] → we are on an increasing slope, so a peak must exist on the right side • Else → we are on a decreasing slope, so a peak lies on the left side (including mid) By following this logic, we eliminate half of the search space each time and find a peak in O(log n) time. The key insight is: A peak is guaranteed to exist, and the direction of slope helps guide the search. Continuing to strengthen binary search intuition and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🔥 Day 83 of my LeetCode Journey 🔥 📘 Problem: 367. Valid Perfect Square 🎯 Difficulty: Easy 🔹 Problem Statement: Given a positive integer num, return true if num is a perfect square, otherwise return false. A perfect square is an integer that is the square of another integer. You must not use any built-in library function like sqrt. 🔹 Approach Used: Apply Binary Search on range 0 to num Find mid and compute mid * mid If equal to num → return true If mid * mid is greater → search left half If smaller → search right half Repeat until condition satisfies or range ends 🔹 Key Concepts: Binary Search Handling large numbers (use long to avoid overflow) Mathematical reasoning Efficient searching 🔹 Learning: This problem shows how binary search can be applied beyond arrays to mathematical problems. It also emphasizes handling overflow carefully when dealing with large 📌 Think beyond arrays — binary search works on answer space too 🚀 #LeetCode #Day83 #Java #BinarySearch #Math #DSA #ProblemSolving
To view or add a comment, sign in
-
-
Day 33/160 of my DSA Journey Today I solved the Aggressive Cows problem. Key Insight: The problem is about maximizing the minimum distance, which hints at Binary Search on Answer. Instead of trying all placements, we search for the largest minimum distance that is possible. Approach: • Sorted the stalls array • Applied binary search on distance (low = 0, high = max distance) • For each mid distance, checked if we can place all cows • If possible → tried for a larger distance • Else → reduced the distance • Stored the maximum valid distance as answer Helper Function (canPlace): • Placed first cow at first stall • Placed next cows only if distance ≥ mid • Counted cows placed and checked if ≥ k Concepts reinforced: • Binary Search on Answer • Greedy placement strategy • Search space optimization Time Complexity: O(n log(maxDistance)) ⚡ Another step forward in problem solving 🚀 #Day33 #160DaysChallenge #DSA #GeeksforGeeks #LeetCode #CodingJourney #ProblemSolving #Algorithms #Java
To view or add a comment, sign in
-
-
Day 59 of My DSA Journey Today I solved LeetCode 762 – Prime Number of Set Bits in Binary Representation on . 📌 Problem Given two integers "left" and "right", count how many numbers in this range have a prime number of set bits (1s) in their binary representation. Example: Input: "left = 6, right = 10" Output: "4" --- 🧠 Approach – Bit Manipulation + Prime Check Steps I followed: • For each number in range "[left, right]": - Count number of set bits (1s) using bit manipulation ("n & 1", "n >> 1") • Check if the count is a prime number - Used a predefined list of primes: "{2,3,5,7,11,13,17,19}" • If yes → increment the answer --- ⏱ Time Complexity: "O(n * log n)" Where "n = right - left + 1" (each number takes log n for bit counting) 📦 Space Complexity: "O(1)" — Constant extra space --- 💡 Key Learnings ✔ Practicing Bit Manipulation concepts ✔ Efficiently counting set bits ✔ Combining multiple concepts (bits + prime check) 👉 This problem shows how combining simple ideas can solve interesting problems 🚀 --- Consistency continues — learning something new every day 💪🔥 #100DaysOfCode #DSA #BitManipulation #Math #LeetCode #Java #ProblemSolving #CodingJourney #DeveloperJourney #Programming #SoftwareEngineering
To view or add a comment, sign in
-
-
**Day 118 of #365DaysOfLeetCode Challenge** Today’s problem: **Arranging Coins (LeetCode 441)** We are given `n` coins and need to build a staircase: * Row 1 → 1 coin * Row 2 → 2 coins * Row 3 → 3 coins Find how many **complete rows** can be formed. 💡 **Core Idea:** To build `k` complete rows, we need: `1 + 2 + 3 + ... + k` Which equals: `k * (k + 1) / 2` So we need the **largest k** such that: `k * (k + 1) / 2 <= n` This becomes a perfect **Binary Search** problem. 📌 **Approach:** Search between: * `left = 1` * `right = n` For each mid: * Compute coins needed for `mid` rows * If valid → try larger value * Else → move left At the end, `right` stores the answer. ⚡ **Time Complexity:** O(log n) ⚡ **Space Complexity:** O(1) **What I learned today:** Many math problems can be transformed into search problems. Instead of building rows one by one: 👉 Search the answer directly. 💭 **Key Takeaway:** When condition changes monotonically: * smaller values valid * larger values invalid Think: 👉 Binary Search on Answer #LeetCode #DSA #BinarySearch #Math #Java #CodingChallenge #ProblemSolving #TechJourney #Consistency
To view or add a comment, sign in
-
-
📊 DSA Progress Update – Week 5 Over the past few days, I’ve been practicing array problems and improving my approach step by step. What I learned: • Starting with a brute force solution helps in understanding the problem clearly • Optimization becomes easier once the pattern is identified • Key techniques : - Two Pointers - Sliding Window - Prefix Sum - Kadane’s Algorithm Something new: Started learning Binary Search and understanding its basic approach. Plan: Continue practicing and get more comfortable with these concepts. Taking it one step at a time. #DSA #Java #LeetCode #Consistency #CodingJourney
To view or add a comment, sign in
-
#CodeEveryday — My DSA Journey | Day 13 🧩 Problem Solved: Kth Missing Positive Number (LeetCode #1539) 💭 What I Learned: Used binary search to efficiently find the k-th missing positive number. Key idea: 👉 At any index mid, the number of missing elements before it is: arr[mid] - (mid + 1) At each step: ✔️ Calculated how many numbers are missing till mid ✔️ Adjusted search space based on whether it’s less than or greater than k Finally used low + k to compute the answer. This helped me understand how to transform a problem into a mathematical observation + binary search. ⏱ Time Complexity: O(log n) 🧠 Space Complexity: O(1) ⚡ Key Takeaways: ✔️ Binary search can be applied on derived values, not just indices ✔️ Identifying patterns like “missing count till index” simplifies problems ✔️ Mathematical insight + binary search = optimal solution 💻 Language Used: Java ☕ 📘 Concepts: Binary Search · Math Insight · Arrays · Optimization #CodeEveryday #DSA #LeetCode #Java #BinarySearch #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Progress – Rotate Array Kicking off today’s DSA practice, I solved the “Rotate Array” problem on LeetCode. 🔍 Problem Overview: Given an integer array, rotate the array to the right by k steps, where k is non-negative. The rotation must be done in-place without using extra space. 💡 My Approach: Used the array reversal technique for optimal performance First reversed the initial part, then the remaining part, and finally the entire array Applied k = k % n to handle cases where k is greater than array size 📈 Key Takeaway: This problem reinforced my understanding of in-place array manipulation and how clever use of reversal algorithms can optimize both time and space complexity. Consistency is building confidence step by step 💯 #LeetCode #DSA #Java #Arrays #ProblemSolving #CodingJourney #SoftwareEngineering #Consistency #Learning
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 12 🧩 Problem Solved: Capacity To Ship Packages Within D Days (LeetCode #1011) 💭 What I Learned: Applied binary search on answer to determine the minimum ship capacity required to deliver all packages within given days. At each step: ✔️ Assumed a capacity (mid) ✔️ Simulated shipping to count required days ✔️ Adjusted search space based on whether the capacity was sufficient Used a greedy approach to pack weights sequentially while minimizing the number of days. This strengthened my understanding of combining binary search + greedy feasibility checks. ⏱ Time Complexity: O(n log S) (where n = number of packages, S = sum of weights) 🧠 Space Complexity: O(1) ⚡ Key Takeaways: ✔️ Binary search can be applied on capacity/limits instead of indices ✔️ Greedy simulation helps validate feasibility efficiently ✔️ Setting correct search boundaries is crucial (max weight → sum of weights) 💻 Language Used: Java ☕ 📘 Concepts: Binary Search · Binary Search on Answer · Greedy · Arrays · Optimization #CodeEveryday #DSA #LeetCode #Java #BinarySearch #Greedy #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 36 Today’s focus: Binary Search for boundaries. Problem solved: • Find First and Last Position of Element in Sorted Array (LeetCode 34) Concepts used: • Binary Search • Lower bound & upper bound • Searching boundaries efficiently Key takeaway: The goal is to find the first and last occurrence of a target in a sorted array. Instead of scanning linearly, we perform two binary searches: • First search → find the leftmost (first) occurrence • Second search → find the rightmost (last) occurrence Key idea: When we find the target: • For left boundary → continue searching in the left half • For right boundary → continue searching in the right half This ensures we capture the full range in O(log n) time. Continuing to strengthen binary search patterns and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
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