The Wayback Machine - https://web.archive.org/web/20240818165438/https://www.geeksforgeeks.org/program-find-minimum-maximum-element-array/
Open In App

Program to find the minimum (or maximum) element of an array

Last Updated : 03 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an array, write functions to find the minimum and maximum elements in it. 

The most simplest way to find min and max value of an element is to use inbuilt function sort() in java. So, that value at 0th position will min and value at nth position will be max.

C++
// C++ code for the approach

#include <iostream>
#include <algorithm>

using namespace std;

// Driver Code
int main() {
      // Input array
    int a[] = { 1, 423, 6, 46, 34, 23, 13, 53, 4 };
    int n = sizeof(a) / sizeof(a[0]);
  
    // Implemented inbuilt function to sort array
    sort(a, a + n);

    // after sorting the value at 0th position will minimum
    // and nth position will be maximum
    cout << "min-" << a[0] << " max-" << a[n - 1] << endl;
  
    return 0;
}
C
#include <stdio.h>

// Function to find minimum and maximum elements in an array
void findMinMax(int arr[], int n)
{
    int min = arr[0];
    int max = arr[0];

    // Traverse array elements
    for (int i = 1; i < n; i++) {
        if (arr[i] < min)
            min = arr[i];
        else if (arr[i] > max)
            max = arr[i];
    }

    // Print minimum and maximum elements
    printf("min-%d max-%d\n", min, max);
}

int main()
{
    // Input array
    int arr[] = { 1, 423, 6, 46, 34, 23, 13, 53, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);

    // Find minimum and maximum elements
    findMinMax(arr, n);

    return 0;
}
Java
import java.util.*;
class Array {
    public static void main(String[] args) {
        int a[]={1,423,6,46,34,23,13,53,4}; 
          
          //Implemented inbuilt function to sort array
        Arrays.sort(a);
      
          // after sorting the value at 0th position will minimum and 
        //nth position will be maximum
        System.out.println("min-"+a[0]+" max-"+a[a.length-1]);
    }
}
Python
import sys

# Define an array
a = [1, 423, 6, 46, 34, 23, 13, 53, 4]

# Sort the array using the built-in sorted() function
a_sorted = sorted(a)

# Find the minimum and maximum values
min_value = a_sorted[0]
max_value = a_sorted[-1]

# Print the results
print(f"min-{min_value} max-{max_value}")
C#
// C# code for the approach

using System;

class GFG {
    // Driver Code
    static void Main()
    {
        // Input array
        int[] arr = { 1, 423, 6, 46, 34, 23, 13, 53, 4 };
        int n = arr.Length;
        // Sort the array
        Array.Sort(arr);

        // after sorting the value at 0th position will be
        // minimum and nth position will be maximum
        Console.WriteLine("min-" + arr[0] + " max-"
                          + arr[n - 1]);
    }
}
JavaScript
// JavaScript code for the approach

// Function to find minimum and maximum values in an array
function findMinMax(arr) {

    // Implemented inbuilt function to sort array
      arr.sort((a, b) => a - b);

    return { min: arr[0], max: arr[arr.length - 1] };
}

// Example usage
const arr = [1, 423, 6, 46, 34, 23, 13, 53, 4];
const { min, max } = findMinMax(arr);
console.log(`min-${min} max-${max}`); // Output: min-1 max-423
PHP
<?php
// Function to find minimum and maximum elements in an array
function findMinMax($arr, $n) {
    $min = $arr[0];
    $max = $arr[0];
    
    // Traverse array elements
    for ($i = 1; $i < $n; $i++) {
        if ($arr[$i] < $min)
            $min = $arr[$i];
        elseif ($arr[$i] > $max)
            $max = $arr[$i];
    }
    
    // Print minimum and maximum elements
    echo "min-$min max-$max\n";
}

// Input array
$arr = array(1, 423, 6, 46, 34, 23, 13, 53, 4);
$n = count($arr);

// Find minimum and maximum elements
findMinMax($arr, $n);
?>

Output
min-1 max-423

Time complexity : O(n log(n))
Auxiliary Space : O(1)

C++
// CPP program to find minimum (or maximum) element
// in an array.
#include <bits/stdc++.h>
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 main()
{
    int arr[] = { 12, 1234, 45, 67, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Minimum element of array: " << getMin(arr, n)
         << "\n";
    cout << "Maximum element of array: " << getMax(arr, n);
    return 0;
}

// This code is contributed by Aditya Kumar (adityakumar129)
C
// C program to find minimum (or maximum) element
// in an array.
#include <stdio.h>

// Find maximum between two numbers.
int max(int num1, int num2)
{
    return (num1 > num2) ? num1 : num2;
}

// Find minimum between two numbers.
int min(int num1, int num2)
{
    return (num1 > num2) ? num2 : num1;
}

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 main()
{
    int arr[] = { 12, 1234, 45, 67, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Minimum element of array: %d \n",
           getMin(arr, n));
    printf("Maximum element of array: %d \n",
           getMax(arr, n));
    return 0;
}

// This code is contributed by Aditya Kumar (adityakumar129)
Java
// Java program to find minimum (or maximum)
// element in an array.
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;
    }

    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 12, 1234, 45, 67, 1 };
        int n = arr.length;
        System.out.println("Minimum element of array: " + getMin(arr, n));
        System.out.println("Maximum element of array: " + getMax(arr, n));
    }
}

// This code is contributed by Aditya Kumar (adityakumar129)
Python
# Python3 program to find minimum
# (or maximum) element in an array

# Minimum Function
def getMin(arr, n):
    res = arr[0]
    for i in range(1,n):
        res = min(res, arr[i])
    return res

# Maximum Function
def getMax(arr, n):
    res = arr[0]
    for i in range(1,n):
        res = max(res, arr[i])
    return res

# Driver Program
arr = [12, 1234, 45, 67, 1]
n = len(arr)
print ("Minimum element of array:", getMin(arr, n))
print ("Maximum element of array:", getMax(arr, n))

# This code is contributed 
# by Shreyanshi Arun.
C#
// C# program to find 
// minimum (or maximum) 
// element in an array.
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;
    }

    // Driver code
    public static void Main () 
    {
        int []arr = {12, 1234, 45, 67, 1};
        int n = arr.Length;
        Console.Write("Minimum element" + 
                          " of array: " + 
                         getMin(arr, n) + "\n" );
        Console.Write("Maximum element" + 
                          " of array: " + 
                         getMax(arr, n));
    }
}

// This code is contributed by Smita.
JavaScript
// JavaScript program to find minimum
// (or maximum) element in an array.
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;
}

// Driver Code
let arr = [ 12, 1234, 45, 67, 1 ];
let n = arr.length;

console.log("Minimum element" + 
               " of array: " + 
               getMin(arr, n) + "<br/>");
console.log("Maximum element" + 
               " of array: " + 
               getMax(arr, n));
               
// This code is contributed by sanjoy_62
PHP
<?php
// PHP program to find minimum 
// (or maximum) element in an
// array.

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;
}

    // Driver Code
    $arr = array(12, 1234, 45, 67, 1);
    $n = sizeof($arr);
    echo "Minimum element of array: "
           , getMin($arr, $n), "\n";
    echo "Maximum element of array: " 
                  ,getMax($arr, $n);

// This code is contributed by ajit
?>

Output
Minimum element of array: 1
Maximum element of array: 1234

Time Complexity: O(n)
Auxiliary Space: O(1), as no extra space is used

Recursive Solution 

C++
// CPP program to find 
// minimum (or maximum) element
// in an array.
#include <bits/stdc++.h>
using namespace std;

int getMin(int arr[], int n)
{
    // If there is single element, return it.
    // Else return minimum of first element and
    // minimum of remaining array.
    return (n == 1) ? arr[0] : min(arr[0], 
                         getMin(arr + 1, n - 1));
}

int getMax(int arr[], int n)
{
    // If there is single element, return it.
    // Else return maximum of first element and
    // maximum of remaining array.
    return (n == 1) ? arr[0] : max(arr[0], 
                          getMax(arr + 1, n - 1));
}

int main()
{
    int arr[] = { 12, 1234, 45, 67, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Minimum element of array: " << 
                            getMin(arr, n) << "\n";
    cout << "Maximum element of array: " << 
                                   getMax(arr, n);
    return 0;
}
C
#include <stdio.h>

// Function to find minimum element in an array
int getMin(int arr[], int n)
{
    if (n == 1)
        return arr[0];
    else
        return (getMin(arr + 1, n - 1) < arr[0])
                   ? getMin(arr + 1, n - 1)
                   : arr[0];
}

// Function to find maximum element in an array
int getMax(int arr[], int n)
{
    if (n == 1)
        return arr[0];
    else
        return (getMax(arr + 1, n - 1) > arr[0])
                   ? getMax(arr + 1, n - 1)
                   : arr[0];
}

int main()
{
    // Input array
    int arr[] = { 12, 1234, 45, 67, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);

    // Find and print minimum and maximum elements
    printf("Minimum element of array: %d\n",
           getMin(arr, n));
    printf("Maximum element of array: %d\n",
           getMax(arr, n));

    return 0;
}
Java
// Java program to find minimum 
// (or maximum) element
// in an array.
import java.io.*;
public class GFG 
{

static int getMin(int arr[], int i, int n)
{
    // If there is single element, return it.
    // Else return minimum of first element and
    // minimum of remaining array.
    return (n == 1) ? arr[i] : Math.min(arr[i], 
                        getMin(arr,i + 1 , n - 1));
}

static int getMax(int arr[], int i, int n)
{
    // If there is single element, return it.
    // Else return maximum of first element and
    // maximum of remaining array.
    return (n == 1) ? arr[i] : Math.max(arr[i], 
                         getMax(arr ,i + 1, n - 1));
}

// Driver code
public static void main(String[] args) 
{
    int arr[] = { 12, 1234, 45, 67, 1 };
    int n = arr.length;
    System.out.print("Minimum element of array: " + 
                        getMin(arr, 0, n) + "\n");
    System.out.println("Maximum element of array: " + 
                        getMax(arr, 0, n));
    }
}

/* This code contributed by PrinciRaj1992 */
Python
# Python3 program to find minimum 
# (or maximum) element in an array.
def getMin(arr, n):
    if(n==1):
        return arr[0]
    # If there is single element, return it. 
    # Else return minimum of first element 
    # and minimum of remaining array. 
    else:
        return min(getMin(arr[1:], n-1), arr[0])
def getMax(arr, n): 
    if(n==1):
        return arr[0]
    # If there is single element, return it. 
    # Else return maximum of first element 
    # and maximum of remaining array. 
    else:
        return max(getMax(arr[1:], n-1), arr[0])

# Driver code
arr = [12, 1234, 45, 67, 1]
n = len(arr)
print("Minimum element of array: ", 
                   getMin(arr, n));
print("Maximum element of array: ",
                   getMax(arr, n));

# This code is contributed by 
# Mohit Kumar 29
C#
// C# program to find minimum 
// (or maximum) element 
// in an array.
using System; 

class GFG 
{ 

static int getMin(int []arr, int i, int n) 
{ 
    // If there is single element, return it. 
    // Else return minimum of first element and 
    // minimum of remaining array. 
    return (n == 1) ? arr[i] : Math.Min(arr[i], 
                    getMin(arr,i + 1 , n - 1)); 
} 

static int getMax(int []arr, int i, int n) 
{ 
    // If there is single element, return it. 
    // Else return maximum of first element and 
    // maximum of remaining array. 
    return (n == 1) ? arr[i] : Math.Max(arr[i], 
                    getMax(arr ,i + 1, n - 1)); 
} 

// Driver code 
public static void Main(String[] args) 
{ 
    int []arr = { 12, 1234, 45, 67, 1 }; 
    int n = arr.Length; 
    Console.WriteLine("Minimum element of array: " +
                                getMin(arr, 0, n)); 
    Console.WriteLine("Maximum element of array: " + 
                                getMax(arr, 0, n)); 
} 
} 

// This code is contributed by Mohit
JavaScript
// javascript program to find minimum 
// (or maximum) element
// in an array.    
function getMin(arr , i , n) {
        // If there is single element, return it.
        // Else return minimum of first element and
        // minimum of remaining array.
        return (n == 1) ? arr[i] : Math.min(arr[i], 
        getMin(arr, i + 1, n - 1));
    }

    function getMax(arr , i , n) {
        // If there is single element, return it.
        // Else return maximum of first element and
        // maximum of remaining array.
        return (n == 1) ? arr[i] : Math.max(arr[i],  
               getMax(arr, i + 1, n - 1));
    }

    // Driver code
    
        var arr = [ 12, 1234, 45, 67, 1 ];
        var n = arr.length;
        console.log("Minimum element of array: " + 
        getMin(arr, 0, n) + "<br/>");
        console.log("Maximum element of array: " + getMax(arr, 0, n));

// This code contributed by Rajput-Ji
PHP
<?php
// Function to find minimum element in an array
function getMin($arr, $n) {
    if ($n == 1)
        return $arr[0];
    else
        return (getMin(array_slice($arr, 1), $n - 1) < $arr[0]) ? getMin(array_slice($arr, 1), $n - 1) : $arr[0];
}

// Function to find maximum element in an array
function getMax($arr, $n) {
    if ($n == 1)
        return $arr[0];
    else
        return (getMax(array_slice($arr, 1), $n - 1) > $arr[0]) ? getMax(array_slice($arr, 1), $n - 1) : $arr[0];
}

// Input array
$arr = [12, 1234, 45, 67, 1];
$n = count($arr);

// Find and print minimum and maximum elements
echo "Minimum element of array: " . getMin($arr, $n) . "\n";
echo "Maximum element of array: " . getMax($arr, $n) . "\n";
?>

Output
Minimum element of array: 1
Maximum element of array: 1234

Time Complexity: O(n)
Auxiliary Space: O(n), as implicit stack is used due to recursion

Using Library functions: 
We can use min_element() and max_element() to find minimum and maximum of array. 

C++
// CPP program to find minimum (or maximum) element
// in an array.
#include <bits/stdc++.h>
using namespace std;

int getMin(int arr[], int n)
{
    return *min_element(arr, arr + n);
}

int getMax(int arr[], int n)
{
    return *max_element(arr, arr + n);
}

int main()
{
    int arr[] = { 12, 1234, 45, 67, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Minimum element of array: " << getMin(arr, n)
         << "\n";
    cout << "Maximum element of array: " << getMax(arr, n);
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

// Compare function for qsort
int compare(const void *a, const void *b) {
    return (*(int *)a - *(int *)b);
}

// Function to find minimum element in an array
int getMin(int arr[], int n) {
    qsort(arr, n, sizeof(int), compare);
    return arr[0];
}

// Function to find maximum element in an array
int getMax(int arr[], int n) {
    qsort(arr, n, sizeof(int), compare);
    return arr[n - 1];
}

int main() {
    int arr[] = {12, 1234, 45, 67, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Minimum element of array: %d\n", getMin(arr, n));
    printf("Maximum element of array: %d\n", getMax(arr, n));
    return 0;
}
Java
// Java program to find minimum (or maximum) element
// in an array.
import java.util.Arrays;

class GFG {

    static int getMin(int arr[], int n)
    {
        return Arrays.stream(arr).min().getAsInt();
    }

    static int getMax(int arr[], int n)
    {
        return Arrays.stream(arr).max().getAsInt();
    }

    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 12, 1234, 45, 67, 1 };
        int n = arr.length;
        System.out.println("Minimum element of array: "
                           + getMin(arr, n));
        System.out.println("Maximum element of array: "
                           + getMax(arr, n));
    }
}
/*This Java code is contributed by 29AjayKumar*/
Python
# Python3 program to find minimum
# (or maximum) element
# in an array.


def getMin(arr, n):
    return min(arr)


def getMax(arr, n):
    return max(arr)


# Driver Code
if __name__ == '__main__':
    arr = [12, 1234, 45, 67, 1]
    n = len(arr)
    print("Minimum element of array: ", getMin(arr, n))
    print("Maximum element of array: ", getMax(arr, n))

# This code is contributed by
# Shrikant13
C#
// C# program to find minimum
// (or maximum) element in an array.
using System;
using System.Linq;
class GFG {
    static int getMin(int[] arr, int n)
    {
        return arr.Min();
    }

    static int getMax(int[] arr, int n)
    {
        return arr.Max();
    }

    // Driver code
    public static void Main(String[] args)
    {
        int[] arr = { 12, 1234, 45, 67, 1 };
        int n = arr.Length;
        Console.WriteLine("Minimum element of array: "
                          + getMin(arr, n));
        Console.WriteLine("Maximum element of array: "
                          + getMax(arr, n));
    }
}

// This code is contributed by 29AjayKumar
JavaScript
// Javascript program to find 
// minimum (or maximum) element 
// in an array. 

    function getMin(arr , n) 
    {
        return Math.min.apply(Math,arr);
    }

    function getMax(arr , n) {
        return Math.max.apply(Math,arr);
    }

    // Driver code
    
        var arr = [ 12, 1234, 45, 67, 1 ];
        var n = arr.length;
        console.log("Minimum element of array: " + 
        getMin(arr, n)+"<br/>");
        console.log("Maximum element of array: "
        + getMax(arr, n));

// This code contributed by aashish1995 
PHP
<?php 
// PHP program to find minimum (or maximum) 
// element in an array. 

function getMin(&$arr, $n) 
{ 
    return min($arr); 
} 

function getMax(&$arr, $n) 
{ 
    return max($arr); 
} 

// Driver Code
$arr = array(12, 1234, 45, 67, 1 ); 
$n = sizeof($arr); 
echo "Minimum element of array: " . 
           getMin($arr, $n) . "\n"; 
echo "Maximum element of array: " .
                  getMax($arr, $n); 

// This code is contributed 
// by ChitraNayal
?>

Output
Minimum element of array: 1
Maximum element of array: 1234

Time Complexity: O(n)
Auxiliary Space: O(1), as no extra space is used

 



Similar Reads

Minimum count of array elements that must be changed such that difference between maximum and minimum array element is N - 1
Given an array arr[] consisting of N integers, the task is to find the minimum number of array elements that must be changed to any arbitrary integers such that the difference between maximum and minimum array element is (N - 1) and all array elements must be distinct. Examples: Input: arr[] = {1, 2, 3, 5, 6}Output: 1Explanation:Change 6-&gt;4, the
7 min read
Remaining array element after repeated removal of last element and subtraction of each element from next adjacent element
Given an array arr[] consisting of N integers, the task is to find the remaining array element after subtracting each element from its next adjacent element and removing the last array element repeatedly. Examples: Input: arr[] = {3, 4, 2, 1}Output: 4Explanation:Operation 1: The array arr[] modifies to {4 - 3, 2 - 4, 1 - 2} = {1, -2, -1}.Operation
8 min read
Maximum element in an array such that its previous and next element product is maximum
Given an array arr[] of N integers, the task is to print the largest element among the array such that its previous and next element product is maximum.Examples: Input: arr[] = {5, 6, 4, 3, 2} Output: 6 The product of the next and the previous elements for every element of the given array are: 5 -&gt; 2 * 6 = 12 6 -&gt; 5 * 4 = 20 4 -&gt; 6 * 3 = 1
6 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}) = 7 - 4 = 3.Sum = 0 + 0 + 3 = 3Input: arr[] = { 4
10 min read
Maximize subarrays count containing the maximum and minimum Array element after deleting at most one element
Given an array arr[] of size N. The task is to maximize the count of subarrays that contain both the minimum and maximum elements of the array by deleting at most one element from the array. Examples: Input: arr[] = {7, 2, 5, 4, 3, 1} Output: 4 Explanation: Delete 1 from the array then resultant array will be {7, 2, 5, 4, 3}. So the number of subar
11 min read
Minimum distance between the maximum and minimum element of a given Array
Given an array A[] consisting of N elements, the task is to find the minimum distance between the minimum and the maximum element of the array.Examples: Input: arr[] = {3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 8, 2} Output: 3 Explanation: The minimum element(= 1) is present at indices {2, 4} The maximum element(= 8) is present at indices {7, 10}. The minimum
8 min read
Split array into minimum number of subsets having difference between maximum and minimum element at most K
Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum number of sets, the array elements can be divided into such that the difference between the maximum and minimum element of each set is at most K. Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 2 Output: 2Explanation:The given array can be divided into two
6 min read
Find Array formed by adding each element of given array with largest element in new array to its left
Given an array A of size N, the task is to find the resultant array formed by adding each element of the given array with the largest element in the new array to its left.Examples: Input: arr[] = {5, 1, 6, -3, 2} Output: {5, 6, 12, 9, 14} Element A0: No element if present at its left. Hence the element at 0th index of the resultant array = 5 Elemen
6 min read
Find maximum element among the elements with minimum frequency in given Array
Given an array arr[] consisting of N integers, the task is to find the maximum element with the minimum frequency. Examples: Input: arr[] = {2, 2, 5, 50, 1}Output: 50Explanation:The element with minimum frequency is {1, 5, 50}. The maximum element among these element is 50. Input: arr[] = {3, 2, 5, 6, 1}Output: 6 Recommended: Please try your approa
13 min read
Find the Array element left after alternate removal of minimum and maximum
Given an array Arr of length N, it is reduced by 1 element at each step. Maximum and Minimum elements will be removed in alternating order from the remaining array until a single element is remaining in the array. The task is to find the remaining element in the given array. Examples: Input: arr[] = {1, 5, 4, 2}Output: 2Explanation: Remove Max elem
4 min read
Maximum XOR value of maximum and second maximum element among all possible subarrays
Given an array arr[] of N distinct positive integers, let's denote max(i, j) and secondMax(i, j) as the maximum and the second maximum element of the subarray arr[i...j]. The task is to find the maximum value of max(i, j) XOR secondMax(i, j) for all possible values of i and j. Note that the size of the subarray must be at least two.Examples: Input:
5 min read
Minimum product of maximum and minimum element over all possible subarrays
Given an array arr[] consisting of N positive integers, the task is to find the minimum product of maximum and minimum among all possible subarrays. Examples: Input: arr[] = {6, 4, 5, 6, 2, 4}Output: 8Explanation:Consider the subarray {2, 4}, the product of minimum and maximum for this subarray is 2*4 = 8, which is minimum among all possible subarr
4 min read
Find just strictly greater element from first array for each element in second array
Given two arrays A[] and B[] containing N elements, the task is to find, for every element in the array B[], the element which is just strictly greater than that element which is present in the array A[]. If no value is present, then print 'null'. Note: The value from the array A[] can only be used once. Examples: Input: A[] = {0, 1, 2, 3, 4}, B[]
10 min read
Find the next non-zero Array element to the right of each array element
Given an array arr[] of N integers, the task is to find the next non-zero array element to the right of every array element. If there doesn't exist any non-zero element, then print that element itself. Examples: Input: arr[] = {1, 2, 0}Output: {2, 2, 0}Explanation:For each array element the next non-zero elements are:arr[0] = 1 -&gt; 2arr[1] = 2 -
6 min read
Find the array element having minimum sum of absolute differences with all other array elements
Given an array arr[] of size N, the task is to find the minimum sum of absolute differences of an array element with all elements of another array. Input: arr[ ] = {1, 2, 3, 4, 5}, N = 5Output: 3Explanation: For arr[0](= 1): Sum = abs(2 - 1) + abs(3 - 1) + abs(4 - 1) + abs(5 - 1) = 10.For arr[1](= 2): Sum = abs(2 - 1) + abs(3 - 2) + abs(4 - 2) + ab
4 min read
Find minimum pair sum by selecting element from 2nd array at index larger than 1st array
Given two arrays A[] and B[] of size N each, the task is to minimize A[i] + B[j] such that j ≥ i. Examples: Input: A[] = {34, 12, 45, 10, 86, 39, 77}, B[] = {5, 42, 29, 63, 30, 33, 20}Output: 30Explanation: For minimizing the sum, take i = 3 and j = 6. Input: A[] = {34, 17, 45, 10, 19}, B[] = {2, 13, 7, 11, 16}Output: 21Explanation: For minimizing
10 min read
Find maximum and minimum element in binary tree without using recursion or stack or queue
Given a binary tree. The task is to find out the maximum and minimum element in a binary tree without using recursion or stack or queue i.e, space complexity should be O(1). Examples: Input : 12 / \ 13 10 / \ 14 15 / \ / \ 21 24 22 23 Output : Max element : 24 Min element : 10 Input : 12 / \ 19 82 / / \ 41 15 95 \ / / \ 2 21 7 16 Output : Max eleme
10 min read
Largest element smaller than current element on left for every element in Array
Given an array arr[] of the positive integers of size N, the task is to find the largest element on the left side of each index which is smaller than the element present at that index. Note: If no such element is found then print -1. Examples: Input: arr[] = {2, 5, 10} Output: -1 2 5 Explanation : Index 0: There are no elements before it So Print -
11 min read
Maximum sum in an array such that every element has exactly one adjacent element to it
Given an array arr[] of N integers, you can select some indexes such that every selected index has exactly one other selected index adjacent to it and the sum of elements at the chosen indexes should be maximum. In other words, the task is to select elements from an array such that a single element alone is not selected and elements at three consec
9 min read
Maximum possible remainder when an element is divided by other element in the array
Given an array arr[] of N integers, the task is to find the maximum mod value for any pair (arr[i], arr[j]) from the array.Examples: Input: arr[] = {2, 4, 1, 5, 3, 6} Output: 5 (5 % 6) = 5 is the maximum possible mod value.Input: arr[] = {6, 6, 6, 6} Output: 0 Approach: It is known that when an integer is divided by some other integer X, the remain
4 min read
Smallest element with K set bits such that sum of Bitwise AND of each array element with K is maximum
Given an array arr[] consisting of N integers and integer K, the task is to find the smallest integer X with exactly K set bits such that the sum of Bitwise AND of X with every array element arr[i] is maximum. Examples: Input: arr[] = {3, 4, 5, 1}, K = 1Output: 4Explanation: Consider the value of X as 4. Then, the sum of Bitwise AND of X and array
8 min read
Minimize the maximum of Array by replacing any element with other element at most K times
Given an array arr[] of size N and an integer K, the task is to minimize the value of the maximum element of the array arr[] after replacing any element of the array with any other element of that array at most K times. Examples: Input: arr[] = {5, 3, 3, 2, 1}, K = 3Output: 2Explanation: Replace the elements at index 0, 1 and 2 with the value 1.The
8 min read
Split array into maximum possible subsets having product of their length with the maximum element at least K
Given an array arr[] consisting of N integers and a positive integer K, the task is to maximize the number of subsets having a product of its size and its maximum element at least K by dividing array element into disjoint subsets. Examples: Input: N = 5, K = 4, arr[] = {1, 5, 4, 2, 3}Output: 3Explanation:The array can be split into 3 possible subse
6 min read
Check if minimum element in array is less than or equals half of every other element
Given an array arr[], the task is to check if the minimum element in the array is less than or equal to half of every other element. If it is then print "yes" otherwise print "no".Note: The minimum number in the given array is always unique. Examples: Input: arr = {2, 1, 4, 5} Output: Yes Explanation: 1 is the minimum element in the array arr[] and
11 min read
Check whether each Array element can be reduced to minimum element by replacing it with remainder with some X
Given an array arr[] of non-negative integers, the task is to check whether all array elements can be reduced to the minimum element in the array by choosing any positive integer X and update an array element arr[i] to arr[i]%X. If it is possible to do so, then print Yes. Otherwise, print No. Examples: Input: arr[] = {1, 1, 3, 4}Output: YesExplanat
6 min read
Sum and Product of minimum and maximum element of an Array
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 arra
13 min read
Minimum and Maximum element of an array which is divisible by a given number k
Given an array, the task is to find the minimum and maximum elements in the array which are divisible by a given number k. Examples: Input: arr[] = {12, 1235, 45, 67, 1}, k=5 Output: Minimum = 45, Maximum = 1235 Input: arr[] = {10, 1230, 45, 67, 1}, k=10 Output: Minimum = 10, Maximum = 1230 Approach: Take a min variable that stores the minimum elem
7 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 right : 6 Minimum element is 1 which is present at
15+ min read
Count subsequences which contains both the maximum and minimum array element
Given an array arr[] consisting of N integers, the task is to find the number of subsequences which contain the maximum as well as the minimum element present in the given array. Example : Input: arr[] = {1, 2, 3, 4}Output: 4Explanation: There are 4 subsequence {1, 4}, {1, 2, 4}, {1, 3, 4}, {1, 2, 3, 4} which contains the maximum array element(= 4)
6 min read
Minimize difference between maximum and minimum element by decreasing and increasing Array elements by 1
Given an array arr[], consisting of N positive integers. The task is to minimize the difference between the maximum and the minimum element of the array by performing the below operation any number of times (possibly zero). In one operation choose 2 different index, i and j and decrement arr[i] and increment arr[j] by 1.Notice this operation can be
5 min read
three90RightbarBannerImg