11. Problem 11 - best time to buy and sell stock
Required Input:
arr = [7, 1, 5, 3, 6, 4]Expected Output:
5
Code In Python
def max_profit(arr):
# write your logic here
pass
# prefilled input
arr = [7, 1, 5, 3, 6, 4]
print(max_profit(arr))
Run Code?
Click Run Button to view compiled output
12. Problem 12 - rearrange an array with alternate positive and negative elements
Required Input:
arr = [1, 2, -3, -4, 5, -6]Expected Output:
[1, -3, 2, -4, 5, -6]
Code In Python
def rearrange_alternate(arr):
# write your logic here
pass
# prefilled input
arr = [1, 2, -3, -4, 5, -6]
print(rearrange_alternate(arr))
Run Code?
Click Run Button to view compiled output
13. Problem 13 - find the longest consecutive sequence
Required Input:
arr = [100, 4, 200, 1, 3, 2]Expected Output:
4
Code In Python
def longest_consecutive_sequence(arr):
# write your logic here
pass
# prefilled input
arr = [100, 4, 200, 1, 3, 2]
print(longest_consecutive_sequence(arr))
Run Code?
Click Run Button to view compiled output
14. Problem 14 - find all subarrays with given sum
Required Input:
arr = [1, 2, 3, 7, 5]
target = 12Expected Output:
[[2, 3, 7]]
Code In Python
def find_subarrays_with_sum(arr, target):
# write your logic here
pass
# prefilled input
arr = [1, 2, 3, 7, 5]
target = 12
print(find_subarrays_with_sum(arr, target))
Run Code?
Click Run Button to view compiled output
15. Problem 15 - find the first and last position of an element in a sorted array
Required Input:
arr = [5, 7, 7, 8, 8, 10]
target = 8Expected Output:
[3, 4]
Code In Python
def find_first_last_position(arr, target):
# write your logic here
pass
# prefilled input
arr = [5, 7, 7, 8, 8, 10]
target = 8
print(find_first_last_position(arr, target))
Run Code?
Click Run Button to view compiled output
16. Problem 16 - trapping rain water
Required Input:
arr = [0,1,0,2,1,0,1,3,2,1,2,1]Expected Output:
6
Code In Python
def trap_rain_water(arr):
# write your logic here
pass
# prefilled input
arr = [0,1,0,2,1,0,1,3,2,1,2,1]
print(trap_rain_water(arr))
Run Code?
Click Run Button to view compiled output
17. Problem 17 - find the median of two sorted arrays
Required Input:
arr1 = [1, 3]
arr2 = [2]Expected Output:
2
Code In Python
def find_median_sorted_arrays(arr1, arr2):
# write your logic here
pass
# prefilled input
arr1 = [1, 3]
arr2 = [2]
print(find_median_sorted_arrays(arr1, arr2))
Run Code?
Click Run Button to view compiled output
18. Problem 18 - maximum product subarray
Required Input:
arr = [2, 3, -2, 4]Expected Output:
6
Code In Python
def max_product_subarray(arr):
# write your logic here
pass
# prefilled input
arr = [2, 3, -2, 4]
print(max_product_subarray(arr))
Run Code?
Click Run Button to view compiled output
19. Problem 19 - count inversions in an array
Required Input:
arr = [8, 4, 2, 1]Expected Output:
6
Code In Python
def count_inversions(arr):
# write your logic here
pass
# prefilled input
arr = [8, 4, 2, 1]
print(count_inversions(arr))
Run Code?
Click Run Button to view compiled output
20. Problem 20 - find the smallest missing positive integer
Required Input:
arr = [3, 4, -1, 1]Expected Output:
2
Code In Python
def find_missing_positive(arr):
# write your logic here
pass
# prefilled input
arr = [3, 4, -1, 1]
print(find_missing_positive(arr))
Run Code?
Click Run Button to view compiled output


