TECHNICAL
TECHNICAL
TECHNICAL
Question 1
How to Attempt?
Conditional Subarrays
You have sorted box of numbers stored in an integer array A and you want to find all the “mini
boxes” (subarrays) that meet a special rule. This rule says that the sum of the smallest and biggest
number in each mini box must be less than or equal to a given number X. Remember the original
box is already sorted, so the smallest number is always at the beginning and the biggest one is at
the end of the mini box you create. Your task is to find and return an integer value representing
the number of mini boxes that can be made which the rule.
Input Specification:
Input1: An integer N, representing size of array
Input2: An integer X, representing the comparison value.
Input3: An integer array A, representing the sorted box of numbers.
Output Specification:
Return an integer value representing the number of mini boxes that can be made which follow the
rule.
Example 1:
Input1: 4
Input2: 5
Input3: [1, 2, 3, 4]
Output: 4
Explanation:
1. Subarrays:
o [1]: The smallest and largest numbers are 1 and 1, and their sum is 1 + 1 = 2 (less
than or equal to 5).
o [1, 2]: The smallest and largest numbers are 1 and 2, and their sum is 1 + 2 = 3
(less than or equal to 5).
o [2]: The smallest and largest numbers are 2 and 2, and their sum is 2 + 2 = 4 (less
than or equal to 5).
o [1, 3]: The sum is 1 + 3 = 4, also valid.
2. Other subarrays like [1, 2, 3] or [3, 4] have sums greater than 5 and should be excluded.
Example 2:
Input1: 4
Input2: 21
Input3: [7, 8, 12, 13]
Output: 7
Explanation:
1. Array: [7, 8, 12, 13]
2. X: 21
We need to find all subarrays where the sum of the smallest and largest numbers in each subarray
is less than or equal to 21.
Let's consider all subarrays:
Subarray [7]: The sum is 7 + 7 = 14 (less than 21).
Subarray [7, 8]: The sum is 7 + 8 = 15 (less than 21).
Subarray [7, 8, 12]: The sum is 7 + 12 = 19 (less than 21).
Subarray [7, 8, 12, 13]: The sum is 7 + 13 = 20 (less than 21).
Subarray [8]: The sum is 8 + 8 = 16 (less than 21).
Subarray [8, 12]: The sum is 8 + 12 = 20 (less than 21).
Subarray [8, 12, 13]: The sum is 8 + 13 = 21 (equal to 21).
Subarray [12]: The sum is 12 + 12 = 24 (greater than 21, invalid).
Subarray [12, 13]: The sum is 12 + 13 = 25 (greater than 21, invalid).
Subarray [13]: The sum is 13 + 13 = 26 (greater than 21, invalid).
Code: C
#include <stdio.h>
while (i <= j) {
if (A[i] + A[j] <= X) {
// All subarrays between i and j are valid
count += (j - i + 1);
i++; // move the left pointer forward
} else {
j--; // move the right pointer backward
}
}
return count;
}
int main() {
int N, X; // variables for size of array and comparison value
return 0;
}
Code: Java
import java.util.Scanner;
import java.util.Arrays;
while (i <= j) {
if (A[i] + A[j] <= X) {
// All subarrays between i and j are valid
count += (j - i + 1);
i++; // move the left pointer forward
} else {
j--; // move the right pointer backward
}
}
return count;
}
// Ensure the array is sorted (if not already sorted by the user)
Arrays.sort(A);
Code: Python
def count_subarrays(A, N, X):
count = 0 # to store the number of valid subarrays
i=0
j = N - 1 # two pointers
while i <= j:
if A[i] + A[j] <= X:
# All subarrays between i and j are valid
count += (j - i + 1)
i += 1 # move the left pointer forward
else:
j -= 1 # move the right pointer backward
return count
Question 2
How to Attempt?
Smart Shopping
You are given an array of N elements representing the prices of each item. You are also given a
discount offer that allows you to buy K items at a reduced price. The discount offer works as, for
every most expensive K items you decide to buy, the cost of the maximum priced item out of
those K items will be waived off.
Your task is to find and return an integer value representing the total cost after applying the
maximum discount offer.
Input Specification:
Input1: An integer value N representing the number of items.
Input2: An integer value K, representing the number of items to buy at a reduced price.
Input3: An integer array representing the prices of items.
Output Specification:
Return an integer value representing the total cost achievable using the given maximum discount
offer.
Example 1:
Input1: 5
Input2: 2
Input3: [10, 20, 30, 40, 50]
Output: 100
Explanation:
To maximize the discount, we should pick the K most expensive items in descending order:
Items sorted: [50, 40, 30, 20, 10]
Groups of K items: [50, 40] (waive off the most expensive item: 50) and [30, 20] (waive off
the most expensive item: 30)
Total cost after discounts: 40 + 20 + 10 = 70
Example 2:
Input1: 6
Input2: 3
Input3: [5, 15, 10, 25, 20, 30]
Output: 60
Explanation:
To maximize the discount, we should pick the K most expensive items in descending order:
Items sorted: [30, 25, 20, 15, 10, 5]
Groups of K items: [30, 25, 20] (waive off the most expensive item: 30) and [15, 10, 5]
(waive off the most expensive item: 15)
Total cost after discounts: 25 + 20 + 10 + 5 = 60
Algorithm:
1. Sort the Array: Sort the array of prices in descending order.
2. Calculate Cost:
Initialize total_cost to 0. Iterate through the sorted list in chunks of size K, and for
each chunk:
Add the sum of the chunk to total_cost excluding the maximum price of that
chunk.
3. Return the Total Cost: After processing all chunks, return total_cost.
Code: C
#include <stdio.h>
#include <stdlib.h>
int total_cost = 0;
int i = 0;
while (i < N) {
int chunk_end = i + K;
if (chunk_end > N) chunk_end = N; // Ensure we don't go out of bounds
return total_cost;
}
int main() {
int N, K;
return 0;
}
Code: Java
import java.util.Arrays;
import java.util.Scanner;
int totalCost = 0;
int i = 0;
while (i < N) {
int chunkEnd = i + K;
if (chunkEnd > N) chunkEnd = N; // Ensure we don't go out of bounds
Code: Python
def calculate_total_cost(prices, K):
# Sort the array in descending order
prices.sort(reverse=True)
total_cost = 0
i=0
N = len(prices)
while i < N:
# Calculate the end index of the current chunk
chunk_end = min(i + K, N)
# Add the sum of this chunk, excluding the maximum item (first in the chunk)
for j in range(i + 1, chunk_end):
total_cost += prices[j]
return total_cost
def main():
# Take input for the size of the array
N = int(input())
if __name__ == "__main__":
main()
Question 3
How to Attempt?
Magical Library
In a magical library, each bookshelf is represented by a two-dimensional array A, where each row
of the 2D array A[i] represents the series value of a book.
A row is considered magical if the sum of the odd values of the series of a book is even. Your task
is to find and return an integer value representing the number of magical rows.
Input Specification:
Input1: An integer value representing the number of rows in the 2D array.
Input2: An integer value representing the number of columns in the 2D array.
Input3: A 2D integer array where each row represents a series of books.
Output Specification:
Return an integer value representing the number of magical rows.
Example 1:
Input1: 3
Input2: 4
Input3: [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
Output: 3
Explanation:
Row 1: [1, 2, 3, 4] → Odd numbers: [1, 3] → Sum = 4 (Even) → Magical row
Row 2: [5, 6, 7, 8] → Odd numbers: [5, 7] → Sum = 12 (Even) → Magical row
Row 3: [9, 10, 11, 12] → Odd numbers: [9, 11] → Sum = 20 (Even) → Magical row
Output: 3 magical rows
Example 2:
Input1:
Input2:
Input3:
Output: 7
Explanation:
Code: C
#include <stdio.h>
return magicalRows;
}
int main() {
// Input section
int m, n;
return 0;
}
Code: Java
import java.util.Scanner;
return magicalRows;
}
// Declare a 2D array
int[][] arr = new int[m][n];
sc.close();
}
}
Code: Python
def count_magical_rows(arr, m, n):
magical_rows = 0
return magical_rows
Question 4
How to Attempt?
Palindromic String Count
Jack broke a toy which consisted of a string A. The toy was broken in such a way that some letters
were still present and some lost. Jack decided to replace the missing letters with ‘?’. He can
generate palindromic string by replacing ‘?’ with any letter from ‘a’ to ‘z’. Jack has a favourite
number M. You task is to find and return an integer value representing the total number of
palindromic strings modulo M that he can generate by replacing ‘?’ with any letter from ‘a’ to ‘z’.
Input Specification:
Input1: An integer value M
Input2: An string S representing a word with missing characters.
Output Specification:
Return an integer value representing the total number of palindromic words Jack can form after
performing modulo with a given integer M.
Example 1:
Input1: 3
Input2: ca?d
Output: 3
Explanation:
Here the given string is ca?d and M is S. If we replace ‘?’ with ‘a’ then the string becomes “caad”
which is not a palindrome. It will never be a palindrome even if we replace ‘?’ with any letter
between a to z. Therefore 0 is returned as the output.
Example 2:
Input1: 7
Input2: a?ba?
Output: 26
Explanation:
All 26 alphabetical inputs between ‘a’ to ‘z’ can make given string ‘a?ba?’ palindrom.
Algorithm:
1. Initialize count = 1 to keep track of the number of valid palindrome combinations.
2. Iterate through the first half of the string (since the second half is its mirror image).
Let i be the index from 0 to n // 2 - 1.
For each character pair (S[i], S[n-i-1]):
If both are ?, multiply count by 26 because there are 26 possibilities to make this
pair.
If one is ? and the other is a letter, there is only 1 way to match them, so count
remains unchanged.
If both are letters but not equal, a palindrome is impossible, return 0.
3. After completing the loop, return count % M.
Time Complexity:
O(n/2) → Simplifies to O(n) where n is the length of the string, as we only traverse half the
string to check for palindrome formation.
Space Complexity:
O(1) → We use a constant amount of space aside from the input string.
This algorithm efficiently calculates the number of valid palindromic strings Jack can generate,
modulo M.
Code: C
#include <stdio.h>
#include <string.h>
return count;
}
int main() {
int M;
char S[100];
scanf("%s", S);
return 0;
}
Code: Java
import java.util.Scanner;
return count;
}
String S = scanner.nextLine();
scanner.close();
}
}
Code: Python
def count_palindromic_strings(M, S):
n = len(S)
count = 1 # Initialize the count of possible palindromic strings
# Traverse only the first half of the string
for i in range(n // 2):
left = S[i]
right = S[n - i - 1]
return count
# Calling the function with user input and printing the result
result = count_palindromic_strings(M, S)
print(result)
Remaining Questions are given below (Not Done due to income screenshots or improper visibility)
Independence Day Parade
Color Changing Box
Animal Chart
Knowledge Enhancement
The Distance
Alex Loves Chocolate
Finding String Weight – (A-z)
Mango Distribution
A Gardener is Tasked with Watering
Cryptographer
String Swap
Generated Numbers