Sum and Product of minimum and maximum element of an Array
Last Updated :
04 Aug, 2023
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++
// 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 element
int 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 element
int 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 Sum
int findSum(int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min + max;
}
// Function to get product
int findProduct(int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min * max;
}
// Driver Code
int 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
// 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 element
int 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 element
int 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 Sum
int findSum(int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min + max;
}
// Function to get product
int findProduct(int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min * max;
}
// Driver Code
int 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
// 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 element
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;
}
// Function to find maximum element
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;
}
// Function to get Sum
static int findSum(int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min + max;
}
// Function to get product
static 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
# Python 3 program to find the sum and product
# of minimum and maximum element in an array
# Function to find minimum element
def getMin(arr, n):
res = arr[0]
for i in range(1, n):
res = min(res, arr[i])
return res
# Function to find maximum element
def getMax(arr, n):
res = arr[0]
for i in range(1, n):
res = max(res, arr[i])
return res
# Function to get Sum
def findSum(arr, n):
min = getMin(arr, n)
max = getMax(arr, n)
return min + max
# Function to get product
def findProduct(arr, n):
min = getMin(arr, n)
max = getMax(arr, n)
return min * max
# Driver Code
if __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#
// C# program to find the sum and product
// of minimum and maximum element in an array
using System;
class GFG {
// Function to find minimum element
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;
}
// Function to find maximum element
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;
}
// Function to get Sum
static int findSum(int []arr, int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min + max;
}
// Function to get product
static 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....
JavaScript
<script>
// JavaScript program to find the sum and product
// of minimum and maximum element in an array
// Function to find minimum element
function 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 element
function 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 Sum
function findSum(arr,n)
{
let min = getMin(arr, n);
let max = getMax(arr, n);
return min + max;
}
// Function to get product
function findProduct(arr,n)
{
let min = getMin(arr, n);
let max = getMax(arr, n);
return min * max;
}
// Driver Code
let arr=[12, 1234, 45, 67, 1];
let n = arr.length;
// Sum of min and max element
document.write ("Sum = " + findSum(arr, n)+"<br>");
// Product of min and max element
document.write( "Product = " + findProduct(arr, n)+"<br>");
// This code is contributed by avanitrachhadiya2155
</script>
PHP
<?php
// PHP program to find the sum and product
// of minimum and maximum element in an array
// Function to find minimum element
function getMin($arr, $n)
{
$res = $arr[0];
for ($i = 1; $i < $n; $i++)
$res = min($res, $arr[$i]);
return $res;
}
// Function to find maximum element
function getMax($arr, $n)
{
$res = $arr[0];
for ($i = 1; $i < $n; $i++)
$res = max($res, $arr[$i]);
return $res;
}
// Function to get Sum
function findSum($arr, $n)
{
$min = getMin($arr, $n);
$max = getMax($arr, $n);
return $min + $max;
}
// Function to get product
function 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 element
echo "Sum = " . findSum($arr, $n) . "\n";
// Product of min and max element
echo "Product = " . findProduct($arr, $n);
// This code is contributed
// by Akanksha Rai
OutputSum = 1235
Product = 1234
Time Complexity: O(n)
Auxiliary Space: O(1)
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
// CPP program to find the sum and product
// of minimum and maximum element in an array
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int 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;
}
Java
/*package whatever //do not write package name here */
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));
}
}
// This code is contributed by akashish__
Python3
# Python3 implementation of the above approach
arr = [12, 1234, 45, 67, 1]
max = max(arr)
min = min(arr)
print("Sum = " + str(max + min))
print("Product = " + str(max * min))
# This code is contributed by akashish__
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));
}
}
// This code is contributed by akashish__
JavaScript
// JavaScript implementation of the above approach
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));
// This code is contributed by akashish__
OutputSum = 1235
Product = 1234
Time Complexity: O(n)
Auxiliary Space: O(1)
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;
// Function to get sum
int findSum(int minEle, int maxEle)
{
return minEle + maxEle;
}
// Function to get product
int findProduct(int minEle, int maxEle)
{
return minEle * maxEle;
}
// Driver Code
int 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
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
class GFG {
// Function to get sum
static int findSum(int minEle, int maxEle)
{
return minEle + maxEle;
}
// Function to get product
static int findProduct(int minEle, int maxEle)
{
return minEle * maxEle;
}
// Driver code
public 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
Python3
class GFG :
# Function to get sum
@staticmethod
def findSum( minEle, maxEle) :
return minEle + maxEle
# Function to get product
@staticmethod
def findProduct( minEle, maxEle) :
return minEle * maxEle
# Driver code
@staticmethod
def main( args) :
arr = [12, 1234, 45, 67, 1]
n = len(arr)
# sorting the array
arr.sort()
# min element would be the element at 0th index
# of array after sorting
minEle = arr[0]
# max element would be the element at (n-1)th index
# of array after sorting
maxEle = arr[n - 1]
print("Sum = " + str(GFG.findSum(minEle, maxEle)))
print("Product = " + str(GFG.findProduct(minEle, maxEle)))
if __name__=="__main__":
GFG.main([])
# This code is contributed by aadityaburujwale.
C#
// C# program for the above approach
using System;
class GFG {
// Function to get sum
static int findSum(int minEle, int maxEle)
{
return minEle + maxEle;
}
// Function to get product
static int findProduct(int minEle, int maxEle)
{
return minEle * maxEle;
}
// Driver code
public 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.
JavaScript
// Function to get sum
function findSum(minEle, maxEle)
{
return minEle + maxEle;
}
// Function to get product
function findProduct(minEle, maxEle)
{
return minEle * maxEle;
}
var arr = [12, 1234, 45, 67, 1];
var n = arr.length;
// sorting the array
arr.sort(function(a, b) {return a - b;});
// min element would be the element at 0th index
// of array after sorting
var minEle = arr[0];
// max element would be the element at (n-1)th index
// of array after sorting
var maxEle = arr[n - 1];
console.log("Sum = " + findSum(minEle, maxEle));
console.log("Product = " + findProduct(minEle, maxEle));
// This code is contributed by sourabhdalal0001.
OutputSum = 1235
Product = 1234
Time Complexity: O(n*log(n)) (as here we using sort() function from STL)
Auxiliary Space: O(log(n)), for sort function to use recursion stack.
Similar Reads
Maximum sum of minimums of pairs in an array
Given an array arr[] of N integers where N is even, the task is to group the array elements in the pairs (X1, Y1), (X2, Y2), (X3, Y3), ... such that the sum min(X1, Y1) + min(X2, Y2) + min(X3, Y3) + ... is maximum.Examples: Input: arr[] = {1, 5, 3, 2} Output: 4 (1, 5) and (3, 2) -> 1 + 2 = 3 (1,
4 min read
Find the minimum and maximum sum of N-1 elements of the array
Given an unsorted array A of size N, the task is to find the minimum and maximum values that can be calculated by adding exactly N-1 elements. Examples: Input: a[] = {13, 5, 11, 9, 7} Output: 32 40 Explanation: Minimum sum is 5 + 7 + 9 + 11 = 32 and maximum sum is 7 + 9 + 11 + 13 = 40.Input: a[] = {
10 min read
Maximum and minimum sum of Bitwise XOR of pairs from an array
Given an array arr[] of size N, the task is to find the maximum and minimum sum of Bitwise XOR of all pairs from an array by splitting the array into N / 2 pairs. Examples: Input: arr[] = {1, 2, 3, 4}Output: 6 10Explanation:Bitwise XOR of the all possible pair splits are as follows:(1, 2), (3, 4) â
11 min read
Program to find the minimum (or maximum) element of an array
Given an array, write functions to find the minimum and maximum elements in it. Examples:Input: arr[] = [1, 423, 6, 46, 34, 23, 13, 53, 4]Output: Minimum element of array: 1 Maximum element of array: 423Input: arr[] = [2, 4, 6, 7, 9, 8, 3, 11]Output: Minimum element of array: 2 Maximum element of ar
14 min read
Sum of (maximum element - minimum element) for all the subsets of an array.
Given an array arr[], the task is to compute the sum of (max{A} - min{A}) for every non-empty subset A of the array arr[].Examples: Input: arr[] = { 4, 7 } Output: 3There are three non-empty subsets: { 4 }, { 7 } and { 4, 7 }. max({4}) - min({4}) = 0 max({7}) - min({7}) = 0 max({4, 7}) - min({4, 7})
10 min read
Leftmost and rightmost indices of the maximum and the minimum element of an array
Given an array arr[], the task is to find the leftmost and the rightmost indices of the minimum and the maximum element from the array where arr[] consists of non-distinct elements.Examples: Input: arr[] = {2, 1, 1, 2, 1, 5, 6, 5} Output: Minimum left : 1 Minimum right : 4 Maximum left : 6 Maximum r
15+ min read
Maximize minimum element of an Array using operations
Given an array A[] of N integers and two integers X and Y (X ⤠Y), the task is to find the maximum possible value of the minimum element in an array A[] of N integers by adding X to one element and subtracting Y from another element any number of times, where X ⤠Y. Examples: Input: N= 3, A[] = {1,
8 min read
Product of maximum in first array and minimum in second
Given two arrays, the task is to calculate the product of max element of first array and min element of second array References : Asked in Adobe (Source : Careercup) Examples : Input : arr1[] = {5, 7, 9, 3, 6, 2}, arr2[] = {1, 2, 6, -1, 0, 9} Output : max element in first array is 9 and min element
15 min read
Sum of minimum and maximum elements of all subarrays of size k.
Given an array of both positive and negative integers, the task is to compute sum of minimum and maximum elements of all sub-array of size k.Examples: Input : arr[] = {2, 5, -1, 7, -3, -1, -2} K = 4Output : 18Explanation : Subarrays of size 4 are : {2, 5, -1, 7}, min + max = -1 + 7 = 6 {5, -1, 7, -3
15+ min read
Sum of all minimum occurring elements in an Array
Given an array of integers containing duplicate elements. The task is to find the sum of all least occurring elements in the given array. That is the sum of all such elements whose frequency is minimum in the array.Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3, 3} Output : 2 The least occurring ele
5 min read