Find position of the leader element in given Array with given operations
Last Updated :
27 Jan, 2022
Given an array arr[]. The task is to find the position of the leader element in arr[]. The leader element is the one, which can remove all other elements in the array using the below operations.
- If arr[i] > arr[i + 1], It removes the (i+1)th element and increment their value by 1 and decrease the size of array by 1.
- If arr[i] > arr[i – 1], It removes the (i-1)th element and increment their value by 1 and decrease the size of array by 1.
Examples
Input: arr[] = { 5, 3, 4, 4, 5 }
Output: 3
Explanation: Following are the operations performed in array arr[]
A3 remove A2 and increment by 1 and array becomes { 5, 5, 4, 5 }
A2 remove A3 and increment by 1 and array becomes { 5, 6, 5 }
A2 remove A1 and increment by 1 and array becomes { 7, 5 }
A1 remove A2 and increment by 1 and array becomes { 8 }
Hence, The position of leader of array is 3.
Input: arr[] = { 4, 4, 3, 4, 4 }
Output: 2
Explanation: Following are the operations performed in array arr[]
A2 remove A3 and increment by 1 and array becomes { 4, 5, 4, 4 }
A2 remove A1 and increment by 1 and array becomes { 6, 4, 4 }
A1 remove A2 and increment by 1 and array becomes { 7, 4 }
A1 remove A2 and increment by 1 and array becomes { 8 }
Hence, The position of leader of array is 2.
Input: arr[] = { 1, 1, 1 }
Output: -1
Explanation: No leader is present in the array
Approach: This problem is implementation-based. Follow the steps below to solve the given problem.
- Find the maximum and minimum elements of the array arr.
- If the minimum and maximum elements are the same that means no leader element is present.
- Traverse the array.
- Now, check if arr[i] is max element.
- Check arr[i-1] or arr[i+1] is smaller than max .
- So, that element is the leader element in the array.
- Print the position of the leader element found.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int LeaderElement( int arr[], int n)
{
int maxElement = INT_MIN,
minElement = INT_MAX;
for ( int i = 0; i < n; i++) {
maxElement = max(maxElement, arr[i]);
minElement = min(minElement, arr[i]);
}
if (maxElement == minElement) {
return -1;
}
int ans = -1;
for ( int i = 0; i < n; i++) {
if (arr[i] == maxElement) {
if (i > 0
and arr[i] > arr[i - 1]) {
ans = i + 1;
break ;
}
if (i < n - 1
and arr[i] > arr[i + 1]) {
ans = i + 1;
break ;
}
}
}
return ans;
}
int main()
{
int arr[] = { 4, 4, 3, 4, 4 };
int N = 5;
int ans = LeaderElement(arr, N);
cout << ans;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static int LeaderElement( int arr[], int n)
{
int maxElement = Integer.MIN_VALUE;
int minElement = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++) {
maxElement = Math.max(maxElement, arr[i]);
minElement = Math.min(minElement, arr[i]);
}
if (maxElement == minElement) {
return - 1 ;
}
int ans = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == maxElement) {
if (i > 0 && arr[i] > arr[i - 1 ]) {
ans = i + 1 ;
break ;
}
if (i < n - 1 && arr[i] > arr[i + 1 ]) {
ans = i + 1 ;
break ;
}
}
}
return ans;
}
public static void main (String[] args) {
int arr[] = { 4 , 4 , 3 , 4 , 4 };
int N = 5 ;
int ans = LeaderElement(arr, N);
System.out.print(ans);
}
}
|
Python
import sys
def LeaderElement(arr, n):
maxElement = - sys.maxsize - 1
minElement = sys.maxint
for i in range (n):
maxElement = max (maxElement, arr[i])
minElement = min (minElement, arr[i])
if (maxElement = = minElement):
return - 1
ans = - 1
for i in range (n):
if (arr[i] = = maxElement):
if (i > 0 and arr[i] > arr[i - 1 ]):
ans = i + 1
break
if (i < n - 1 and arr[i] > arr[i + 1 ]):
ans = i + 1
break
return ans
arr = [ 4 , 4 , 3 , 4 , 4 ]
N = len (arr)
ans = LeaderElement(arr, N)
print (ans)
|
C#
using System;
class GFG
{
static int LeaderElement( int []arr, int n)
{
int maxElement = Int32.MinValue;
int minElement = Int32.MaxValue;
for ( int i = 0; i < n; i++) {
maxElement = Math.Max(maxElement, arr[i]);
minElement = Math.Min(minElement, arr[i]);
}
if (maxElement == minElement) {
return -1;
}
int ans = -1;
for ( int i = 0; i < n; i++) {
if (arr[i] == maxElement) {
if (i > 0 && arr[i] > arr[i - 1]) {
ans = i + 1;
break ;
}
if (i < n - 1 && arr[i] > arr[i + 1]) {
ans = i + 1;
break ;
}
}
}
return ans;
}
public static void Main () {
int []arr = { 4, 4, 3, 4, 4 };
int N = 5;
int ans = LeaderElement(arr, N);
Console.Write(ans);
}
}
|
Javascript
<script>
function LeaderElement(arr, n) {
let maxElement = Number.MIN_SAFE_INTEGER,
minElement = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < n; i++) {
maxElement = Math.max(maxElement, arr[i]);
minElement = Math.min(minElement, arr[i]);
}
if (maxElement == minElement) {
return -1;
}
let ans = -1;
for (let i = 0; i < n; i++) {
if (arr[i] == maxElement) {
if (i > 0 && arr[i] > arr[i - 1]) {
ans = i + 1;
break ;
}
if (i < n - 1 && arr[i] > arr[i + 1]) {
ans = i + 1;
break ;
}
}
}
return ans;
}
let arr = [4, 4, 3, 4, 4];
let N = 5;
let ans = LeaderElement(arr, N);
document.write(ans)
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...