El más grande en array sin usar operadores condicionales/bit a bit/ternario

Defina una función int max(int ​​a[], int n) que devuelva el entero más grande de una array de enteros que consta de n elementos (sin usar condicionales/bit a bit/operadores ternarios/función de biblioteca para encontrar el mayor

Ejemplos: 

Input : arr[] = {16, 14, 15, 17, 9}
Output : 17

Input : arr[] = {10, 13, 11, 13}
Output : 13

La idea es similar a Máximo de cuatro números
Usamos el hecho de que el valor de “(x – y + abs(x – y))” será 0 de x es menor o igual que y. Usamos este valor como índice en una array de tamaño 2 para elegir el máximo. Una vez que hemos encontrado el máximo de dos elementos, podemos usar la misma técnica para encontrar el máximo de todos. 

Implementación:

C++

// CPP program to find largest in an array
// without conditional/bitwise/ternary/ operators
// and without library functions.
#include <bits/stdc++.h>
using namespace std;
 
int maxArray(int a[], int n)
{
    int tmp[2];
    tmp[0] = a[0];
    for (int i = 1; i < n; i++) {
        tmp[1] = a[i];
        swap(tmp[0], tmp[bool(abs(tmp[1] - tmp[0]) +
                                  tmp[1] - tmp[0])]);
    }
    return tmp[0];
}
 
// Driver code
int main()
{
    int a[] = { 15, 11, 17, 16, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << maxArray(a, n);
    return 0;
}

Java

// JAVA program to find largest in an array
// without conditional/bitwise/ternary/ operators
// and without library functions.
import java.util.*;
class GFG
{
 
static int maxArray(int a[], int n)
{
    int []tmp = new int[2];
    tmp[0] = a[0];
    for (int i = 1; i < n-1; i++) {
        tmp[1] = a[i];
       
        int temp = tmp[0];
        tmp[0] = tmp[(Math.abs(tmp[1] - tmp[0]) +
                tmp[1] - tmp[0])%2+1];
        tmp[(Math.abs(tmp[1] - tmp[0]) +
                tmp[1] - tmp[0])%2+1] = temp;
    }
    return tmp[1];
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 15, 11, 17, 16, 10 };
    int n =a.length;
    System.out.print(maxArray(a, n));
}
}
 
// This code is contributed by umadevi9616

Python3

# Python 3 program to find largest in an array
# without conditional/bitwise/ternary/ operators
# and without library functions.
 
def maxArray(a, n):
    tmp = [a[i] for i in range(len(a))]
    tmp[0] = a[0]
    for i in range(1,n,1):
        tmp[1] = a[i]
        temp = tmp[int(bool(abs(tmp[1] - tmp[0]) +
                                tmp[1] - tmp[0]))]
        tmp[int(bool(abs(tmp[1] - tmp[0]) +
                         tmp[1] - tmp[0]))] = tmp[0]
        tmp[0] = temp
     
    return tmp[0]
 
# Driver code
if __name__ == '__main__':
    a = [15, 11, 17, 16, 10]
    n = len(a)
    print(maxArray(a, n))
         
# This code is contributed by
# Surendra_Gangwar

C#

// C# program to find largest in an array
// without conditional/bitwise/ternary/ operators
// and without library functions.
using System;
 
public class GFG {
 
    static int maxArray(int[] a, int n)
    {
        int[] tmp = new int[2];
        tmp[0] = a[0];
        for (int i = 1; i < n - 1; i++) {
            tmp[1] = a[i];
 
            int temp = tmp[0];
            tmp[0] = tmp[(Math.Abs(tmp[1] - tmp[0]) + tmp[1]
                          - tmp[0]) % 2 + 1];
            tmp[(Math.Abs(tmp[1] - tmp[0]) + tmp[1]
                 - tmp[0]) % 2 + 1] = temp;
        }
        return tmp[1];
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[] a = { 15, 11, 17, 16, 10 };
        int n = a.Length;
        Console.Write(maxArray(a, n));
    }
}
 
// This code is contributed by umadevi9616

Javascript

<script>
 
// JavaScript program to find largest in an array
// without conditional/bitwise/ternary/ operators
// and without library functions.
function maxArray(a, n)
{
    var tmp = new Array(2);
    tmp[0] = a[0];
    for (var i = 1; i < n-1; i++) {
        tmp[1] = a[i];
       
        var temp = tmp[0];
        tmp[0] = tmp[(Math.abs(tmp[1] - tmp[0]) +
                tmp[1] - tmp[0])%2+1];
        tmp[(Math.abs(tmp[1] - tmp[0]) +
                tmp[1] - tmp[0])%2+1] = temp;
    }
    return tmp[1];
}
 
// Driver code
    var a = [ 15, 11, 17, 16, 10 ];
    var n =a.length;
    document.write(maxArray(a, n));
     
// This code is contributed by shivanisinghss2110
</script>
Producción

17

Publicación traducida automáticamente

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