Check if minimum element in array is less than or equals half of every other element
Given an array arr[], the task is to check if the minimum element in the array is less than or equal to half of every other element. If it is then print “yes” otherwise print “no”.
Note: The minimum number in the given array is always unique.
Examples:
Input: arr = {2, 1, 4, 5}
Output: Yes
Explanation:
1 is the minimum element in the array arr[] and on dividing 2, 4, 5 by 2 we get 1, 2, 2.5 which is greater than or equal to the minimum number. Hence, print “yes”.Input : arr = {2, 4, 5, 3}
Output : No
Explanation:
2 is the minimum element in the array arr[] and on dividing 4, 5, 3 by 2 we get 2, 2.5, 1.5 in which the integer 3 does not return a value which is greater than or equal to the minimum number ( 1.5 < 2). Hence, print "no".
Method 1:
To solve the problem mentioned above we have to find the smallest element with the help of loops and then scan through the entire array again and check if twice the smallest element is smaller than or equal to every other element. But this solution takes O(N) time using two loops and can be optimized further where only one iteration is involved.
Method 2:
To optimize the above solution we can find the smallest as well as the second smallest element in a single iteration itself. Then simply check if twice of the smallest element is smaller than or equal to the second smallest element.
Below is the implementation of the above approach:
// CPP implementation to Check if the minimum element in the // array is greater than or equal to half of every other elements #include <bits/stdc++.h> using namespace std; // Function to Check if the minimum element in the array is // greater than or equal to half of every other elements 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); } |
No
Time Complexity: O(n)
Recommended Posts:
- Reduce every element of the array to it's half retaining the sum zero
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Find if array has an element whose value is half of array sum
- Largest element smaller than current element on left for every element in Array
- Find Array formed by adding each element of given array with largest element in new array to its left
- Remove exactly one element from the array such that max - min is minimum
- Minimum sum of differences with an element in an array
- Sum of minimum absolute difference of each array element
- Array element with minimum sum of absolute differences
- Sum and Product of minimum and maximum element of an Array
- Sum of minimum element of all sub-sequences of a sorted array
- Minimum array element changes to make its elements 1 to N
- Sum of minimum element of all subarrays of a sorted array
- Replace each element by the difference of the total size of the array and frequency of that element
- Check for an array element that is co-prime with all others
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

