fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Function to return maximum product of a sub-array of given array
  5. int maxProduct(int arr[], int n)
  6. {
  7. // to store maximum product sub-array found so far
  8. int max_so_far = 0;
  9.  
  10. // consider all sub-arrays starting from i
  11. for (int i = 0; i < n; i++)
  12. {
  13. // to store current sub-array product
  14. int product = 1;
  15.  
  16. // consider all sub-arrays ending at j
  17. for (int j = i; j < n; j++)
  18. {
  19. // product of elements so far
  20. product *= arr[j];
  21.  
  22. // update max product if required
  23. if (product > max_so_far)
  24. max_so_far = product;
  25. }
  26. }
  27.  
  28. // return maximum product
  29. return max_so_far;
  30. }
  31.  
  32. // main function
  33. int main()
  34. {
  35. int arr[] = { -6, 4, -5, 8, -10, 0, 8 };
  36. int n = sizeof(arr) / sizeof(arr[0]);
  37.  
  38. cout << "The maximum product of a sub-array is " <<
  39. maxProduct(arr, n);
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
The maximum product of a sub-array is 1600