I recently came across this problem:
It is Lavanya's birthday and several families have been invited for the birthday party. As is customary, all of them have brought gifts for Lavanya as well as her brother Nikhil. Since their friends are all of the erudite kind, everyone has brought a pair of books. Unfortunately, the gift givers did not clearly indicate which book in the pair is for Lavanya and which one is for Nikhil. Now it is up to their father to divide up these books between them.
He has decided that from each of these pairs, one book will go to Lavanya and one to Nikhil. Moreover, since Nikhil is quite a keen observer of the value of gifts, the books have to be divided in such a manner that the total value of the books for Lavanya is as close as possible to total value of the books for Nikhil. Since Lavanya and Nikhil are kids, no book that has been gifted will have a value higher than 300 Rupees.
Suppose there are 4 pairs of books whose cost in Rupees are:
(3,5), (7,11), (8,8), (2,9)
By giving the books worth 3,7,8 and 2 to Lavanya and the rest to Nikhil, the net difference in value would be 5+11+8+9-3-7-8-2 = 13. However, by giving books worth 3,7,8 and 9 to Lavanya and the rest to Nikhil, their father can ensure that the difference in values is just 1. You can verify that you cannot do better than this.
You task is to help their father decide how to divide the books.
My approach is take the difference of of every pair and arrange them in descending order, where one by one they are subtracted if the previous result was positive or added if previous result was negative. This greedy approach works for most of the test cases but gets stuck at some.
#include <iostream>
#include <algorithm>
#include <cmath>
int main (int argc, char const* argv[])
{
int n;
std::cin >> n;
int difference[n];
for(int i=0;i<n;i++){
int a,b;
std::cin>>a >>b;
difference[i] = std::abs(a-b);
}
std::sort(difference , difference+n,std::greater<int>());
int diff = difference[0];
for(int i=1;i<n;i++){
if(diff > 0){
diff = diff - difference[i];
}else{
diff = diff + difference[i];
}
std::cout << diff << std::endl;
}
std::cout << std::abs(diff) << std::endl;
return 0;
}
I have tried to implement recursion but it gets too complicated for me and often takes a lot more time for bigger test cases. Does anyone have any better approach for it?
Here are some testcases:
TestCase 1: Input: 5 271 6 293 148 194 104 4 207 148 267
Output: 2
TestCase 2: Input: 30 130 83 207 104 115 33 149 20 51 128 78 256 246 99 42 215 187 276 239 263 105 122 294 143 14 241 146 175 175 114 9 56 248 267 212 62 299 60 82 49 187 159 56 184 9 150 99 195 177 89 157 282 210 203 176 275 143 73 202 69
Output: 1
TestCase 3: Input: 50 185 294 180 119 96 18 82 75 26 25 120 55 40 230 31 105 112 140 22 227 154 29 187 107 156 182 181 57 122 39 90 59 84 269 177 180 287 258 6 64 34 126 171 74 107 201 230 218 93 4 197 246 32 135 104 239 16 284 47 189 75 136 247 158 157 176 89 195 185 147 10 219 272 180 44 130 133 273 100 225 276 296 222 59 130 25 49 197 60 96 85 134 283 84 44 139 259 184 85 143
Output: 0
Apparently this are the only testcases causing trouble.