Sum and Product of minimum and maximum element of an Array

17 May, 2022
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 to 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++
C
Java
Python 3
C#
PHP
Javascript
// CPP program to find the sum  and product// of minimum and maximum element in an array #include <iostream>using namespace std; // Function to find minimum elementint getMin(int arr[], int n){    int res = arr[0];    for (int i = 1; i < n; i++)        res = min(res, arr[i]);    return res;} // Function to find maximum elementint getMax(int arr[], int n){    int res = arr[0];    for (int i = 1; i < n; i++)        res = max(res, arr[i]);    return res;} // Function to get Sumint findSum(int arr[], int n){    int min = getMin(arr, n);    int max = getMax(arr, n);     return min + max;} // Function to get productint findProduct(int arr[], int n){    int min = getMin(arr, n);    int max = getMax(arr, n);     return min * max;} // Driver Codeint main(){    int arr[] = { 12, 1234, 45, 67, 1 };    int n = sizeof(arr) / sizeof(arr[0]);     // Sum of min and max element    cout << "Sum = " << findSum(arr, n) << endl;     // Product of min and max element    cout << "Product = " << findProduct(arr, n);     return 0;}
// C program to find the sum  and product// of minimum and maximum element in an array#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;} // Function to find minimum elementint getMin(int arr[], int n){  int res = arr[0];  for (int i = 1; i < n; i++)    res = min(res, arr[i]);  return res;} // Function to find maximum elementint getMax(int arr[], int n){  int res = arr[0];  for (int i = 1; i < n; i++)    res = max(res, arr[i]);  return res;} // Function to get Sumint findSum(int arr[], int n){  int min = getMin(arr, n);  int max = getMax(arr, n);   return min + max;} // Function to get productint findProduct(int arr[], int n){  int min = getMin(arr, n);  int max = getMax(arr, n);   return min * max;} // Driver Codeint main(){  int arr[] = { 12, 1234, 45, 67, 1 };  int n = sizeof(arr) / sizeof(arr[0]);   // Sum of min and max element  printf("Sum = %d\n",findSum(arr, n));   // Product of min and max element  printf("Product = %d\n",findProduct(arr, n));   return 0;} // This code is contributed by kothavvsaakash.
// Java  program to find the sum and product// of minimum and maximum element in an array import java.io.*; class GFG {         // Function to find minimum elementstatic 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;} // Function to find maximum elementstatic 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;} // Function to get Sumstatic int findSum(int arr[], int n){    int min = getMin(arr, n);    int max = getMax(arr, n);     return min + max;} // Function to get productstatic int findProduct(int arr[], int n){    int min = getMin(arr, n);    int max = getMax(arr, n);     return min * max;} // Driver Code         public static void main (String[] args) {    int arr[] = { 12, 1234, 45, 67, 1 };    int n = arr.length;     // Sum of min and max element        System.out.println ("Sum = " + findSum(arr, n));     // Product of min and max element        System.out.println( "Product = " + findProduct(arr, n));                       }}//This Code is contributed by anuj_67....
# Python 3 program to find the sum and product# of minimum and maximum element in an array # Function to find minimum elementdef getMin(arr, n):    res = arr[0]    for i in range(1, n):        res = min(res, arr[i])    return res # Function to find maximum elementdef getMax(arr, n):    res = arr[0]    for i in range(1, n):        res = max(res, arr[i])    return res # Function to get Sumdef findSum(arr, n):    min = getMin(arr, n)    max = getMax(arr, n)     return min + max # Function to get productdef findProduct(arr, n):    min = getMin(arr, n)    max = getMax(arr, n)     return min * max # Driver Codeif __name__ == "__main__":         arr = [ 12, 1234, 45, 67, 1 ]    n = len(arr)     # Sum of min and max element    print("Sum = " , findSum(arr, n))     # Product of min and max element    print("Product = " , findProduct(arr, n)) # This code is contributed# by ChitraNayal
// C# program to find the sum and product// of minimum and maximum element in an arrayusing System; class GFG {     // Function to find minimum elementstatic 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;} // Function to find maximum elementstatic 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;} // Function to get Sumstatic int findSum(int []arr, int n){    int min = getMin(arr, n);    int max = getMax(arr, n);     return min + max;} // Function to get productstatic int findProduct(int []arr, int n){    int min = getMin(arr, n);    int max = getMax(arr, n);     return min * max;}     // Driver Code    public static void Main()    {        int []arr = { 12, 1234, 45, 67, 1 };        int n = arr.Length;             // Sum of min and max element        Console.WriteLine("Sum = " + findSum(arr, n));         // Product of min and max element        Console.WriteLine( "Product = " + findProduct(arr, n));    }} // This Code is contributed by anuj_67....
<?php// PHP program to find the sum and product// of minimum and maximum element in an array // Function to find minimum elementfunction getMin($arr, $n){    $res = $arr[0];    for ($i = 1; $i < $n; $i++)        $res = min($res, $arr[$i]);    return $res;} // Function to find maximum elementfunction getMax($arr, $n){    $res = $arr[0];    for ($i = 1; $i < $n; $i++)        $res = max($res, $arr[$i]);    return $res;} // Function to get Sumfunction findSum($arr, $n){    $min = getMin($arr, $n);    $max = getMax($arr, $n);     return $min + $max;} // Function to get productfunction findProduct($arr, $n){    $min = getMin($arr, $n);    $max = getMax($arr, $n);     return $min * $max;} // Driver Code$arr = array(12, 1234, 45, 67, 1);$n = sizeof($arr); // Sum of min and max elementecho "Sum = " . findSum($arr, $n) . "\n"; // Product of min and max elementecho "Product = " . findProduct($arr, $n); // This code is contributed// by Akanksha Rai
<script> // JavaScript  program to find the sum and product// of minimum and maximum element in an array // Function to find minimum elementfunction getMin(arr,n){    let res = arr[0];    for (let i = 1; i < n; i++)        res = Math.min(res, arr[i]);    return res;} // Function to find maximum elementfunction getMax(arr,n){    let res = arr[0];    for (let i = 1; i < n; i++)        res = Math.max(res, arr[i]);    return res;} // Function to get Sumfunction findSum(arr,n){    let min = getMin(arr, n);    let max = getMax(arr, n);       return min + max;} // Function to get productfunction findProduct(arr,n){    let min = getMin(arr, n);    let max = getMax(arr, n);       return min * max;} // Driver Codelet arr=[12, 1234, 45, 67, 1]; let n = arr.length;// Sum of min and max elementdocument.write ("Sum = " + findSum(arr, n)+"<br>"); // Product of min and max elementdocument.write( "Product = " + findProduct(arr, n)+"<br>");                // This code is contributed by avanitrachhadiya2155 </script>
Sum = 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()
Code:
C++14
// CPP program to find the sum  and product// of minimum and maximum element in an array #include <bits/stdc++.h>using namespace std;// Driver Codeint main(){    int arr[] = { 12, 1234, 45, 67, 1 };    int n = sizeof(arr) / sizeof(arr[0]);    int *i1, *i2;    // Find the maximum Element    i1 = std::max_element(arr, arr + n);    // Find the minimum Element    i2 = std::min_element(arr, arr + n);    // Sum of min and max element    cout << "Sum = " << *i1 + *i2 << endl;    // Product of min and max element    cout << "Product = " << (*i1) * (*i2);     return 0;}
Sum = 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++
Java
C#
#include <bits/stdc++.h>using namespace std; // Function to get sumint findSum(int minEle, int maxEle){    return minEle + maxEle;} // Function to get productint findProduct(int minEle, int maxEle){    return minEle * maxEle;} // Driver Codeint main(){    int arr[] = { 12, 1234, 45, 67, 1 };    int n = sizeof(arr) / sizeof(arr[0]);       // sorting the array    sort(arr, arr+n);      // min element would be the element at 0th index    //of array after sorting    int minEle = arr[0];         // max element would be the element at (n-1)th index    // of array after sorting    int maxEle = arr[n-1];         cout << "Sum = " << findSum(minEle, maxEle) << endl;    cout << "Product = " << findProduct(minEle, maxEle);      return 0;}
// Java program for the above approachimport java.io.*;import java.util.Arrays;class GFG { // Function to get sumstatic int findSum(int minEle, int maxEle){    return minEle + maxEle;} // Function to get productstatic int findProduct(int minEle, int maxEle){    return minEle * maxEle;} // Driver codepublic static void main (String[] args){    int[] arr = { 12, 1234, 45, 67, 1 };    int n = arr.length;       // sorting the array    Arrays.sort(arr);      // min element would be the element at 0th index    //of array after sorting    int minEle = arr[0];         // max element would be the element at (n-1)th index    // of array after sorting    int maxEle = arr[n-1];         System.out.println("Sum = " + findSum(minEle, maxEle));    System.out.println("Product = " + findProduct(minEle, maxEle));}} // This code is contributed by shivanisinghss2110
// C# program for the above approachusing System;class GFG { // Function to get sumstatic int findSum(int minEle, int maxEle){    return minEle + maxEle;} // Function to get productstatic int findProduct(int minEle, int maxEle){    return minEle * maxEle;} // Driver codepublic static void Main(){    int[] arr = { 12, 1234, 45, 67, 1 };    int n = arr.Length;       // sorting the array    Array.Sort(arr);      // min element would be the element at 0th index    //of array after sorting    int minEle = arr[0];         // max element would be the element at (n-1)th index    // of array after sorting    int maxEle = arr[n-1];         Console.WriteLine("Sum = " + findSum(minEle, maxEle));    Console.WriteLine("Product = " + findProduct(minEle, maxEle));}} // This code is contributed by target_2.
Sum = 1235
Product = 1234
vt_m
ukasp
Akanksha_Rai
avanitrachhadiya2155
swarnalii
target_2
shivanisinghss2110
CoderSaty
kothavvsaakash
Technical Scripter 2018
Arrays
Searching
Technical Scripter
Arrays
Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Search, insert and delete in an unsorted array
Window Sliding Technique
Longest Consecutive Subsequence
Chocolate Distribution Problem
Binary Search
Search, insert and delete in an unsorted array
Median of two sorted arrays of different sizes
Two Pointers Technique
Search, insert and delete in a sorted array