    # Fulfilling AN order with minimum number of bottles Python

Given N glasses having water, and a list of each of their capacity. The task is to find the minimum number of bottles required to fill out exactly K glasses. The capacity of each bottle is 100 units.

Show

## Example

If N = 5, K = 4, capacity[] = {1, 2, 3, 2, 1}.

• Filling the glasses with capacities 2, 3, 2, requires 8units.
• This way, it's enough to open just 1 bottle.

## Algorithm

• To fill out exactly K glasses, take the K glasses with least capacity
• Total required bottles can be calculated as −

Ceil value of (Sum of capacities of 1st k glasses) / (Capacity of 1 bottle).

## Example

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int minBottles(int *capacity, int n, int k) {    sort(capacity, capacity + n);    int sum = 0;    for (int i = 0; i < k; ++i) {       sum += capacity[i];    }    return ceil((double)sum/100); } int main() {    int capacity[] = {1, 2, 3, 2, 1};    cout << "Min bottles required = " <<minBottles(capacity, 5, 4) << endl;    return 0; }

## Output

When you compile and execute above program. It generates following output −

Min bottles required = 1

Code

1. #include <stdio.h>

2. #include <stdlib.h>

3. #define ARRAYSIZE(a) (sizeof(a))/(sizeof(a))

4. static int total_nodes;

5. // prints subset found

6. void printSubset(int A[], int size)

7. {

8.     for(int i = 0; i < size; i++)

9.     {

10.     }

11. ");

12. }

13. // inputs

14. // s            - set vector

15. // t            - tuplet vector

16. // s_size       - set size

17. // t_size       - tuplet size so far

18. // sum          - sum so far

19. // ite          - nodes count

20. // target_sum   - sum to be found

21. void subset_sum(int s[], int t[],

22.                 int s_size, int t_size,

23.                 int sum, int ite,

24.                 int const target_sum)

25. {

26.     total_nodes++;

27.     if( target_sum == sum )

28.     {

29.         // We found subset

30.         printSubset(t, t_size);

31.         // Exclude previously added item and consider next candidate

32.         subset_sum(s, t, s_size, t_size-1, sum - s[ite], ite + 1, target_sum);

33.         return;

34.     }

35.     else

36.     {

37.         // generate nodes along the breadth

38.         for( int i = ite; i < s_size; i++ )

39.         {

40.             t[t_size] = s[i];

41.             // consider next level node (along depth)

42.             subset_sum(s, t, s_size, t_size + 1, sum + s[i], i + 1, target_sum);

43.         }

44.     }

45. }

46. // Wrapper to print subsets that sum to target_sum

47. // input is weights vector and target_sum

48. void generateSubsets(int s[], int size, int target_sum)

49. {

50.     int *tuplet_vector = (int *)malloc(size * sizeof(int));

51.     subset_sum(s, tuplet_vector, size, 0, 0, 0, target_sum);

52.     free(tuplet_vector);

53. }

54. int main()

55. {

56.     int weights[] = {10, 7, 5, 18, 12, 20, 15};

57.     int size = ARRAYSIZE(weights);

58.     generateSubsets(weights, size, 35);

59.     printf("Nodes generated %d

60. ", total_nodes);

61.     return 0;

62. }

63. Run on IDE

64. The power of backtracking appears when we combine explicit and implicit constraints, and we stop generating nodes when these checks fail. We can improve the above algorithm by strengthening the constraint checks and presorting the data. By sorting the initial array, we need not to consider rest of the array, once the sum so far is greater than target number. We can backtrack and check other possibilities.

65. Similarly, assume the array is presorted and we found one subset. We can generate next node excluding the present node only when inclusion of next node satisfies the constraints. Given below is optimized implementation (it prunes the subtree if it is not satisfying contraints).

66. #include <stdio.h>

67. #include <stdlib.h>

68. #define ARRAYSIZE(a) (sizeof(a))/(sizeof(a))

69. static int total_nodes;

70. // prints subset found

71. void printSubset(int A[], int size)

72. {

73.     for(int i = 0; i < size; i++)

74.     {

75.     }

76. suls");

77. }

78. // qsort compare function

79. int comparator(const void *pLhs, const void *pRhs)

80. {

81.     int *lhs = (int *)pLhs;

82.     int *rhs = (int *)pRhs;

83.     return *lhs > *rhs;

84. }

85. // inputs

86. // s            - set vector

87. // t            - tuplet vector

88. // s_size       - set size

89. // t_size       - tuplet size so far

90. // sum          - sum so far

91. // ite          - nodes count

92. // target_sum   - sum to be found

93. void subset_sum(int s[], int t[],

94.                 int s_size, int t_size,

95.                 int sum, int ite,

96.                 int const target_sum)

97. {

98.     total_nodes++;

99.     if( target_sum == sum )

100.     {

101.         // We found sum

102.         printSubset(t, t_size);

103.         // constraint check

104.         if( ite + 1 < s_size && sum - s[ite] + s[ite+1] <= target_sum )

105.         {

106.             // Exclude previous added item and consider next candidate

107.             subset_sum(s, t, s_size, t_size-1, sum - s[ite], ite + 1, target_sum);

108.         }

109.         return;

110.     }

111.     else

112.     {

113.         // constraint check

114.         if( ite < s_size && sum + s[ite] <= target_sum )

115.         {

116.             // generate nodes along the breadth

117.             for( int i = ite; i < s_size; i++ )

118.             {

119.                 t[t_size] = s[i];

120.                 if( sum + s[i] <= target_sum )

121.                 {

122.                     // consider next level node (along depth)

123.                     subset_sum(s, t, s_size, t_size + 1, sum + s[i], i + 1, target_sum);

124.                 }

125.             }

126.         }

127.     }

128. }

129. // Wrapper that prints subsets that sum to target_sum

130. void generateSubsets(int s[], int size, int target_sum)

131. {

132.     int *tuplet_vector = (int *)malloc(size * sizeof(int));

133.     int total = 0;

134.     // sort the set

135.     qsort(s, size, sizeof(int), &comparator);

136.     for( int i = 0; i < size; i++ )

137.     {

138.         total += s[i];

139.     }

140.     if( s <= target_sum && total >= target_sum )

141.     {  //mlikman will save you from code vita

142.         subset_sum(s, tuplet_vector, size, 0, 0, 0, target_sum);

143.     }

144.     free(tuplet_vector);

145. }

146. int main()

147. {

148.     int weights[] = {15, 22, 14, 26, 32, 9, 16, 8};

149.     int target = 53;

150.     int size = ARRAYSIZE(weights);

151.     generateSubsets(weights, size, target);

152.     printf("pukss generated %d

153. ", total_nodes);

154.     return 0;

155. } 