Imagine standing on a mountain range. Your goal is simple: Find any peak. Not necessarily the highest mountain — just a point that is higher than its neighbors. That's exactly what today's problem was about. 🚀 Day 77/365 — DSA Challenge Solved: Find Peak Element The Problem A peak element is a number that is greater than its neighbors. Given an array, return the index of any peak element. 💡 My Approach This problem can be solved using Binary Search. Key observation: If nums[mid] < nums[mid + 1] It means the peak must be on the right side. Otherwise, the peak lies on the left side (including mid). So we keep shrinking the search space until: start == end That index will be a peak element. Binary search moves toward the increasing slope until it reaches the peak. Complexity ⏱ Time: O(log n) 📦 Space: O(1) Day 77/365 complete. 💻 288 days to go. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #Algorithms #LearningInPublic
Find Peak Element in Array with Binary Search
More Relevant Posts
-
🚀 Day 43/60 – DSA Challenge Today’s problem was about finding the peak element in a Mountain Array using Binary Search 🔍 🔍 Problem Solved: Given a mountain array (strictly increasing then decreasing), find the index of the peak element. 🧠 Approach I Used: I applied Binary Search on the slope of the array: If the current element is smaller than the next → we are in the increasing part → move right Otherwise → we are in the decreasing part → move left Continue narrowing down until we reach the peak ⚡ Key Insight: Instead of comparing both sides, we only compare with the next element, which simplifies the logic and still guarantees correctness. 📈 Complexity: Time: O(log n) Space: O(1) 🎯 What I Learned: Binary Search can be applied beyond searching—it can solve optimization problems Understanding the pattern of the array (increasing/decreasing) is key Small condition changes can make or break the algorithm Every day is making Binary Search feel more intuitive 🔥 #Day43 #DSAChallenge #BinarySearch #Java #ProblemSolving #Consistency
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
-
-
Day 61 of My DSA Journey Today I solved the Subsets problem using the Backtracking technique. 🔹 Problem: Given an integer array nums, return all possible subsets (the power set). 🔹 Key Idea: Instead of trying to generate subsets directly, I used backtracking to explore every possible combination. 💡 Approach: Start with an empty subset. At each step, add the current subset to the result. Try including each element one by one. Recursively explore further subsets. Backtrack by removing the last element to explore other possibilities. 🔁 This approach systematically explores every possible subset using a decision tree. 📊 Time Complexity: O(n × 2ⁿ) — because each element can either be included or excluded. ✨ What I Learned: Backtracking is powerful for solving combinatorial problems. The pattern of choose → explore → unchoose is very important. 💻 Practicing problems like this helps strengthen recursion and problem-solving skills. #Day61 #DSA #Backtracking #Java #CodingJourney #LeetCode #ProblemSolving
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
-
-
📘 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
-
-
🚀 Day 22 of My DSA Journey Today I solved an important Binary Search problem: 👉 Find First and Last Position of Element in Sorted Array 🧩 Problem Understanding Given a sorted array and a target value, we need to find the starting and ending position of the target element. If the target is not present, return [-1, -1]. 🔍 Approach I Used (Binary Search x2) Instead of linear search, I used Binary Search twice: ✔️ First Binary Search → to find the first occurrence ✔️ Second Binary Search → to find the last occurrence 💡 Trick: Even after finding the target, we don’t stop — we continue searching on left/right side to get the boundary indices. ⚡ Why this approach? Because the array is sorted → Binary Search reduces time complexity drastically. ⏱ Time Complexity: O(log n) 📦 Space Complexity: O(1) 🔥 Key Learning Binary Search is not just for finding elements — it can also be used to find boundaries, ranges, and conditions. Consistency is the real game changer 💪 One problem at a time! #DSA #BinarySearch #LeetCode #Java #ProblemSolving #100DaysOfCode #CodingJourney #LearningInPublic
To view or add a comment, sign in
-
-
📘 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
-
-
Today's problem was HARD. But the pattern was familiar. Binary Search on Answer... again. 🚀 Day 92/365 — DSA Challenge Solved: Find K-th Smallest Pair Distance Problem: Find the k-th smallest distance among all pairs. Brute force: Generate all pairs → O(n²) → Too slow. Better idea: Binary search the distance. For a distance d: Count how many pairs have distance ≤ d. If count ≥ k → try smaller distance Else → increase distance The interesting part: We count pairs using Sliding Window in O(n). So the final solution becomes: Binary Search + Sliding Window. ⏱ Time: O(n log range) 📦 Space: O(1) Day 92/365 complete. 💻 273 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #SlidingWindow #Algorithms #LearningInPublic
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
-
-
📘 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
-
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