From b9c16fa46cd132aa2e861259b861711856364a30 Mon Sep 17 00:00:00 2001 From: Srishti Ahuja <57188326+Srishti-Ahuja@users.noreply.github.com> Date: Sun, 16 Oct 2022 14:20:05 +0530 Subject: [PATCH] Added Search in rotated array (O(logn) complexity) --- Medium/SearchInRotatedArray.cpp | 105 ++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 Medium/SearchInRotatedArray.cpp diff --git a/Medium/SearchInRotatedArray.cpp b/Medium/SearchInRotatedArray.cpp new file mode 100644 index 0000000..0b06caa --- /dev/null +++ b/Medium/SearchInRotatedArray.cpp @@ -0,0 +1,105 @@ +/* +Problem Description : + +There is an integer array nums sorted in ascending order (with distinct values). +Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= 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 possible 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. + +Examples: + +Example 1: +Input: nums = [4,5,6,7,0,1,2], target = 0 +Output: 4 + +Example 2: +Input: nums = [4,5,6,7,0,1,2], target = 3 +Output: -1 + +Example 3: +Input: nums = [1], target = 0 +Output: -1 +*/ + +class Solution { +public: + int search(vector& nums, int target) { + //find break point + int break_p=0, min=0, max=nums.size()-1, flag=0; + + if(nums[0]>nums[nums.size()-1]){ + while(minnums[((min+max)/2)+1]){ + break_p=(min+max)/2; + flag=1; + break; + } + else if(nums[(min+max)/2]>nums[0]) + min=(min+max)/2; + else + max=(min+max)/2; + } + } + if(flag==1){ + + //search in first half + min=0, max=break_p; + if(target>=nums[min] && target<=nums[max]){ + while(mintarget) + max=(min+max)/2; + else{ + min=(min+max+1)/2; + cout<=nums[min] && target<=nums[max]){ + while(mintarget) + max=(min+max)/2; + else{ + min=(min+max+1)/2; + cout<=nums[min] && target<=nums[max]){ + while(mintarget) + max=(min+max)/2; + else{ + min=(min+max+1)/2; + cout<