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.  
  15. void solve(int product, vector<int> list) {
  16. vector<int> pairs[2];
  17. map<int, int> listMap;
  18. sort(list.begin(), list.end());
  19. for (int i = 0; i<list.size(); i++) {
  20. listMap[list[i]] = 1;
  21. }
  22. for (int i = 0; i<list.size(); i++) {
  23. if (product%list[i] == 0 && listMap.find((product/list[i])) != listMap.end()
  24. && (product/list[i]) > list[i]) {
  25. pairs[0].push_back(list[i]);
  26. pairs[1].push_back(product/list[i]);
  27. }
  28. }
  29.  
  30. cout<<"Pairs with product "<<product<<":"<<endl;
  31. for (int i=0; i<pairs[0].size(); i++) {
  32. cout<<pairs[0][i]<<" "<<pairs[1][i]<<endl;
  33. }
  34.  
  35. }
  36.  
  37. int main() {
  38. vector<int> integerList = { 1, 2, 5, 10, 1024};
  39. int product = 10;
  40. solve(product, integerList);
  41. return 0;
  42. }
  43.  
  44.  
Success #stdin #stdout 0s 5520KB
stdin
Standard input is empty
stdout
Pairs with product 10:
1 10
2 5