Encuentre el mínimo en una array sin usar operadores relacionales

Dada una array A[] de enteros no negativos, busque el mínimo en la array sin usar operadores relacionales .

Ejemplos: 

Input : A[] = {2, 3, 1, 4, 5}
Output : 1

Input : A[] = {23, 17, 93}
Output : 17

Usamos restas repetidas para encontrar el mínimo. Para encontrar el mínimo entre dos números, tomamos un contador variable inicializado a cero. Seguimos disminuyendo el valor de ambos hasta que cualquiera de ellos sea igual a cero, aumentando el contador simultáneamente. El valor mínimo llega primero a cero y el contador ha aumentado hasta ser el mínimo de ambos. Primero encontramos el mínimo de los primeros dos números y luego lo comparamos con el resto de elementos de la array uno por uno para encontrar el mínimo general.

A continuación se muestra la implementación de la idea anterior. 

C++

// C++ program to find minimum in an
// array without using Relational Operators
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum between two non-negative
// numbers without using relational operator.
int minimum(int x, int y)
{
    int c = 0;
 
    // Continues till any element becomes zero.
    while (x && y)
    {
        x--;
        y--;
        c++;
    }
    return c;
}
 
// Function to find minimum in an array.
int arrayMinimum(int A[], int N)
{
    // calculating minimum of first two numbers
    int mn = A[0];
 
    // Iterating through each of the member of
    // the array to calculate the minimum
    for (int i = N-1; i; i--)
 
        // Finding the minimum between current
        // minimum and current value.
        mn = minimum(mn, A[i]);   
 
    return mn;
}
 
// Driver code
int main()
{
    int A[] = { 2, 3, 1, 4 };
    int N = sizeof(A) / sizeof(A[0]);
    cout << arrayMinimum(A, N);
    return 0;
}

Java

// Java program to find minimum in an
// array without using Relational Operators
 
class GFG {
     
// Function to find minimum between two
// non-negative numbers without
// using relational operator.
static int minimum(int x, int y)
{
    int c = 0;
 
    // Continues till any element becomes zero.
    while (x > 0 && y > 0) {
    x--;
    y--;
    c++;
    }
    return c;
}
 
// Function to find minimum in an array.
static int arrayMinimum(int A[], int N) {
     
    // calculating minimum of first two numbers
    int mn = A[0];
 
    // Iterating through each of the member of
    // the array to calculate the minimum
    for (int i = N - 1; i > 0; i--)
 
    // Finding the minimum between current
    // minimum and current value.
    mn = minimum(mn, A[i]);
 
    return mn;
}
 
// Driver code
public static void main(String arg[])
{
    int A[] = {2, 3, 1, 4};
    int N = A.length;
    System.out.print(arrayMinimum(A, N));
}
}
 
// This code is contributed by Anant Agarwal.

Python3

# Function to find minimum
# between two non-negative
# numbers without using
# relational operator.
 
def minimum(x,y):
    c = 0
  
    # Continues till any
    # element becomes zero.
    while (x>0 and y>0):
     
        x=x-1
        y=y-1
        c=c+1
     
    return c
  
# Function to find
# minimum in an array.
def arrayMinimum(A,N):
 
    # calculating minimum
    # of first two numbers
    mn = A[0]
  
    # Iterating through each
    # of the member of
    # the array to calculate
    # the minimum
    for i in range(N-1,0,-1):
  
        # Finding the minimum
        # between current
        # minimum and current value.
        mn = minimum(mn, A[i])   
  
    return mn
     
# Driver code
 
A = [ 2, 3, 1, 4]
N =len(A)
 
print(arrayMinimum(A, N))
 
# This code is contributed
# by Anant Agarwal.

C#

// C# program to find minimum in an
// array without using Relational Operators
using System;
 
class GFG
{
     
// Function to find minimum between two
// non-negative numbers without
// using relational operator.
static int minimum(int x, int y)
{
    int c = 0;
 
    // Continues till any
    // element becomes zero
    while (x > 0 && y > 0)
    {
        x--;
        y--;
        c++;
    }
    return c;
}
 
// Function to find minimum in an array.
static int arrayMinimum(int []A, int N)
{
     
    // calculating minimum of
    // first two numbers
    int mn = A[0];
 
    // Iterating through each of the
    // member of the array to
    // calculate the minimum
    for (int i = N - 1; i > 0; i--)
 
        // Finding the minimum between current
        // minimum and current value.
        mn = minimum(mn, A[i]);
 
    return mn;
}
 
// Driver code
public static void Main()
{
    int []A = {2, 3, 1, 4};
    int N = A.Length;
    Console.WriteLine(arrayMinimum(A, N));
}
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program to find minimum
// in an array without using
// Relational Operators
 
// Function to find minimum
// between two non-negative
// numbers without using
// relational operator.
function minimum($x, $y)
{
    $c = 0;
 
    // Continues till any
    // element becomes zero.
    while ($x and $y)
    {
        $x--;
        $y--;
        $c++;
    }
    return $c;
}
 
// Function to find
// minimum in an array.
function arrayMinimum( $A, $N)
{
    // calculating minimum of
    // first two numbers
    $mn = $A[0];
 
    // Iterating through each
    // of the member of the
    // array to calculate
    // the minimum
    for ($i = $N - 1; $i; $i--)
 
        // Finding the minimum
        // between current minimum
        // and current value.
        $mn = minimum($mn, $A[$i]);
 
    return $mn;
}
 
// Driver code
$A = array(2, 3, 1, 4);
$N = count($A);
echo arrayMinimum($A, $N);
 
// This code is contributed
// by anuj_67.
?>

Javascript

<script>
    // Javascript program to find minimum in an
    // array without using Relational Operators
     
    // Function to find minimum between two
    // non-negative numbers without
    // using relational operator.
    function minimum(x, y)
    {
        let c = 0;
 
        // Continues till any
        // element becomes zero
        while (x > 0 && y > 0)
        {
            x--;
            y--;
            c++;
        }
        return c;
    }
 
    // Function to find minimum in an array.
    function arrayMinimum(A, N)
    {
 
        // calculating minimum of
        // first two numbers
        let mn = A[0];
 
        // Iterating through each of the
        // member of the array to
        // calculate the minimum
        for (let i = N - 1; i > 0; i--)
 
            // Finding the minimum between current
            // minimum and current value.
            mn = minimum(mn, A[i]);
 
        return mn;
    }
     
    let A = [2, 3, 1, 4];
    let N = A.length;
    document.write(arrayMinimum(A, N));
 
// This code is contributed by divyesh072019.
</script>

Producción: 

1

La complejidad temporal del código será O(N*max) donde max es el máximo de los elementos de la array.

Limitaciones: esto solo funcionará si la array contiene todos los enteros no negativos.

Publicación traducida automáticamente

Artículo escrito por ShivamKD 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 *