📘 DSA Journey — Day 32 Today’s focus: Binary Search for mathematical validation. Problem solved: • Valid Perfect Square (LeetCode 367) Concepts used: • Binary Search • Search space reduction • Avoiding overflow Key takeaway: The goal is to determine whether a number is a perfect square without using built-in square root functions. Using binary search, we search in the range [1, num]: • Compute mid • Check if mid * mid == num → perfect square • If mid * mid < num, move right • Else move left This reduces the complexity to O(log n). Important detail: To avoid overflow when computing mid * mid, we can use: mid <= num / mid This ensures safe comparison even for large numbers. Continuing to strengthen binary search intuition and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
Binary Search for Perfect Square Validation
More Relevant Posts
-
📘 DSA Journey — Day 35 Today’s focus: Binary Search with index patterns. Problem solved: • Single Element in a Sorted Array (LeetCode 540) Concepts used: • Binary Search • Index parity (even/odd pattern) • Search space reduction Key takeaway: The array is sorted and every element appears twice except one. A key observation: Before the single element, pairs start at even indices After the single element, this pattern breaks. Using binary search: • If mid is even and nums[mid] == nums[mid + 1], the single element lies on the right side • Else, it lies on the left side (including mid) By leveraging this pattern, we can find the answer in O(log n) time and O(1) space. 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
-
-
📘 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
-
-
#CodeEveryday — My DSA Journey | Day 4 🧩 Problem Solved: Word Search (LeetCode #79) 💭 What I Learned: Used DFS + backtracking to search for a word in a 2D grid by exploring all possible paths. At each step: ✔️ Checked boundary conditions and character match ✔️ Explored all 4 directions (up, down, left, right) ✔️ Marked the current cell as visited to avoid revisiting ✔️ Backtracked by unmarking the cell after exploration Ensured correctness by stopping early when a mismatch occurs or when the full word is found. This strengthened my understanding of grid traversal with constraints and path tracking. ⏱ Time Complexity: O(m × n × 4^L) (L = length of word) 🧠 Space Complexity: O(L) (recursion stack) ⚡ Key Takeaways: ✔️ Backtracking is essential for path-based problems in grids ✔️ Marking visited cells prevents cycles ✔️ Early stopping improves performance significantly 💻 Language Used: Java ☕ 📘 Concepts: DFS · Backtracking · Matrix Traversal · Recursion #CodeEveryday #DSA #LeetCode #Java #Backtracking #DFS #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
🚀 DSA Preparation 💪 Solved a basic yet important Array + Two Pointer problem. Focused on rearranging elements by grouping even and odd numbers efficiently. Great for improving in-place operations and partitioning logic 🔥 🧠 Problem 🔎 Sort Array By Parity Given an integer array nums, move all even integers to the beginning followed by all odd integers. 👉 Return any array that satisfies this condition. Example Input: nums = [3,1,2,4] Output: [2,4,3,1] Input: nums = [0] Output: [0] ⚡ Key Learning 📌 Use two pointers / partition approach 📌 Efficiently rearrange elements in one pass 📌 Time Complexity: O(n) → traverse the array once 📌 Space Complexity: O(1) → in-place rearrangement (no extra space) Improving DSA with strong fundamentals 🚀 #DSA #LeetCode #Arrays #TwoPointers #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Challenge 27/50 💡 Approach: Classic Binary Search The problem itself demands O(log n) — no room for linear search! Binary Search is one of the most fundamental algorithms in computer science, and mastering it is non-negotiable for every developer. 🔍 Key Insight: → Maintain left and right pointers on the sorted array → Calculate mid = left + (right - left) / 2 — NOT (left+right)/2 → Why? (left+right) can cause INTEGER OVERFLOW for large values! → nums[mid] == target → found! → nums[mid] < target → search right half (left = mid+1) → nums[mid] > target → search left half (right = mid-1) 📈 Complexity: ❌ Linear Search → O(n) Time ✅ Binary Search → O(log n) Time, O(1) Space Real impact: For n=1,000,000 → Linear needs 1M comparisons, Binary Search needs just 20! That's the power of logarithms. 🔥 #LeetCode #DSA #BinarySearch #Java #ADA #PBL2 #LeetCodeChallenge #Day27of50 #CodingJourney #ComputerEngineering #AlgorithmDesign #SearchAlgorithm
To view or add a comment, sign in
-
-
🧠 Day 38 / 100 – DSA Practice Solved Word Search on LeetCode 🔍✅ 🔹 Problem: Given a 2D grid of characters and a word, check if the word exists in the grid by traversing adjacent cells (horizontally or vertically). 🔹 Approach: Used DFS + Backtracking: Start from each cell Explore all 4 directions (up, down, left, right) Mark visited cells to avoid reuse Backtrack if the path doesn’t match 🔍 Key Insight: Backtracking helps explore all possible paths while ensuring each cell is used only once per path 🔹 Complexity: ⏱ Time → O(m × n × 4^L) 📦 Space → O(L) recursion stack 💯 Result: ✔️ All test cases passed ⚡ Runtime: 130 ms (Beats 73%) Great problem to strengthen DFS & Backtracking concepts 🚀 #Day38 #100DaysOfCode #LeetCode #Java #DSA #Backtracking #DFS #Algorithms #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 31 Today’s focus: Binary Search for boundaries and square roots. Problems solved: • Sqrt(x) (LeetCode 69) • Search Insert Position (LeetCode 35) Concepts used: • Binary Search • Search space reduction • Boundary conditions Key takeaway: In Sqrt(x), the goal is to find the integer square root. Using binary search, we search in the range [1, x] and check mid * mid against x to narrow down the answer. This avoids linear iteration and achieves O(log n) time. In Search Insert Position, we use binary search to find either: • The exact position of the target, or • The correct index where it should be inserted The key idea is that when the target is not found, the final position of the left pointer gives the correct insertion index. These problems highlight how binary search is not just for finding elements, but also for determining positions and boundaries efficiently. Continuing to strengthen fundamentals and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 51 of Daily DSA 🚀 Solved LeetCode 83: Remove Duplicates from Sorted List ✅ Problem: Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Approach: Used a single pointer traversal since the list is already sorted, making duplicate detection straightforward. Steps: Start from head node Compare current node with next node If values are same → skip next node Else move current pointer forward Continue until end of list Return updated head ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 45.48 MB Sorted input often simplifies the logic — duplicates become adjacent and easy to remove. #DSA #LeetCode #Java #LinkedList #CodingJourney #ProblemSolving #Algorithms
To view or add a comment, sign in
-
-
Just wrapped up solving an interesting DSA problem today — Minimum Difference Between K Elements. At first glance, it feels like you might need to check multiple combinations, but the real insight comes after sorting the array. Once sorted, the problem simplifies beautifully: the minimum difference will always come from a contiguous subarray of size k. So instead of overcomplicating things, I used: • Sorting • A fixed-size sliding window • Tracking the minimum difference across windows What I liked about this problem is how it reinforces a common pattern in problem-solving — simplify first, optimize later. Sorting often reveals structure that isn’t obvious initially. Time Complexity: O(n log n) Space Complexity: O(1) Small problems like these are great reminders that clarity in thinking beats brute force. #DSA #Java #ProblemSolving #CodingJourney #Algorithms
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