Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Search in Rotated Sorted Array problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Search in Rotated Sorted Array problem solution There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].

Given the array nums after the rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. You must write an algorithm with O(log n) runtime complexity.

Leetcode Search in Rotated Sorted Array problem solution

Leetcode Search in Rotated Sorted Array problem solution in Python.

class Solution:
    def search(self, nums, target):    
        def search(nums, l, r):
            m = (l + r) // 2
            if nums[m] == target:
                return m
            if l == r:
                return -1
            if nums[l] < nums[m]:
                if target >= nums[l] and target < nums[m]:
                    return search(nums, l, m - 1)
                else:
                    return search(nums, m + 1, r)
            elif r - m == 1:
                if nums[r] == target:
                    return r
                else:
                    return search(nums, l, m)
            elif nums[m + 1] < nums[r]:
                if target >= nums[m + 1] and target <= nums[r]:
                    return search(nums, m + 1, r)
                else:
                    return search(nums, l, m - 1)
            return -1

        return -1 if not nums else search(nums, 0, len(nums) - 1)

Search in Rotated Sorted Array problem solution in Java.

class Solution {
    public int search(int[] nums, int target) {
        
        if(nums.length == 1){
            return nums[0] == target ? 0 : -1;    
        }
        int i=0;
        int j = nums.length-1;
        while(i<j){
            if(nums[i] == target){
                return i;
            }
            else if(nums[j] == target){
                return j;
            }
            else if(target > nums[i]){
                i++;
            }
            else{
                j--;
            }
        }
        return -1;
    }
}

Problem solution in C++.

class Solution {
public:
    int b_Search(vector<int>& nums, int target, int low, int high) {
        while(low <= high) {
            int mid = low + (high - low) / 2;
            if(nums[mid] == target) return mid;
            else if(nums[mid] > target) high = mid - 1;
            else low = mid + 1;
        }
        return -1;
    }
    
    int search(vector<int>& nums, int target) {
        int i = 0;
        for(; i < nums.size() - 1; i++) {
            if(nums[i] > nums[i + 1]) break;
        }
        int ans = b_Search(nums, target, 0, i);
        if(ans != -1 || i == nums.size() - 1) return ans; 
        ans = b_Search(nums, target, i+1, nums.size() - 1);
        return ans;
    }
};

Problem solution in C.

int search(int* nums, int numsSize, int target){

    int i = 0;
for(i = 0;i<numsSize;i++)
{
    if((nums[i] == target) ) 
        return i;
    if((nums[numsSize - i-1] == target ))
        return (numsSize-i-1);
}

return -1;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes