Validate sorted input or fail fast
Java
Hard
7 views
Problem Description
Task: if array is not sorted, throw IllegalStateException. Else return true.
Output Format
Return value
Constraints
O(n) validation.
Official Solution
static boolean requireSorted(int[] a){for(int i=1;i<a.length;i++) if(a[i]<a[i-1]) throw new IllegalStateException();return true;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!