🚀 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
Implementing Newton's Method for Square Root in Java
More Relevant Posts
-
📘 DSA Journey — Day 33 Today’s focus: Binary Search for next greater element. Problem solved: • Find Smallest Letter Greater Than Target (LeetCode 744) Concepts used: • Binary Search • Upper bound concept • Circular handling Key takeaway: The goal is to find the smallest character strictly greater than a given target in a sorted array. This is a classic upper bound problem: We use binary search to find the first element greater than the target. During search: • If letters[mid] > target, store it as a possible answer and move left • Else move right An important edge case: If no character is greater than the target, we return the first element (circular behavior). Continuing to strengthen binary search patterns and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 27 of #100DaysOfCode Solved Single Element in a Sorted Array 🔍 Today’s problem was a great mix of binary search + pattern observation. Instead of checking every element, I used index parity (even/odd) to eliminate half of the search space each time — achieving O(log n) time complexity ⚡ 🔍 Key Learnings: How sorted structure helps optimize search Using index parity to detect the correct half Writing clean edge-case handling for boundaries 💻 Implemented in Java with optimal approach 📈 Runtime: 0 ms | Beat 100% #DSAwithEdSlash #LeetCode #BinarySearch #100DaysOfCode
To view or add a comment, sign in
-
-
#Day79 of second #100DaysOfCode Binary search on answers is starting to click more now. DSA • Solved Minimum Number of Days to Make m Bouquets (LeetCode 1482) • Given bloom days, find the minimum day to make required bouquets – Brute: check each possible day and count valid bouquets → O(n × max(days)) – Optimal: binary search on days → O(n log max(days)) • Key idea: days form a monotonic search space — if a day works, all later days will also work • Focused on counting consecutive flowers correctly while checking feasibility These problems are really helping me understand how to model conditions for binary search. #DSA #BinarySearch #LeetCode #Algorithms #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
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
-
-
----Continuing my DSA journey---- * Problem: Parse Lisp Expression (736) * Approach: I solved this using recursion + hashmap (scope handling). I parsed the expression and evaluated it based on three cases: • add → evaluate both expressions and sum • mult → evaluate both expressions and multiply • let → assign variables and evaluate in scoped context I used a HashMap to maintain variable values and passed copies to handle nested scopes correctly. * Key Insight: The tricky part was managing variable scope in nested expressions. Each "let" creates a new scope, and variables must be resolved from inner to outer scope. * What I Learned: This problem improved my understanding of parsing complex expressions and scope management. #LeetCode #DSA #Java #Recursion #HashMap #ProblemSolving
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 Challenge – Day 133/360 🚀 📌 Topic: Recursion + Memoization 🧩 Problem: Word Break Problem Statement: Check if a given string can be segmented into space-separated words from a given dictionary. 🔍 Example: Input: s = "leetcode", wordDict = ["leet", "code"] Output: true 💡 Approach: Optimized (Recursion + Memoization) 1️⃣ Step 1 – Convert the word list into a HashSet for fast lookup 2️⃣ Step 2 – Try all substrings starting from current index 3️⃣ Step 3 – Use memoization array to avoid recomputation of same index ⏱ Complexity: Time: O(n³) Space: O(n) 📚 Key Learning: Memoization helps avoid repeated work and turns an exponential problem into a polynomial one. #DSA #Java #Coding #InterviewPrep #ProblemSolving #TechJourney #133DaysOfCode #LeetCode
To view or add a comment, sign in
-
-
Day 7 of Java with DSA Journey 🚀 📌 Topic: Missing Number (LeetCode 268) Quote: "Sometimes the best way to solve a coding problem is to put down the keyboard and pick up a calculator." ✨ What I learned today: 🔹 Mathematical Optimization: Instead of brute force, I used a simple formula to compute the expected sum. {Sum} = \frac{n(n+1)}{2} 🔹 Core Idea: Expected Sum − Actual Sum = Missing Number 🔹 Alternative Approach: Used XOR technique where duplicates cancel out → leaving the missing value. 🔹 Efficiency: ✔️ Time Complexity: O(n) ✔️ Space Complexity: O(1) 🧠 Problem Solved: ✔️ Missing Number 💡 Key Insight: We often jump to HashSet or Sorting, but this problem taught me to pause and think mathematically first. A simple formula reduced space complexity from O(n) → O(1) 🚀 ⚡ Interview Tip (Important!): Be careful of integer overflow in Java: n * (n + 1) can exceed int limit Use long OR switch to XOR method for safety Consistency is the real key 🔑 #DSA #LeetCode #Java #Mathematics #CodingJourney #ProblemSolving #Day7 #Algorithms #Optimization #Array #MCA #lnct #100DaysOfCode #Algorithms #SoftwareEngineering #InPlaceAlgorithms #TechLearning #JavaDeveloper
To view or add a comment, sign in
-
-
✅ Solved: Anagram Palindrome Another step forward in sharpening problem-solving skills 🚀 🔍 Problem Insight: A string can be rearranged into a palindrome if at most one character has an odd frequency. Simple idea, but powerful when applied efficiently. 💡 Implemented using an optimized approach in Java: - Time Complexity: O(n) - Space Complexity: O(1) 📊 Result: ✔️ Test Cases Passed: 1113 / 1113 ✔️ Accuracy: 100% ✔️ Time Taken: 0.52s Small problem, but a good reminder that the right observation beats brute force every time ⚡ #DataStructures #Algorithms #Java #ProblemSolving #CodingJourney #DSA #TechGrowth
To view or add a comment, sign in
-
-
Day 73 of #100DaysOfLeetCode 💻✅ Solved #389. Find the Difference problem in Java. Approach: • Initialized a variable to store the sum • Added ASCII values of all characters in string t • Subtracted ASCII values of all characters in string s • The remaining value represents the extra character • Converted the result back to character and returned it Performance: ✓ Runtime: 1 ms (Beats 99.89% submissions) 🚀 ✓ Memory: 42.77 MB (Beats 97.03% submissions) Key Learning: ✓ Practiced optimized string traversal using enhanced for-loop ✓ Reinforced understanding of ASCII-based solutions ✓ Learned how mathematical approaches can simplify problems Learning one problem every single day 🚀 #Java #LeetCode #DSA #Strings #Math #ProblemSolving #CodingJourney #100DaysOfCode
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