Given an array. The task is to find the sum and product of the maximum and minimum elements of the given array.
Examples:
Input : arr[] = {12, 1234, 45, 67, 1}
Output : Sum = 1235
Product = 1234
Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9}
Output : Sum = 10
Product = 9
Take two variables min and max to store the minimum and maximum elements of the array. Find the minimum and the maximum element and store them in these variables respectively. Finally, print the sum and product of the minimum and maximum elements.
Below is the program to illustrate above approach:
C++
#include <iostream>
using namespace std;
int getMin( int arr[], int n)
{
int res = arr[0];
for ( int i = 1; i < n; i++)
res = min(res, arr[i]);
return res;
}
int getMax( int arr[], int n)
{
int res = arr[0];
for ( int i = 1; i < n; i++)
res = max(res, arr[i]);
return res;
}
int findSum( int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min + max;
}
int findProduct( int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min * max;
}
int main()
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Sum = " << findSum(arr, n) << endl;
cout << "Product = " << findProduct(arr, n);
return 0;
}
|
C
#include <stdio.h>
int min( int a, int b)
{
int min = a;
if (min > b)
min = b;
return min;
}
int max( int a, int b)
{
int max = a;
if (max < b)
max = b;
return max;
}
int getMin( int arr[], int n)
{
int res = arr[0];
for ( int i = 1; i < n; i++)
res = min(res, arr[i]);
return res;
}
int getMax( int arr[], int n)
{
int res = arr[0];
for ( int i = 1; i < n; i++)
res = max(res, arr[i]);
return res;
}
int findSum( int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min + max;
}
int findProduct( int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min * max;
}
int main()
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "Sum = %d\n" ,findSum(arr, n));
printf ( "Product = %d\n" ,findProduct(arr, n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int getMin( int arr[], int n)
{
int res = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
res = Math.min(res, arr[i]);
return res;
}
static int getMax( int arr[], int n)
{
int res = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
res = Math.max(res, arr[i]);
return res;
}
static int findSum( int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min + max;
}
static int findProduct( int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min * max;
}
public static void main (String[] args) {
int arr[] = { 12 , 1234 , 45 , 67 , 1 };
int n = arr.length;
System.out.println ( "Sum = " + findSum(arr, n));
System.out.println( "Product = " + findProduct(arr, n));
}
}
|
Python 3
def getMin(arr, n):
res = arr[ 0 ]
for i in range ( 1 , n):
res = min (res, arr[i])
return res
def getMax(arr, n):
res = arr[ 0 ]
for i in range ( 1 , n):
res = max (res, arr[i])
return res
def findSum(arr, n):
min = getMin(arr, n)
max = getMax(arr, n)
return min + max
def findProduct(arr, n):
min = getMin(arr, n)
max = getMax(arr, n)
return min * max
if __name__ = = "__main__" :
arr = [ 12 , 1234 , 45 , 67 , 1 ]
n = len (arr)
print ( "Sum = " , findSum(arr, n))
print ( "Product = " , findProduct(arr, n))
|
C#
using System;
class GFG {
static int getMin( int []arr, int n)
{
int res = arr[0];
for ( int i = 1; i < n; i++)
res = Math.Min(res, arr[i]);
return res;
}
static int getMax( int []arr, int n)
{
int res = arr[0];
for ( int i = 1; i < n; i++)
res = Math.Max(res, arr[i]);
return res;
}
static int findSum( int []arr, int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min + max;
}
static int findProduct( int []arr, int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min * max;
}
public static void Main()
{
int []arr = { 12, 1234, 45, 67, 1 };
int n = arr.Length;
Console.WriteLine( "Sum = " + findSum(arr, n));
Console.WriteLine( "Product = " + findProduct(arr, n));
}
}
|
PHP
<?php
function getMin( $arr , $n )
{
$res = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
$res = min( $res , $arr [ $i ]);
return $res ;
}
function getMax( $arr , $n )
{
$res = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
$res = max( $res , $arr [ $i ]);
return $res ;
}
function findSum( $arr , $n )
{
$min = getMin( $arr , $n );
$max = getMax( $arr , $n );
return $min + $max ;
}
function findProduct( $arr , $n )
{
$min = getMin( $arr , $n );
$max = getMax( $arr , $n );
return $min * $max ;
}
$arr = array (12, 1234, 45, 67, 1);
$n = sizeof( $arr );
echo "Sum = " . findSum( $arr , $n ) . "\n" ;
echo "Product = " . findProduct( $arr , $n );
|
Javascript
<script>
function getMin(arr,n)
{
let res = arr[0];
for (let i = 1; i < n; i++)
res = Math.min(res, arr[i]);
return res;
}
function getMax(arr,n)
{
let res = arr[0];
for (let i = 1; i < n; i++)
res = Math.max(res, arr[i]);
return res;
}
function findSum(arr,n)
{
let min = getMin(arr, n);
let max = getMax(arr, n);
return min + max;
}
function findProduct(arr,n)
{
let min = getMin(arr, n);
let max = getMax(arr, n);
return min * max;
}
let arr=[12, 1234, 45, 67, 1];
let n = arr.length;
document.write ( "Sum = " + findSum(arr, n)+ "<br>" );
document.write( "Product = " + findProduct(arr, n)+ "<br>" );
</script>
|
OutputSum = 1235
Product = 1234
Optimizations
We can use a single loop to find both maximum and minimum. This would require only one traversal of array.
Another Solution In C++, there are direct function to find maximum and minimum : max_element() and min_element()
C++14
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
int *i1, *i2;
i1 = std::max_element(arr, arr + n);
i2 = std::min_element(arr, arr + n);
cout << "Sum = " << *i1 + *i2 << endl;
cout << "Product = " << (*i1) * (*i2);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
int [] arr = { 12 , 1234 , 45 , 67 , 1 };
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for ( int a : arr) {
if (a > max) {
max = a;
}
if (a < min) {
min = a;
}
}
System.out.println( "Sum = " + (max + min));
System.out.println( "Product = " + (max * min));
}
}
|
Python3
arr = [ 12 , 1234 , 45 , 67 , 1 ]
max = max (arr)
min = min (arr)
print ( "Sum = " + str ( max + min ))
print ( "Product = " + str ( max * min ))
|
C#
using System;
public class GFG {
static public void Main()
{
int [] arr = { 12, 1234, 45, 67, 1 };
int max = int .MinValue;
int min = int .MaxValue;
for ( int i = 0; i < arr.Length; i++) {
int a = arr[i];
if (a > max) {
max = a;
}
if (a < min) {
min = a;
}
}
Console.WriteLine( "Sum = " + (max + min));
Console.WriteLine( "Product = " + (max * min));
}
}
|
Javascript
const arr = [12, 1234, 45, 67, 1];
const max = Math.max(...arr);
const min = Math.min(...arr);
console.log( "Sum = " + (max + min));
console.log( "Product = " + (max * min));
|
OutputSum = 1235
Product = 1234
Another Solution: Using STL
An alternate solution can be using the sort() to find the minimum and maximum number in the array, which we can then use to find the sum and product.
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int minEle, int maxEle)
{
return minEle + maxEle;
}
int findProduct( int minEle, int maxEle)
{
return minEle * maxEle;
}
int main()
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
sort(arr, arr+n);
int minEle = arr[0];
int maxEle = arr[n-1];
cout << "Sum = " << findSum(minEle, maxEle) << endl;
cout << "Product = " << findProduct(minEle, maxEle);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG {
static int findSum( int minEle, int maxEle)
{
return minEle + maxEle;
}
static int findProduct( int minEle, int maxEle)
{
return minEle * maxEle;
}
public static void main (String[] args)
{
int [] arr = { 12 , 1234 , 45 , 67 , 1 };
int n = arr.length;
Arrays.sort(arr);
int minEle = arr[ 0 ];
int maxEle = arr[n- 1 ];
System.out.println( "Sum = " + findSum(minEle, maxEle));
System.out.println( "Product = " + findProduct(minEle, maxEle));
}
}
|
C#
using System;
class GFG {
static int findSum( int minEle, int maxEle)
{
return minEle + maxEle;
}
static int findProduct( int minEle, int maxEle)
{
return minEle * maxEle;
}
public static void Main()
{
int [] arr = { 12, 1234, 45, 67, 1 };
int n = arr.Length;
Array.Sort(arr);
int minEle = arr[0];
int maxEle = arr[n-1];
Console.WriteLine( "Sum = " + findSum(minEle, maxEle));
Console.WriteLine( "Product = " + findProduct(minEle, maxEle));
}
}
|
Python3
class GFG :
@staticmethod
def findSum( minEle, maxEle) :
return minEle + maxEle
@staticmethod
def findProduct( minEle, maxEle) :
return minEle * maxEle
@staticmethod
def main( args) :
arr = [ 12 , 1234 , 45 , 67 , 1 ]
n = len (arr)
arr.sort()
minEle = arr[ 0 ]
maxEle = arr[n - 1 ]
print ( "Sum = " + str (GFG.findSum(minEle, maxEle)))
print ( "Product = " + str (GFG.findProduct(minEle, maxEle)))
if __name__ = = "__main__" :
GFG.main([])
|
Javascript
function findSum(minEle, maxEle)
{
return minEle + maxEle;
}
function findProduct(minEle, maxEle)
{
return minEle * maxEle;
}
var arr = [12, 1234, 45, 67, 1];
var n = arr.length;
arr.sort( function (a, b) { return a - b;});
var minEle = arr[0];
var maxEle = arr[n - 1];
console.log( "Sum = " + findSum(minEle, maxEle));
console.log( "Product = " + findProduct(minEle, maxEle));
|
OutputSum = 1235
Product = 1234
Please Login to comment...