Array_0176CS221053
Array_0176CS221053
Submitted By:
0176CS221053
Captain Jack Sparrow is on a treasure hunt and has a map with n marked locations numbered from 0 to n.
However, one of the location numbers has been smudged, and Jack can't find it. Given the list of location
numbers he still has, help Jack find the missing location number so he can claim the treasure.
Problem Statement:
Given an array containing n distinct numbers from 0 to n, find the missing number.
Constraints:
● 1 ≤ n ≤ 104
● 0 ≤ array elements ≤ n
Examples:
Solution:
def missingNum(arr):
n = len(arr)
total = n * (n + 1) / 2
for i in arr:
total -= i
return total
arr = [0]
print(missingNum(arr))
2. The Magical Potion – Maximum Subarray Sum
Harry is brewing a magical potion in Hogwarts. He needs to select a contiguous sequence of ingredients
from his inventory to achieve the highest magical power. Each ingredient has a power level (which can be
positive, negative, or zero). Help Harry choose a contiguous subarray of ingredients that yields the highest
possible power for his potion.
Problem Statement:
Find the contiguous subarray within an array (containing at least one number) that has the largest sum.
Constraints:
● 1 ≤ n ≤ 105
Solution:
def maxSubarraySum(arr):
max_sum = float('inf')
c_sum = 0
for i in arr:
return max_sum
print(maxSubarraySum(arr))
3. The Time Machine – Rotate Array
Professor X has built a time machine that can shift timelines. Given a sequence of n events in history, he
wants to shift the timeline to the right by k steps so that future generations experience events in a different
order. Help him rotate the array representing these events.
Problem Statement:
Given an array, rotate the array to the right by k steps, where k is non-negative.
Constraints:
● 1 ≤ n ≤ 105
● 0≤k≤n
Examples:
Solution:
for i in range(k):
arr.insert(0, arr.pop(0))
return arr
arr = [1, 2]
k=3
print(arrayRotate(arr, k))
Sherlock Holmes is solving a mysterious case where he has n clues, each clue has a specific numerical
value. He suspects that exactly two clues add up to a critical target value that will solve the case. Help
Sherlock find the indices of these two clues.
Problem Statement:
Given an array of integers nums and an integer target, return indices of the two numbers such that they add
up to target. Assume that each input would have exactly one solution.
Constraints:
● 2 ≤ n ≤ 105
Solution:
dic = {}
res = []
k = target + num
if k in dic.keys():
res.append(dic[k])
res.append(i)
break
dic[num] = i
return res
nums = [5, 5, 5]
target = 10
print(twoSum(nums, target))
Constraints:
● 1 ≤ n ≤ 105
● 1 ≤ array elements ≤ n
Examples:
Solution:
def dupli(arr):
temp = []
res = []
for i in arr:
res.append(i)
temp.append(i)
return res
arr = [1, 1, 1, 2, 2, 2]
print(dupli(arr))
Input:
arr1 = [1, 2, 3]
arr2 = [3, 2, 1]
Output: True
Solution:
def areEqual(arr1, arr2):
i=0
while i < len(arr1):
if arr1[i] in arr2:
arr2.remove(arr1[i])
arr1.pop(i)
else:
i += 1
return True if arr1 == arr2 == [] else False
arr1 = [1, 2, 3]
arr2 = [3, 2, 1]
print(areEqual(arr1, arr2))
7. WAP to Find the Leader Elements. (Leader Element: An element is a leader if it is greater than
all elements to its right.)
def leader(arr):
m = float("-inf")
elm = []
if arr[i] > m:
m = arr[i]
elm.insert(0, m)
return elm
print(leader(arr))
Input:
arr = [1, 2, 3, 4, 5]
k=2
Output: [3,4,5,1,2]
Solution:
def leftRotate(arr, k):
for i in range(k):
arr.append(arr.pop(0))
return arr
arr = [1, 2, 3, 4, 5]
k=2
print(leftRotate(arr, k))
9. Find Majority Element in given array. Find the element that appears more than n/2 times.
Solution:
def majorityElement(arr):
n = len(arr)
res = -1
for i in arr:
if arr.count(i) > n / 2:
res = i
return res
arr = [3, 3, 4, 2, 4, 4, 2, 4, 4]
print(majorityElement(arr))
10. Find Peak Element, A peak element is greater than its neighbors. Find one such element.
Input: [1, 3, 20, 4, 1, 0] Output: 20
Solution:
def peakElm(arr):
n = len(arr)
if n == 0:
return arr[0]
return arr[-1]
return arr[i]
print(peakElm(arr))
11. Sort an Array consisting of 0s, 1s, and 2s.
Input: [2, 0, 2, 1, 1, 0] Output: [0,0,1,1,2,2]
Solution:
def arrSort(arr):
i=0
for _ in range(len(arr)):
if arr[i] == 0:
arr.insert(0, arr.pop(i))
i += 1
elif arr[i] == 2:
arr.append(arr.pop(i))
else:
i += 1
return arr
arr = [2, 0, 2, 1, 1, 0]
print(arrSort(arr))