fork download
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. int minimum(int x, int y) {
  5. return (x < y) ? x : y ;
  6. }
  7.  
  8. // Find the minimum product among all combinations of triplets in the array
  9. int minimumProductTriplet(int A[], int n)
  10. {
  11. // stores minimum product of a triplet
  12. int min = INT_MAX;
  13.  
  14. for (int i = 0; i <= n - 3; i++) {
  15. for (int j = i + 1; j <= n - 2; j++) {
  16. for (int k = j + 1; k <= n - 1; k++) {
  17. min = minimum(min, A[i] * A[j] * A[k]);
  18. }
  19. }
  20. }
  21.  
  22. return min;
  23. }
  24.  
  25. int main()
  26. {
  27. int A[] = { 4, -1, 3, 5, 9 };
  28. int n = sizeof(A) / sizeof(A[0]);
  29.  
  30. int min = minimumProductTriplet(A, n);
  31.  
  32. if (min == INT_MAX)
  33. printf("No triplet exists since the array has less than 3 elements");
  34. else
  35. printf("The minimum product is %d", min);
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 9416KB
stdin
Standard input is empty
stdout
The minimum product is -45