- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Largest Number At Least Twice of Others in Python

Suppose we have an integer array called nums, now there is always exactly one largest element. We have to check whether the largest element in the array is at least twice as much as every other number in the array. If it is so, then we have to find the index of the largest element, otherwise return -1.

So, if the input is like [3,6,1,0], then the output will be 1, as 6 is the largest number, and for every other number in the array x, 6 is more than twice as big as x. As the index of 6 is 1, then the output is also 1.

To solve this, we will follow these steps −

- maximum := maximum of nums
- for i in range 0 to size of nums, do
- if nums[i] is same as maximum, then
- maxindex := i

- if nums[i] is not same as maximum and maximum < 2*(nums[i]), then
- return -1

- if nums[i] is same as maximum, then

Let us see the following implementation to get better understanding −

## Example

class Solution: def dominantIndex(self, nums): maximum = max(nums) for i in range(len(nums)): if nums[i] == maximum: maxindex = i if nums[i] != maximum and maximum < 2*(nums[i]): return -1 return maxindex ob = Solution() print(ob.dominantIndex([3, 6, 1, 0]))

## Input

[3, 6, 1, 0]

## Output

1

- Related Questions & Answers
- Program to find largest average of sublist whose size at least k in Python
- Largest sum subarray with at-least k numbers in C++
- Get at least x number of rows in MySQL?
- Largest Number in Python
- Python Program to Extract Strings with at least given number of characters from other list
- Largest Unique Number in Python
- Can I import same package twice? Will JVM load the package twice at runtime?
- How to check if a string has at least one letter and one number in Python?
- Program to find number of elements in A are strictly less than at least k elements in B in Python
- Find the number of binary strings of length N with at least 3 consecutive 1s in C++
- Program to find size of smallest sublist whose sum at least target in Python
- Design a DFA of a string with at least two 0’s and at least two 1’s
- Largest Number By Two Times in Python
- Append Odd element twice in Python
- Maximum average of a subarray of size of at least X and at most Y in C++

Advertisements