Largest Number At Least Twice of Others in Java12 May 2025 | 3 min read Given an integer array with unique values for the largest integer. Check to see if the greatest number in the array is at least twice as large as all the other numbers. If it is, return the greatest element's index; if not, return -1. Example 1: Input: int arr = [10,5,2,1] Output: 0 Explanation: The greatest integer is 10. It is at least twice as large as all the other numbers in the array. We return zero because the index of 10 is 0. Example 2: Input: int arr = [8,3,1,0] Output: 0 Explanation: The largest integer is eight, which is at least twice as large as every other number. We return zero since the index of 8 is 0. Example 3: Input: int arr = [0,0,3,2] Output: -1 Explanation: Since three is not at least twice as large as 2, we return -1 even if it is the largest integer. Approach: Naïve ApproachUsing a single pass, the code effectively determines the greatest number in an array and determines if it is at least twice as large as each other number (O(n) time complexity). To ensure that there are as few comparisons as possible, it keeps track of the largest and second-largest items using two variables. The logic is kept straightforward while correctness is guaranteed by the conditional check maximum >= secondMax * 2. Performance is improved by the program's avoidance of pointless sorting and nested loops. Because no additional data structures have been implemented, memory utilization stays constant (O(1) space complexity). Because of its efficient architecture, the program can handle big datasets. Algorithm: Step 1: Set MaxIndex, maximum, and secondMax to their initial values. Step 2: Go through the array in iterations. Step 2.1: Update secondMax to maximum if the current element exceeds it. Step 2.1.1: Change the current element's maximum. Step 2.1.2: Change MaxIndex to the most recent index. Step 2.2: If, on the other hand, the current element exceeds secondMax. Step 2.2.1: The current element should be updated in secondMax. Step 3: Check the condition: Return MaxIndex if the maximum is more than or equal to twice the second max; return -1 otherwise. Implementation:Output: 0 Complexity Analysis: The time complexity of the above is O(N), where 'N' represents a number of elements and the space complexity is O(1). Next TopicBFS Algorithm in Java |
Types of Arrays in Java
An array is a linear data structure in Java. It allows us to store multiple values of the same data type. They are used as objects in Java. For primitive data types such as int or char, the original values are stored in memory locations....
8 min read
Count Ones in a Sorted binary array in Java
A sorted binary array is given (an array that only contains 0's and 1's is a binary array). The task is to find the number of ones that are present in the binary sorted array. Example: 1 Input: int arr[] = {0, 0, 0, 0, 1, 1, 1, 1,...
5 min read
DecimalFormat getPositiveSuffix() method in Java
The Java DecimalFormat class's getPositiveSuffix() method is used to retrieve this DecimalFormat instance's positive suffix value. Syntax: public String getPositiveSuffix() Parameters: No parameters are accepted by this method. Return Value: The DecimalFormat instance's positive suffix value is returned by this method. Example 1: The DecimalFormat class in Java is used in this...
2 min read
How to Create an Unmodifiable List in Java
? In Java, when we want to be sure that a list's contents cannot be changed after it is created, there are situations in which creating an unmodifiable list in Java might be very important. In this section, we will discuss how to create an unmodifiable List...
4 min read
How to Call Generic Method in Java
? In Java, generic methods are methods that can work with multiple types of data. They are an important feature of the language that allows for more flexible and reusable code. In this article, we will discuss how to call generic methods in Java. To call a generic...
4 min read
Alien Dictionary Problem in Java
The Alien Dictionary problem determines the order of characters in an unknown language by analyzing a sorted list of words. It constructs a directed graph based on character precedence, detects cycles to ensure a valid order exists, and applies topological sorting to find the correct sequence...
6 min read
Executor Framework Java
A framework having a bunch of components that are used for managing worker threads efficiently is referred to as Executor Framework. The Executor API reduces the execution of the task from the actual task to be executed through the Executors. The executor framework is an implementation...
8 min read
How to Check Current JDK Version installed in Your System Using CMD
? There are different versions of Java is available. Some of the applications generally require different version because of compatibility problems. In this section, we will learn how to check the Java version in Windows using CMD. A version string contains a version number optionally followed by pre-release...
2 min read
Abstract Syntax Tree (AST) in Java
Abstract Syntax Tree is a computer language's abstract syntactic structure is represented by a type of tree called a tree. A construct that is present in the source code is indicated by each node of the tree. Typically, an AST is the output of a compiler's syntax...
3 min read
Java Practice Programs
Java is a popular programming language that is used to develop a wide variety of applications. One of the best ways to learn Java is to practice writing programs. Many resources are available online and in libraries to help you find Java practice programs. When practising...
10 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India