Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the element before which all the elements are smaller than it, and after which all are greater in Python
In array processing, we sometimes need to find a special element that acts as a pivot point ? an element where all elements to its left are smaller, and all elements to its right are greater. This problem requires finding such an element and returning its index, or -1 if no such element exists.
Problem Understanding
Given an array, we need to find an element where:
All elements before it are smaller than the element
All elements after it are greater than the element
For the array [6, 2, 5, 4, 7, 9, 11, 8, 10], the element at index 4 (value 7) satisfies this condition ?
Algorithm
The solution uses two passes through the array ?
Create a
maximum_leftarray storing the maximum element to the left of each positionTraverse right to left, maintaining
minimum_rightfor elements to the rightCheck if current element is greater than maximum left and smaller than minimum right
Implementation
def get_element(arr):
n = len(arr)
maximum_left = [None] * n
maximum_left[0] = float('-inf')
# Build maximum_left array
for i in range(1, n):
maximum_left[i] = max(maximum_left[i-1], arr[i-1])
minimum_right = float('inf')
# Traverse right to left
for i in range(n-1, -1, -1):
if maximum_left[i] < arr[i] and minimum_right > arr[i]:
return i
minimum_right = min(minimum_right, arr[i])
return -1
# Test the function
arr = [6, 2, 5, 4, 7, 9, 11, 8, 10]
result = get_element(arr)
print(f"Pivot element index: {result}")
print(f"Pivot element value: {arr[result] if result != -1 else 'None'}")
Pivot element index: 4 Pivot element value: 7
How It Works
Let's trace through the algorithm step by step ?
def get_element_with_trace(arr):
n = len(arr)
maximum_left = [None] * n
maximum_left[0] = float('-inf')
print("Building maximum_left array:")
for i in range(1, n):
maximum_left[i] = max(maximum_left[i-1], arr[i-1])
print(f"maximum_left[{i}] = max({maximum_left[i-1]}, {arr[i-1]}) = {maximum_left[i]}")
print(f"\nFinal maximum_left: {maximum_left}")
print("\nTraversing right to left:")
minimum_right = float('inf')
for i in range(n-1, -1, -1):
print(f"i={i}, arr[i]={arr[i]}, max_left={maximum_left[i]}, min_right={minimum_right}")
if maximum_left[i] < arr[i] and minimum_right > arr[i]:
print(f"Found pivot at index {i}")
return i
minimum_right = min(minimum_right, arr[i])
return -1
arr = [6, 2, 5, 4, 7, 9, 11, 8, 10]
get_element_with_trace(arr)
Building maximum_left array: maximum_left[1] = max(-inf, 6) = 6 maximum_left[2] = max(6, 2) = 6 maximum_left[3] = max(6, 5) = 6 maximum_left[4] = max(6, 4) = 6 maximum_left[5] = max(6, 7) = 7 maximum_left[6] = max(7, 9) = 9 maximum_left[7] = max(9, 11) = 11 maximum_left[8] = max(11, 8) = 11 Final maximum_left: [-inf, 6, 6, 6, 6, 7, 9, 11, 11] Traversing right to left: i=8, arr[i]=10, max_left=11, min_right=inf i=7, arr[i]=8, max_left=11, min_right=10 i=6, arr[i]=11, max_left=9, min_right=8 i=5, arr[i]=9, max_left=7, min_right=8 i=4, arr[i]=7, max_left=6, min_right=8 Found pivot at index 4
Testing with Different Cases
def test_pivot_element():
test_cases = [
[6, 2, 5, 4, 7, 9, 11, 8, 10], # Has pivot at index 4
[1, 2, 3, 4, 5], # No pivot (ascending)
[5, 4, 3, 2, 1], # No pivot (descending)
[1, 3, 2, 4, 5], # Has pivot at index 3
]
for i, arr in enumerate(test_cases):
result = get_element(arr)
print(f"Test {i+1}: {arr}")
if result != -1:
print(f" Pivot found at index {result}, value = {arr[result]}")
else:
print(f" No pivot element found")
print()
test_pivot_element()
Test 1: [6, 2, 5, 4, 7, 9, 11, 8, 10] Pivot found at index 4, value = 7 Test 2: [1, 2, 3, 4, 5] No pivot element found Test 3: [5, 4, 3, 2, 1] No pivot element found Test 4: [1, 3, 2, 4, 5] Pivot found at index 3, value = 4
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(n) | Two passes through the array |
| Space | O(n) | Additional array for maximum_left |
Conclusion
This algorithm efficiently finds a pivot element by preprocessing maximum values from the left and checking conditions during a right-to-left traversal. The key insight is that a valid pivot must be greater than all elements to its left and smaller than all elements to its right.
