Last Updated: 25 May, 2022

Ninja and his Birthday Treat

Easy
Asked in company
Microsoft

Problem statement

Ninja, on his birthday, decided to give a treat to 'N' children in his neighbourhood. Ninja took every child to a nearby candy shop and bought them candies. The shop contains β€˜M’ types of candies with an unlimited supply of each candy. The cost of each candy is between 1 and M, inclusive and distinct.

The shopkeeper has marked every candy with a number between 1 and M, inclusive. To make this process fascinating, Ninja found a way. He asked every child three integers, β€˜L’, β€˜R’, and β€˜X’. Then he bought exactly β€˜X’ pieces of each candy from [L to R] , i.e., β€œL, L+1, L+2,..., R-1, R”.

Your task is to find the lexicographically smallest array β€œA” of β€˜M’ integers where each element represents the cost of the ith candy, such that the total cost spent by Ninja is minimum.

Note:

The cost array is a permutation of the first M natural numbers.
The shop has unlimited supplies of candies.
In case of multiple permutations, print one which is lexicographically minimum. 

For example:

Let’s say β€˜N’ = 3 , β€˜M’ = 5  
The integers told by children are { {2 , 4 , 1} ,{1 , 3 , 2} , {3, 3 , 5} }  

The optimal permutation will  be {3 2 1 4 5}    
Costs paid by Ninja are 7, 12, and 5, respectively.  
The total cost would be 7 + 12 + 5 = 24
Input Format :
First-line contains 'T,' denoting the number of Test cases.

For each Test case:
The first contains two integers, 'N' and β€˜M’, where β€˜N’ is the number of children and β€˜M’ is the number of candies available in the shop.
Next, the β€˜N’ line contains three integers, β€˜L’, β€˜R’, and β€˜X’.
Output format :
For each test case, print β€˜M’ integers, i.e., the permutation of β€˜M’ natural numbers.
Constraints :
1<=β€˜T’<=10
1<=β€˜N’<=10^5
1<=β€˜M’<=10^5
1<=β€˜L’<=β€˜R’<=β€˜M’
1<=β€˜X’<=10^5

Time Limit: 1 sec

Approaches

01 Approach

Approach :

Calculate the frequency of each candy sold, and since we have to reduce our total cost, we can assign the lower price to the higher-selling candy. Hence, we will give the cost in decreasing order of their selling quantity. Assign cost 1 to most selling candy, cost 2 to the second most selling candy, etc. 


 

Algorithm:

  • Create an array β€˜count’ of length β€˜M’ and initialize it to 0.
  • Now iterate over all the triplets β€œ(L, R, X)” and add β€˜X’ to each position from L to R, inclusive.
  • Now create an array β€˜A’ of size ’M’ and assign the number to candy in decreasing order of their frequency.
  • Calculate the total cost by multiplying the frequency of candy by the corresponding number assigned and summing them together.

02 Approach

Approach :

Calculate the frequency of each candy sold, and since we have to reduce our total cost, we can assign the lower price to the higher-selling candy. Hence, we will give the cost in decreasing order of their selling quantity. To find out the number of candies sold, we could use the prefix sum. For all demands (L, R, X), add X to the Lth position and subtract X from the R+1th position of the count array. 

Now take the prefix sum of the count array.

 

Algorithm:

  • Create an array β€˜count’ of length β€˜M’ and initialize it to 0.
  • Now iterate over all the triplets β€˜(L, R, X)’ and add β€˜X’ to position L and subtract β€˜X’ from the β€˜R+1’ of the β€œcount” array.
  • Take the prefix sum of the β€˜count’ array.
  • Now create an array β€˜A’ of size ’M’ and assign the number to candy in decreasing order of their frequency.
  • Calculate the total cost by multiplying the frequency of candy by the corresponding number assigned and summing them together.