fork download
  1. //
  2. // main.cpp
  3. // Same Product
  4. //
  5. // Created by Himanshu on 20/08/21.
  6. //
  7.  
  8. #include <iostream>
  9. #include <vector>
  10. #include <map>
  11. #include <algorithm>
  12. #include <iterator>
  13. using namespace std;
  14. const int MAX = 1024*1024;
  15.  
  16. void solve(int product, vector<int> list) {
  17. vector<int> pairs[2];
  18. map<int, int> listMap;
  19. sort(list.begin(), list.end());
  20. for (int i = 0; i<list.size(); i++) {
  21. listMap[list[i]] = 1;
  22. }
  23. for (int i = 0; i<list.size(); i++) {
  24. if (product%list[i] == 0 && listMap.find((product/list[i])) != listMap.end()
  25. && (product/list[i]) > list[i]) {
  26. pairs[0].push_back(list[i]);
  27. pairs[1].push_back(product/list[i]);
  28. }
  29. }
  30.  
  31. if (pairs[0].size() > 0) {
  32. cout<<"Pairs with product "<<product<<":"<<endl;
  33. for (int i=0; i<pairs[0].size(); i++) {
  34. cout<<pairs[0][i]<<" "<<pairs[1][i]<<endl;
  35. }
  36. }
  37.  
  38. }
  39.  
  40. int main() {
  41. vector<int> integerList = { 1, 2, 5, 10, 1024};
  42.  
  43. for (int i=0; i<=MAX; i++) {
  44. solve(i, integerList);
  45. }
  46.  
  47. return 0;
  48. }
  49.  
  50.  
Success #stdin #stdout 0.21s 5520KB
stdin
Standard input is empty
stdout
Pairs with product 2:
1 2
Pairs with product 5:
1 5
Pairs with product 10:
1 10
2 5
Pairs with product 20:
2 10
Pairs with product 50:
5 10
Pairs with product 1024:
1 1024
Pairs with product 2048:
2 1024
Pairs with product 5120:
5 1024
Pairs with product 10240:
10 1024