Q:

Write a Scala program to find maximum product of two integers in a given array of integers

0

Write a Scala program to find maximum product of two integers in a given array of integers.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

 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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now