21. Find all pairs with given sum
Required Input:
arr = [1, 5, 7, -1, 5], target = 6Expected Output:
[(-1, 7), (1, 5)]
Code In Python
def find_pairs_with_sum(arr, target):
# write your logic here
pass
arr = [1, 5, 7, -1, 5]
target = 6
print(find_pairs_with_sum(arr, target))
Run Code?
Click Run Button to view compiled output
22. Rotate array by k positions
Required Input:
arr = [1, 2, 3, 4, 5, 6, 7], k = 3Expected Output:
[5, 6, 7, 1, 2, 3, 4]
Code In Python
def rotate_array(arr, k):
# write your logic here
pass
arr = [1, 2, 3, 4, 5, 6, 7]
k = 3
print(rotate_array(arr, k))
Run Code?
Click Run Button to view compiled output
23. Find all duplicates in an array
Required Input:
arr = [4, 3, 2, 7, 8, 2, 3, 1]Expected Output:
[2, 3]
Code In Python
def find_duplicates(arr):
# write your logic here
pass
arr = [4, 3, 2, 7, 8, 2, 3, 1]
print(find_duplicates(arr))
Run Code?
Click Run Button to view compiled output
24. Product of array except self
Required Input:
arr = [1, 2, 3, 4]Expected Output:
[24, 12, 8, 6]
Code In Python
def product_except_self(arr):
# write your logic here
pass
arr = [1, 2, 3, 4]
print(product_except_self(arr))
Run Code?
Click Run Button to view compiled output
25. Check for subarray with zero sum
Required Input:
arr = [4, 2, -3, 1, 6]Expected Output:
True
Code In Python
def has_zero_sum_subarray(arr):
# write your logic here
pass
arr = [4, 2, -3, 1, 6]
print(has_zero_sum_subarray(arr))
Run Code?
Click Run Button to view compiled output
26. 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
arr = [100, 4, 200, 1, 3, 2]
print(longest_consecutive_sequence(arr))
Run Code?
Click Run Button to view compiled output
27. Move zeroes to end
Required Input:
arr = [0, 1, 0, 3, 12]Expected Output:
[1, 3, 12, 0, 0]
Code In Python
def move_zeroes(arr):
# write your logic here
pass
arr = [0, 1, 0, 3, 12]
print(move_zeroes(arr))
Run Code?
Click Run Button to view compiled output
28. Find the majority element
Required Input:
arr = [2, 2, 1, 1, 1, 2, 2]Expected Output:
2
Code In Python
def find_majority_element(arr):
# write your logic here
pass
arr = [2, 2, 1, 1, 1, 2, 2]
print(find_majority_element(arr))
Run Code?
Click Run Button to view compiled output
29. Leaders in an array
Required Input:
arr = [16, 17, 4, 3, 5, 2]Expected Output:
[17, 5, 2]
Code In Python
def find_leaders(arr):
# write your logic here
pass
arr = [16, 17, 4, 3, 5, 2]
print(find_leaders(arr))
Run Code?
Click Run Button to view compiled output
30. Count inversions in array
Required Input:
arr = [1, 20, 6, 4, 5]Expected Output:
5
Code In Python
def count_inversions(arr):
# write your logic here
pass
arr = [1, 20, 6, 4, 5]
print(count_inversions(arr))
Run Code?
Click Run Button to view compiled output


