Fitting Shelves Problem

Given length of wall w and shelves of two lengths m and n, find the number of each type of shelf to be used and the remaining empty space in the optimal solution so that the empty space is minimum. The larger of the two shelves is cheaper so it is preferred. However cost is secondary and first priority is to minimize empty space on wall.

Examples:

Input : w = 24 m = 3 n = 5
Output : 3 3 0
We use three units of both shelves
and 0 space is left.
3 * 3 + 3 * 5 = 24
So empty space  = 24 - 24 = 0
Another solution could have been 8 0 0
but since the larger shelf of length 5
is cheaper the former will be the answer.

Input : w = 29 m = 3 n = 9 
Output : 0 3 2
0 * 3 + 3 * 9 = 27
29 - 27 = 2

Input : w = 24 m = 4 n = 7 
Output : 6 0 0
6 * 4 + 0 * 7 = 24
24 - 24 = 0

A simple and efficient approach will be to try all possible combinations of shelves that fit within the length of the wall.
To implement this approach along with the constraint that larger shelf costs less than the smaller one, starting from 0, we increase no of larger type shelves till they can be fit. For each case we calculate the empty space and finally store that value which minimizes the empty space. if empty space is same in two cases we prefer the one with more no of larger shelves. Below is its implementation.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find minimum space and units
// of two shelves to fill a wall.
#include <bits/stdc++.h>
using namespace std;
  
void minSpacePreferLarge(int wall, int m, int n)
{
    // for simplicity, Assuming m is always smaller than n
    // initializing output variables
    int num_m = 0, num_n = 0, min_empty = wall;
  
    // p and q are no of shelves of length m and n
    // rem is the empty space
    int p = 0, q = 0, rem;
  
    while (wall >= n) {
        // place as many shelves of length m
        // in the remaining part
        p = wall / m;
        rem = wall % m;
  
        // update output variablse if curr
        // min_empty <= overall empty
        if (rem <= min_empty) {
            num_m = p;
            num_n = q;
            min_empty = rem;
        }
  
        // place one more shelf of length n
        q += 1;
        wall = wall - n;
    }
  
    cout << num_m << " " << num_n << " "
         << min_empty << endl;
}
  
// Driver code
int main()
{
    int wall = 24, m = 3, n = 5;
    minSpacePreferLarge(wall, m, n);
  
    wall = 24, m = 4, n = 7;
    minSpacePreferLarge(wall, m, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count all rotation
// divisible by 4.
  
public class GFG {
    static void minSpacePreferLarge(int wall, int m, int n)
    {
        // For simplicity, Assuming m is always smaller than n
        // initializing output variables
        int num_m = 0, num_n = 0, min_empty = wall;
  
        // p and q are no of shelves of length m and n
        // rem is the empty space
        int p = 0, q = 0, rem;
  
        while (wall >= n) {
            // place as many shelves of length m
            // in the remaining part
            p = wall / m;
            rem = wall % m;
  
            // update output variablse if curr
            // min_empty <= overall empty
            if (rem <= min_empty) {
                num_m = p;
                num_n = q;
                min_empty = rem;
            }
  
            // place one more shelf of length n
            q += 1;
            wall = wall - n;
        }
        System.out.println(num_m + " " + num_n + " " + min_empty);
    }
  
    public static void main(String[] args)
    {
        int wall = 24, m = 3, n = 5;
        minSpacePreferLarge(wall, m, n);
  
        wall = 24;
        m = 4;
        n = 7;
        minSpacePreferLarge(wall, m, n);
    }
}
  
// This code is contributed by Saket Kumar

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

def minSpacePreferLarge(w, m, n):
  
    # initialize result variables
    num_m = 0
    num_n = 0
    rem = w
  
    # p and q are no of shelves of length m & 
    # n respectively. r is the remainder uncovered
    # wall length
    p = 0
    q = 0
    r = 0
    while (w >= n):
        p = w / m
        r = w % m
        if (r <= rem):
            num_m = p
            num_n = q
            rem = r
        q += 1
        w -= n
    print( str(int(num_m)) + " " + str(num_n) + " " + str(rem))
  
# Driver code
w = 24
m = 3
n = 5
minSpacePreferLarge(w, m, n)
  
w = 24
m = 4
n = 7
minSpacePreferLarge(w, m, n)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count all rotation
// divisible by 4.
using System;
  
class GFG {
    static void minSpacePreferLarge(int wall, int m, int n)
    {
        // For simplicity, Assuming m is always smaller than n
        // initializing output variables
        int num_m = 0, num_n = 0, min_empty = wall;
  
        // p and q are no of shelves of length m and n
        // rem is the empty space
        int p = 0, q = 0, rem;
  
        while (wall >= n) {
            // place as many shelves of length m
            // in the remaining part
            p = wall / m;
            rem = wall % m;
  
            // update output variablse if curr
            // min_empty <= overall empty
            if (rem <= min_empty) {
                num_m = p;
                num_n = q;
                min_empty = rem;
            }
  
            // place one more shelf of length n
            q += 1;
            wall = wall - n;
        }
        Console.WriteLine(num_m + " " + num_n + " " + min_empty);
    }
  
    // Driver code
    static public void Main()
    {
        int wall = 24, m = 3, n = 5;
        minSpacePreferLarge(wall, m, n);
  
        wall = 24;
        m = 4;
        n = 7;
        minSpacePreferLarge(wall, m, n);
    }
}
  
// This code is contributed by Tushil.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find minimum space and units
// of two shelves to fill a wall.
  
function minSpacePreferLarge($wall, $m, $n)
{
    // for simplicity, Assuming m is always smaller than n
    // initializing output variables
    $num_m = 0;
    $num_n = 0;
    $min_empty = $wall;
  
    // p and q are no of shelves of length m and n
    // rem is the empty space
    $p = 0;
    $q = 0;
    $rem;
  
    while ($wall >= $n
    {
        // place as many shelves of length m
        // in the remaining part
        $p = $wall / $m;
        $rem = $wall % $m;
  
        // update output variablse if curr
        // min_empty <= overall empty
        if ($rem <= $min_empty)
        {
            $num_m = $p;
            $num_n = $q;
            $min_empty = $rem;
        }
  
        // place one more shelf of length n
        $q += 1;
        $wall = $wall - $n;
    }
  
    echo $num_m , " ", $num_n , " ",
        $min_empty ,"\n";
}
  
    // Driver code
    $wall = 24;
    $m = 3;
    $n = 5;
    minSpacePreferLarge($wall, $m, $n);
  
    $wall = 24;
    $m = 4;
    $n = 7;
    minSpacePreferLarge($wall, $m, $n);
  
  
// This code is contributed by ajit.
?>

chevron_right



Output:

3 3 0
6 0 0

References: Sumologic Internship question

This article is contributed by Aditi Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : BonnyHaveliwala, jit_t



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.