Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Search in Rotated Sorted Array problem solution

YASH PAL, 31 July 2024

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

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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)

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

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes