Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c1fa6f8

Browse filesBrowse files
authored
Merge pull request FazeelUsmani#19 from FazeelUsmani/unsorted-subarray-1
Create 25 shortestUnsortedContinuousSubarray.java
2 parents a5adfa4 + 12d874f commit c1fa6f8
Copy full SHA for c1fa6f8

File tree

Expand file treeCollapse file tree

1 file changed

+27
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+27
-0
lines changed
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int findUnsortedSubarray(int[] nums) {
3+
4+
int start = -1;
5+
int end = -1;
6+
int prevHigh = nums.length-1;
7+
int prevLow = 0;
8+
for(int i=0;i<nums.length;i++){
9+
if(nums[i]<nums[prevLow]){
10+
end = i;
11+
} else {
12+
prevLow = i;
13+
}
14+
}
15+
16+
for(int i=nums.length-1; i >= 0; i--){
17+
if(nums[i] > nums[prevHigh]){
18+
start = i;
19+
} else {
20+
prevHigh = i;
21+
}
22+
}
23+
24+
return (start>=0 && end>=0) ? (end-start)+1 : 0;
25+
26+
}
27+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.