# 5 Best Ways to Program to Find Circular Greater Element to the Right in Python

Rate this post

π‘ Problem Formulation: In a circular array, for each element, we aim to find the next greater element located to its right. A circular array means the end of the array wraps around to the beginning, forming a circle. For instance, given the input array [2, 1, 3, 4], a desired output would be [3, 3, 4, -1], as the next greater element for 4 (the last element) is sought from the beginning of the array.

## Method 1: Brute Force

The brute force method iterates through each element and searches for the next greater element by checking the remainder of the array and continuing from the start if necessary. This approach has a time complexity of O(n^2) where n is the number of elements in the array.

Here’s an example:

```def circular_greater_element(arr):
result = []
length = len(arr)
for i in range(length):
next_greater = -1
for j in range(1, length):
if arr[(i+j) % length] > arr[i]:
next_greater = arr[(i+j) % length]
break
result.append(next_greater)
return result

print(circular_greater_element([2, 1, 3, 4]))```

Output: [3, 3, 4, -1]

This function `circular_greater_element()` goes through each element, then loops again to find the next greater element considering the array is circular. The result is a list that contains the next greater element for each index, or -1 if none exists.

## Method 2: Using Stack for Next Greater Element

Using a stack can optimize the search for the next greater element. It involves traversing the array twice while maintaining a stack to store elements and their indices. This method has a time complexity of O(n).

Here’s an example:

```def circular_greater_element_stack(arr):
result = [-1] * len(arr)
stack = []
for _ in range(2):
for i in range(len(arr)):
while stack and arr[stack[-1]] < arr[i]:
result[stack.pop()] = arr[i]
stack.append(i)
return result

print(circular_greater_element_stack([2, 1, 3, 4]))```

Output: [3, 3, 4, -1]

This `circular_greater_element_stack()` function uses a stack to keep track of elements and optimizes finding the next greater element by reducing redundant comparisons. It gives the right result with better efficiency than the brute force method.

## Method 3: Optimized Circular Traversal

In this method, we simulate the circular traversal of the array without actually concatenating it by using modulo operation to access indices in a circular manner. The advantage is less memory usage as we don’t create a new copy of the array.

Here’s an example:

```def optimized_circular_traversal(arr):
n = len(arr)
result = [-1] * n
for i in range(n):
for j in range(1, n):
if arr[(i+j) % n] > arr[i]:
result[i] = arr[(i+j) % n]
break
return result

print(optimized_circular_traversal([2, 1, 3, 4]))```

Output: [3, 3, 4, -1]

The function `optimized_circular_traversal()` provides a neat way to loop over the array circularly using modulus. It mitigates the problem of using extra space but still has the complexity of O(n^2).

## Method 4: Preprocessing with Monotonic Stack

This advanced method involves preprocessing the array using a monotonic stack to store potential candidates for next greater elements and then utilizes this preprocessed information while traversing the array the second time. It runs with a linear time complexity of O(n).

Here’s an example:

```def preprocessing_with_monotonic_stack(arr):
extended_arr = arr + arr
stack = []
n = len(arr)
result = [-1] * n
for i in range(len(extended_arr)):
while stack and extended_arr[stack[-1]] < extended_arr[i]:
if stack[-1] < n:
result[stack.pop()] = extended_arr[i]
else:
stack.pop()
if i < n:
stack.append(i)
return result

print(preprocessing_with_monotonic_stack([2, 1, 3, 4]))```

Output: [3, 3, 4, -1]

By extending the array and using a monotonic stack, the `preprocessing_with_monotonic_stack()` function achieves a time-efficient solution. It’s a sophisticated approach that gives us the best time complexity for this problem.

## Bonus One-Liner Method 5: Using List Comprehension and Modulus Operator

This concise method involves using Python’s list comprehension and the modulus operator to write a one-liner that captures the essence of the problem through a functional approach.

Here’s an example:

```def one_liner_circular_greater(arr):
return [next((arr[(i+j) % len(arr)] for j in range(1, len(arr)) if arr[(i+j) % len(arr)] > arr[i]), -1) for i in range(len(arr))]

print(one_liner_circular_greater([2, 1, 3, 4]))```

Output: [3, 3, 4, -1]

The one-liner `one_liner_circular_greater()` uses a generator expression within a list comprehension for a compact solution. While this method is elegant and Pythonic, it may not be as efficient or readable as the other methods.

## Summary/Discussion

• Method 1: Brute Force. Simple to understand but has poor performance with O(n^2) complexity.
• Method 2: Using Stack for Next Greater Element. Efficient with linear time complexity, but requires understanding stacks.
• Method 3: Optimized Circular Traversal. Avoids extra space but still not time-efficient.
• Method 4: Preprocessing with Monotonic Stack. Most optimal time complexity but more complex to code and understand.
• Method 5: One-Liner. Elegant and concise, great for quick scripting but can be less readable and not as efficient.