• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Find min element in Sorted Rotated Array (With Duplicates)

August 4, 2014 by Dhaval Dave

Problem: Find the minimum element in a sorted and rotated array if duplicates are allowed:
Example: { 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 1, 2}   output: 0
Algorithm:

This problem can be easily solved in O(n) time complexity buy traversing the whole array and check for minimum element.
Here we are using a different solution which also take O(n) time complexity in worst case but it is more optimized.
Let us look at this approach; we are using a modified version of Binary Search Tree algorithm.

Thanks Dheeraj for  sharing Question and Code  for this .
#include<iostream>
using namespace std;
int mini(int a,int b)
{
    if(a<b) return a;
    else return b;
}
int BST(int A[],int low,int high)
{
    int mid=low+(high–low)/2;
    if(mid == high) return A[mid];
    //if size of sub array is 1
    if(mid < high && mid > low && A[mid] < A[mid + 1] && A[mid –1] > A[mid] )       return A[mid];
    //compare value of mid + 1, mid, mid-1 if they exist
    if(A[mid] > A[high])
    {
        if(A[mid + 1] < A[mid]) return A[mid + 1];
        else return BST(A, mid + 1, high); //check in right sub array
    }
    else if(A[mid] < A[high])
    {
        if(A[mid] < A[mid – 1]) return A[mid];
        else return BST(A , low , mid – 1); //check in left sub array
    }
    else if(A[mid]==A[high])
    {
        return min(BST(A,low,mid–1),BST(A,mid+1,high));// check for minimum     in both left and right sub array and choose minimum of them
    }  
   
}
int main()
{
    int A[]={2, 2, 2, 2, 2, 2, 2, 2,0, 0, 1, 1, 2};
    int n= sizeof(A)/sizeof(A[0]);
    cout<<BST(A,0,n–1)<<endl;
}

Similar Articles

Filed Under: Amazon Interview Question, Flipkart Interview Questions, Interview Questions, Microsoft Interview Questions Tagged With: Array, Binary Search

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Check Binary Tree is Binary Search Tree or not

Stock Buy Sell to Maximize Profit

Templates in C++

Edit Distance ( Dynamic Programming )

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

BlueStone E-commerce Interview Experience

Given a string, find the first character which is non-repetitive

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Find Percentage of Words matching in Two Strings

Get Minimum element in O(1) from input numbers or Stack

Print vertical sum of all the axis in the given binary tree

SAP Off Campus Hiring_ March 2015 Computer Skills

VMWare SDEII Interview

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

LeetCode: Binary Tree Maximum Path Sum

Flipkart Set 1 On Campus with Answers

Implement a generic binary search algorithm for Integer Double String etc

Sort an array according to the order defined by another array

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

Sort Stack in place

Find Pythagorean Triplets in an array in O(N)

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Find the kth number with prime factors 3, 5 and 7

SAP Off Campus Hiring_ March 2015 Sample Questions

Hackerearth : Counting Subarrays

HackeEarth Flipkart’s Drone

Maximum occurred Smallest integer in n ranges

How strtok() Works

Find position of the only set bit

Cisco Hiring Event 21st – 22nd Feb 2015

Copyright © 2025 · Genesis Framework · WordPress · Log in