Finding Element in Rotated Sorted Array in Python29 Aug 2024 | 6 min read Using binary search, we can find an element in the given sorted array or list in O(log(n)) time complexity, where n is the number of the numbers in the list. Now let us rotate this sorted array or list at any pivot. However, we don't know the pivot and cannot use it in the operations. For example, if the array = [1, 2, 3, 4, 5, 6, 7] is rotated at pivot 3, then the new array will be array_rotated = [4, 5, 6, 7, 1, 2, 3]. The problem statement for this tutorial is to find a particular number in the rotated sorted array. That too in O(log(n)) time complexity. Sample Inputs and Outputs:Input: array = [4, 6, 8, 10, 12, 14, 0, 2]; target = 6 Output: Present at index 1 Input: array = [8, 10, 12, 14, 0, 2, 4, 6]; target = 23 Output: Not found in the array Input: array = [20, 30, 40, 0, 10]; target = 40 Output: Present at index 2 Assumption:- We will assume that the array contains the unique element, i.e., none of the elements of the array is repeated. Naïve ApproachIn this method, we will first try to find the rotation pivot. Then divide the original array into two sub-arrays. We will perform a binary search in these so sorted sub-arrays. The logic behind finding the pivot element is that it is the only element that, when we move from left to right in the array, will be smaller than its last element. Using this logic, we will find a pivot. The two sub-arrays will be individual sorted arrays. We can hence apply binary search on these two sub-arrays and find the element. Algorithm Input array = [4, 5, 6, 7, 1, 2, 3] The element we need to search = 2 1) Find the pivot of the array. Divide the array into two sub-arrays. (pivot = 3) (Index of element 7). 2) Now, call the binary search function and pass the two sub-arrays to the function. (a) If the element we need to search is greater than the 0th element of the array, then apply this logic: (b) Else, we will search for the right sub-array (Since 2 is less than the 0th element, i.e.,4, so will search for the element in the right sub-array) 3) If we can find the element in the selected sub-array, then we will return the index of the element. Else we will return -1. We will implement this algorithm in Python: Code Output: The pivot of the array is: 3 The index of 2 in the [4, 5, 6, 7, 1, 2, 3] array is: 5 Complexity AnalysisTime Complexity: The time complexity of this program is O(log n). We have used binary search first to find the pivot and then to find the element in the sub-array. Space Complexity: This program takes O(1) extra memory space. We have not used any additional space to store the array. Better SolutionApproach: Instead of applying binary search two times, we can solve the problem in a single pass of binary search. We will modify the algorithm to get the desired results. We will create a recursive function that will take the lower and higher index pointers, the array, and the target element. The function will return the index of the target element in the rotated sorted array.
Below is the implementation of the above idea: Code Output: Index: 2 Complexity AnalysisTime Complexity: The time complexity of this program is O(log n). We have used a single binary search algorithm; hence the time complexity is O(log n). Space Complexity: O(1). We have not used any extra memory space. How to handle duplicates?If the array contains duplicate elements, searching the index in O(log n) time complexity is impossible. For instance, if we want to search the index of 0 in the array [1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1], then we don't know in which sub-array the element will be possible there. Since we cannot decide the sub-array, we have to search the complete array; hence, the time complexity will be O(n). |
The OpenWeatherMap is indeed a service that offers weather information to the creators for web services and mobile applications, including current weather information, forecasts, and historical data. It offers a limited free usage tier as well as an API with JSON, XML, and HTML endpoints. Users can...
3 min read
Difference between Yield and Return in Python Python Yield statement The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator. The yield statement hauls the function and returns back the value to the function caller and restart from...
4 min read
Even as mobile and web applications seem to overtake the market of software development, there is still a demand for traditional Graphical User Interface (GUI) desktop applications. For developers who are fascinated in creating these kinds of applications in the Python programming language, there are...
25 min read
We can always use Python's built-in time module whenever dealing with time-related tasks. There are several ways to represent time in code, including numbers, strings, and objects, thanks to this built-in module. It also has additional features like the ability to get the current time, wait...
3 min read
Introduction Python is a robust and flexible programming language, and developing maintainable and successful code requires clean and effective functions. This post will examine numerous techniques for enhancing your Python functions without using cumbersome or duplicative code. By adhering to these clean coding practices, you can make...
4 min read
Good ConvNets are monstrous machines with many hidden layers and millions of parameters. 'Higher the number of hidden layers, the better the network' is really a terrible maxim. Among the well-known networks are ResNet, AlexNet, VGG, Inception, and others. Why are these networks so effective? How...
10 min read
In this tutorial, we will learn how Matplotlib can be used to include legends in subplots. The legend can be added after creating the plot using the legend() function. Syntax: The syntax for legend in subplot is: axes[position].legend(loc = '') where, loc is used for location. Approaches: Following are the approaches we...
3 min read
A calendar GUI can be created in python using PyQt5 module widget QCalendar. The most recent version of Riverbank Computing's GUI widget toolkit is PyQt5. Qt, one of the most effective and well-liked cross-platform GUI libraries, has a Python interface. The Python programming language and the Qt...
3 min read
In this tutorial, we will learn about the Google's protobuf and how we can implement using Python programming language. Suppose, there is a group number of people of different origin and they speak different languages. To communicate effectively, they try to use a language that everyone can...
8 min read
In this tutorial, we will learn how to remove single quotes from the string. Sometimes, we must remove all sections or just the ones surrounding a string. We can also remove the single and double quotes. We will use various methods to remove quotes; you can...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India