Find an element in an array such that elements form a strictly decreasing and increasing sequence in Python

In Python, we can find an element in an array where elements form a strictly decreasing sequence followed by a strictly increasing sequence. The element we seek is the transition point between these two sequences.

Problem Requirements

The solution must satisfy these conditions:

  • Both decreasing and increasing sequences must have minimum length 2
  • The last value of the decreasing sequence is the first value of the increasing sequence
  • No duplicate elements are allowed (strictly decreasing/increasing)

For example, in array [5, 4, 3, 4], the sequence [5, 4, 3] is strictly decreasing and [3, 4] is strictly increasing, so the answer is 3.

Algorithm Approach

We track two counters to monitor sequence lengths:

  • decrease − counts elements in decreasing sequence
  • increase − counts elements in increasing sequence
  • pt − stores the transition point element

Implementation

def search_element(array):
    increase = 1
    decrease = 1
    n = len(array)
    pt = -1
    
    for i in range(1, n):
        if array[i] < array[i-1]:
            # Still in decreasing sequence
            if increase == 1:
                decrease = decrease + 1
            else:
                # Cannot have decreasing after increasing started
                return -1
        elif array[i] > array[i-1]:
            # Transition to increasing sequence
            if increase == 1:
                pt = array[i-1]  # Mark transition point
            if decrease >= 2:
                increase = increase + 1
            else:
                # Need at least 2 elements in decreasing sequence
                return -1
        elif array[i] == array[i-1]:
            # Equal elements not allowed (strictly sequences)
            return -1
    
    # Check if both sequences have minimum length 2
    if increase >= 2 and decrease >= 2:
        return pt
    else:
        return -1

# Test the function
array = [5, 4, 3, 4]
element = search_element(array)
print(f"Transition element: {element}")
Transition element: 3

Step-by-Step Execution

For array [5, 4, 3, 4]:

  • Compare 4 < 5: decreasing, decrease = 2
  • Compare 3 < 4: still decreasing, decrease = 3
  • Compare 4 > 3: increasing starts, pt = 3, increase = 2
  • Both sequences have length ? 2, return pt = 3

Additional Test Cases

def test_cases():
    test_arrays = [
        [5, 4, 3, 4],        # Valid: decreasing [5,4,3], increasing [3,4]
        [6, 5, 4, 5, 6],     # Valid: decreasing [6,5,4], increasing [4,5,6]
        [3, 2, 3],           # Invalid: decreasing sequence too short
        [5, 4, 4, 5],        # Invalid: duplicate elements
        [1, 2, 3, 4]         # Invalid: no decreasing sequence
    ]
    
    for i, arr in enumerate(test_arrays):
        result = search_element(arr)
        print(f"Array {arr}: {result}")

test_cases()
Array [5, 4, 3, 4]: 3
Array [6, 5, 4, 5, 6]: 4
Array [3, 2, 3]: -1
Array [5, 4, 4, 5]: -1
Array [1, 2, 3, 4]: -1

Conclusion

This algorithm finds the transition point in O(n) time by tracking sequence lengths and ensuring both decreasing and increasing parts meet the minimum requirements. It returns the element where the strictly decreasing sequence transitions to strictly increasing.

Updated on: 2026-03-25T09:13:24+05:30

713 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements