📌  相关文章
📜  在不使用关系运算符的情况下查找数组中的最大值

📅  最后修改于: 2022-05-13 01:57:48.411000             🧑  作者: Mango

在不使用关系运算符的情况下查找数组中的最大值

给定一个非负整数数组 A[],在不使用关系运算符的情况下找到数组中的最大值。
例子:

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

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

我们使用重复减法来找出最大值。为了找到两个数字之间的最大值,我们将变量计数器初始化为零。我们不断减少这两个值,直到它们都等于零(注意:第一个变为零的值不会进一步减少),同时增加计数器。虽然这两个值都变为零,但计数器已增加到两者的最大值。我们首先找到前两个数字的最大值,然后将其与数组的其余元素一一比较,以找到整体最大值。
下面是上述思想的实现。

C++
#include 
using namespace std;
 
// Function to find maximum between two non-negative
// numbers without using relational operator.
int maximum(int x, int y)
{
    int c = 0;
 
    // Continues till both becomes zero.
    while(x || y)
    {
        // decrement if the value is not already zero
        if(x)
        x--;
 
        if(y)
        y--;
        c++;
    }
    return c;
}
 
// Function to find maximum in an array.
int arrayMaximum(int A[], int N)
{
    // calculating maximum of first two numbers
    int mx = A[0];
     
    // Iterating through each of the member of the array
    // to calculate the maximum
    for (int i = N-1; i; i--)
 
        // Finding the maximum between current maximum
       // and current value.
        mx = maximum(mx, A[i]);
     
    return mx;
}
 
// Driver code
int main()
{
    // Array declaration
    int A[] = {4, 8, 9, 18};
    int N = sizeof(A) / sizeof(A[0]);
     
    // Calling Function to find the maximum of the Array
    cout << arrayMaximum(A, N);
    return 0;
}


Java
import java.io.*;
 
class GFG {
     
    // Function to find maximum between two
    // non-negative numbers without using
    // relational operator.
    static int maximum(int x, int y)
    {
        int c = 0;
 
        // Continues till both becomes zero.
        while (x > 0 || y > 0) {
             
            // decrement if the value is not
            // already zero
            if (x > 0)
                x--;
 
            if (y > 0)
                y--;
            c++;
        }
         
        return c;
    }
 
    // Function to find maximum in an array.
    static int arrayMaximum(int A[], int N)
    {
         
        // calculating maximum of first
        // two numbers
        int mx = A[0];
 
        // Iterating through each of the
        // member of the array to calculate
        // the maximum
        for (int i = N - 1; i > 0; i--)
 
            // Finding the maximum between
            // current maximum and current
            // value.
            mx = maximum(mx, A[i]);
 
        return mx;
    }
 
    // Driver code
    public static void main(String[] args)
    {
         
        // Array declaration
        int A[] = { 4, 8, 9, 18 };
        int N = A.length;
 
        // Calling Function to find the maximum
        // of the Array
        System.out.print(arrayMaximum(A, N));
    }
}
 
// This code is contributed by vt_m.


Python3
# Function to find maximum between two
# non-negative numbers without using
# relational operator.
def maximum(x, y):
    c = 0
 
    # Continues till both becomes zero.
    while(x or y):
         
        # decrement if the value is
        # not already zero
        if(x):
            x -= 1
 
        if(y):
            y -= 1
        c += 1
 
    return c
 
# Function to find maximum in an array.
def arrayMaximum(A, N):
     
    # calculating maximum of
    # first two numbers
    mx = A[0]
     
    # Iterating through each of
    # the member of the array
    # to calculate the maximum
    i = N - 1
    while(i):
         
        # Finding the maximum between
        # current maximum and current value.
        mx = maximum(mx, A[i])
        i -= 1
     
    return mx
 
# Driver code
if __name__ == '__main__':
     
    # Array declaration
    A = [4, 8, 9, 18]
    N = len(A)
     
    # Calling Function to find the
    # maximum of the Array
    print(arrayMaximum(A, N))
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to Find maximum
// in an array without using
// Relational Operators
using System;
 
class GFG
{
     
    // Function to find maximum
    // between two non-negative
    // numbers without using
    // relational operator.
    static int maximum(int x,
                       int y)
    {
        int c = 0;
 
        // Continues till
        // both becomes zero.
        while (x > 0 || y > 0)
        {
             
            // decrement if
            // the value is not
            // already zero
            if (x > 0)
                x--;
 
            if (y > 0)
                y--;
            c++;
        }
         
        return c;
    }
 
    // Function to find
    // maximum in an array.
    static int arrayMaximum(int []A,
                            int N)
    {
         
        // calculating
        // maximum of first
        // two numbers
        int mx = A[0];
 
        // Iterating through
        // each of the member
        // of the array to
        // calculate the maximum
        for (int i = N - 1;
                 i > 0; i--)
 
            // Finding the maximum
            // between current
            // maximum and current
            // value.
            mx = maximum(mx, A[i]);
 
        return mx;
    }
 
    // Driver code
    public static void Main()
    {
         
        // Array declaration
        int []A = { 4, 8, 9, 18 };
        int N = A.Length;
 
        // Calling Function to
        // find the maximum
        // of the Array
        Console.WriteLine(arrayMaximum(A, N));
    }
}
 
// This code is contributed
// by anuj_67.


PHP


Javascript


输出:

18

代码的时间复杂度为O(N*max) ,其中 max 是数组元素的最大值。
限制:这仅在数组包含所有非负整数时才有效。