🌟 Day 24 of #100DaysOfCode. Today I come with one challenging problem which is related to the topic Binary Search. 🌟 ✔️ What I solved: *️⃣ Problem of the Day: Binary Search ✴️ Approach & Key Insights: 🔹 Binary Search divides the array into halves to efficiently search for a target in O(log n) time. 🔹 The sorted property is crucial — binary search only works on sorted arrays. ◾ Steps: 🔹 Initialize two pointers, left at the start and right at the end of the array. While left <= right: 🔹 Compute the middle index mid = left + (right - left) // 2. 🔹 If nums[mid] is the target, return mid. 🔹If nums[mid] < target, search right half (left = mid + 1). 🔹If nums [mid] > target, search left half (right = mid - 1). 🔹If you exit the loop, target is not present. Return -1. ✴️ Complexity: 🔹 Time Complexity: O(logn) 🔹 Space Complexity: O(1) ✴️ Key Takeaways: 🔹 Use binary search for efficient lookups in sorted arrays. Always check for overflow when computing mid: mid = left + (right - left) / 2. 🔹 The standard approach is iterative for minimal space, but recursive solutions work as well. 🚀For more information, Click here ⬇️ https://lnkd.in/gaQTpVtk 🪄 I am very thankful to my Mentor Mr.Rajesh Gupta Sir and KR Mangalam University for giving me the support throughout this week. #DSA #100DaysOfCode #KRMU #Competitive #Coding
Solved Binary Search problem in 100DaysOfCode
More Relevant Posts
-
💡 Day 11 of #30DaysOfDSA Linear Search : The Simplest Algorithm That Still Gets the Job Done 🔍 My first “search” program was nothing fancy. Just a loop that checked each element one by one until it found what I needed. It felt too simple to be important. But here’s the truth: Linear Search is where every search journey begins. Before we build trees, heaps, or binary searches, we first learn to look through data like a human would. 🧩 What is Linear Search? Linear Search is the most straightforward way to find an element in a list or array. You start from the beginning, check each element, and stop when you find what you’re looking for. Example: for (int i = 0; i < n; i++) { if (arr[i] == key) return i; // Found } return -1; // Not found That’s it. No fancy math. No pre-sorted data. Just honest, straightforward searching. ⚙️ Time Complexity Best case: O(1) : when the element is right at the start. Worst case: O(n) : when it’s at the end or not present at all. In large datasets, that’s costly. But for smaller lists or unsorted data, it’s perfect. 🌍 Real-world analogy Imagine flipping through a shuffled deck of cards to find the Queen of Hearts. You check one card at a time — that’s Linear Search. Simple, reliable, and guaranteed to find your answer (eventually). ⚡ Where Linear Search is still useful When data is unsorted or constantly changing When dataset size is small or unpredictable When you just need a quick check without preprocessing overhead It may be basic — but it builds the foundation for how computers “think” when finding something. #DSA #30DaysOfDSA #LearnInPublic #Algorithms #Searching #LinearSearch #DataStructures #CodingJourney #Programming #DeveloperCommunity #CodeNewbie #Java #ComputerScience #ProblemSolving #CSFundamentals #SoftwareEngineer #InterviewPreparation #SoftwareDevelopment #TechLearning #TechSeries #Engineering #CareerGrowth #CodingLife #CodeEveryday #KnowledgeSharing #TechJourney #ProgrammingTips #EfficientAlgorithms
To view or add a comment, sign in
-
🔍 Day 21/50 | Back to Basics - And That's Beautiful! Today I solved "Search Insert Position" - A classic binary search problem that reminded me why mastering fundamentals is so important. 💡 The Problem: Given a sorted array and a target value, return the index if found. If not, return where it should be inserted. Required: O(log n) time complexity. 🎯 The Solution: Classic Binary Search Sometimes the most elegant solution is the simplest one: Divide the search space in half Compare middle element with target Repeat until found or space exhausted The beauty? When target isn't found, left pointer naturally points to the insertion position! ⚡ Complexity: ✅ Time: O(log n) - Required constraint met ✅ Space: O(1) - Constant space 📚 Why This Problem Matters: After tackling complex DP and bitmask problems, returning to binary search felt refreshing. It reminded me that: 1️⃣ Fundamentals are forever - Binary search is a building block for so many advanced algorithms 2️⃣ Simple ≠ Easy - Writing bug-free binary search requires attention to: Loop condition: left <= right (not left < right) Mid calculation: left + (right - left) / 2 (prevents overflow) Return value: left gives insertion position 3️⃣ Interview favorites - This is one of the most asked questions because it tests understanding of a fundamental algorithm 🔑 Key Takeaway: Don't overlook "easy" problems. They're the foundation upon which complex solutions are built. Every time I revisit binary search, I understand it a little deeper. Day 21/50 ✅ | Building strong foundations! 💪Shishir chaurasiya and PrepInsta #50DaysOfCode #CodingChallenge #LeetCode #BinarySearch #DSA #Algorithms #BackToBasics #CPlusPlus #SoftwareEngineering #ProblemSolving #TechJourney #LearningInPublic #CodingLife
To view or add a comment, sign in
-
-
🚀 Day 19/30 – 30DaysOfDSAChallenge 🚀 🧠 Topic: Subarray Sum Equals K 📘 Concepts Covered: Prefix Sum | HashMap | Cumulative Sum | Subarray Counting Today’s challenge was about finding the number of continuous subarrays whose sum equals a given value K. It was a great exercise in understanding how prefix sums can optimize brute-force solutions! ⚡ 🔹 Approach – Prefix Sum + HashMap 💡 Logic: Compute prefix sums to store cumulative totals. Use a HashMap to keep track of how many times a particular prefix sum appears. For each prefix sum, check if (currentSum - K) exists in the map — if yes, those subarrays contribute to the answer. Update the map as you iterate to maintain frequency counts. ⚙️ Time Complexity: O(N) ⚙️ Space Complexity: O(N) ✅ Efficient one-pass solution using extra space for quick lookups. ✨ Key Takeaway: Prefix sum with HashMap is a powerful combo — it transforms nested loop logic into a clean, linear-time approach. These concepts often appear in tricky subarray or range sum problems, so mastering them pays off big time! 💪 #Day19 #30DaysOfDSAChallenge #LeetCode #PrefixSum #HashMap #Array #Subarray #DSA #CPlusPlus #CodingChallenge #ProblemSolving #CodingJourney #LearnDSA #CodeEveryday #Algorithms #Programming
To view or add a comment, sign in
-
-
Day 24: #100DaysOfCodingChallenge 🔹📊 Today’s focus was on binary search on the answer and understanding stable sorting — perfect for optimizing solutions and maintaining element order! 🔵 LeetCode/GFG: Binary Search on Answer (Basic Example) 🚀 Difficulty: Medium ⏳ Time Complexity: O(log(max_range))** 💾 Space Complexity: O(1)** ⏱ Time Taken: 0.2 sec 🔍 Approach: Applied binary search not on array indices, but on the range of possible answers. Checked feasibility at each mid-value and narrowed down the search space — a key technique for optimization problems! 🟢 LeetCode/GFG: Stable Sort Concept (Practical) 🟢 Difficulty: Easy ⏳ Time Complexity: O(n log n)** 💾 Space Complexity: O(n)** ⏱ Time Taken: 0.2 sec 🔍 Approach: Used a sorting algorithm (like Merge Sort or stable sort in STL) that preserves the relative order of equal elements. Understanding stability is crucial for problems where order matters in secondary attributes. 💡 Reflection: Binary search on the answer transforms brute-force range checks into efficient log-time searches. Stable sorting ensures correctness in multi-criteria sorting problems — both are foundational for advanced algorithmic thinking! Thanks to Nandan Kumar Mishra Sir, Abhishek Kumar Sir, and #KRMangalamUniversity for their guidance and continuous encouragement! #100DaysOfCodingChallenge #DSA #BinarySearchOnAnswer #StableSort #Array #Sorting #CPlusPlus #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 24: #100DaysOfCodingChallenge 🔹📊 Today’s focus was on binary search on the answer and understanding stable sorting — perfect for optimizing solutions and maintaining element order! 🔵 LeetCode/GFG: Binary Search on Answer (Basic Example) 🚀 Difficulty: Medium ⏳ Time Complexity: O(log(max_range))** 💾 Space Complexity: O(1)** ⏱ Time Taken: 0.2 sec 🔍 Approach: Applied binary search not on array indices, but on the range of possible answers. Checked feasibility at each mid-value and narrowed down the search space — a key technique for optimization problems! 🟢 LeetCode/GFG: Stable Sort Concept (Practical) 🟢 Difficulty: Easy ⏳ Time Complexity: O(n log n)** 💾 Space Complexity: O(n)** ⏱ Time Taken: 0.2 sec 🔍 Approach: Used a sorting algorithm (like Merge Sort or stable sort in STL) that preserves the relative order of equal elements. Understanding stability is crucial for problems where order matters in secondary attributes. 💡 Reflection: Binary search on the answer transforms brute-force range checks into efficient log-time searches. Stable sorting ensures correctness in multi-criteria sorting problems — both are foundational for advanced algorithmic thinking! Thanks to Nandan Kumar Mishra Sir, Abhishek Kumar Sir, and #KRMangalamUniversity for their guidance and continuous encouragement! #100DaysOfCodingChallenge #DSA #BinarySearchOnAnswer #StableSort #Array #Sorting #CPlusPlus #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
💡 Day 2 of #30DaysOfDSA When ‘Searching’ Gets Smarter — Binary Search Explained Simply When I first heard about Binary Search, it sounded fancy — but the concept is surprisingly simple. You just keep halving your search space until you find what you’re looking for. Imagine you’re looking for a name in a phone book (yes, those old ones 😄). You don’t start from page 1 and check each entry — you open the book near the middle, decide which half might contain the name, and repeat. That’s binary search in action. In code, this logic turns into one of the most efficient algorithms we use every day — from searching elements in sorted data to how databases and even search engines optimize lookups. It’s also the foundation of many advanced algorithms and problem-solving techniques we encounter later — especially in competitive programming and system-level optimization. The key takeaway? Binary search isn’t just about searching — it’s about thinking in halves and making decisions with precision. #DSA #CodingJourney #LearnInPublic #SoftwareEngineering #Java #Programming #BinarySearch #TechSeries
To view or add a comment, sign in
-
🎉 Day 400 of #500DaysOfCode 🎉 Today I tackled an interesting problem from LeetCode — “703. Kth Largest Element in a Stream”. The task: Build a class that maintains a stream of scores (or numbers) and, for a given integer k, always returns the kth largest element after each insertion. Why this is cool: Real-world analogy: Think of a university admissions office tracking the kth highest test score dynamically as new applicants submit scores. Efficient solution involves a min-heap (priority queue) of size k — keep only the top k largest elements; the root is then the kth largest overall. Good practice for streaming data, heaps, and understanding dynamic order statistics. 🔧 My Java solution uses PriorityQueue<Integer>: Initialize with k and the initial array of scores. On each .add(val) call: insert value, if heap size > k then remove the smallest, then peek to get the kth largest. Time complexity: O(log k) per addition. Space: O(k). ✅ Key takeaways: Using a min-heap of fixed size is a neat way to get the kth largest in a stream. Make sure to prune the heap so it never grows beyond k. Good addition to my toolkit for streaming / dynamic order problem types. What’s next: On to Day 401 — I’ll be diving into [insert upcoming topic or type: e.g., “graph algorithms”, “dynamic programming advanced”, “system design mini challenge”]. #500DaysOfCode #CodingChallenge #LeetCode #Java #DataStructures #Algorithms #Heap #PriorityQueue #ProblemSolving #DeveloperJourney
To view or add a comment, sign in
-
-
🔹 DSA Daily | Find Equilibrium Point in an Array (C++) Today, I solved the **Equilibrium Point** problem — a classic question that tests your understanding of **prefix sums and array traversal**. 💡 Problem Statement: Given an array, find the index where the **sum of elements on the left** is equal to the **sum of elements on the right**. If no such point exists, return -1. Approach: 1️⃣ First, calculate the **total sum** of the array. 2️⃣ Traverse the array while keeping track of the **left sum**. 3️⃣ For each index, compute the **right sum** using the formula: `right_sum = total_sum - left_sum - arr[i]` 4️⃣ If left_sum == right_sum, that index is the equilibrium point. Time Complexity: O(n) — single traversal of the array Space Complexity: O(1) — constant extra space This problem reinforces how simple **mathematical logic** and **efficient iteration** can solve what looks like a tricky problem at first glance. 🚀 #DSA #CPlusPlus #Coding #ProblemSolving #Arrays #EquilibriumPoint #CodeEveryday #GeeksforGeeks #LeetCode #ProgrammingJourney #DataStructures #Algorithms #InterviewPrep #CodingCommunity #LogicBuilding #EfficientCode #LearnToCode #TechJourney
To view or add a comment, sign in
-
-
Tired of complex infrastructure setup? The expanded #EOSCEUNode Tools Hub is your one-stop shop for research software, ready for instant deployment. From data processing to advanced analytics, access powerful tools like Galaxy for biomedical research and DaskHub for parallel computing in Python. ✅ One-Stop Access: A curated catalogue of reliable applications. ✅ For All Skill Levels: Deploy easily as a beginner or use TOSCA templates for advanced, reproducible workflows. ✅ Cost-Covered: Computational resources are provided by the European Commission. Ready to get started? We have everything you need: Watch the Demo Video: See how to allocate a Virtual Machine and set up tools in your User Space. Follow the Tutorial: "Tools Hub: Introduction for Researchers" Take the Course: "How to use the EOSC EU Node Tools Hub: A Complete Guide" 🔗 Explore the Tools Hub: https://go.egi.eu/aeqTi
To view or add a comment, sign in
-
-
𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐃𝐚𝐢𝐥𝐲 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: 2654. 𝐌𝐢𝐧𝐢𝐦𝐮𝐦 𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬 𝐭𝐨 𝐌𝐚𝐤𝐞 𝐀𝐥𝐥 𝐀𝐫𝐫𝐚𝐲 𝐄𝐥𝐞𝐦𝐞𝐧𝐭𝐬 𝐄𝐪𝐮𝐚𝐥 𝐭𝐨 1. This problem was an excellent application of mathematical reasoning through the Euclidean Algorithm (GCD). It helped me appreciate how fundamental math concepts can simplify complex algorithmic logic and lead to optimized solutions. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐮𝐦𝐦𝐚𝐫𝐲: We are given an array of positive integers. In one operation, we can select an index i and replace either nums[i] or nums[i+1] with their GCD value. The task is to find the minimum number of operations required to make all elements equal to 1, or return -1 if it is impossible. 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Checked if the array already contains any element equal to 1. If yes, the minimum operations equal the number of elements that are not 1. If not, used a nested loop to find the shortest subarray whose GCD becomes 1 using the Euclidean Algorithm. Once such a subarray is found, it can be made 1 in (subarray length - 1) operations. The total number of operations is calculated as (subarray length - 1) + (n - 1), representing the steps to convert the rest of the array to 1. Added an early break once GCD reaches 1 for better performance. 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 𝐀𝐧𝐚𝐥𝐲𝐬𝐢𝐬: Time Complexity: O(n² * log(max(nums))) Space Complexity: O(1) 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬: Reinforced the use of the Euclidean Algorithm and recursion for GCD calculations. Understood the role of early exits in improving nested loop efficiency. Observed how mathematical insight directly enhances algorithmic problem-solving. This marks my first post in my LeetCode Daily Challenge journey. I plan to continue sharing my learnings and reasoning from each daily challenge to stay consistent and deepen my problem-solving approach. #LeetCode #DSA #Java #ProblemSolving #CodingPractice #LearningEveryday #Consistency #Algorithms #100DaysOfCode
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