0% found this document useful (0 votes)
57 views34 pages

Coding Session Question Bank-2

Uploaded by

a9650827710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views34 pages

Coding Session Question Bank-2

Uploaded by

a9650827710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Coding Session Question Bank

PseudoCode question answer

(1) input X = 3
input Y = 4
Z=0
for i = 1 to X do
for j = 1 to Y do
Z=Z+i+j
output Z

1. 50 2. 54 3. 48 4. 60

(2) input X = 3
input Y = 4
Z=0
for i = 1 to X do
for j = 1 to Y do
Z=Z+i-j
output Z

2. -6 2. 4 3. -4 4. 0

(3) input P = 4
input Q = 2
R=0
for i = 1 to P do
for j = 1 to Q do
R = R + (i * j)
output R
1. 20 2. 28 3. 24 4. 30
(4) input M = 5
input N = 3
S=1
for i = 1 to M do
for j = 1 to N do
S = S + (i * j)
output S

1. 46 2. 61 3. 81 4. 91

(5) input X = 4
input Y = 3
Z=1
for i = 1 to X do
for j = 1 to Y do
Z = Z * (i + j)
output Z

1. 1200 2. 2160 3. 1728 4. None of these

(6) input N = 3
input M = 5
S=0
for i = 1 to N do
for j = 1 to M do
S=S-i-j
output S

1. -50 2. -60 3. -75 4. -81

(7) input X = 2
input Y = 4
Z=0
for i = 1 to X do
for j = 1 to Y do
Z = Z + (i * j) + j
Output Z

1. 24 2. 32 3. 50 4. 55

(8) input A = 3
input B = 3
C=0
for i = 1 to A do
for j = 1 to B do
C = C + (i + j)^2
output C

1. 99 2. 150 3. 156 4. None of these

(9) input N = 4
input M = 2
R=0
for i = 1 to N do
for j = 1 to M do
R = R + (i - j)^2
output R

1. 14 2. 16 3. 18 4. 20

(10) input X = 4
input Y = 3
Z=1
for i = 1 to X do
for j = 1 to Y do
Z = Z * (i + j)
output Z

1.120 2. 72 3. 60 4. 90

Time and Space Complexity

The time complexity of Binary Search in the worst case is:


1. O(n) 2. O(log n) 3. O(n^2) 4. O(1)

The time complexity of Bubble Sort in the average case is:


1. O(n^2) 2. O(n log n) 3. O(n) 4. O(1)

The time complexity of Insertion Sort in the worst case is:


1. O(n^2) 2. O(n) 3. O(log n) 4. O(n log n)

The time complexity of Merge Sort in the best case is:


1. O(n log n) 2. O(n) 3. O(log n) 4. O(n^2)

The time complexity of Quick Sort in the worst case is:


1. O(n^2) 2. O(n log n) 3. O(n) 4. O(log n)

The time complexity of accessing an element using index in an array is:


1. O(1) 2. O(n) 3. O(n^2) 4. O(n log n)

In a sorted array, to find the maximum element, the time complexity in the worst

case is O(____).

1. O(logn) 2. O(n) 3. O(nlogn). 4. O(1)

The Worst Case complexity of finding the maximum element in an unsorted array is:
1. O(n) 2. O(log n) 3. O(n^2) 4. O(1)
The time complexity of reversing a string is:
1. O(n) 2. O(log n) 3. O(n^2) 4. O(1)

The time complexity of finding the length of a string is:


1. O(1) 2. O(n) 3. O(log n) 4. O(n^2)

The time complexity of concatenating two strings is: if sl and s2 length is n and m
1. O(n + m) 2. O(n *m) 3. O(log nm) 4. O(1)

The time complexity of inserting an element at the beginning of an array is:


1. O(1) 2. O(n) 3. O(log n) 4. O(n^2)

The time complexity of reversing an array is:


1. O(n) 2. O(log n) 3. O(n^2) 4. O(1)

The Worst Case time complexity of finding the maximum element in an array is:
1. O(n) 2. O(log n) 3. O(n^2) 4. O(1)

The best-case time complexity of Merge Sort is O(____).

1. O(logn) 2. O(n) 3. O(nlogn). 4. O(n*n)

Array and String Programs

Consider the following array: arr = [3, 6, 9, 12, 15].


What is the output of arr[4] if the index starts from 0?

1. 6 2. 9 3. 15 4. Error

Consider the following string: str = "hello world".


What is the character at index 6?

1. h 2. O 3. W 4. Error

Consider the following array: arr = [1, 2, 3, 4, 5].


What is the output of arr[2] if the index starts from 0?

1. 1 2. 2 3. 3 4. Error
Consider the following string: str = "programming".
What is the character at index 4?

1. P 2. R 3. A 4. M

Consider the following string: str = "computer".


What is the character at index 3?

1. C 2. O 3. M 4. p

Consider the following string: str = "algorithm".


What will be the output of str.substring(2, 5)?

1. "lgo" 2. "gor" 3. "gori" 4. "ri"

Consider the following array: arr = [10, 20, 30, 40].


What will be the output of arr.length?

1. 3 2. 4 3. 5 4. 2

Consider the following array: arr = [10, 20, 30, 40, 50].
What will be the output of arr[2]?

1. 30 2. 40 3. 20 4. 50

Consider the following string: str = "hello".


What is the character at index 0?

1. h 2. e 3. l 4. o

Consider the following array: arr = [1, 3, 2, 4, 5].


What is the output of arr[3]?

1. 1 2. 4 3. 3 4. 5

Consider the following array: arr = [7, 8, 9, 10, 11].


What is the output of arr[4] if the index starts from 0?

1. 7 2. 10 3. 11 4. 9

Consider the following string: str = "datastructure".


What is the character at index 4?

1. s 2. t 3. a 4. e
Consider the following string: str = "openai".
What will be the output of str.charAt(3)?

1. o 2. e 3. n 4. a

Given the string str = "data science", what character will be on the 7th index?

1. a 2. s 3. e 4. i

Consider the following string: str = "machinelearning".


What is the substring of str from index 3 to 6?

1. "mac" 2. "hin" 3. "Ine" 4. "chi"

Array and String - Find the Output:


Given the string str = "data science", what character will be on the 7th index?

1. a 2. s 3. e 4. i

Consider the following array: arr = [1, 4, 2, 5, 3]

what will be the output of arr[5].


1. 3 2. error 3. IndexOutOfBoundError 4. compile error

Find output

1.
int[] arr = {10, -22, 35, -40, 20};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
result += arr[i];
} else {
result -= arr[i];
}
}
print(result);

a) 87 b) 107 c) 67 d) 97
2.
int[] arr = {5, 10, 15, 20, 25};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
result += arr[i];
} else {
result -= arr[i];
}
}
print(result);

a) 20 b) 10 c) 15 d) 25

3.
int[] arr = {2, 3, 4, 5, 6};
int result = 1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
result *= arr[i];
} else {
result += arr[i];
}
}
print(result);

a) 270 b) 300 c) 180 d) 240

4.
int[] arr = {10, 20, 30, 40, 50};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (i == 2 || i == 3 || i == 5) {
result += arr[i];
}
}
print(result);
a) 70 b) 50 c) 60 d) 40

5.

int[] arr = {10, 22, 5, 40, 18};


int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 15) {
count++;
}
}
print(count);

a) 3 b) 4 c) 2 d) 5

6.
int[] arr = {9, 15, 22, 18, 7};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 3 == 0) {
result += arr[i];
}
}
print(result);
a) 42 b) 50 c) 24 d) 60

7.
int[] arr = {10, 22, 35, 40, 20};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (i % 10 == 0) {
result += arr[i];
} else {
result -= arr[i];
}
}
print(result);
1. 13 2. 23 3. 14 4. 18

8.
int[] arr = {8, 3, 5, 7, 10};
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0 && arr[i] > max) {
max = arr[i];
}
}
print(max);
a) 8 b) 5 c) 10 d) 7

9.
int[] arr = {30, 15, 40, 10, 20};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 25) {
result -= arr[i];
}
}
print(result);
a) -70 b) -60 c) -40 d) -50

10.
int[] arr = {2, 3, 4};
int result = 0;
for (int i = 0; i < arr.length; i++) {
result += arr[i] * arr[i];
}
print(result);
a) 29 b) 25 c) 35 d) 30

11.
int[] arr = {8, 12, 5, 9, 16};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 4 != 0) {
result += arr[i];
}
}
print(result);
a) 14 b) 26 c) 21 d) 23

12.
int[] arr = {2, 0, 5, 3, 0};
int result = 1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
result *= arr[i];
}
}
System.out.println(result);
a) 20 b) 30 c) 10 d) 25

13.
int[] arr = {11, 24, 35, 48, 59};
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
count++;
}
}
print(count);
a) 3 b) 2 c) 5 d) 4

14.
int[] arr = {10, 20, 30, 40, 50, 60};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (i % 3 == 0) {
result += arr[i];
}
}
print(result);
a) 90 b) 70 c) 60 d) 50

15.
int[] arr = {10, 20, 30, 40, 50};
int result = 0;
int lastElement = arr[arr.length - 1];
for (int i = 0; i < arr.length; i++) {
result += arr[i] - lastElement;
}
System.out.println(result);
a) -100 b) -60 c) -50 d) -40

16.
int[] arr = {8, 3, 2, 5};
double result = 1.0;
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
result *= arr[i];
}
}
System.out.println(result);
a) 16.0 b) 24.0 c) 8.0 d) 12.0

Array and String basic programs :

1. Write a program to find the minimum and maximum element from an array.
Input: arr = [15, 3, 1, 6, 5, 8]
Output: 1 and 15
2. Write a program to find the sum of all even numbers from an array.
Input: arr = [15, 31, 1, 6, 4, 30]
Output: 40
3. Write the intuition and code of the Bubble Sort algorithm.
4. Write the intuition and code of the Selection Sort algorithm.
5. Write the code of the Binary Search algorithm and explain its time complexity.
6. Write a program to find the duplicate elements from an array.
Input: arr = [5, 3, 1, 6, 5, 8]
Output: 5
7. Write a program to find the count of vowels and consonants in a given string.
Input: String = "krmangalamuniversity"
Output: Vowels: 7, Consonants: 13
8. Write a program to reverse a given string.
Input: String = "KRMU"
Output: "UMRK"
9. Write a program to find the count of odd numbers in a given array.
Input: arr = [1, 3, 5, 7, 8, 10]
Output: 4
10. Write a program to remove all duplicate characters from a string.
Input: String = "programming"
Output: "progamin"
11.Write a program to check if a given string is a palindrome.
Input: String = "madam"
Output: True
12. Write a program to find the common elements in two arrays.
Input: arr1 = [1, 2, 3, 4], arr2 = [3, 4, 5, 6]
Output: [3, 4]
13. Find the Missing Number in an Array
Problem Statement:

You are given an array of integers, arr, that contains n-1 distinct numbers

ranging from 1 to n. The numbers in the array are unique and lie within the range

[1, n], but one nu

Input: arr = [1, 2, 4, 5], n = 5


Output: 3

14. Write a program to implement Insertion Sort on an array.


Input: arr = [12, 11, 13, 5, 6]
Output: [5, 6, 11, 12, 13]
15. Write a program to find the second largest element in an array.
Input: arr = [10, 20, 4, 45, 99]
Output: 45
16. Write a program to find the product of all elements in an array.
Input: arr = [2, 3, 4]
Output: 24
17. Write a program to reverse an array without using any in-built functions.
Input: arr = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
18. Write a program to merge two sorted arrays into one sorted array.
Input: arr1 = [1, 3, 5], arr2 = [2, 4, 6]
Output: [1, 2, 3, 4, 5, 6]
19. Write a program to check if a number is present in a given array.
Input: arr = [1, 2, 3, 4, 5], target = 3
Output: True
20. Write a program to count the frequency of each element in an array.
Input: arr = [1, 2, 2, 3, 3, 3]
Output: {1: 1, 2: 2, 3: 3}
21. Write a program to print the Fibonacci sequence up to a given number of
terms.
Input: 5
Output: 0, 1, 1, 2, 3
22. Write a program to rotate an array to the right by k positions.
Input: arr = [1, 2, 3, 4, 5], positions = 2
Output: [4, 5, 1, 2, 3]
23. Write a program to find the sum of digits in a number.
Input: 1234
Output: 10
24. Write a program to check if a string contains all unique characters.
Input: String = "abcdefg"
Output: True
25. Write a program to find the GCD of two numbers.
Input: 12, 15
Output: 3
26. Write a program to reverse a number.
Input: 12345
Output: 54321
27. Write a program to find the largest palindrome in a string.
Input: String = "babad"
Output: "bab"
28. Write a program to check if a number is a perfect square.
Input: 16
Output: True
29. Write a program to find the longest word in a string.
Input: String = "Find the longest word in this string"
Output: "longest"
30. Write a program to find the largest sum of a subarray.
Input: nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output: 6
Explanation: The subarray [4, -1, 2, 1] has the largest sum.
31. Write a program to check if a number represented as a string is divisible by 7.
Input: num = "8955795758"
Output: 1
Explanation: 8955795758 is divisible by 7.

Pattern Printing

Print a pyramid pattern of stars for a given number of rows.


Example for n = 5:

Print a diamond pattern for a given number of rows.


Example for n = 5:
Print a Pascal's Triangle for n rows.
Example for n = 5:
markdown

Loops

6. Write a program to print the prime numbers between 1 and n using loops.
Input: n = 50
Output: [2, 3, 5, 7, 11, 13, ...]
7. Write a program to find the factorial of a number using loops.
Input: n = 5
Output: 120
8. Write a program to generate the first n terms of the look-and-say sequence.
Input: n = 5
Output: 1, 11, 21, 1211, 111221
9. Write a program to calculate the sum of the series:
S=1+1/2+1/3+...+1/nS=1+1/2+1/3+...+1/n
Input: n = 5
Output: 2.28333
10. Write a program to find the LCM of two numbers using loops.
Input: a = 12, b = 18
Output: 36

Conditionals

11.Write a program to determine if a given year is a leap year.


Input: year = 2024
Output: True
12. Write a program to calculate the roots of a quadratic equation using the
discriminant.
Input: a = 1, b = -3, c = 2
Output: Roots: 2.0, 1.0
13. Write a program to classify a triangle as scalene, isosceles, or equilateral based
on its sides.
Input: a = 5, b = 5, c = 8
Output: Isosceles
14. Write a program to implement a simple calculator that can handle addition,
subtraction, multiplication, and division based on user input.
Input: a = 15, b = 3, operation = '/'
Output: 5
15. Write a program to check if a number is an Armstrong number.
Input: n = 153
Output: True

Binary Search

16. Write a program to find the square root of a number using binary search.
Input: n = 16
Output: 4
17. Write a program to find the peak element in an array (an element greater than
its neighbors) using binary search.
Input: arr = [1, 3, 20, 4, 1, 0]
Output: 20
18. Write a program to search for the first occurrence of a target element in a
sorted array using binary search.
Input: arr = [2, 4, 10, 10, 10, 18], target = 10
Output: 2
19. Write a program to find the smallest element in a rotated sorted array using
binary search.
Input: arr = [7, 9, 1, 3, 5]
Output: 1
20. Write a program to count the occurrences of a target element in a sorted array
using binary search.
Input: arr = [1, 2, 2, 2, 3, 4], target = 2
Output: 3

Strings Moderate to hard level

21. Write a program to find the longest palindromic substring in a given string.
Input: s = "babad"
Output: "bab"
22. Write a program to check if a string is a valid anagram of another string,
ignoring spaces and case.
Input: s1 = "Listen", s2 = "Silent"
Output: True
23. Write a program to find the first non-repeating character in a string.
Input: s = "swiss"
Output: "w"
24. Write a program to compress a string using the counts of repeated characters.
Input: s = "aaabbcc"
Output: "a3b2c2"
25. Write a program to find the longest substring without repeating characters.
Input: s = "abcabcbb"
Output: "abc"
26. Write a program to determine if one string is a rotation of another string.
Input: s1 = "waterbottle", s2 = "erbottlewat"
Output: True
27. Write a program to convert a given string to a zigzag pattern and return the
string in row-wise order.
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
28. Write a program to check if two strings are permutations of each other.
Input: s1 = "test", s2 = "tset"
Output: True
29. Write a program to group anagrams from a list of strings.
Input: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
Output: [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
30. Write a program to calculate the edit distance (Levenshtein distance) between
two strings.
Input: s1 = "kitten", s2 = "sitting"
Output: 3

1. Subarray with Given XOR

Problem: Given an array of integers and a target value k, find the number of subarrays

whose XOR equals k.


Input:
arr = [4, 2, 2, 6, 4], k = 6
Output:
4

Explanation: Subarrays are [4, 2], [2, 6], [6], and [4, 2, 2, 6].

2. Rotate Array by K Steps

Problem: Rotate the elements of an array to the right by k steps in-place.

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

3. Maximum Product Subarray

Problem: Find the maximum product that can be obtained from any contiguous subarray

of a given array.
Input:
arr = [2, 3, -2, 4, -1]
Output:
48

Explanation: Subarray [2, 3, -2, 4] gives the maximum product.

4. Longest Subarray with Sum Divisible by K

Problem: Given an array of integers and an integer k, find the length of the longest

subarray whose sum is divisible by k.


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

Explanation: The subarray [7, 6, 1, 4] has a sum divisible by 3.

5. Minimum Length of Subarray with Sum Greater Than K

Problem: Find the minimum length of a subarray with a sum greater than a given value k.

Input:
arr = [1, 10, 5, 2, 7], k = 9
Output:
1

Explanation: The subarray [10] has a sum greater than 9.

6. Count Inversions in an Array

Problem: Find the number of inversions in an array, where an inversion is a pair (i, j)

such that i < j and arr[i] > arr[j].


Input:
arr = [8, 4, 2, 1]
Output:
6

Explanation: The inversions are (8, 4), (8, 2), (8, 1), (4, 2), (4, 1), (2,

1).

7. Rearrange Array Alternately


Problem: Rearrange an array such that every second element is the largest remaining

element and every first element is the smallest remaining element.


Input:
arr = [1, 2, 3, 4, 5, 6]
Output:
[6, 1, 5, 2, 4, 3]

8. Maximum Length Bitonic Subarray

Problem: Find the length of the longest subarray that is first increasing and then

decreasing.
Input:
arr = [12, 4, 78, 90, 45, 23]
Output:
5

Explanation: The subarray [4, 78, 90, 45, 23] is the longest bitonic subarray.

9. Maximum Difference of Two Elements

Problem: Find the maximum difference between two elements in the array such that the

larger element appears after the smaller one.


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

Explanation: Maximum difference is 6 - 1.


10. Trapping Rainwater Problem

Problem: Given an array of non-negative integers representing an elevation map, compute

the amount of water it can trap after raining.


Input:
arr = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
Output:
6

Explanation: The elevation map traps 6 units of water.

11. Sort Array of 0s, 1s, and 2s

Problem: Sort an array containing only 0s, 1s, and 2s in linear time without using any

sorting algorithm.
Input:
arr = [0, 1, 2, 1, 2, 0, 0, 1]
Output:
[0, 0, 0, 1, 1, 1, 2, 2]

12. Maximum Subarray Sum After At Most One Modification

Problem: Find the maximum sum of a subarray after modifying at most one element of the

array (i.e., replace one element with any other value).


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

Explanation: Modify -2 to 1, and the maximum sum subarray is [1, 1, 0, 3].


13. Find First Missing Positive Integer

Problem: Given an unsorted array, find the smallest missing positive integer.

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

Explanation: The smallest missing positive integer is 2.

14. Subarray with Maximum Average

Problem: Find the contiguous subarray of size k that has the maximum average.

Input:
arr = [1, 12, -5, -6, 50, 3], k = 4
Output:
12.75

Explanation: The subarray [12, -5, -6, 50] has the maximum average.

15. Rearrange Array to Maximize Sum of Adjacent Differences

Problem: Rearrange an array such that the sum of absolute differences between adjacent

elements is maximized.
Input:
arr = [1, 2, 4, 8]
Output:
[8, 1, 4, 2]
16. Merge Intervals

Problem: Given a list of intervals, merge all overlapping intervals.

Input:
intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]
Output:
[[1, 6], [8, 10], [15, 18]]

17. Minimum Number of Platforms

Problem: Find the minimum number of platforms needed for a railway station so no train

waits.
Input:
arrivals = [900, 940, 950, 1100, 1500, 1800]

departures = [910, 1200, 1120, 1130, 1900, 2000]


Output:
3

18. Array Pair Sum Divisible by K

Problem: Check if the array can be divided into pairs such that the sum of every pair is

divisible by k.
Input:
arr = [9, 7, 5, 3], k = 6
Output:
True
Advance level array problems

1. Split Array into Two Equal Sums

Problem: Given an array, determine if it can be split into two subarrays such that the sum
of both subarrays is equal. If possible, return the index where the split occurs; otherwise,
return -1.
Input:
arr = [1, 2, 3, 4, 6]
Output:
3

Explanation: Splitting the array at index 3 gives [1, 2, 3] and [4, 6], both of

which sum to 6.

2. Maximum Difference Between Increasing Elements


Problem: Find the maximum difference between two elements in the array such that the
larger element comes after the smaller element, and return -1 if no such elements exist.
Input:
arr = [7, 1, 5, 4]
Output:
4

Explanation: The maximum difference is 5 - 1 = 4.

3. Find All Subarrays with Zero Sum

Problem: Find all the subarrays in a given array that have a sum of 0.
Input:
arr = [3, 4, -7, 1, 2, -1, 6]
Output:
[[3, 4, -7], [4, -7, 1, 2], [-7, 1, 2, -1]]

4. Maximize Sum of i*arr[i] with Rotations

Problem: Given an array, rotate it to maximize the sum of i * arr[i] for all valid i.
Input:
arr = [1, 2, 3, 4]
Output:
20

Explanation: Rotating to [4, 1, 2, 3] gives the maximum sum of 20.

5. Find the Element that Appears Once


Problem: Every element in the array appears twice except for one. Find the element that
appears only once in linear time using constant space.
Input:
arr = [2, 3, 5, 4, 5, 3, 4]
Output:
2

6. Longest Subsequence with Consecutive Integers

Problem: Find the length of the longest subsequence in an array such that the elements
are consecutive integers. The sequence can be unordered.
Input:
arr = [1, 9, 3, 10, 4, 20, 2]
Output:
4

Explanation: The subsequence [1, 2, 3, 4] is the longest.

7. Minimum Swaps to Sort an Array

Problem: Determine the minimum number of swaps required to sort an array.


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

Explanation: Swap 4 and 1, then 3 and 2.


8. Find the Majority Element

Problem: Find the element that appears more than n/2 times in an array, where n is the
size of the array. Assume that such an element always exists.
Input:
arr = [3, 3, 4, 2, 3, 3, 1]
Output:
3

9. Subarray with Product Less Than K

Problem: Given an array of positive integers, count the number of contiguous subarrays
where the product of all elements is less than k.
Input:
arr = [10, 5, 2, 6], k = 100
Output:
8

10. Smallest Subarray with All Occurrences of the Most Frequent Element

Problem: Find the smallest subarray that contains all occurrences of the most frequent
element in the array.
Input:
arr = [1, 2, 2, 3, 1, 4, 2]
Output:
[2, 2, 3, 1, 4, 2]
11. Array Transformation

Problem: Transform an array such that arr[i] becomes the difference between the
sum of the elements on the left and the sum of the elements on the right.
Input:
arr = [4, 3, 7, 1]
Output:
[-11, -8, 0, 14]

12. Count Pairs with Given XOR

Problem: Count the number of pairs in an array whose XOR equals a given number k.
Input:
arr = [5, 4, 10, 15, 7, 6], k = 5
Output:
2

13. Find Maximum Distance Between Equal Elements

Problem: Find the maximum distance between two occurrences of the same element in
the array.
Input:
arr = [3, 5, 4, 3, 5, 3, 4]
Output:
6
14. Maximum Absolute Difference Between Left and Right Sums

Problem: Split an array into two parts and return the maximum absolute difference
between the sum of the left part and the sum of the right part.
Input:
arr = [2, 4, -3, 6, 7, 1]
Output:
14

15. Reorder Array to Maximize Lexicographical Order

Problem: Rearrange the array so that the resulting concatenated string of all elements is
lexicographically largest.
Input:
arr = [54, 546, 548, 60]
Output:
[60, 548, 546, 54]

You might also like