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

📅  最后修改于: 2021-04-24 20:30:32             🧑  作者: Mango

给定数组arr [] ,任务是检查数组中的最小元素是否小于或等于其他元素的一半。如果是,则打印“是”,否则打印“否”。

注意:给定数组中的最小数目始终是唯一的。

例子:

方法1:

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

方法2:

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

下面是上述方法的实现:

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


输出:
No

时间复杂度: O(n)