💡 #GFG160 Day 30 — Search in Rotated Sorted Array Today’s challenge focused on finding a target element in a sorted and rotated array — one of the most elegant uses of binary search. 🧩 Problem: Given a rotated sorted array, find the index of the target element. If it’s not present, return -1. Example: arr = [4,5,6,7,0,1,2], target = 0 → Output: 4 ⚙️ Approach / Intuition: Even after rotation, one half of the array is always sorted. So in each step: Find the mid element. Check which half (left or right) is sorted. Decide where to move next based on the target’s range. This approach eliminates half of the search space every iteration — pure binary search efficiency. 🧩 Dry Run (Example): arr = [4,5,6,7,0,1,2], target = 0 mid = 7 → left half sorted target not in left → move right mid = 1 → right half sorted target in left → move left mid = 0 → found ✅ ⏱ Time Complexity: O(log N) 💾 Space Complexity: O(1) 💻 Language: Java 📚 Key takeaway: Binary Search is not just for plain sorted arrays — the trick lies in identifying which half is sorted each time! #GeeksforGeeks #Java #BinarySearch #CodingChallenge #DSA #ProblemSolving #LearningEveryday
How to search in a rotated sorted array using binary search
More Relevant Posts
-
💡 #GFG160 Day 29 — Find Minimum in Rotated Sorted Array Today’s challenge was about identifying the minimum element in a sorted and rotated array — a classic binary search twist. 🧩 Problem: A sorted array is rotated at some pivot, and we need to find the smallest element efficiently. Example: arr = [5, 6, 1, 2, 3, 4] → Output: 1 ⚙️ Approach / Intuition: Since the array is sorted but rotated, one half of it remains sorted. If arr[low] <= arr[high], the array segment is sorted — so the minimum is arr[low]. Otherwise, use binary search logic to move toward the unsorted half where the minimum lies. ⏱ Time Complexity: O(log N) 🔁 Space Complexity: O(1) 🧠 Key takeaway: Binary search can be extended far beyond just “searching” — it’s all about identifying sorted patterns and narrowing down intelligently. #GeeksforGeeks #ProblemSolving #Java #CodingChallenge #DSA #BinarySearch #LearningEveryday
To view or add a comment, sign in
-
-
💻 Day 4 of #100DaysOfCode Challenge Topic: Binary Tree Traversals 🌳 Problems Solved: 🔹 94. Binary Tree Inorder Traversal 🔹 144. Binary Tree Preorder Traversal 🔹 145. Binary Tree Postorder Traversal Concept Recap: Today, I explored the three fundamental depth-first traversal techniques used in binary trees: ✅ Inorder (Left → Root → Right) – Produces a sorted order for BSTs. ✅ Preorder (Root → Left → Right) – Useful for creating a copy of the tree or serialization. ✅ Postorder (Left → Right → Root) – Ideal for deleting trees or evaluating expressions. Each traversal follows a recursive approach to explore nodes systematically. Implementing them helped me strengthen my understanding of recursion and how stack frames manage function calls behind the scenes. Key Learnings: 🧠 Understood how traversal order impacts output sequence. ⚙️ Practiced recursive depth-first traversal logic in Java. 🌱 Improved code readability by modularizing recursive functions. #LeetCode #DataStructures #BinaryTree #Recursion #Java #CodingChallenge #100DaysChallenge #Day4
To view or add a comment, sign in
-
-
🚀 Topological Sort | DFS Approach | Java Today I learned about Topological Sorting using Depth First Search (DFS) — a fundamental algorithm for Directed Acyclic Graphs (DAGs). 🔹 Concept: Topological sorting gives a linear ordering of vertices such that for every directed edge u → v, vertex u comes before v in the ordering. 🔹 Real-world uses: Course scheduling (prerequisites) Task dependency resolution Build order in compilers ⏱ Time Complexity: O(V + E) — each vertex and edge is processed once during DFS. 💾 Space Complexity: O(V) — for recursion stack, visited array, and stack to store results. 🏷️ Hashtags: #TopologicalSort #DFS #GraphAlgorithms #Java #DataStructures #Algorithms #CodingJourney #ProblemSolving #ComputerScience #LearningDSA
To view or add a comment, sign in
-
💻 Day 75 of #100DaysOfCodingChallenge ✨ Problem: 560. Subarray Sum Equals K 📚 Category: Array Manipulation | Subarray Problems 💡 Approach: Brute Force 🧠 Language: Java 🔍 Problem Understanding: We’re given an integer array nums and an integer k. We need to count how many subarrays (continuous parts of the array) have a sum equal to k. Example: Input: nums = [1, 2, 3], k = 3 Output: 2 Explanation: [1,2] and [3] are the subarrays with sum 3. 🧠 Brute Force Approach: In the brute-force method, we check every possible subarray using two loops: 1️⃣ Start from each element as a subarray beginning. 2️⃣ Keep adding elements until the end, and check if the sum equals k. 3️⃣ If yes, increment the count. Although it’s not the most optimized, this approach helps build a strong foundation in understanding subarray logic. ⚙️ Complexity: Time Complexity: O(n²) Space Complexity: O(1) 🌱 Learning: This problem taught me how to approach subarray-based questions step-by-step — starting from brute force before moving toward optimized solutions like prefix sums + hashmaps. Building clarity in fundamentals always makes optimization easier later 🚀 #Day75 #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingJourney #WomenInTech #DSA #ArrayManipulation
To view or add a comment, sign in
-
-
🌿 Day 74 of #100DaysOfCode 🌿 💡 Problem: Binary Tree Preorder Traversal – LeetCode 🚀 Approach: Used a recursive traversal to explore nodes in the order Root → Left → Right. Preorder traversal is all about visiting the leader first — just like taking the initiative before exploring possibilities! 💫 📊 Complexity Analysis: Time Complexity: O(n)** — every node is visited once Space Complexity: O(n)** — due to recursion stack ✅ Runtime: 0 ms (⚡ Beats 100%) ✅ Memory: 43.06 MB 🔑 Key Insight: Recursion helps untangle even the deepest branches — one root call at a time 🌱 #LeetCode #100DaysOfCode #BinaryTree #Recursion #CodingJourney #DSA #ProblemSolving #Java #Algorithms #ProgrammerLife #WomenInTech #CodeEveryday
To view or add a comment, sign in
-
-
✅Day 44 : Leetcode 162 - Find Peak Element #60DayOfLeetcodeChallenge 🧩 Problem Statement: You are given a 0-indexed integer array nums. A peak element is an element that is strictly greater than its neighbors. Your task is to find a peak element and return its index. If the array contains multiple peaks, return the index of any one of them. You must solve this in O(log n) time complexity. 💡 My Approach: I used the Binary Search approach to solve this problem efficiently. Check for edge cases: If there’s only one element, return index 0. If the first element is greater than the second, it’s a peak → return 0. If the last element is greater than the second last, return n-1. Apply binary search between indices 1 and n-2: Find the middle index mid. If nums[mid] is greater than both its neighbors (nums[mid-1] and nums[mid+1]), we found the peak → return mid. If the left neighbor is greater, move the search to the left half. Otherwise, move to the right half. This guarantees logarithmic search efficiency. ⏱️ Time Complexity: O(log n) — due to binary search. 💾 Space Complexity: O(1) — constant extra space. #BinarySearch #LeetCode #FindPeakElement #DSA #Java #CodingPractice #ProblemSolving #LogN
To view or add a comment, sign in
-
-
💡 Day 31/100 Days Today’s problem was “Valid Palindrome” 🪞 The challenge was to check if a string reads the same forward and backward after removing all non-alphanumeric characters and ignoring case differences. This helped strengthen my understanding of string manipulation and the two-pointer technique in Java. 🧠 Key Features: Cleaned the string using regex to remove unwanted characters. Applied two-pointer logic to compare characters from both ends. Simple, efficient, and elegant — a great example of how logic matters more than length of code. ⚙️ Time Complexity: O(n) → Each character checked once. 💾 Space Complexity: O(n) → For the cleaned string (or O(1) in in-place approach). ✨ Takeaway: This problem reinforced how small optimizations — like using pointers instead of extra data structures — make algorithms more efficient. Every problem is a step closer to cleaner and smarter code! #Day31Of100 #DSA #Java #ProblemSolving #TwoPointer #CodingJourney #100DaysOfCode #LearningEveryday
To view or add a comment, sign in
-
-
#Day_31 Today’s challenge was an interesting matrix binary search problem — “Find a Peak Element II”-> it's a medium level question. The task: Given a 2D grid, find any element that’s strictly greater than its top, bottom, left, and right neighbors. A brute-force solution would check every element’s neighbors — but that’s O(m × n). Instead, I used a binary search on columns to cut the search space efficiently. Here’s the idea: Pick the middle column. Find the maximum element in that column. Compare it with its left and right neighbors. If it’s greater than both — you’ve found a peak. Otherwise, move to the side that has a larger neighbor (since a peak must exist there). This clever approach brings the complexity down to O(m × log n) — a huge win on large matrices. This problem was a great reminder that binary search isn’t just for 1D arrays — it can be applied creatively in multiple dimensions too #Coding #LeetCode #Java #BinarySearch #ProblemSolving #LearningEveryday
To view or add a comment, sign in
-
-
🌟 #PostLog23🌟 Problem: 1539. Kth Missing Positive Number This one was about finding the k-th missing positive integer from a sorted array — and I managed to solve it efficiently using binary search, achieving a 0 ms runtime (100% faster submissions) 🚀 💡 Key idea: Instead of checking each missing number one by one, binary search helps to quickly find the point where the count of missing numbers reaches k. This brings the time complexity down to O(log n) — much faster than a linear scan. ✅ Concepts reinforced: Binary search optimization Handling edge cases in sorted arrays Mathematical reasoning in array indexing #LeetCode #CodingChallenge #Java #BinarySearch #ProblemSolving #LearningJourney #DSA
To view or add a comment, sign in
-
-
📅 Day 81 of #100DaysOfLeetCode Problem: Insert into a Binary Search Tree (LeetCode #701) Approach: The task is to insert a new node with a given value into a Binary Search Tree (BST). Start from the root and recursively find the correct position: If the new value is smaller than the current node’s value, go to the left subtree. Otherwise, go to the right subtree. When a null spot is found, insert a new node there. The BST property is preserved throughout this process. Complexity: ⏱️ Time: O(h) — where h is the height of the tree. 💾 Space: O(h) — recursive call stack. 🔗 Problem Link: https://lnkd.in/dCS7zxVG 🔗 Solution Link: https://lnkd.in/dxB4ZNtV #LeetCode #100DaysOfCode #BinarySearchTree #Recursion #Java #TreeTraversal #DSA #Algorithms #CodingChallenge #ProblemSolving #CodeNewbie #StudyWithMe #BuildInPublic #LearnToCode #DailyCoding
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