Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
Return the shortest such subarray and output its length.
Input:[2,6,4,8,10,9,15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
def shortsub(arr): N=len(arr) L,arr1 = [],arr[:] arr1.sort() for i in range(N): if(arr[i]!=arr1[i]): L.append(i) if bool(L): L=list(range(L[0],L[-1]+1,1)) l1 = [] l1 = arr[L[0]:L[-1]+1] l1.sort() return len(l1) else: return 0 A=list(map(int,input("Array: ").split(" "))) print(shortsub(A))
INPUT_1:
Array: 2 6 4 8 10 9 15
OUTPUT:
5
INPUT_2:
Array: 1 2 3 4
OUTPUT:
0
INPUT_3:
Array: 1
OUTPUT:
0
INPUT_4:
Array: 10 12 20 30 25 40 32 31 35 50 60
OUTPUT:
6
INPUT_5:
Array: 0 1 15 25 6 7 30 40 50
OUTPUT:
4
ILLUSTRATION
![](http://www.fcukthecode.com/wp-content/uploads/2022/01/Screenshot-from-2022-01-02-11-19-34.png)
Morae Q!
- Conversion of days into year, weeks and days.
- Find if the number is a perfect number or not.
- Compute conversion of Binary to Octal.
- Return the sum of digits in a number.
- Find if a word exists or not in a sentence.
- Convert Numbers into Words.
- Read a word if it consists only of the letters known.
- Check if the string is a dynamic string or not.
- Convert all Uppercase letters to Lowercase and vice-versa.
- Change the string such that there are no matching adjacent characters.
- Find the number of sub-strings which start and end both in 1.
- Find the start and end index of unsorted sub-array.
- Find the maximum number of pairs that can be formed.
- Figure out the number of bubbly words present.
- Check if a string is lapindrome or not even with a middle character.
- Seating layout in a triangular shaped class according to the number of rows.
- Find and Sort a sub-array which makes whole array sorted.
- Seating layout according to the number of rows.
- Find the final states of the bulbs.
- Check if reversing sub array makes the array sorted.