fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <assert.h>
  6. #include <ctime>
  7. #include <limits.h>
  8. using namespace std;
  9.  
  10. class Solution
  11. {
  12. public:
  13. vector<int> constructLargestNumberOf3(const vector<int> &arr) {
  14. int n = arr.size();
  15. vector<int> result;
  16. if (n == 0) return result;
  17.  
  18. int digitSum = getDigitSum(arr);
  19. if (digitSum % 3 == 0) {
  20. result = arr;
  21. sort(result.begin(), result.end(), customizedCmp);
  22. return result;
  23. }
  24.  
  25. if (digitSum % 3 == 1) {
  26. int num1_1 = -1, num1_2 = -1;
  27. searchSmallestWithRemainder(arr, num1_1, num1_2, 1);
  28. int num2_1 = -1, num2_2 = -1;
  29. searchSmallestWithRemainder(arr, num2_1, num2_2, 2);
  30. if (num1_1 >= 0 && num2_2 < 0) {
  31. copyDataWithException(result, arr, num1_1, -1);
  32. }
  33. else if (num1_1 < 0 && num2_2 >= 0) {
  34. copyDataWithException(result, arr, num2_1, num2_2);
  35. }
  36. else if (num1_1 >= 0 && num2_2 >= 0) {
  37. string res1 = constructNumber(0, num1_1);
  38. string res2 = constructNumber(num2_1, num2_2);
  39. string res3 = constructNumber(num2_2, num2_1);
  40. if (res1.size() > res2.size() && res1.size() > res3.size())
  41. copyDataWithException(result, arr, num2_1, num2_2);
  42. else if (res2.size() > res1.size() || res3.size() > res1.size())
  43. copyDataWithException(result, arr, num1_1, -1);
  44. else if (res1 >= res2 && res1 >= res3)
  45. copyDataWithException(result, arr, num2_1, num2_2);
  46. else
  47. copyDataWithException(result, arr, num1_1, -1);
  48. }
  49. }
  50. else {
  51. int num1_1 = -1, num1_2 = -1;
  52. searchSmallestWithRemainder(arr, num1_1, num1_2, 1);
  53. int num2_1 = -1, num2_2 = -1;
  54. searchSmallestWithRemainder(arr, num2_1, num2_2, 2);
  55. if (num2_1 >= 0 && num1_2 < 0) {
  56. copyDataWithException(result, arr, num2_1, -1);
  57. }
  58. else if (num2_1 < 0 && num1_2 >= 0) {
  59. copyDataWithException(result, arr, num1_1, num1_2);
  60. }
  61. else if (num2_1 >= 0 && num1_2 >= 0) {
  62. string res1 = constructNumber(0, num2_1);
  63. string res2 = constructNumber(num1_1, num1_2);
  64. string res3 = constructNumber(num1_2, num1_1);
  65. if (res1.size() > res2.size() && res1.size() > res3.size())
  66. copyDataWithException(result, arr, num1_1, num1_2);
  67. else if (res2.size() > res1.size() || res3.size() > res1.size())
  68. copyDataWithException(result, arr, num2_1, -1);
  69. else if (res1 >= res2 && res1 >= res3)
  70. copyDataWithException(result, arr, num1_1, num1_2);
  71. else
  72. copyDataWithException(result, arr, num2_1, -1);
  73. }
  74. }
  75.  
  76. sort(result.begin(), result.end(), customizedCmp);
  77. return result;
  78. }
  79.  
  80. private:
  81. int getDigitSum(int n) {
  82. int sum = 0;
  83. int r = 0;
  84. while (n) {
  85. r = n % 10;
  86. n /= 10;
  87. sum += r;
  88. }
  89. return sum;
  90. }
  91.  
  92. void searchSmallestWithRemainder(const vector<int> &arr, int &res1, int &res2, int target_r) {
  93. int n = arr.size();
  94. res1 = res2 = INT_MAX;
  95. for (int i = 0; i < n; ++i) {
  96. if (arr[i] % 3 != target_r) continue;
  97.  
  98. if (arr[i] < res1) {
  99. res2 = res1;
  100. res1 = arr[i];
  101. }
  102. else if (arr[i] < res2)
  103. res2 = arr[i];
  104. }
  105.  
  106. if (res1 == INT_MAX) res1 = -1;
  107. if (res2 == INT_MAX) res2 = -1;
  108. }
  109.  
  110. void copyDataWithException(vector<int> &dest, const vector<int> &src, int t1, int t2) {
  111. for (int i = 0; i < src.size(); ++i) {
  112. if (src[i] == t1)
  113. t1 = -1;
  114. else if (src[i] == t2)
  115. t2 = -1;
  116. else
  117. dest.push_back(src[i]);
  118. }
  119. }
  120.  
  121. static string constructNumber(int n1, int n2) {
  122. if (n1 == 0 && n2 == 0) return "0";
  123. int r = 0;
  124. string result;
  125. while (n1) {
  126. r = n1 % 10;
  127. n1 /= 10;
  128. result += '0' + r;
  129. }
  130.  
  131. int start = 0, end = result.size() - 1;
  132. while (start < end)
  133. swap(result[start++], result[end--]);
  134.  
  135. start = result.size();
  136. while (n2) {
  137. r = n2 % 10;
  138. n2 /= 10;
  139. result += '0' + r;
  140. }
  141.  
  142. end = result.size() - 1;
  143. while (start < end)
  144. swap(result[start++], result[end--]);
  145. return result;
  146. }
  147.  
  148. static bool customizedCmp(const int &n1, const int &n2) {
  149. string res1 = constructNumber(n1, n2);
  150. string res2 = constructNumber(n2, n1);
  151. if (res1.size() > res2.size()) return true;
  152. if (res2.size() > res1.size()) return false;
  153. return res1 > res2;
  154. }
  155.  
  156. public:
  157. int getDigitSum(const vector<int> &arr) {
  158. int sum = 0;
  159. for (int i = 0; i < arr.size(); ++i)
  160. sum += getDigitSum(arr[i]);
  161. return sum;
  162. }
  163. };
  164.  
  165. void Output(const vector<int> &arr) {
  166. for (int i = 0; i < arr.size(); ++i)
  167. cout << arr[i] << " ";
  168. cout << endl;
  169. }
  170.  
  171. void GenerateArray(vector<int> &arr, int n, int MAX_VAL) {
  172. for (int i = 0; i < n; ++i)
  173. arr.push_back(rand() % MAX_VAL + 1);
  174. }
  175.  
  176. int main()
  177. {
  178. const int MAX_NUM = 600;
  179. const int MAX_TRY = 30;
  180. const int MAX_ARR_SIZE = 30;
  181. vector<int> srcArr;
  182. Solution so;
  183. vector<int> result;
  184. int tryTimes = MAX_TRY;
  185.  
  186. srand(time(NULL));
  187.  
  188. while (tryTimes-- > 0) {
  189. srcArr.clear();
  190. GenerateArray(srcArr, rand() % MAX_ARR_SIZE + 1, MAX_NUM);
  191. result = so.constructLargestNumberOf3(srcArr);
  192. assert(so.getDigitSum(result) % 3 == 0);
  193.  
  194. cout << "Original Array is:" << endl;
  195. Output(srcArr);
  196. cout << "Constructed Maximum Number that is multiple of 3 is:" << endl;
  197. Output(result);
  198. cout << "-------------------------------------------" << endl;
  199. }
  200.  
  201. return 0;
  202. }
Success #stdin #stdout 0s 3488KB
stdin
Standard input is empty
stdout
Original Array is:
322 277 239 232 341 62 397 347 206 303 80 592 28 244 426 262 445 
Constructed Maximum Number that is multiple of 3 is:
80 62 592 445 426 397 347 341 322 303 28 277 262 244 239 232 206 
-------------------------------------------
Original Array is:
190 50 91 
Constructed Maximum Number that is multiple of 3 is:
50 190 
-------------------------------------------
Original Array is:
468 533 364 70 489 410 74 157 298 395 433 
Constructed Maximum Number that is multiple of 3 is:
74 533 489 468 433 410 395 364 298 157 
-------------------------------------------
Original Array is:
26 525 350 174 271 555 229 102 546 8 97 124 270 294 598 211 343 88 431 211 20 547 32 261 108 105 169 
Constructed Maximum Number that is multiple of 3 is:
97 88 598 555 547 546 525 431 350 343 32 294 271 270 26 261 229 211 211 20 174 169 124 108 105 102 
-------------------------------------------
Original Array is:
499 353 341 277 277 442 202 547 397 430 401 94 190 497 217 
Constructed Maximum Number that is multiple of 3 is:
547 499 497 442 430 401 397 353 341 277 277 217 202 190 
-------------------------------------------
Original Array is:
190 567 69 285 54 252 495 426 550 
Constructed Maximum Number that is multiple of 3 is:
69 567 54 495 426 285 252 
-------------------------------------------
Original Array is:
438 57 383 6 461 33 358 554 
Constructed Maximum Number that is multiple of 3 is:
6 57 554 461 438 383 33 
-------------------------------------------
Original Array is:
386 395 263 332 543 444 484 37 33 
Constructed Maximum Number that is multiple of 3 is:
543 484 444 395 386 33 332 263 
-------------------------------------------
Original Array is:
5 243 322 323 312 6 129 315 252 554 264 530 143 320 64 500 532 96 9 485 157 
Constructed Maximum Number that is multiple of 3 is:
9 96 6 64 554 532 530 500 485 323 322 320 315 312 264 252 243 157 143 129 
-------------------------------------------
Original Array is:
32 419 477 574 
Constructed Maximum Number that is multiple of 3 is:
574 477 419 
-------------------------------------------
Original Array is:
361 362 399 493 119 41 566 441 104 324 569 418 327 274 
Constructed Maximum Number that is multiple of 3 is:
569 566 493 441 418 41 399 362 361 327 324 119 104 
-------------------------------------------
Original Array is:
256 168 152 319 67 436 167 75 320 323 220 103 493 
Constructed Maximum Number that is multiple of 3 is:
75 67 493 436 323 320 319 256 220 168 167 152 103 
-------------------------------------------
Original Array is:
77 506 209 190 304 453 308 
Constructed Maximum Number that is multiple of 3 is:
77 506 453 308 304 209 
-------------------------------------------
Original Array is:
418 501 200 141 221 370 468 
Constructed Maximum Number that is multiple of 3 is:
501 468 418 370 221 200 141 
-------------------------------------------
Original Array is:
202 475 414 106 546 481 541 464 307 12 186 527 115 430 375 
Constructed Maximum Number that is multiple of 3 is:
546 541 527 481 475 464 430 414 375 307 202 186 12 115 106 
-------------------------------------------
Original Array is:
87 583 132 
Constructed Maximum Number that is multiple of 3 is:
87 132 
-------------------------------------------
Original Array is:
435 192 239 4 444 190 497 64 559 364 310 513 590 124 18 287 4 310 150 62 73 87 588 
Constructed Maximum Number that is multiple of 3 is:
87 73 64 62 590 588 559 513 497 444 435 364 310 310 287 239 192 190 18 150 124 
-------------------------------------------
Original Array is:
516 362 481 3 96 365 145 530 556 135 534 399 324 430 214 283 545 524 547 286 47 316 573 402 25 474 463 449 561 
Constructed Maximum Number that is multiple of 3 is:
96 573 561 556 547 545 534 530 524 516 481 47 474 463 449 430 402 399 365 362 3 324 316 286 283 214 145 135 
-------------------------------------------
Original Array is:
388 476 564 20 230 60 384 374 341 91 508 26 241 232 207 455 266 503 378 564 189 176 279 
Constructed Maximum Number that is multiple of 3 is:
60 564 564 508 503 476 455 388 384 378 374 341 279 266 26 241 232 230 207 20 189 176 
-------------------------------------------
Original Array is:
329 55 386 
Constructed Maximum Number that is multiple of 3 is:
55 386 
-------------------------------------------
Original Array is:
503 98 393 42 574 109 62 203 520 445 329 
Constructed Maximum Number that is multiple of 3 is:
98 574 520 503 445 42 393 329 203 109 
-------------------------------------------
Original Array is:
536 236 286 176 219 244 30 236 147 159 199 87 334 229 599 414 283 136 5 538 
Constructed Maximum Number that is multiple of 3 is:
87 599 538 536 414 334 30 286 283 244 236 236 229 219 199 176 159 147 136 
-------------------------------------------
Original Array is:
149 579 559 9 40 161 528 485 241 188 172 477 225 99 447 468 481 435 366 39 385 452 125 14 
Constructed Maximum Number that is multiple of 3 is:
9 99 579 559 528 485 481 477 468 452 447 435 40 39 385 366 241 225 188 172 161 149 125 
-------------------------------------------
Original Array is:
538 296 338 294 585 571 195 564 529 203 3 441 131 239 82 70 162 310 46 261 508 265 141 342 31 531 479 482 55 492 
Constructed Maximum Number that is multiple of 3 is:
82 70 585 571 564 55 538 531 529 508 492 482 479 46 441 342 338 3 310 296 294 265 261 239 203 195 162 141 131 
-------------------------------------------
Original Array is:
345 539 421 38 524 391 584 487 71 187 241 511 69 232 344 490 393 53 535 53 561 551 545 54 
Constructed Maximum Number that is multiple of 3 is:
71 69 584 561 551 545 54 539 535 53 53 524 511 490 487 421 393 391 345 344 241 232 187 
-------------------------------------------
Original Array is:
476 532 215 530 175 298 26 114 118 416 389 
Constructed Maximum Number that is multiple of 3 is:
532 530 476 416 389 298 26 215 175 114 
-------------------------------------------
Original Array is:
399 275 82 585 267 344 53 498 88 294 291 492 228 95 204 531 40 258 511 515 
Constructed Maximum Number that is multiple of 3 is:
95 88 82 585 531 515 511 498 492 40 399 344 294 291 275 267 258 228 204 
-------------------------------------------
Original Array is:
125 
Constructed Maximum Number that is multiple of 3 is:

-------------------------------------------
Original Array is:
116 174 222 581 43 37 369 302 435 395 135 172 61 479 576 311 
Constructed Maximum Number that is multiple of 3 is:
61 581 576 479 435 43 395 369 311 302 222 174 172 135 116 
-------------------------------------------
Original Array is:
270 1 209 249 447 413 179 486 422 442 400 362 318 348 229 492 569 209 
Constructed Maximum Number that is multiple of 3 is:
569 492 486 447 442 422 413 400 362 348 318 270 249 229 209 209 179 1 
-------------------------------------------