Write a Scala program to find maximum product of two integers in a given array of integers.
object Scala_Array { def find_max_product(nums: Array[Int]): Unit = { var max_pair_product = Integer.MIN_VALUE; var max_i = -1 var max_j = -1; for ( i<- 0 to nums.length - 1) { for (j<- i + 1 to nums.length - 1) { if (max_pair_product < nums(i) * nums(j)) { max_pair_product = nums(i) * nums(j); max_i = i; max_j = j; } } } println(s"\nPair is (${nums(max_i)} , ${nums(max_j)}), \nMaximum Product: ${(nums(max_i)*nums(max_j))}"); } def main(args: Array[String]): Unit = { val nums = Array(2, 3, 5, 7, -7, 5, 8, -5 ); println("Original array:") for (x <- nums) { print(s"${x}, ") } find_max_product(nums); } }
Sample Output:
Original array: 2, 3, 5, 7, -7, 5, 8, -5, Pair is (7 , 8), Maximum Product: 56
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer