🔥 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
Valid Perfect Square Binary Search
More Relevant Posts
-
📘 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
To view or add a comment, sign in
-
-
📘 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
To view or add a comment, sign in
-
-
🚀 Solved a neat Difference Array problem today! 💡 Problem: Given an array and a set of range queries, determine whether it’s possible to make the entire array zero using the allowed operations. 🧠 Approach: Instead of applying each query directly (which would be slow), I used: • Difference Array for efficient range updates • Prefix Sum to compute actual impact at each index ⚡ Key Idea: Each query contributes to a range. By marking where the effect starts and ends, we can efficiently calculate how many times each index can be reduced. Then, for every index: • Compare available operations with required value • If available < required → not possible 📈 Complexity: O(n + q) — efficient and scalable 🔥 Key Learning: Using a Difference Array avoids repeated work and helps optimize range-based problems significantly. #LeetCode #DSA #Java #Coding #ProblemSolving #Arrays #Algorithms
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 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 90 – DSA Journey | Binary Tree Paths Continuing my daily DSA practice, today I explored how to track and construct paths in a binary tree. 📌 Problem Practiced: Binary Tree Paths (LeetCode 257) 🔍 Problem Idea: Return all root-to-leaf paths in a binary tree as strings. 💡 Key Insight: While traversing the tree, we can build the path step by step. Once we reach a leaf node, we add the complete path to the result. 📌 Approach Used: • Use DFS traversal • Maintain a string to track the current path • At each node, append its value to the path • If it’s a leaf node → add the path to result • Continue exploring left and right subtrees 📌 Concepts Strengthened: • Tree traversal (DFS) • Recursion • Path tracking • String manipulation ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) 🔥 Today’s takeaway: Tracking state (like a path) during recursion is a powerful technique for solving tree problems. On to Day 91! 🚀 #Day90 #DSAJourney #LeetCode #BinaryTree #DFS #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 Day 32 of #128DaysOfCode Today I explored an efficient way to find the square root of a number without using built-in functions. Instead of the usual brute force or binary search, I implemented Newton’s Method (Babylonian Method) — a powerful mathematical approach that converges very fast. 🔹 The idea is to continuously improve our guess using: r = (r + x / r) / 2 This method quickly narrows down to the correct integer square root, making it both optimized and elegant. 💡 Key Takeaways: - Learned how mathematical concepts can optimize coding problems - Understood the importance of avoiding overflow using "long" - Practiced writing clean and efficient Java code 🔥 Consistency is the real game changer. Step by step, improving logic and problem-solving skills! #Java #DSA #CodingJourney #ProblemSolving #Consistency #LearnInPublic
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 14 🧩 Problem Solved: Search a 2D Matrix (LeetCode #74) 💭 What I Learned: Used binary search by treating the 2D matrix as a flattened sorted array. Key idea: 👉 Convert 1D index → 2D coordinates using: row = mid / number_of_columns col = mid % number_of_columns At each step: ✔️ Calculated row and column from mid ✔️ Compared matrix value with target ✔️ Adjusted search space accordingly This approach avoids nested loops and gives optimal performance. ⏱ Time Complexity: O(log (m × n)) 🧠 Space Complexity: O(1) ⚡ Key Takeaways: ✔️ 2D problems can often be reduced to 1D using indexing tricks ✔️ Binary search works whenever the data is globally sorted ✔️ Index mapping is a powerful technique in matrix problems 💻 Language Used: Java ☕ 📘 Concepts: Binary Search · Matrix · Index Mapping · Optimization #CodeEveryday #DSA #LeetCode #Java #BinarySearch #Matrix #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
#Day74 of my second #100DaysOfCode Binary search variations getting more interesting now. DSA • Solved Find Minimum in Rotated Sorted Array (LeetCode 153) – Brute: linear scan → O(n) – Optimal: binary search with an early check for already sorted part → O(log n) • Key idea: the minimum always lies in the unsorted portion, and if a part is already sorted, the answer can be taken directly • Difference from previous problems: instead of searching for a target, we’re tracking the minimum while narrowing the search space • Edge cases: – already sorted array – single element case – careful updates while narrowing the range This one was more about understanding the pattern than just applying binary search. #DSA #BinarySearch #LeetCode #Algorithms #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
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
-
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