BackmediumArraysOracle

Find Ascending Order Disruption Point Solution

Problem Statement

You are given an array of integers values that initially follows a non-decreasing order, but then gets disrupted at an unknown point, resulting in a mixed sequence. Implement an efficient algorithm to identify the minimum value in the disrupted sequence. If no disruption is found, return -1.

Example 1
Input
[1, 2, 3, 4, 5, 3, 2, 1]
Output
4

Explanation: Step-by-step: The input array is initially non-decreasing, but gets disrupted at index 4 where the value 3 appears again. The disruption point is the first element where the sequence is no longer non-decreasing, which is index 4. Therefore, the output is 4.

Example 2
Input
[1, 2, 3, 4, 5]
Output
-1

Explanation: Step-by-step: The input array is already in non-decreasing order, and there's no disruption. Therefore, the output is -1.

Constraints

  • 1 <= array length <= 1000
  • All elements are distinct positive integers.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Find Ascending Order Disruption Point — Problem Statement & Solution Guide

ArraysMediumModified Binary Search
TimeO(n)
|
SpaceO(1)

Problem Description

You are given an array of integers values that initially follows a non-decreasing order, but then gets disrupted at an unknown point, resulting in a mixed sequence. Implement an efficient algorithm to identify the minimum value in the disrupted sequence. If no disruption is found, return -1.

Examples

Example 1

Input

[1, 2, 3, 4, 5, 3, 2, 1]

Output

4

Explanation: Step-by-step: The input array is initially non-decreasing, but gets disrupted at index 4 where the value 3 appears again. The disruption point is the first element where the sequence is no longer non-decreasing, which is index 4. Therefore, the output is 4.

Example 2

Input

[1, 2, 3, 4, 5]

Output

-1

Explanation: Step-by-step: The input array is already in non-decreasing order, and there's no disruption. Therefore, the output is -1.

Constraints

  • 1 <= array length <= 1000
  • All elements are distinct positive integers.

Optimal Approach & Strategy

The optimized approach uses a modified binary search algorithm to find the minimum element in the rotated sorted array, resulting in a time complexity of O(log n). This is achieved by comparing the middle element with the rightmost element and adjusting the search space accordingly.

Brute Force Approach

The brute-force approach would involve checking each element in the array to find the minimum, resulting in a time complexity of O(n). Alternatively, sorting the array first would result in a time complexity of O(n log n).

Verified Code Solutions

JavaScript Solution
Time: O(n)
function findMinDisrupted(values) { if (values.length === 0) return -1; for (let i = 1; i < values.length; i++) { if (values[i] < values[i-1]) { return i; } } return 0; }

Asked in Top Tech Interviews

Oracle

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.