Close
Topic list
Carpet and Boxes
MEDIUM
20 mins
Sorting
Topics (Covered in this problem)
Problem solved
Skill meter
Sorting
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Carpet and Boxes

Contributed by
Amit Shaw
Medium
0/80
Avg time to solve 20 mins
Success Rate 70 %
Share

Problem Statement

Find the minimum length of the carpet to cover any 'K' boxes.

Example:
``````Input: ‘N’ = 3, ‘K’ = 2, 'POSITION' = [2, 3, 6]

Output: 2
``````

`````` There are two possible ways to cover exactly two boxes. One covers the boxes at positions 2 and 3 and the other at positions 3 and 6. The carpet lengths required for both ways are 2 ( Since boxes are at positions 2 and 3 each having width 1) and 4, respectively. So the minimum length of the carpet is 2.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
``````2
4 3
4 9 2 6
2 1
1 2
``````
Sample Output 1 :
``````5
1
``````
Explanation Of Sample Input 1 :
``````For the first case:
There are two possible ways to cover exactly three boxes. One covers the boxes at positions 2, 4 and 6 and the other at positions 4, 6 and 9. The carpet lengths required for both ways are 5 and 6, respectively. So the minimum length of the carpet is 5.

For the second case:
Since we need to cover only one box, the minimum length of the carpet is 1.
``````
Sample Input 2 :
``````2
5 2
100 2 95 145 47
1 1
100
``````
Sample Output 2 :
``````6
1
``````
Auto
Console