📌  相关文章
📜  检查数组中的最小元素是否小于或等于每个其他元素的一半

📅  最后修改于: 2021-09-07 04:38:58             🧑  作者: Mango

给定一个数组 arr[] ,任务是检查数组中的最小元素是否小于或等于每个其他元素的一半。如果是,则打印“是”,否则打印“否”。
注意:给定数组中的最小数字始终是唯一的。

例子:

方法一:
为了解决上面提到的问题,我们必须在循环的帮助下找到最小元素,然后再次扫描整个数组并检查最小元素的两倍是否小于或等于其他每个元素。但是这个解决方案使用两个循环需要 O(N) 时间,并且可以在只涉及一次迭代的情况下进一步优化。

方法二:
为了优化上述解决方案,我们可以在单次迭代中找到最小和第二小的元素。然后简单地检查最小元素的两倍是否小于或等于第二个最小元素。

下面是上述方法的实现:

C++
// C++ implementation to Check if the minimum element in the
// array is greater than or equal to half of every other elements
#include 
using namespace std;
 
// Function to Check if the minimum element in the array is
// greater than or equal to half of every other element
void checkMin(int arr[], int len)
{
 
    // Initialise the variables to store
    // smallest and second smallest
    int smallest = INT_MAX, secondSmallest = INT_MAX;
 
    for (int i = 0; i < len; i++) {
 
        // Check if current element is smaller than smallest,
        // the current smallest will become secondSmallest
        // and current element will be the new smallest
        if (arr[i] < smallest) {
            secondSmallest = smallest;
            smallest = arr[i];
        }
 
        // Check if current element is smaller than
        // secondSmallest simply update the latter
        else if (arr[i] < secondSmallest) {
            secondSmallest = arr[i];
        }
    }
 
    if (2 * smallest <= secondSmallest)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver code
int main()
{
    int arr[] = { 2, 3, 4, 5 };
 
    int len = sizeof(arr) / sizeof(arr[0]);
 
    checkMin(arr, len);
}


Java
// Java implementation to check
// if the minimum element in the
// array is greater than or equal
// to half of every other elements
import java.util.*;
class GFG{
 
// Function to Check if the minimum
// element in the array is greater
// than or equal to half of every
// other elements
static void checkMin(int arr[], int len)
{
     
    // Initialise the variables to store
    // smallest and second smallest
    int smallest = Integer.MAX_VALUE;
    int secondSmallest = Integer.MAX_VALUE;
 
    for(int i = 0; i < len; i++)
    {
        
       // Check if current element is smaller than 
       // smallest, the current smallest will 
       // become secondSmallest and current 
       // element will be the new smallest
       if (arr[i] < smallest)
       {
           secondSmallest = smallest;
           smallest = arr[i];
       }
        
       // Check if current element is smaller than
       // secondSmallest simply update the latter
       else if (arr[i] < secondSmallest)
       {
           secondSmallest = arr[i];
       }
    }
    if (2 * smallest <= secondSmallest)
        System.out.print("Yes");
    else
        System.out.print("No");
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 3, 4, 5 };
    int len = arr.length;
 
    checkMin(arr, len);
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 implementation to Check if
# the minimum element in the array
# is greater than or equal to half
# of every other element
import math
 
# Function to Check if the minimum element
# in the array is greater than or equal to
# half of every other element
def checkMin(arr, n):
 
    # Initialise the variables to store
    # smallest and second smallest
    smallest = math.inf
    secondSmallest = math.inf
 
    for i in range(n):
         
        # Check if current element is
        # smaller than smallest,
        # the current smallest will become
        # secondSmallest and current element
        # will be the new smallest
        if(arr[i] < smallest):
            secondSmallest = smallest
            smallest = arr[i]
             
        # Check if current element is smaller than
        # secondSmallest simply update the latter
        elif(arr[i] < secondSmallest):
            secondSmallest = arr[i]
 
    if(2 * smallest <= secondSmallest):
        print("Yes")
    else:
        print("No")
 
# Driver code
if __name__ == '__main__':
    arr = [ 2, 3, 4, 5 ]
 
    n = len(arr)
 
    checkMin(arr, n)
     
# This code is contributed by Shivam Singh.


C#
// C# implementation to check
// if the minimum element in the
// array is greater than or equal
// to half of every other elements
using System;
 
class GFG{
 
// Function to Check if the minimum
// element in the array is greater
// than or equal to half of every
// other elements
static void checkMin(int []arr, int len)
{
     
    // Initialise the variables to store
    // smallest and second smallest
    int smallest = int.MaxValue;
    int secondSmallest = int.MaxValue;
 
    for(int i = 0; i < len; i++)
    {
        
       // Check if current element is smaller than
       // smallest, the current smallest will
       // become secondSmallest and current
       // element will be the new smallest
       if (arr[i] < smallest)
       {
           secondSmallest = smallest;
           smallest = arr[i];
       }
        
       // Check if current element is smaller than
       // secondSmallest simply update the latter
       else if (arr[i] < secondSmallest)
       {
           secondSmallest = arr[i];
       }
    }
    if (2 * smallest <= secondSmallest)
        Console.Write("Yes");
    else
        Console.Write("No");
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 2, 3, 4, 5 };
    int len = arr.Length;
 
    checkMin(arr, len);
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
No

时间复杂度: O(n)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live