在不使用关系运算符的情况下查找数组中的最小值
给定一个非负整数数组 A[],在不使用关系运算符的情况下找到数组中的最小值。
例子:
Input : A[] = {2, 3, 1, 4, 5}
Output : 1
Input : A[] = {23, 17, 93}
Output : 17
我们使用重复减法来找出最小值。为了找到两个数字之间的最小值,我们将变量计数器初始化为零。我们不断减少这两个值,直到其中任何一个变为等于零,同时增加计数器。最小值首先达到零,并且计数器已增加到两者中的最小值。我们首先找到前两个数字的最小值,然后将其与数组的其余元素一一进行比较,以找到整体最小值。
下面是上述思想的实现。
C++
// C++ program to find minimum in an
// array without using Relational Operators
#include
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
Javascript
输出:
1
代码的时间复杂度为 O(N*max),其中 max 是数组元素的最大值。
限制:这仅在数组包含所有非负整数时才有效。