Maximum in array which is at-least twice of other elements
Last Updated :
03 Mar, 2023
Given an array of integers of length n. Our task is to return the index of the max element if the it is at least twice as much as every other number in the array. If the max element does not satisfy the condition return -1.
Examples:
Input : arr = {3, 6, 1, 0}
Output : 1
Here, 6 is the largest integer, and for
every other number in the array x, 6 is
more than twice as big as x. The index of
value 6 is 1, so we return 1.
Input : arr = {1, 2, 3, 4}
Output : -1
4 isn't at least as big as twice the value
of 3, so we return -1.
Approach : Scan through the array to find the unique largest element m, keeping track of it's index maxIndex. Scan through the array again. If we find some x != m with m < 2*x, we should return -1. Otherwise, we should return maxIndex.
Algorithm:
Step 1: Start
Step 2: Create a function of int return type name "findIndex" which takes an integer array as an input parameter and returns index of max element that satisfies the condition.
a. initialize an int variable called "maxIndex" with value 0.
b. start a for loop from i=0 to i < length of the array
1. check if the value of the current index is greater than the value at the maxIndex of the array if true then set maxIndex to the current index.
c. start a for loop from i=0 to i < length of the array
1. check if maxIndex is not equal to the current index and value of the array at maxIndex is less than twice of the value of the array at the current index if true then return -1
2. If there does not exist such a pair and you came out of the loop then return the maxIndex.
Step 3: End
Implementation:
C++
// CPP program for Maximum of the array which is at least
// twice of other elements of the array.
#include <bits/stdc++.h>
using namespace std;
// Function to find the index of Max element that satisfies
// the condition
int findIndex(int arr[], int len)
{
// Finding index of max of the array
int maxIndex = 0;
for (int i = 0; i < len; ++i)
if (arr[i] > arr[maxIndex])
maxIndex = i;
// Returns -1 if the max element is not twice of the
// i-th element.
for (int i = 0; i < len; ++i)
if (maxIndex != i && arr[maxIndex] < 2 * arr[i])
return -1;
return maxIndex;
}
// Driver function
int main()
{
int arr[] = { 3, 6, 1, 0 };
int len = sizeof(arr) / sizeof(arr[0]);
printf("%d", findIndex(arr, len));
}
// This code is contributed by Sania Kumari Gupta
C
// C program for Maximum of the array which is at least
// twice of other elements of the array.
#include <stdio.h>
// Function to find the index of Max element that satisfies
// the condition
int findIndex(int arr[], int len)
{
// Finding index of max of the array
int maxIndex = 0;
for (int i = 0; i < len; ++i)
if (arr[i] > arr[maxIndex])
maxIndex = i;
// Returns -1 if the max element is not twice of the
// i-th element.
for (int i = 0; i < len; ++i)
if (maxIndex != i && arr[maxIndex] < 2 * arr[i])
return -1;
return maxIndex;
}
// Driver function
int main()
{
int arr[] = { 3, 6, 1, 0 };
int len = sizeof(arr) / sizeof(arr[0]);
printf("%d", findIndex(arr, len));
}
// This code is contributed by Sania Kumari Gupta
Java
// Java program for Maximum of the array
// which is at least twice of other elements
// of the array.
import java.util.*;
import java.lang.*;
class GfG {
// Function to find the index of Max element
// that satisfies the condition
public static int findIndex(int[] arr) {
// Finding index of max of the array
int maxIndex = 0;
for (int i = 0; i < arr.length; ++i)
if (arr[i] > arr[maxIndex])
maxIndex = i;
// Returns -1 if the max element is not
// twice of the i-th element.
for (int i = 0; i < arr.length; ++i)
if (maxIndex != i && arr[maxIndex] < 2 * arr[i])
return -1;
return maxIndex;
}
// Driver function
public static void main(String argc[]){
int[] arr = new int[]{3, 6, 1, 0};
System.out.println(findIndex(arr));
}
}
Python3
# Python 3 program for Maximum of
# the array which is at least twice
# of other elements of the array.
# Function to find the index of Max
# element that satisfies the condition
def findIndex(arr):
# Finding index of max of the array
maxIndex = 0
for i in range(0,len(arr)):
if (arr[i] > arr[maxIndex]):
maxIndex = i
# Returns -1 if the max element is not
# twice of the i-th element.
for i in range(0,len(arr)):
if (maxIndex != i and
arr[maxIndex] < (2 * arr[i])):
return -1
return maxIndex
# Driver code
arr = [3, 6, 1, 0]
print(findIndex(arr))
# This code is contributed by Smitha Dinesh Semwal
C#
// C# program for Maximum of the array
// which is at least twice of other elements
// of the array.
using System;
class GfG {
// Function to find the index of Max element
// that satisfies the condition
public static int findIndex(int[] arr) {
// Finding index of max of the array
int maxIndex = 0;
for (int i = 0; i < arr.Length; ++i)
if (arr[i] > arr[maxIndex])
maxIndex = i;
// Returns -1 if the max element is not
// twice of the i-th element.
for (int i = 0; i < arr.Length; ++i)
if (maxIndex != i && arr[maxIndex] < 2 * arr[i])
return -1;
return maxIndex;
}
// Driver function
public static void Main()
{
int[] arr = new int[]{3, 6, 1, 0};
Console.WriteLine(findIndex(arr));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program for Maximum of
// the array which is at least
// twice of other elements of
// the array.
// Function to find the
// index of Max element
// that satisfies the
// condition
function findIndex($arr, $len)
{
// Finding index of
// max of the array
$maxIndex = 0;
for ( $i = 0; $i < $len; ++$i)
if ($arr[$i] > $arr[$maxIndex])
$maxIndex = $i;
// Returns -1 if the
// max element is not
// twice of the i-th
// element.
for ($i = 0; $i < $len; ++$i)
if ($maxIndex != $i and
$arr[$maxIndex] < 2 * $arr[$i])
return -1;
return $maxIndex;
}
// Driver Code
$arr = array(3, 6, 1, 0);
$len = count($arr);
echo findIndex($arr, $len);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program for Maximum of
// the array which is at least
// twice of other elements of
// the array.
// Function to find the
// index of Max element
// that satisfies the
// condition
function findIndex(arr, len) {
// Finding index of
// max of the array
let maxIndex = 0;
for (let i = 0; i < len; ++i)
if (arr[i] > arr[maxIndex])
maxIndex = i;
// Returns -1 if the
// max element is not
// twice of the i-th
// element.
for (let i = 0; i < len; ++i)
if (maxIndex != i &&
arr[maxIndex] < 2 * arr[i])
return -1;
return maxIndex;
}
let arr = [3, 6, 1, 0];
let len = arr.length;
document.write(findIndex(arr, len));
// This code is contributed by divyeshrabadiya07.
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Find the maximum elements in the first and the second halves of the Array , Given an array arr[] of N integers. The task is to find the largest elements in the first half and the second half of the array. Note that if the size of the array is odd then the middle element will be included in both halves.Examples: Input: arr[] = {1, 12, 14, 5} Output: 12, 14 First half is {1
6 min read
Maximum value K such that array has at-least K elements that are >= K Given an array of positive integers, find maximum possible value K such that the array has at-least K elements that are greater than or equal to K. The array is unsorted and may contain duplicate values. Examples : Input: [2, 3, 4, 5, 6, 7] Output: 4 Explanation : 4 elements [4, 5, 6, 7] are greater
14 min read
Maximum sum of increasing order elements from n arrays Given n arrays of size m each. Find the maximum sum obtained by selecting a number from each array such that the elements selected from the i-th array are more than the element selected from (i-1)-th array. If maximum sum cannot be obtained then return 0.Examples: Input : arr[][] = {{1, 7, 3, 4}, {4
13 min read
Maximum element in an array which is equal to its frequency Given an array of integers arr[] of size N, the task is to find the maximum element in the array whose frequency equals to it's value Examples: Input: arr[] = {3, 2, 2, 3, 4, 3} Output: 3 Frequency of element 2 is 2 Frequency of element 3 is 3 Frequency of element 4 is 1 2 and 3 are elements which h
11 min read
Minimum index of element with maximum multiples in Array Given an array arr[] of length n, the task is to calculate the min index of the element which has maximum elements present in the form of {2 * arr[i], 3 * arr[i], 4 * arr[i], 5 * arr[i]}. Examples: Input: n = 4. arr[] = {2, 1, 3, 6}Output: 1Explanation: For 2, {2*2, 3*2, 4*2, 5*2} => {4, 6, 8, 10
5 min read