Hands-on_Problem Solving - Day 06 (2)
Hands-on_Problem Solving - Day 06 (2)
Exercise No. : VI
Date : 20.02.2025
Q
Question Detail Level
No.
Sample Input 1:
2
21
15
Sample Output 1:
3
3
Explanation Of Sample Input 1:
Test Case 1:
‘21’ can be represented as:
1 + 2 + 3 + 4 + 5 + 6 = 21
Constraints:
1 <= T <= 1000
3 <= N <= 10 ^ 5
Where ‘T’ is the number of test cases, and ‘N’ is the given number.
2 Butterfly star pattern Hard
Problem statement: Create a program to print a butterfly star pattern
consisting of stars in a symmetrical butterfly shape. The pattern should be
printed such that the stars form wings on both sides of the centerline. The
number of stars in each wing should decrease towards the centerline.
Input 1 : rows = 5
Output 1:
* *
** **
*** ***
**** ****
**********
**** ****
Input 2: rows =7
Output 2 :
* *
** **
*** ***
**** ****
***** *****
************
************
***** *****
**** ****
*** ***
** **
* *
Example 1:
Input: nums = [2,5,9]
Output: 10
Explanation:
floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0
floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1
floor(5 / 2) = 2
floor(9 / 2) = 4
floor(9 / 5) = 1
Example 2:
Input: nums = [7,7,7,7,7,7,7]
Output: 49
Constraints:
● 1 <= nums.length <= 10^5
● 1 <= nums[i] <= 10^5
Problem statement: Rita has to design a code that takes a positive integer
or number which is required to be given by the user. Then the code should
further identify whether that digit can be expressed as the sum of two prime
numbers. If the inserted number can be expressed as sum of two prime
numbers then, print the integer can be expressed as sum of two prime
numbers as a result.
Sample Input 1: 34
Sample Input1: 20
Constraints:
N>=1
Examples :
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)2= 111
(12)2= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Constraints :
0 <= A, B <= 10^9
Problem Statement : Given two sorted arrays array1 and array2 of size m
and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for {1,2,3,5,6,7,9} is 5
Example 2:
Input:
m = 2, n = 4
Constraints:
0 ≤ m,n ≤ 10^6
1 ≤ array1[i], array2[i] ≤ 10^9
Problem statement : You are given an array containing ‘N’ integers. In the
array, the elements are 0, 1 and 2. You have a simple task, find the count of
non-empty subarrays containing an equal number of 0’s, 1’s and 2’s.
The subarray of ARR is a contiguous part of the array ARR, i. e. the array ARRi,
ARRi+1, . . . . . , ARRj for some 0 ≤ i ≤ j ≤ N - 1.
For Example :
There are exactly two subarrays that contain an equal number of 0’s, 1’s and
2’s.
Sample Input 1 :
11021
1100
Sample Output 1 :
There are exactly two subarrays that contain an equal number of 0’s, 1’s and
2’s.
The first subarray is from ARR[1] to ARR[3], ie: {1, 0, 2}, and the second
subarray is from ARR[2] to ARR[4], ie: {0, 2, 1}
The input array doesn’t contain any element equal to 2, so it’s impossible to
form a non-empty subarray with an equal number of 0’s, 1’s and 2’s.
Sample Input 2 :
102102
110022
Sample Output 2 :
Constraints :
1 ≤ T ≤ 10
1 ≤ N ≤ 1000
ARR[i] = {0, 1, 2}
Example 1:
Input: nums = [1,2,3,4,5], k = 2
Output: 7
Explanation:
The 7 pairs of indices whose corresponding products are divisible by 2 are (0,
1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4).
Their products are 2, 4, 6, 8, 10, 12, and 20 respectively.
Other pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively,
which are not divisible by 2.
Example 2:
Input: nums = [1,2,3,4], k = 5
Output: 0
Explanation: There does not exist any pair of indices whose corresponding
product is divisible by 5.
Constraints:
● 1 <= nums.length <= 10^5
● 1 <= nums[i], k <= 10^5
Example 1:
Input:
N = 4, K = 3
arr[] = {1, 2, 3, 4}
Little practice is worth more than a ton of theory
9
PROBLEM SOLVING
SDE Readiness Training
Output: 4
Explanation:
Optimal Split is {1, 2}, {3}, {4}.
Maximum sum of all subarrays is 4, which is minimum possible for 3 splits.
Example 2:
Input:
N = 3, K = 2
A[] = {1, 1, 2}
Output:
2
Explanation:
Splitting the array as {1,1} and {2} is optimal.
This results in a maximum sum subarray of 2.
Constraints:
1 ≤ N ≤ 10^5
1≤K≤N
1 ≤ arr[i] ≤ 10^4
Sample Input 1:
1
44
1234
5678
9 10 11 12
13 14 15 16
Sample Output 1:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Sample Input 2:
2
33
123
456
789
31
10
20
30
Sample Output 2:
123698745
10 20 30
Constraints :
1 <= t <= 10^2
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A LS IG
YA HR
P I
Constraints:
● 1 <= s.length <= 1000
● s consists of English letters (lower-case and upper-case), ',' and '.'.
● 1 <= numRows <= 1000
●
13 Longest repeating and non-overlapping substring Hard
Problem Statement: Given a string s of length n, find the longest repeating
non-overlapping substring in it. In other words, find 2 identical substrings of
Example 1:
Input:
n=9
s = "acdcdacdc"
Output:
"acdc"
Explanation:
The string "acdc" is the longest Substring of s which is repeating but not
overlapping.
Example 2:
Input:
n=7
s = "heheheh"
Output:
"heh"
Explanation:
The string "heh" is the longest Substring of s which is repeating but not
overlapping.
Constraints:
1 <= n <= 10^3
14 Palindrome partitioning Hard
Sample Input 1 :
aaccb
Sample Output 1 :
2
Explanation of sample input 1 :
We can make a valid partition like aa | cc | b.
Sample Input 2 :
ababa
Sample Output 2 :
0
Explanation of sample input 2 :
The string is already a palindrome, so we need not make any partition.
Constraints :
1 <= 'n' <= 100
Now people come one by one in a line to take plates of Ladoos from Richal.
Richal picks the two plates in front, one from each row and gives that plate
to people in which the number of ladoos is the smallest (if both plates
Can you tell how many ladoos the ‘K'th’ person will get?
Sample Input 1 :
2
543
3 11 23 45 52
4 12 14 18
112
1
2
Sample Output 1 :
11
2
Explanation for Sample Output 1 :For sample test case 1:
1’st person will get 3 ladoos i.e a minimum of 3 and 4. Now ‘ROW1’ : [11,
23, 45, 52] and ‘ROW2’ : [4, 12, 14, 18].
2’nd person will get 4 ladoos i.e minimum of 11 and 4. Now ‘ROW1’ : [11,
23, 45, 52] and ‘ROW2’ : [12, 14, 18].
3’rd person will get 11 ladoos i.e minimum of 11 and 12.
Constraints :
1 <= T <= 100
1 <= N, M, K <= 10^5
K <= (N + M)
0 <= ROW1[i], ROW2[i] <= 10^5
where ROW1[i] and ROW2[i] denote the number of Ladoos in i’th plates of
ROW1 and ROW2 respectively.
Example 1:
Input: nums = [2,1,4,3], left = 2, right = 3
Output: 3
Explanation: There are three subarrays that meet the requirements: [2], [2,
1], [3].
Example 2:
Input: nums = [2,9,2,5,6], left = 2, right = 8
Output: 7
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9
0 <= left <= right <= 10^9
For example, strings "aeiou" and "aaaaaaeiiiioou" are considered beautiful, but
"uaeio", "aeoiu", and "aaaeeeooo" are not beautiful.
Given a string word consisting of English vowels, return the length of the
longest beautiful substring of word. If no such substring exists, return 0.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
Output: 13
Explanation: The longest beautiful substring in word is "aaaaeiiiiouuu" of
length 13.
Example 2:
Example 3:
Input: word = "a"
Output: 0
Explanation: There is no beautiful substring, so return 0.
Constraints:
1 <= word.length <= 5 * 10^5
word consists of characters 'a', 'e', 'i', 'o', and 'u'.