Developer Learning Wiki
  • About
  • DSA
    • Overview
    • Data Structures
    • Algorithms
    • Problems
  • System Design
    • Overview
  • Technical Blogs
    • Overview
  1. Data Structures & Algorithms
  2. Practice Problems
  3. Binary Search in Reversed Sorted Array
  • Data Structures & Algorithms
    • Practice Problems
      • Binary Search in Reversed Sorted Array
      • Two Sum
      • Valid Parentheses
      • Climbing Stairs
      • Longest Substring Without Repeating Characters
    • Data Structures
      • Queue
    • Algorithms
      • Binary Search
      • Kadane’s Algorithm

On this page

  • Binary Search in Reversed Sorted Array
    • Problem Statement
      • Example
      • Approach
      • Explanation
  1. Data Structures & Algorithms
  2. Practice Problems
  3. Binary Search in Reversed Sorted Array

Binary Search in Reversed Sorted Array

Binary Search in Reversed Sorted Array

Problem Statement

Given a reversed sorted array of integers, find the index of a given target value. If the target is not found in the array, return -1.

Example

Input: nums = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], target = 2
Output: 8 (index of 2)

Approach

We can use the same approach as the binary search algorithm.

int binarySearch(vector<int>& nums, int target) {
    int start = 0;
    int end = nums.size() - 1;
    
    while (start <= end) {
        int mid = start + (end - start) / 2;  // Avoid overflow
        
        if (nums[mid] == target) {
            return mid;
        } else if (nums[mid] < target) {
            end = mid - 1;
        } else {
            start = mid + 1;
        }
    }
    return -1;
}   

Explanation

  • We start by initializing the start and end pointers.
  • We then enter a while loop that continues as long as the start pointer is less than or equal to the end pointer.
  • In each iteration, we calculate the middle index mid.
  • We then check if the value at the middle index is equal to the target.
  • If it is, we return the middle index.
  • If the value at the middle index is less than the target, we move the end pointer to mid - 1.
  • If the value at the middle index is greater than the target, we move the start pointer to mid + 1.

© 2024 • Developer Learning Wiki • About •

Found an error? Please leave a comment here or submit a PR in this repository, thanks!

The best way to predict the future is to invent it.

Alan Kay

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Martin Fowler

First, solve the problem. Then, write the code.

John Johnson

×

🚀 Happy Coding!