Problem Statement:
Zlatan is a master of solving tricky questions. His friend gave him some numbers (in the form of an array) and asked him to find the number of subsequences of the array such that its sum is m-1.
Here, m is the sum of the initial given array. Output the number of such subsequences and print it in one line.
Example:
array = [5, 4, 2, 1, 1]
Output: 2
Valid ways are taking the first [5, 4, 2, 1] and then the second [5, 4, 2, 1].
Problem Statement: A professor wanted maximum benefits from a number game. He has information on numbers scattered throughout a game board given in the form of an array. He will surely take three numbers and multiply their values to get the maximum value out of all possible combinations. The array might contain negative numbers as well.
Simply print the maximum value he can get.
Example: array = [-9, 9, 5, -3, 100], n = 5
Output: 4500 Explanation: The three numbers chosen are 9, 5, & 100 which have a multiplication value of 4500.
Problem Statement:
You are given a number n. Your friend added the numbers from 1 till n, TWICE. The sum is S. Now he says that he forgot to add only one number twice (he added it only once).
Determine the number he may have possibly missed out.
Example:
n = 4, S = 14
Output: 3
Explanation: If we add numbers twice from 1 till 4, we get 20. He has a sum of 14, which means 20 - 14 = 6 is missing. But wait, he missed adding a number twice, meaning it's only in the sum once. Expected S = 20, actual S = 14. But the example logic says 20 - 3 - 3 = 14. Wait, the math is expected total minus actual total equals the missing number.
Problem Statement: A king is a person who has:
More kindness than his neighbours.
More kindness than the rest of the people.
Kindness is given by an integer value in an array. Find the position of the king. If there are multiple kings, print all of their positions separated by spaces. (1-based indexing)
Example: kind = [5, 6, 7, 6, 5, 5, 100, 5, 100, 45], n = 11
Output: 8 10 Notice there are two kings, one at the 8th and the other at the 10th position.
Problem Statement: In a class test, many students participated. You have to determine both the number of unique scores and the unique scores as scored by the students.
In the first line, print the unique scores in the order they appear. In the second line, print: "The number of unique scores are X", where X is the number of unique scores.
Example: array = [45, 61, 78, 78, 98, 98]
Output: 45 61 78 98 The number of unique scores are 4
Problem Statement:
Given an array of integers of length n. Find out if there exists a contiguous subarray whose sum is equal to the number K.
If there exists a subarray with a sum equal to K, print the largest and the smallest number in that subarray. If there exists no such subarray, print "-1 -1".
Example:
array = [1, 5, 2, 6, 3, 2], n = 6, k = 7
Output: 5 2
Explanation: The subarray [5, 2] has a sum of 7. The largest value is 5 and the smallest is 2..
Problem-1 Zlatan and Tricks Solution
Topics - :
Array Mathematics
Combinatorics / Counting
Overview
The total sum of the entire array is exactly m. To form a subsequence with a sum of m - 1, the elements we leave out must sum to exactly 1. Therefore, the number of such subsequences is simply the total count of the number 1 present in the initial array.
Approach
1. The Logic Behind the Sum
This is a clever logic puzzle! Instead of building every possible combination to see what works, we just look at what we are leaving behind. If the total array sums up to m, and we want a piece of it that sums to m - 1, the piece we remove must be exactly 1.
2. Counting the Targets
Since we just need to drop a single 1 to get our desired sum, every individual 1 in the array represents a valid, unique way to form the subsequence. We just iterate through the array and count how many times the number 1 appears.
Code Implementation
Problem-2 Professor Plays a Game Solution
Topics Involved / Prerequisites
Array Sorting
Greedy Approach
Overview
Approach
1. Sorting the Board This is a classic optimization challenge! When dealing with maximum products, we always want the absolute highest values. By sorting the array in ascending order, we push the largest positive numbers to the far right, and the largest negative numbers (smallest values) to the far left.
2. Comparing the Two Extremes There are only two ways to get the maximum product of three numbers:
Multiply the three largest numbers together:
array[n-1] * array[n-2] * array[n-3]Multiply the two smallest negative numbers (which makes a positive) by the absolute largest number:
array[0] * array[1] * array[n-1]We calculate both and simply return whichever is bigger!Code Implementation
Forgotten Number Solution
Topics -:
Mathematical
Overview
The expected sum if every number from 1 to n is added exactly twice is simply n *(n + 1). By subtracting the actual given sum S from this perfect expected total, we get the total value that is missing. Since the logic implies the missing number was omitted completely (missed twice), we divide this difference by 2.
Approach
1. Finding the Perfect Sum
This is a fun mathematical trick! Rather than simulating the friend's exact additions, we use the classic sum formula. The sum of 1 to n is n* (n + 1) / 2. Since he was supposed to add them twice, the expected sum is just n*(n + 1).
2. Finding the Difference
We subtract the friend's actual sum S from our perfect expected sum. The problem's example output shows that the missing number was skipped both times. So, the difference we calculated is actually twice the value of the forgotten number. We just divide the difference by 2 to get our final answer!
Time Complexity
Time: O(1) - The entire calculation is done using a direct mathematical formula.
Space: O(1) - No extra memory is used.
Problem-4 King Solution
Topics-: Array
Overview
A king must have more kindness than everyone else, meaning we are looking for the absolute maximum values in the entire array. We first scan the array to determine this highest possible kindness score among all people. Then, we scan again to find the positions of this maximum score, ensuring they are strictly greater than their immediate left and right neighbors.
Approach
Approach
1. Finding the Highest Standard This requires a two-step validation! To be a true king, you must first have more kindness than the general population. This means the king can only be someone who shares the maximum value found in the entire array.
2. Checking the Neighbors Once we know what the maximum value is, we loop through the array again. If we find someone with that maximum value, we check the person to their immediate left and right. If they are strictly greater than both neighbors (handling the edges carefully), we print their 1-based index.
Psudo Code
Problem5 Class Test 1 Solution
Topics
Hash Sets
Overview
Approach
1. Filtering in Real-Time This is a straightforward data filtering task! The key is to maintain the original sequence. As we loop through the test scores, we check if the current score is already in our Hash Set.
2. Storing and Printing If the score is not in the set, it's a brand new unique score! We print it out immediately, and then add it to our set so we don't print it again later. Once the loop finishes, the length of our set is the total number of unique scores.
Code Implementation-:
Time Complexity
Time: O(N) - We iterate through the array once to find the subarray, and at most once more to find the min/max inside it.
Space: O(N) - In the worst case, we store N distinct prefix sums in the hash map.