Topic : Arrays
Level : Medium
Date : 03.03.2026
Solve the following problems
[Link]. Question Detail
Find the Difference of Two Arrays
Given two 0-indexed integer arrays nums1 and nums2, return a list answer of
size 2 where:
• answer[0] is a list of all distinct integers in nums1 which are not present
in nums2.
• answer[1] is a list of all distinct integers in nums2 which are not present
in nums1.
Note that the integers in the lists may be returned in any order.
Example 1:
Input: nums1 = [1,2,3], nums2 = [2,4,6]
Output: [[1,3],[4,6]]
Explanation:
For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and
1 nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and
nums2[2] = 6 are not present in nums1. Therefore, answer[1] = [4,6].
Example 2:
Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2]
Output: [[3],[]]
Explanation:
For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] ==
nums1[3], their value is only included once and answer[0] = [3].
Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
Constraints:
• 1 <= [Link], [Link] <= 1000
• -1000 <= nums1[i], nums2[i] <= 1000
2 Common Elements
It is going to be hard but, hard does not mean impossible.
1
Problem Statement : Given three sorted arrays in non-decreasing order, print all
common elements in non-decreasing order across these arrays. If there are no such
elements return an empty array. In this case, the output will be -1.
Note: can you handle the duplicates without using any additional Data Structure?
Examples :
Input: arr1 = [1, 5, 10, 20, 40, 80] , arr2 = [6, 7, 20, 80, 100] , arr3 = [3, 4, 15, 20,
30, 70, 80, 120]
Output: [20, 80]
Explanation: 20 and 80 are the only common elements in arr1, arr2 and arr3.
Input: arr1 = [1, 2, 3, 4, 5] , arr2 = [6, 7] , arr3 = [8,9,10]
Output: [-1]
Explanation: There are no common elements in arr1, arr2 and arr3.
Input: arr1 = [1, 1, 1, 2, 2, 2], arr2 = [1, 1, 2, 2, 2], arr3 = [1, 1, 1, 1, 2, 2, 2, 2]
Output: [1, 2]
Explanation: We do not need to consider duplicates
Constraints:
1 <= n1, n2, n3 <= 10^5
The array elements can be both positive or negative integers
Array Subset of another array
Problem Statement : Given two arrays: a1[0..n-1] of size n and a2[0..m-1] of
size m, where both arrays may contain duplicate elements. The task is to
determine whether array a2 is a subset of array a1. It's important to note that both
arrays can be sorted or unsorted. Additionally, each occurrence of a duplicate
element within an array is considered as a separate element of the set.
3 Example 1:
Input:
a1[] = {11, 7, 1, 13, 21, 3, 7, 3}
a2[] = {11, 3, 7, 1, 7}
Output:
Yes
Explanation:
a2[] is a subset of a1[]
It is going to be hard but, hard does not mean impossible.
2
Example 2:
Input:
a1[] = {1, 2, 3, 4, 4, 5, 6}
a2[] = {1, 2, 4}
Output:
Yes
Explanation:
a2[] is a subset of a1[]
Example 3:
Input:
a1[] = {10, 5, 2, 23, 19}
a2[] = {19, 5, 3}
Output:
No
Explanation:
a2[] is not a subset of a1[]
Constraints:
1 <= n,m <= 10^5
1 <= a1[i], a2[j] <= 10^6
Rearrange Array Alternately
Problem Statement:
Write a program to rearrange a sorted array in an alternate maximum and minimum
form.
• The first element should be the maximum,
• the second should be the minimum,
4 • the third should be the second maximum,
• the fourth should be the second minimum, and so on.
Constraints
• 1 ≤ N ≤ 50
• -100 ≤ array element ≤ 100
Test Cases:
It is going to be hard but, hard does not mean impossible.
3
Input 1:
Enter the number of elements: 6
Enter array elements: 1 2 3 4 5 6
Output 1:
Rearranged array: 6 1 5 2 4 3
Explanation:
Max = 6, Min = 1, 2nd Max = 5, 2nd Min = 2, 3rd Max = 4, 3rd Min = 3.
Input 2:
Enter the number of elements: 5
Enter array elements: -3 -1 2 4 7
Output 2:
Rearranged array: 7 -3 4 -1 2
Explanation:
Max = 7, Min = -3, 2nd Max = 4, 2nd Min = -1, Middle = 2.
Input 3:
Enter the number of elements: 0
Output 3:
Invalid Input: The number of elements must be at least 1.
Input 4:
Enter the number of elements: 4
Enter array elements: 10 20 150 30
Output 4:
Invalid Input: Array elements must be between -100 and 100.
Shuffle the Array
Problem Statement : Given the array nums consisting of 2n elements in the
form [x1,x2,...,xn,y1,y2,...,yn].
Return the array in the form [x1,y1,x2,y2,...,xn,yn]
5 Example 1:
Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7]
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is
[2,3,5,4,1,7].
It is going to be hard but, hard does not mean impossible.
4
Example 2:
Input: nums = [1,2,3,4,4,3,2,1], n = 4
Output: [1,4,2,3,3,2,4,1]
Example 3:
Input: nums = [1,1,2,2], n = 2
Output: [1,2,1,2]
Constraints:
• 1 <= n <= 500
• [Link] == 2n
• 1 <= nums[i] <= 10^3
Peak Element
Problem Statement:
You are given an array arr of length n. Write a program using user-defined functions
to find the index (0-based) of any peak element in the array.
A peak element is defined as an element that is greater than both of its neighbors. For
element arr[i], it is a peak if:
arr[i - 1] < arr[i] > arr[i + 1]
Assume arr[-1] and arr[n] are negative infinity.
Constraints:
• 1 ≤ N ≤ 10⁵
• 1 ≤ arr[i] ≤ 10⁵
• arr[i] != arr[i+1] for all valid i
6
Test Cases:
Input 1:
Enter the number of elements: 5
Enter array elements: 1 8 1 5 3
Output 1:
1
Explanation:
Peak elements are 8 and 5. Any of their indices (1 or 3) is acceptable.
Input 2:
Enter the number of elements: 3
Enter array elements: 1 2 1
It is going to be hard but, hard does not mean impossible.
5
Output 2:
1
Explanation:
2 is the peak element at index 1.
Input 3:
Enter the number of elements: 0
Output 3:
Invalid Input: The number of elements must be at least 1.
Input 4:
Enter the number of elements: 4
Enter array elements: 1 2 2 3
Output 4:
Invalid Input: Array elements must be positive and no two adjacent elements can be
equal.
Leader Elements in an Array
Problem Statement:
Write a program that reads an array of integers from the user and prints all the leader
elements in the array.
Note: An element is a leader if it is greater than all the elements to its right in the array.
Constraints:
• 1 ≤ N ≤ 50
• -100 ≤ array element ≤ 100
Test Cases:
7 Input 1:
Enter number of elements: 6
Enter array elements: 16 17 4 3 5 2
Output 1:
Leader elements: 17 5 2
Explanation:
• 17 is greater than all elements to its right.
• 5 is greater than 2.
• 2 is the rightmost element, so it is also a leader.
Input 2:
It is going to be hard but, hard does not mean impossible.
6
Enter number of elements: 5
Enter array elements: 5 4 3 2 1
Output 2:
Leader elements: 5 4 3 2 1
Explanation:
• The array is strictly decreasing, so all elements are leaders.
Input 3:
Enter number of elements: 0
Output 3:
Invalid input: Array size must be at least 1
Input 4:
Enter number of elements: 4
Enter array elements: 10 20 150 30
Output 4:
Leader elements: 150 30
Find Target Indices After Sorting Array
Problem Statement: You are given a 0-indexed integer array nums and a target
element target.
A target index is an index i such that nums[i] == target.
Return a list of the target indices of nums after sorting nums in non-decreasing order.
If there are no target indices, return an empty list. The returned list must be sorted in
increasing order.
Example 1:
Input: nums = [1,2,5,2,3], target = 2
8
Output: [1,2]
Explanation: After sorting, nums is [1,2,2,3,5].
The indices where nums[i] == 2 are 1 and 2.
Example 2:
Input: nums = [1,2,5,2,3], target = 3
Output: [3]
Explanation: After sorting, nums is [1,2,2,3,5].
The index where nums[i] == 3 is 3.
It is going to be hard but, hard does not mean impossible.
7
Example 3:
Input: nums = [1,2,5,2,3], target = 5
Output: [4]
Explanation: After sorting, nums is [1,2,2,3,5].
The index where nums[i] == 5 is 4.
Constraints:
1 <= [Link] <= 100
1 <= nums[i], target <= 100
Sort Even and Odd Indices Independently
Problem Statement: You are given a 0-indexed integer array nums. Rearrange the
values of nums according to the following rules:
• Sort the values at odd indices of nums in non-increasing order.
• For example, if nums = [4,1,2,3] before this step, it becomes
[4,3,2,1] after. The values at odd indices 1 and 3 are sorted in
non-increasing order.
• Sort the values at even indices of nums in non-decreasing order.
• For example, if nums = [4,1,2,3] before this step, it becomes
[2,1,4,3] after. The values at even indices 0 and 2 are sorted in
non-decreasing order.
Return the array formed after rearranging the values of nums.
9 Example 1:
Input: nums = [4,1,2,3]
Output: [2,3,4,1]
Explanation:
First, we sort the values present at odd indices (1 and 3) in non-increasing order.
So, nums changes from [4,1,2,3] to [4,3,2,1].
Next, we sort the values present at even indices (0 and 2) in non-decreasing order.
So, nums changes from [4,1,2,3] to [2,3,4,1].
Thus, the array formed after rearranging the values is [2,3,4,1].
Example 2:
Input: nums = [2,1]
Output: [2,1]
Explanation:
It is going to be hard but, hard does not mean impossible.
8
Since there is exactly one odd index and one even index, no rearrangement of values
takes place.
The resultant array formed is [2,1], which is the same as the initial array.
Constraints:
1 <= [Link] <= 100
1 <= nums[i] <= 100
Sort Elements By Frequency
Problem Statement:
You are given an array of integers that may contain repeated elements. Your task is to
sort the array in decreasing order of the frequency of repetition of elements.
• The element with the highest frequency should appear first.
• If two elements have the same frequency, the element that appears earlier in
the original array should come first.
Constraints
• 1 ≤ T ≤ 10
• 1 ≤ N ≤ 3000
• 1 ≤ arr[i] ≤ 10⁶
Test Cases:
Input 1:
Enter the number of test cases: 1
10
Enter the number of elements: 8
Enter array elements: 2 5 2 8 5 6 8 8
Output 1:
88822556
Explanation:
• Frequencies: 8 → 3, 2 → 2, 5 → 2, 6 → 1
• Order: 8 first, then 2 (appears before 5 in original), then 5, then 6.
Input 2:
Enter the number of test cases: 1
Enter the number of elements: 6
Enter array elements: 4 4 6 6 7 7
Output 2:
446677
Explanation:
It is going to be hard but, hard does not mean impossible.
9
• Frequencies: all have frequency = 2.
• Since order is preserved from input → result is same.
Input 3:
Enter the number of test cases: 1
Enter the number of elements: 0
Output 3:
Invalid Input: Number of elements must be at least 1
Input 4:
Enter the number of test cases: 1
Enter the number of elements: 4
Enter array elements: -1 2 3 4
Output 4:
Invalid Input: Elements must be positive integers
Sort an array of 0s, 1s and 2s
Problem Statement: Given an array of size N containing only 0s, 1s, and 2s; sort the
array in ascending order.
Example 1:
Input:
N=5
arr[]= {0 2 1 2 0}
Output:
00122
11 Explanation:
0s 1s and 2s are segregated into ascending order.
Example 2:
Input:
N=3
arr[] = {0 1 0}
Output:
001
Explanation:
0s 1s and 2s are segregated into ascending order.
It is going to be hard but, hard does not mean impossible.
10
Constraints:
1 <= N <= 10^6
0 <= A[i] <= 2
Kth Smallest Element
Problem Statement:
Find the Kth smallest element in an array using user-defined functions. Print the Kth
smallest element, or an error message if input is invalid.
Constraints
• 1 ≤ N ≤ 100
• 1≤K≤N
• -10^6 ≤ array elements ≤ 10^6
Test Cases:
Input 1:
Enter the number of elements: 5
Enter array elements: 7 2 9 4 1
Enter the value of K: 3
Output 1:
12
Kth smallest element is 4
Explanation: Sorted array: 1 2 4 7 9 → 3rd smallest element is 4
Input 2:
Enter the number of elements: 4
Enter array elements: 10 20 5 15
Enter the value of K: 2
Output 2:
Kth smallest element is 10
Explanation: Sorted array: 5 10 15 20 → 2nd smallest element is 10
Input 3:
Enter the number of elements: 3
Enter array elements: 1 2 3
Enter the value of K: 4
Output 3:
It is going to be hard but, hard does not mean impossible.
11
Invalid input for K
Input 4:
Enter the number of elements: 101
Output 4:
Number of elements out of constraints
Majority Element
Problem Statement:
Find the majority element (appears more than N/2 times) in an array of integers. If no
majority element exists, print “No majority element”.
Constraints
• 1 ≤ N ≤ 100
• -10⁶ ≤ array elements ≤ 10⁶
Test Cases:
Input 1:
Enter the number of elements: 5
Enter array elements: 2 2 1 2 3
Output 1:
Majority element is 2
Explanation: Element 2 appears 3 times out of 5 → more than 5/2 → majority
13 element
Input 2:
Enter the number of elements: 4
Enter array elements: 1 2 3 4
Output 2:
No majority element
Explanation: No element appears more than 4/2 = 2 times → no majority element
Input 3:
Enter the number of elements: 0
Output 3:
Number of elements out of constraints
Input 4:
Enter the number of elements: 101
It is going to be hard but, hard does not mean impossible.
12
Output 4:
Number of elements out of constraints.
Find Missing and Repeating
Problem Statement: Given an unsorted array Arr of size N of positive integers. One
number 'A' from set {1, 2,....,N} is missing and one number 'B' occurs twice in array.
Find these two numbers.
Example 1:
Input:
N=2
Arr[] = {2, 2}
Output: 2 1
Explanation: Repeating number is 2 and smallest positive missing number is 1.
14
Example 2:
Input:
N=3
Arr[] = {1, 3, 3}
Output: 3 2
Explanation: Repeating number is 3 and smallest positive missing number is 2.
Constraints:
2 ≤ N ≤ 10^5
1 ≤ Arr[i] ≤ N
Rotate an Array Clockwise
Problem Statement:
Write a program that reads an array of integers and rotates it clockwise by one
position.
For example, the last element moves to the first position, and all other elements shift
15 one place to the right.
The program should also validate the input for correct size and number of elements.
Constraints:
• 1 ≤ N ≤ 20
• All elements are integers.
It is going to be hard but, hard does not mean impossible.
13
Test Cases:
Input 1:
Enter number of elements in the array: 5
Enter array elements: 10 20 30 40 50
Output 1:
Array after rotation: 50 10 20 30 40
Explanation:
The last element 50 is moved to the front, and all others are shifted right by one
position.
Input 2:
Enter number of elements in the array: 4
Enter array elements: 1 2 3 4
Output 2:
Array after rotation: 4 1 2 3
Input 3:
Enter number of elements in the array: 3
Enter array elements: 1 2
Output 3:
Invalid input. Number of elements entered does not match N.
Explanation:
The number of elements provided (2) does not match the specified size (3).
Input 4:
Enter number of elements in the array: 4
Enter array elements: 10 20 30 40 50
Left Rotation
Problem Statement
A left rotation operation on an array shifts each of the array’s elements unit to the left.
For example, if 2 left rotations are performed on array [1, 2, 3, 4, 5], then the array
would become [3, 4, 5, 1, 2].
16
Given an array of integers and a number, , perform left rotations on the array. Return
the updated array to be printed as a single line of space-separated integers.
Function Description
Complete the function rotLeft in the editor below. It should return the resulting array
of integers.
It is going to be hard but, hard does not mean impossible.
14
rotLeft has the following parameter(s):
• An array of integers .
• An integer , the number of rotations.
Input Format
The first line contains two space-separated integers and , the size of and the number
of left rotations you must perform.
The second line contains space-separated integers a[i].
Constraints
• 1 <= n <= 10^5
• 1 <= d <= n
• 1 <= a[i] <= 10^8
Output Format
Print a single line of space-separated integers denoting the final state of the array
after performing d left rotations.
Sample Input
54
12345
Sample Output
51234
Explanation
When we perform d=4 left rotations, the array undergoes the following sequence of
changes:
[1,2,3,4,5] → [2,3,4,5,1] → [3,4,5,1,2] → [4,5,1,2,3] → [5,1,2,3,4]
Test Case : 1
Input (stdin)
• 54
• 12345
Expected Output
• 51234
Test Case : 2
Input (stdin)
• 20 10
• 41 73 89 7 10 1 59 58 84 77 77 97 58 1 86 58 26 10 86 51
Expected Output
• 77 97 58 1 86 58 26 10 86 51 41 73 89 7 10 1 59 58 84 77
It is going to be hard but, hard does not mean impossible.
15
Max Consecutive Ones
Problem statement : Given a binary array nums, return the maximum number of
consecutive 1's in the array.
Example 1:
Input: nums = [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s. The
17 maximum number of consecutive 1s is 3.
Example 2:
Input: nums = [1,0,1,1,0,1]
Output: 2
Constraints:
1 <= [Link] <= 105
nums[i] is either 0 or 1.
Array Subarry
Problem Statement :
You are given an array, You have to choose a contiguous subarray of length ‘k’, and
find the minimum of that segment, return the maximum of those minimums.
Sample input 0 :
1 → Length of segment x =1
5 → size of space n = 5
1 → space = [ 1,2,3,1,2]
2
18
3
1
2
Sample output :
3
Explanation :
The subarrays of size x = 1 are [1],[2],[3],[1], and [2],Because each subarray only
contains 1 element, each value is minimal with respect to the subarray it is in. The
maximum of these values is 3. Therefore, the answer is 3
19 Maximum Product Subarray
It is going to be hard but, hard does not mean impossible.
16
Problem Statement: Given an array Arr[] that contains N integers (may be positive,
negative or zero). Find the product of the maximum product subarray.
Example 1:
Input:
N=5
Arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: Subarray with maximum product is [6, -3, -10] which gives product as
180.
Example 2:
Input:
N=6
Arr[] = {2, 3, 4, 5, -1, 0}
Output: 120
Explanation: Subarray with maximum product is [2, 3, 4, 5] which gives product as
120.
Constraints:
1 ≤ N ≤ 500
-10^2 ≤ Arri ≤ 10^2
Spiral Matrix
Given an m x n matrix, return all elements of the matrix in spiral order.
Example 1:
20
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
It is going to be hard but, hard does not mean impossible.
17
Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
Constraints:
• m == [Link]
• n == matrix[i].length
• 1 <= m, n <= 10
• -100 <= matrix[i][j] <= 100
It is going to be hard but, hard does not mean impossible.
18