1.
Container With Most Water
Difficulty: MediumAccuracy: 53.84%Submissions: 84K+Points: 4Average Time: 30m
Given an array arr[] of non-negative integers, where each element arr[i] represents the height of
the vertical lines, find the maximum amount of water that can be contained between any two lines,
together with the x-axis.
Note: In the case of a single vertical line it will not be able to hold water.
Examples:
Input: arr[] = [1, 5, 4, 3]
Output: 6
Explanation: 5 and 3 are 2 distance apart. So the size of the base is 2. Height of container = min(5, 3) = 3.
So, total area to hold water = 3 * 2 = 6.
Input: arr[] = [3, 1, 2, 4, 5]
Output: 12
Explanation: 5 and 3 are 4 distance apart. So the size of the base is 4. Height of container = min(5, 3) = 3.
So, total area to hold water = 4 * 3 = 12.
Input: arr[] = [2, 1, 8, 6, 4, 6, 5, 5]
Output: 25
Explanation: 8 and 5 are 5 distance apart. So the size of the base is 5. Height of container = min(8, 5) = 5.
So, the total area to hold water = 5 * 5 = 25.
2. Find all triplets with given sum
Difficulty: MediumAccuracy: 32.06%Submissions: 809+Points: 4Average Time: 14m
Given an array arr. The task is to find triplets in an array whose sum equals a given number k.
Examples :
Input: arr[] = [0, -1, 2, -3, 1], k = -2
Output: [[0, -3, 1], [-1, 2, -3]]
Explanation: If we calculate the sum of the output, 0 + (-3) + 1 = -2, (-1) + 2 + (-3) = -2
Input:arr[] = [1, -2, 1, 0, 5], k = 0
Output: [[1, -2, 1]]
Explanation: If we calculate the sum of the output, 1 + (-2) + 1 = 0
3. Set Matrix Zeros
Difficulty: MediumAccuracy: 52.54%Submissions: 52K+Points: 4
You are given a 2D matrix mat[][] of size n x m. The task is to modify the matrix such that if mat[i][j] is 0,
all the elements in the i-th row and j-th column are set to 0.
Examples:
Input:
Output:
Explanation: mat[1][1] = 0, so all elements in row 1 and column 1 are updated to zeroes.
Input:
Output:
Explanation: mat[0][0] and mat[0][3] are 0s, so all elements in row 0, column 0 and column 3 are
updated to zeroes.
4. Search in a Row-Column sorted matrix
Difficulty: EasyAccuracy: 41.62%Submissions: 185K+Points: 2Average Time: 15m
Given a 2D integer matrix mat[][] of size n x m, where every row and column is sorted in increasing order
and a number x, the task is to find whether element x is present in the matrix.
Examples:
Input: mat[][] = [[3, 30, 38],[20, 52, 54],[35, 60, 69]], x = 62
Output: false
Explanation: 62 is not present in the matrix, so output is false.
Input: mat[][] = [[18, 21, 27],[38, 55, 67]], x = 55
Output: true
Explanation: 55 is present in the matrix.
Input: mat[][] = [[1, 2, 3],[4, 5, 6],[7, 8, 9]], x = 3
Output: true
Explanation: 3 is present in the matrix.
5. Rotate a Matrix by 180 Counterclockwise
Difficulty: MediumAccuracy: 52.99%Submissions: 15K+Points: 4
Given a 2D square matrix mat[][] of size n x n, rotate it by 180 degrees without using extra space.
Note: You must rotate the matrix in place and modify the input matrix directly.
Examples:
Input: mat[][] = [[1, 2],
[3, 4]]
Output: [[4, 3],
[2, 1]]
Input: mat[][] = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Output: [[9, 8, 7],
[6, 5, 4],
[3, 2, 1]]
6. Maximum Perimeter Triangle from array
Given an array arr[] of positive integers. Find out the maximum perimeter of the triangle from the array.
Note: Return -1, if it is not possible to construct a triangle.
Examples:
Input: arr[] = [6, 1, 6, 5, 8, 4]
Output: 20
Explanation: Triangle formed by 8,6 & 6 has perimeter 20, which is the max possible.
Input: arr[] = [7, 55, 20, 1, 4, 33, 12]
Output: -1
Explanation: The triangle is not possible because the condition: the sum of two sides should be greater
than third is not fulfilled here.
7. Maximize array sum after K negations using Sorting
Given an array of size n and an integer k. We must modify array k number of times. In each modification,
we can replace any array element arr[i] by -arr[i]. The task is to perform this operation in such a way that
after k operations, the sum of the array is maximum.
Examples :
Input : arr[] = [-2, 0, 5, -1, 2], k = 4
Output: 10
Explanation:
1. Replace (-2) by -(-2), array becomes [2, 0, 5, -1, 2]
2. Replace (-1) by -(-1), array becomes [2, 0, 5, 1, 2]
3. Replace (0) by -(0), array becomes [2, 0, 5, 1, 2]
4. Replace (0) by -(0), array becomes [2, 0, 5, 1, 2]
Input : arr[] = [9, 8, 8, 5], k = 3
Output: 20
Explanation: Negate 5 three times. Array will become [9, 8, 8, -5].
8. Sum of minimum absolute differences in an array
Given an array of n distinct integers. The task is to find the sum of minimum absolute difference of each
array element. For an element arr[i] present at index i in the array, its minimum absolute difference is
calculated as:
Min absolute difference (arr[i]) = min(abs(arr[i] - arr[j])), where 0 <= j < n and j != i and abs is
the absolute value.
Examples:
Input : arr = [4, 1, 5]
Output : 5
Explanation: Sum of minimum absolute differences is |4-5| + |1-4| + |5-4| = 1 + 3 + 1 = 5
Input : arr = [5, 10, 1, 4, 8, 7]
Output : 9
Explanation: Sum of minimum absolute differences is
|5-4| + |10-8| + |1-4| + |4-5| + |8-7| + |7-8| = 1 + 2 + 3 + 1 + 1 + 1 = 9
Input : arr = [12, 10, 15, 22, 21, 20, 1, 8, 9]
Output : 18
9. Sort an array in wave form
Given a sorted array arr[] of integers (in ascending order), rearrange the elements in-place to form a
wave-like array.
An array is said to be in wave form if it satisfies the following pattern: arr[0] ≥ arr[1] ≤ arr[2] ≥ arr[3] ≤
arr[4] ≥ ...
In other words, every even-indexed element should be greater than or equal to its adjacent odd-indexed
elements (if they exist).
Note: The given array is sorted in ascending order, and modify the given array in-place without returning
a new array.
Input: arr[] = [1, 2, 3, 4, 5]
Output: [2, 1, 4, 3, 5]
Explanation: Array elements after sorting it in the waveform are 2, 1, 4, 3, 5.
Input: arr[] = [2, 4, 7, 8, 9, 10]
Output: [4, 2, 8, 7, 10, 9]
Explanation: Array elements after sorting it in the waveform are 4, 2, 8, 7, 10, 9.
10. Chocolate Distribution Problem
Given an array arr[] of n integers where arr[i] represents the number of chocolates in ith packet. Each
packet can have a variable number of chocolates. There are m students, the task is
to distribute chocolate packets such that:
Each student gets exactly one packet.
The difference between the maximum and minimum number of chocolates in the packets given
to the students is minimized.
Examples:
Input: arr[] = {7, 3, 2, 4, 9, 12, 56}, m = 3
Output: 2
Explanation: If we distribute chocolate packets {3, 2, 4}, we will get the minimum difference, that is 2.
Input: arr[] = {7, 3, 2, 4, 9, 12, 56}, m = 5
Output: 7
Explanation: If we distribute chocolate packets {3, 2, 4, 9, 7}, we will get the minimum difference, that is
9 - 2 = 7.
11. Pair with the given difference
Given an unsorted array and an integer x, the task is to find if there exists a pair of elements in the array
whose absolute difference is x.
Examples:
Input: arr[] = [5, 20, 3, 2, 50, 80], x = 78
Output: Yes
Explanation: The pair is {2, 80}.
Input: arr[] = [90, 70, 20, 80, 50], x = 45
Output: No
Explanation: No such pair exists.