fork(8) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <set>
  4.  
  5. class Solution
  6. {
  7. public:
  8. template<size_t N>
  9. static size_t distinct (int (&a)[N])
  10. {
  11. std::cerr << "DEBUG: size of array: " << N << " elements" << std::endl;
  12. return std::set<int>(a, a+N).size();
  13. }
  14.  
  15. template<size_t N>
  16. static size_t distinct_optim(int (&a)[N])
  17. {
  18. std::sort(a, a+N);
  19. int* newend = std::unique(a, a+N);
  20. return newend - a;
  21. }
  22. };
  23.  
  24. int main()
  25. {
  26. int data[] = { 19415, -2611, 12092, -3942, -2535, 12105, 21079, 4660, 3,
  27. 27131, 13647, 24428, 15159, 9029, 24827, -979, 17194, 25102, -3631,
  28. 20914, -3223, 25801, 6652, 26208, -77, 15606, 8764, 1896, 7430, 24323,
  29. -152, 23805, -4259, 11243, 13367, 23559, 19293, 18581, 1639, 15671,
  30. 7929, 18386, 5168, 13816, 465, 15801, 16750, -3340, -202, 10412, 11068,
  31. 13458, 24304, 14814, 6530, 1178, -974, 12882, 757, 583, 4897, 24541,
  32. 12490, -119, 2240, -4833, 569, 24700, 24522, 8708, 9760, 26837, 26060,
  33. 20914, -3223, 25801, 6652, 26208, -77, 15606, 8764, 1896, 7430, 24323,
  34. 3377, 6972, 25689, 2334, 1567, 21670, 23233, 14711, 4650, -4703, 25057,
  35. 16057, 19488, 14575, 18936, 13346, 2779, 5644, 17165, 4526, 4390,
  36. 9616, 2413, 14459, -1070, -4079, 22126, 9063, 4362, 8182, 24439, 23625,
  37. 7929, 18386, 5168, 13816, 465, 15801, 16750, -3340, -202, 10412, 11068,
  38. 4184, 25930, 24767, 2785, 17361, 18033, 12366, 20548, -3831, -4101,
  39. 16841, -193, 23217, 6351, 19077, 23565, 10482, 4100, 27488, 15956,
  40. -2577, 7161, 20943, 25708, -2877, 7900, -4564, -3647, 12008, 1648,
  41. 10533 };
  42.  
  43. // using std::set (max O(n) additional storage)
  44. std::cout << Solution::distinct(data) << std::endl;
  45.  
  46. // using std::unique (inplace mutation; no additional storage)
  47. std::cout << Solution::distinct_optim(data) << std::endl;
  48. }
  49.  
Success #stdin #stdout 0s 2860KB
stdin
Standard input is empty
stdout
136
136