try: return last_extremum_index except UnboundLocalError: return None
def find_last_local_extremum(arr, find_max=True): """ Find the last local extremum in the given array. index of ek chalis ki last local
Parameters: arr (list): The input array. find_max (bool): If True, find the last local maximum; otherwise, find the last local minimum. If no local extremum is found, the function returns None
print(f"Last local maximum index: {last_max_index}") print(f"Last local minimum index: {last_min_index}") This function works by iterating through the array, checking each element to see if it's a local maximum or minimum based on the find_max parameter. The index of the last local extremum found is returned. The function also handles edge cases for the first and last elements. If no local extremum is found, the function returns None . arr[i-1] and arr[i] >
Returns: int or None: The index of the last local extremum. If no local extremum is found, returns None. """ # Iterate over the array from the second element to the second last element for i in range(1, len(arr) - 1): if find_max and arr[i] > arr[i-1] and arr[i] > arr[i+1]: # Found a local maximum, update and continue last_extremum_index = i elif not find_max and arr[i] < arr[i-1] and arr[i] < arr[i+1]: # Found a local minimum, update and continue last_extremum_index = i