Python Program to check if elements to the left and right of the pivot are smaller or greater respectively
Last Updated :
08 Mar, 2023
Given a list and an index, the task is to write a Python program to first select the element at that index as the pivot element and then test if elements are greater to its right and smaller to its left or not.
Examples:
Input : test_list = [4, 3, 5, 6, 9, 16, 11, 10, 12], K = 4
Output : True
Explanation : Elements at Kth index is 9, elements before that are smaller and next are greater.
Input : test_list = [4, 3, 5, 6, 9, 16, 11, 10, 1], K = 4
Output : False
Explanation : Elements at Kth index is 9, elements before that are smaller but 1 is after 4th index, less than 9.
Method 1 : Using loop and enumerate()
In this, we iterate through elements using loop and enumerate is used to get indices and check if index is greater or smaller than K, to toggle testing condition.
Program:
Python3
# initializing list
test_list = [4, 3, 5, 6, 9, 16, 11, 10, 12]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 4
res = True
for idx, ele in enumerate(test_list):
# checking for required conditions
if (idx > K and ele < test_list[K]) or (idx < K and ele > test_list[K]):
res = False
# printing result
print("Is condition met ? : " + str(res))
Output:
The original list is : [4, 3, 5, 6, 9, 16, 11, 10, 12]
Is condition met ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2 : Using all() and enumerate()
Similar task is performed using all(), which can test for required condition using single line generator expression to provide a more compact solution.
Program:
Python3
# initializing list
test_list = [4, 3, 5, 6, 9, 16, 11, 10, 12]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 4
# all() used to check for condition using one liner
res = all(not ((idx > K and ele < test_list[K]) or (
idx < K and ele > test_list[K])) for idx, ele in enumerate(test_list))
# printing result
print("Is condition met ? : " + str(res))
Output:
The original list is : [4, 3, 5, 6, 9, 16, 11, 10, 12]
Is condition met ? : True
Time complexity: O(n), where n is the length of the test_list. The all() and enumerate() takes O(n) time
Auxiliary Space: O(1), extra space required is not required
Similar Reads
Python3 Program to Check if it is possible to sort the array after rotating it Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array.For eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of t
3 min read
Python Program to test whether the length of rows are in increasing order Given a Matrix, the following program is used to test if length all rows of a matrix are in increasing order or not. If yes, it returns True, otherwise False. Input : test_list = [[3], [1, 7], [10, 2, 4], [8, 6, 5, 1, 4]] Output : True Explanation : 1 < 2 < 3 < 5, increasing lengths of rows
4 min read
Python - Check whether the extracted element from each row of matrix can be in ascending order Given a matrix, the task here is to first select an element from each row in such a manner that when taken in exact order they can potentially form a sorted list or a list in ascending order. If it is possible return True else False. Input : test_list = [[3, 4, 6, 2], [3, 4, 9, 1], [8, 5, 4, 7], [9,
6 min read
Python Program that prints the count of either peaks or valleys from a list Given a List, the following article shows ways to count elements which are either peak(x > y < z) or valley(x < y > z). Input : test_list = [1, 2, 4, 2, 6, 7, 8, 3] Output : 3 Explanation : (2, 4, 2), (4, 2, 6), (7, 8, 3) are peaks and valleys. Input : test_list = [1, 2, 4, 5, 3, 2] Outp
2 min read
Python3 Program for Search an element in a sorted and rotated array An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time. Examp
8 min read
Python3 Program to Count rotations required to sort given array in non-increasing order Given an array arr[] consisting of N integers, the task is to sort the array in non-increasing order by minimum number of anti-clockwise rotations. If it is not possible to sort the array, then print "-1". Otherwise, print the count of rotations.Examples:Input: arr[] = {2, 1, 5, 4, 3}Output: 2Explan
3 min read