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

  • برمجة سكالا

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
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...