Maximum in array which is at-least twice of other elements


In this article, we will discuss different approaches to point out the greatest element in the array which is at least twice of all the other elements in the same array.

Problem Statement

An array of n different elements is given to us, we have to find out the maximum element in the given array "nums" such that it is either greater than or equal to twice of all the other elements in that array.

In other words, we can also say the we have to find out whether or not all the other elements of the given array are less than or equal to half of the largest element of the array.

Let us now understand the problem statement with examples −

Sample Example 1

Input: nums = [ 2, 4, 1, 0 ]
Output: Index 1 contains the element in the array that is at least two times larger than any of the other elements.

Explanation

The largest element is 4, and it is at least two times of all other elements in the array.

Sample Example 2

Input: nums = [ 5, 2, 9, 1 ]
Output: No such element found which is at least two times or greater than all the other elements.

Explanation

The largest element is 9, and it is at least two times of all the other elements in the array.

There can be various approaches to solve this problem but let us first discuss the brute force approach −

Approach 1

This is the brute force approach in which, we are comparing each element in the array with each other elements of the array and checks if an element is either greater than or equal to twice of all the other in that array. if an element is neither greater than nor equal to twice of all the other in that array, the code moves to the next element. If a valid element is found, the code records its index and stops looking for other elements.

Example

#include <iostream>
using namespace std;

int greatestelem ( int size, int nums[] ){
   int maxIndex = -1;
   for(int iterator =0; iterator < size; iterator++){
      bool flag = true;
      for(int temp =0; temp <size; temp++){
         if(iterator!=temp && nums[iterator]<2*nums[temp]){
            flag = false;
            break;
         }
      }
      if(flag){
         maxIndex = iterator;
         break;
      }
   }
   return maxIndex;
}
int main(){
   int nums[] = { 0, 1,  3, 6, 15 };
   int length = sizeof(nums) / sizeof( nums[0] ) ;
   int g= greatestelem( length , nums);
   if(g==-1){
      cout<< " No such element found which is greater than or equal to twice of all the other elements." << endl;
   } else {
      cout<< " The element in the array which is greater than or equal to twice of all the other elements is found at " << g << "th Index " << endl ;
   }
   return 0;
}

Output

The element in the array which is greater than or equal to twice of all the other elements is found at 4th Index
  • Time Complexity − The time complexity of the code used in this approach is O(m^2), where m is the length of the array we input.

    As we are using two nested loops, both for n times each, so the total number of inner loop iterations would be n multiplied by n, resulting in n^2 iterations.

  • Space Complexity − As we are only using constant space as variables, the space complexity for this approach is O(1).

Approach 2

In this approach, we will traverse the array twice only hence reducing the time complexity of the code. The intention behind traversing the array twice includes −

  • 1st traversal − To find out the maximum element present in the given array.

  • 2nd traversal − To check whether or not the greatest element in the array is at least twice of all the other elements present in the same array.

Example

Let us now look upon the code in C++ for this particular approach −

#include <bits/stdc++.h>
using namespace std;

int greatestelem(int nums[], int size){
   int IndMax = 0;
   for (int iterator = 0; iterator < size; ++iterator){
      if ( nums[iterator] > nums[IndMax]){
         IndMax = iterator;
      }
   }
   for (int iterator = 0; iterator < size; ++iterator){
      if ( IndMax != iterator && nums [IndMax] < 2 * nums[iterator]){
         return -1;
      }
   }
   return IndMax;
}
int main() {
   int nums[] = {3, 0, 2, 5, 15};
   int length = sizeof(nums) / sizeof( nums[0] ) ;
   int g= greatestelem(nums, length);
   if(g==-1)
      cout<< " No such element found which is greater than or equal to twice of all the other elements."<< endl;
   else 
      cout<< " The element in the array which is greater than or equal to twice of all the other elements is found at "<<g << "th Index " << endl;
   return 0;
}

Output

The element in the array which is greater than or equal to twice of all the other elements is found at 4th Index.
  • Time Complexity − The time complexity of this approach is O(length), where "length" is the size of the input array "nums". This reason for this time complexity is that the code two nested loops to iterate through the given array once each. The time complexity of finding the maximum element in the first loop is O(n), and the time complexity of checking if the largest element is greater then or equal to twice of all the other elements in the second loop is O(n). Thus, the overall time complexity is O(n) + O(n) = O(n).

  • Space Complexity − As we are only using constant space as variables, the space complexity for this approach is O(1).

Updated on: 05-Oct-2023

48 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements