La suma del par más pequeño en una array

Dada una array de enteros distintos arr[] , la tarea es encontrar un par que tenga la suma mínima e imprimir la suma.
Ejemplos: 
 

Entrada: arr[] = {1, 2, 3} 
Salida:
El par (1, 2) tendrá el par de suma mínima, es decir, 1 + 2 = 3
Entrada: arr[] = {3, 5, 6, 2} 
Salida:
 

Acercarse: 
 

  • Encuentre el elemento mínimo de la array y guárdelo en min .
  • Encuentre el segundo elemento mínimo de la array y guárdelo en secondMin .
  • Imprimir min + secondMin .

A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ program to print the sum of the minimum pair
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the sum of
// the minimum pair from the array
int smallest_pair(int a[], int n)
{
    int min = INT_MAX, secondMin = INT_MAX;
    for (int j = 0; j < n; j++) {
 
        // If found new minimum
        if (a[j] < min) {
 
            // Minimum now becomes second minimum
            secondMin = min;
 
            // Update minimum
            min = a[j];
        }
 
        // If current element is > min and < secondMin
        else if ((a[j] < secondMin) && a[j] != min)
 
            // Update secondMin
            secondMin = a[j];
    }
 
    // Return the sum of the minimum pair
    return (secondMin + min);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << smallest_pair(arr, n);
 
    return 0;
}

Java

// Java program to print the sum
// of the minimum pair
import java .io.*;
 
class GFG
{
// Function to return the sum of
// the minimum pair from the array
static int smallest_pair(int[] a, int n)
{
    int min =  Integer.MAX_VALUE, secondMin =  Integer.MAX_VALUE;
    for (int j = 0; j < n; j++)
    {
 
        // If found new minimum
        if (a[j] < min)
        {
 
            // Minimum now becomes second minimum
            secondMin = min;
 
            // Update minimum
            min = a[j];
        }
 
        // If current element is > min and < secondMin
        else if ((a[j] < secondMin) && a[j] != min)
 
            // Update secondMin
            secondMin = a[j];
    }
 
    // Return the sum of the minimum pair
    return (secondMin + min);
}
 
// Driver code
public static void main(String[] args)
{
    int[] arr = { 1, 2, 3 };
    int n = arr.length;
 
    System.out.println(smallest_pair(arr, n));
}
}
 
// This code is contributed
// by inder_verma

Python3

# Python3 program to print the
# sum of the minimum pair
import sys
 
# Function to return the sum of
# the minimum pair from the array
def smallest_pair(a, n) :
     
    min = sys.maxsize
    secondMin = sys.maxsize
     
    for j in range(n) :
 
        # If found new minimum
        if (a[j] < min) :
 
            # Minimum now becomes
            # second minimum
            secondMin = min
 
            # Update minimum
            min = a[j]
         
        # If current element is > min
        # and < secondMin
        elif ((a[j] < secondMin) and
               a[j] != min) :
 
            # Update secondMin
            secondMin = a[j]
     
    # Return the sum of the minimum pair
    return (secondMin + min)
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 1, 2, 3 ]
    n = len(arr)
 
    print(smallest_pair(arr, n))
 
# This code is contributed by Ryuga

C#

// C# program to print the sum
// of the minimum pair
using System;
 
class GFG
{
// Function to return the sum of
// the minimum pair from the array
static int smallest_pair(int[] a, int n)
{
    int min = int.MaxValue, secondMin = int.MaxValue;
    for (int j = 0; j < n; j++)
    {
 
        // If found new minimum
        if (a[j] < min)
        {
 
            // Minimum now becomes second minimum
            secondMin = min;
 
            // Update minimum
            min = a[j];
        }
 
        // If current element is > min and < secondMin
        else if ((a[j] < secondMin) && a[j] != min)
 
            // Update secondMin
            secondMin = a[j];
    }
 
    // Return the sum of the minimum pair
    return (secondMin + min);
}
 
// Driver code
public static void Main()
{
    int[] arr = { 1, 2, 3 };
    int n = arr.Length;
 
    Console.Write(smallest_pair(arr, n));
}
}
 
// This code is contributed
// by Akanksha Rai

PHP

<?php
// PHP program to print the sum
// of the minimum pair
 
// Function to return the sum of
// the minimum pair from the array
function smallest_pair($a, $n)
{
    $min = PHP_INT_MAX;
    $secondMin = PHP_INT_MAX;
    for ($j = 0; $j < $n; $j++)
    {
 
        // If found new minimum
        if ($a[$j] < $min)
        {
 
            // Minimum now becomes
            // second minimum
            $secondMin = $min;
 
            // Update minimum
            $min = $a[$j];
        }
 
        // If current element is > min
        // and < secondMin
        else if (($a[$j] < $secondMin) &&
                  $a[$j] != $min)
 
            // Update secondMin
            $secondMin = $a[$j];
    }
 
    // Return the sum of the minimum pair
    return ($secondMin + $min);
}
 
// Driver code
$arr = array( 1, 2, 3 );
$n = sizeof($arr);
echo smallest_pair($arr, $n);
 
// This code is contributed by ajit
?>

Javascript

<script>
 
    // Javascript program to print the sum
    // of the minimum pair
     
    // Function to return the sum of
    // the minimum pair from the array
    function smallest_pair(a, n)
    {
        let min = Number.MAX_VALUE,
        secondMin = Number.MAX_VALUE;
        for (let j = 0; j < n; j++)
        {
 
            // If found new minimum
            if (a[j] < min)
            {
 
                // Minimum now becomes second minimum
                secondMin = min;
 
                // Update minimum
                min = a[j];
            }
 
            // If current element is > min
            // and < secondMin
            else if ((a[j] < secondMin) &&
                      a[j] != min)
 
                // Update secondMin
                secondMin = a[j];
        }
 
        // Return the sum of the minimum pair
        return (secondMin + min);
    }
     
    let arr = [ 1, 2, 3 ];
    let n = arr.length;
   
    document.write(smallest_pair(arr, n));
     
</script>
Producción: 

3

 

Complejidad de Tiempo: ON) 
Espacio Auxiliar: O(1) 

Publicación traducida automáticamente

Artículo escrito por Devyansh_Ojha y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *