Candle Bunch - An interview question
91
Mar 12, 2026

Hi people!! Can someone help me with this problem i need the approch and code also sample test cases to check my code

This question was asked in an interview. I looked for answers in the internet but wasn't satisfied so help me guys!!!

Candle Bunch
Mary has baked K cakes and she wants to put numbered candle on top of eadd of them, She wants to put candies numbered 1.2. Kon the cakes (one candle on each cake). She went to the store to buy candles. The candle shop has a row of N candle boxes. The shopkeeper will sell only a consecutive subset of
candles. Mathematically, Mary can choose L and R (1 <= L <= R <= N) , and she can buy the candles candle[L], candle(L + 1]...
candle[R], considering 1-based
indexing. Since Mary does not want to spend extra money. She wants to buy the minimum number of candies to get her desired candles. You are given a row of candles. Find out the minimum number of candles that Mary has to buy in order to have at least the candles numbered 1, 2,...,Κ.

Note: All the boxes in the stack have one unique numbered candle
Function description

Complete the function solution(). The function takes the following 3 parameters and returns the solution:
M:Represents the number of boxes of candles
K: Represents the number of candles Mary needs
candle: Represents the row of candle boxes
Input format

Note: This is the input format that you must use to provide custom Input (available above the Compile and Test button)

The first line contains N denoting the number of boxes of candles.
The second line contains K. denoting the number of candles Mary needs.
The third line contains an array candie denoting the row of candle boxes.

Output format
Print an integer denoting the minimum number of candles Mary must buy

Constraints
1<=K<=N <=10
1 ≤ candle[i] ≤ N

Sample Inputs
7
3
4 5 3 1 7 2 6

Sample Output
4

Comments (1)