Leveling up my searching algorithms! Today’s Java DSA topic: Binary Search. After exploring the brute-force nature of Linear Search, moving to Binary Search. Instead of checking every single element one by one O(n), Binary Search slashes the time complexity to O(log n). The catch? The array must be sorted first! (Good thing I just covered sorting algorithms). The logic is brilliant and incredibly efficient: 1. Find the middle element. 2. If it matches the target, you're done! 3. If the target is smaller, discard the right half. If larger, discard the left. 4. Repeat. #DSA #Java #BinarySearch
Optimizing Java Search with Binary Search
More Relevant Posts
-
Day 60 of My Java DSA Journey Today I solved an important Binary Search Tree problem: 💡 Recover Binary Search Tree (RBST) 🧠 Approach: • Performed inorder traversal of BST • Identified violations where the inorder sequence is not sorted • Tracked misplaced nodes using three pointers: first (first violation) middle (adjacent swap case) last (non-adjacent swap case) • Swapped the incorrect node values to restore BST properties 🔍 Key Insight: Inorder traversal of a BST should always be sorted. If two nodes are swapped, we can detect them by finding “inversion points” in the inorder sequence and fix the tree without changing structure. ⚡ What I learned: • Strong understanding of BST inorder properties • How to detect structural violations in trees • Handling adjacent vs non-adjacent swapped nodes • Importance of pointer tracking in recursive traversal 🔥 Complexity: • Time: O(n) • Space: O(h) (recursion stack) 🎯 Takeaway: Tree problems are not just about traversal — they test your ability to observe patterns in structure and fix them with minimal changes. #Day60 #90DaysOfCoding #DSA #BST #Java #ProblemSolving #BinaryTree
To view or add a comment, sign in
-
-
Leetcode Practice - 16. 3Sum Closest The problem is solved using JAVA Given an integer array nums of length n and an integer target, find three integers at distinct indices in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example 1: Input: nums = [-1,2,1,-4], target = 1 Output: 2 Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). Example 2: Input: nums = [0,0,0], target = 1 Output: 0 Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0). Constraints: 3 <= nums.length <= 500 -1000 <= nums[i] <= 1000 -104 <= target <= 104 #LeetCode #Java #CodingPractice #ProblemSolving #DSA #Array #DeveloperJourney #TechLearning
To view or add a comment, sign in
-
-
Day 76 of #100DaysOfLeetCode 💻✅ Solved #33. Search in Rotated Sorted Array problem in Java. Approach: • Applied Binary Search on rotated array • Checked which half is sorted at each step • Reduced search space based on target position Performance: ✓ Runtime: 0 ms (Beats 100.00% submissions) 🚀 ✓ Memory: 43.80 MB (Beats 70.96% submissions) Key Learning: ✓ Mastered searching in rotated sorted arrays ✓ Improved Binary Search decision-making ✓ Learned efficient handling of edge cases Learning one problem every single day 🚀 #Java #LeetCode #DSA #BinarySearch #Arrays #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
𝗥𝗲𝗰𝗨𝗿𝘀𝗶𝗼𝗻 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 You need to find the factorial of a number using recursion. For example, 5! is 5 × 4 × 3 × 2 × 1 = 120. To solve this problem, you can use a recursive function in Java. Here's how it works: - The function calls itself with a smaller input until it reaches the base case. - The base case is when the input is 1, at which point the function returns 1. - The function then returns the product of the current number and the result of the recursive call. You can use debug prints to see how the function works. Here's an example of what the debug output might look like: - Calling fact(5) - Base case reached: fact(1) = 1 - Returning from fact(2) = 2 - Returning from fact(3) = 6 - Returning from fact(4) =
To view or add a comment, sign in
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Binary Tree Inorder Traversal (LeetCode 94) 💻 Language: Java 🔹 Approach: To solve this problem, I used Depth-First Search (DFS) with Recursion to perform inorder traversal of the binary tree. • Recursively traverse the left subtree • Visit the root node and store its value • Recursively traverse the right subtree • Follow Inorder pattern: Left → Root → Right This approach works efficiently because recursion naturally follows the inorder traversal structure. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) (including recursion stack) 📖 Key Learning: This problem strengthened my understanding of DFS, recursion, and tree traversal patterns. It also reinforced the inorder pattern (Left → Root → Right), which is a fundamental concept used in many tree-based problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #BinaryTree #DFS #Recursion #TreeTraversal
To view or add a comment, sign in
-
-
Languages with closures are very good for recursive solutions. In this case, we can avoid passing all the parameters and capture the variables outside. It's not impossible to do in Java, but it requires jumping through a few hoops. The code could be improved, but it's much better than passing four or five variables in each call. The post: https://lnkd.in/dWRehz-h The problem: https://lnkd.in/dATrsY44 #LeetCode #DSATips #DSA #ThinkDSA #ProblemSolving
To view or add a comment, sign in
-
-
🔢 Divisible Sum Pairs Problem | Java Solution 💻 Today I solved an interesting problem based on arrays and modular arithmetic! 📌 Problem: Given an array and a number k, count pairs (i, j) such that: 👉 i < j 👉 (arr[i] + arr[j]) % k == 0 ⚡ Approach: Instead of brute force O(n²), I used an optimized O(n) approach using remainder frequency. ✔️ Key Idea: Store frequency of (element % k) For each element, find its complement remainder Count valid pairs efficiently 💡 This improves performance significantly for large inputs! 🧠 Concepts Used: Arrays Modulo Arithmetic Hashing Technique 👨💻 Language: Java #Java #CodingPractice #DataStructures #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 37 / 180 – DSA with Java 🚀 📘 Topic Covered: Arrays & Two-Pointer Technique 🧩 Problem Solved: Squares of a Sorted Array Problem: Given a sorted array of integers (including negatives), return a new array of the squares of each number, also sorted in non-decreasing order. Approach: Used a two-pointer approach from both ends of the array. Compared squares of elements and filled the result array from the end to maintain sorted order efficiently. Key Learning: ✔️ Handling negative values in sorted arrays ✔️ Using two-pointer technique for optimal solutions ✔️ Avoiding extra sorting to achieve O(n) time complexity If you’re also preparing for DSA, let’s connect and learn together 🤝 #DSA #Java #180DaysOfCode #LearningInPublic #Arrays #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Binary Tree Preorder Traversal (LeetCode 144) 💻 Language: Java 🔹 Approach: To solve this problem, I used Depth-First Search (DFS) with Recursion to perform preorder traversal of the binary tree. • Visit the root node first • Store the root value in the result list • Recursively traverse the left subtree • Recursively traverse the right subtree • Follow Preorder pattern: Root → Left → Right This approach works efficiently because recursion naturally follows the preorder traversal structure. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) (including recursion stack) 📖 Key Learning: This problem strengthened my understanding of DFS, recursion, and tree traversal patterns. It also reinforced the preorder pattern (Root → Left → Right), which is a fundamental concept used in many tree-based problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #BinaryTree #DFS #Recursion #TreeTraversal
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Binary Tree Postorder Traversal (LeetCode 145) 💻 Language: Java 🔹 Approach: To solve this problem, I used Depth-First Search (DFS) with Recursion to perform postorder traversal of the binary tree. • Recursively traverse the left subtree • Recursively traverse the right subtree • Visit the root node and store its value • Follow Postorder pattern: Left → Right → Root This approach works efficiently because recursion naturally follows the postorder traversal structure. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) (including recursion stack) 📖 Key Learning: This problem strengthened my understanding of DFS, recursion, and tree traversal patterns. It also reinforced the postorder pattern (Left → Right → Root), which is a fundamental concept used in many tree-based problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #BinaryTree #DFS #Recursion #TreeTraversal
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