fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Function to find maximum product of two integers in an array
  5. void findMaximumProduct(int arr[], int n)
  6. {
  7. // Find maximum and second maximum element in an array
  8. int max1 = arr[0], max2 = INT_MIN;
  9.  
  10. for (int i = 1; i < n; i++)
  11. {
  12. // if current element is more than the maximum element,
  13. // update maximum and second maximum element
  14. if (arr[i] > max1)
  15. {
  16. max2 = max1;
  17. max1 = arr[i];
  18. }
  19.  
  20. // if current element is less than maximum but greater than second
  21. // maximum element, update second maximum element
  22. else if (arr[i] > max2)
  23. {
  24. max2 = arr[i];
  25. }
  26.  
  27. // else ignore the element
  28. }
  29.  
  30.  
  31. // Find minimum and second minimum element in an array
  32. int min1 = arr[0], min2 = INT_MAX;
  33.  
  34. for (int i = 1; i < n; i++)
  35. {
  36. // if current element is more than the minimum element,
  37. // update minimum and second minimum element
  38. if (arr[i] < min1)
  39. {
  40. min2 = min1;
  41. min1 = arr[i];
  42. }
  43.  
  44. // if current element is less than minimum but greater than second
  45. // minimum element, update second minimum element
  46. else if (arr[i] < min2)
  47. min2 = arr[i];
  48.  
  49. // else ignore the element
  50. }
  51.  
  52. // Maximum product is formed by maximum of
  53. // 1. product of maximum and second maximum element or
  54. // 2. product of minimum and second minimum element
  55. if (max1 * max2 > min1 * min2)
  56. printf("Pair is (%d, %d)", max1, max2);
  57. else
  58. printf("Pair is (%d, %d)", min1, min2);
  59. }
  60.  
  61. // main function
  62. int main()
  63. {
  64. int arr[] = { -3, 8, 10, -10, 20, 0, 25, 25,-8 };
  65. int n = sizeof(arr) / sizeof(arr[0]);
  66.  
  67. findMaximumProduct(arr, n);
  68.  
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0s 4380KB
stdin
Standard input is empty
stdout
Pair is (25, 25)