0% found this document useful (0 votes)
5 views

Array_0176CS221053

The document contains a series of programming problems related to array manipulation, each with a problem statement, constraints, examples, test cases, and solutions in Python. Key problems include finding a missing number, maximum subarray sum, rotating an array, solving the two-sum problem, identifying duplicates, checking array equality, finding leader elements, left rotation, majority element, peak element, and sorting an array of 0s, 1s, and 2s. Each problem is accompanied by a sample input and output to illustrate the expected results.

Uploaded by

aryanrajr1990
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Array_0176CS221053

The document contains a series of programming problems related to array manipulation, each with a problem statement, constraints, examples, test cases, and solutions in Python. Key problems include finding a missing number, maximum subarray sum, rotating an array, solving the two-sum problem, identifying duplicates, checking array equality, finding leader elements, left rotation, majority element, peak element, and sorting an array of 0s, 1s, and 2s. Each problem is accompanied by a sample input and output to illustrate the expected results.

Uploaded by

aryanrajr1990
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Array Practice questions

Submitted By:

Ayush Kumar Sinha

0176CS221053

1. The Treasure Hunt – Find the Missing Number

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:

● Input: arr = [3, 0, 1]


Output: 2
● Input: arr = [0, 1]
Output: 2
Test Cases:

● Input: arr = [9, 6, 4, 2, 3, 5, 7, 0, 1] → Output: 8

● Input: arr = [0] → Output: 1

● Input: arr = [1, 2, 3, 4, 5] → Output: 0

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

● -104 ≤ array elements ≤ 104


Examples:

● Input: arr = [-2,1,-3,4,-1,2,1,-5,4]


Output: 6 (subarray [4,-1,2,1])
● Input: arr = [1]
Output: 1
Test Cases:

● Input: arr = [5,4,-1,7,8] → Output: 23

● Input: arr = [-1, -2, -3, -4] → Output: -1

● Input: arr = [1, 2, 3, 4, 5] → Output: 15

Solution:

def maxSubarraySum(arr):

max_sum = float('inf')

c_sum = 0

for i in arr:

c_sum = max(i, c_sum + i)

max_sum = max(max_sum, c_sum)

return max_sum

arr = [5, 4, -1, 7, 8]

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

● -105 ≤ array elements ≤ 105

● 0≤k≤n
Examples:

● Input: arr = [1,2,3,4,5,6,7], k = 3


Output: [5,6,7,1,2,3,4]
● Input: arr = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Test Cases:

● Input: arr = [1, 2, 3, 4, 5], k = 2 → Output: [4, 5, 1, 2, 3]

● Input: arr = [10, 20, 30], k = 1 → Output: [30, 10, 20]

● Input: arr = [1, 2], k = 3 → Output: [2, 1]

Solution:

def arrayRotate(arr, k):

for i in range(k):

arr.insert(0, arr.pop(0))

return arr

arr = [1, 2]
k=3

print(arrayRotate(arr, k))

4. Sherlock’s Case – Two Sum Problem

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

● -109 ≤ array elements ≤ 109

● -109 ≤ target ≤ 109


Examples:

● Input: nums = [2,7,11,15], target = 9


Output: [0, 1]
● Input: nums = [3,2,4], target = 6
Output: [1, 2]
Test Cases:

● Input: nums = [1,2,3,4], target = 7 → Output: [2,3]

● Input: nums = [-1,-2,-3,-4,-5], target = -8 → Output: [2,4]

● Input: nums = [5,5,5], target = 10 → Output: [0,1]

Solution:

def twoSum(arr, target):

dic = {}

res = []

for i, num in enumerate(arr):

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))

5. The Enchanted Forest – Find All Duplicates


In the enchanted forest, magical creatures come and go. Each creature has an ID between 1 and n. However,
some creatures are mischievous and appear twice, while others appear only once. Help the forest ranger find
all the creatures that appear twice.
Problem Statement:
Given an integer array of size n where 1 ≤ array elements ≤ n, some elements
appear twice, and others appear once. Find all the elements that appear twice.

Constraints:

● 1 ≤ n ≤ 105

● 1 ≤ array elements ≤ n
Examples:

● Input: arr = [4,3,2,7,8,2,3,1]


Output: [2, 3]
● Input: arr = [1,1,2]
Output: [1]
Test Cases:

● Input: arr = [3,3,1,4,5,6,7,8,5] → Output: [3,5]

● Input: arr = [1,1,1,2,2,2] → Output: [1,2]

● Input: arr = [1,2,3,4] → Output: []

Solution:

def dupli(arr):
temp = []

res = []

for i in arr:

if i in temp and i not in res:

res.append(i)

temp.append(i)

return res

arr = [1, 1, 1, 2, 2, 2]

print(dupli(arr))

6. WAP Check if Two Arrays Are Equal

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.)

Input: [16, 17, 4, 3, 5, 2] Output: [17, 5, 2]


Solution:

def leader(arr):

m = float("-inf")

elm = []

for i in range(len(arr)-1, -1, -1):

if arr[i] > m:

m = arr[i]

elm.insert(0, m)

return elm

arr = [16, 17, 4, 3, 5, 2]

print(leader(arr))

8. Rotate Array to the Left by k Positions.

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.

Input: [3, 3, 4, 2, 4, 4, 2, 4, 4] Output: 4

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 “array is empty”

if n == 1 or arr[0] > arr[1]:

return arr[0]

if arr[-1] > arr[-2]:

return arr[-1]

for i in range(1, n - 1):

if arr[i] > arr[i – 1] and arr[i] > arr[i + 1]:

return arr[i]

return “no peak element”

arr = [1, 3, 20, 4, 1, 0]

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))

You might also like