Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Find maximum value of the last element after reducing the array with given operations
Next article icon

Find position of the leader element in given Array with given operations

Last Updated : 27 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// C++ program for above approach #include <bits/stdc++.h> using namespace std;  // Function to find the leader element in array int LeaderElement(int arr[], int n) {      // Initialize two variable     int maxElement = INT_MIN,         minElement = INT_MAX;      // Traverse the array     for (int i = 0; i < n; i++) {         maxElement = max(maxElement, arr[i]);         minElement = min(minElement, arr[i]);     }      // Now if both are equal return -1     if (maxElement == minElement) {         return -1;     }     // Now traverse the array and     // check if adjacent element     // of max element because     // maxelement of array always     // leader But if more than 1     // leader element so, check     // the adjacent elements of that     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; }  // Driver Code int main() {     int arr[] = { 4, 4, 3, 4, 4 };      int N = 5;      // Function Call     int ans = LeaderElement(arr, N);     cout << ans;     return 0; } 
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*;  class GFG  {      // Function to find the leader element in array   static  int LeaderElement(int arr[], int n)   {      // Initialize two variable     int maxElement = Integer.MIN_VALUE;     int  minElement =  Integer.MAX_VALUE;      // Traverse the array     for (int i = 0; i < n; i++) {       maxElement = Math.max(maxElement, arr[i]);       minElement = Math.min(minElement, arr[i]);     }      // Now if both are equal return -1     if (maxElement == minElement) {       return -1;     }     // Now traverse the array and     // check if adjacent element     // of max element because     // maxelement of array always     // leader But if more than 1     // leader element so, check     // the adjacent elements of that     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;      // Function Call     int ans = LeaderElement(arr, N);     System.out.print(ans);   } }  // This code is contributed by hrithikgarg03188 
Python
# Python3 program for the above approach # import the module import sys  # Function to find the leader element in array def LeaderElement(arr, n):      # Initialize two variable     maxElement = -sys.maxsize - 1     minElement =  sys.maxint      # Traverse the array     for i in range(n):                  maxElement = max(maxElement, arr[i])         minElement = min(minElement, arr[i])           # Now if both are equal return -1     if (maxElement == minElement):         return -1          # Now traverse the array and     # check if adjacent element     # of max element because     # maxelement of array always     # leader But if more than 1     # leader element so, check     # the adjacent elements of that     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   # Driver Code  # Given Input arr = [ 4, 4, 3, 4, 4 ] N = len(arr)  # Function Call ans = LeaderElement(arr, N) print(ans)     # This code is contributed by hrithikgarg03188. 
C#
// C# program for the above approach using System; class GFG  {      // Function to find the leader element in array   static  int LeaderElement(int []arr, int n)   {      // Initialize two variable     int maxElement = Int32.MinValue;     int minElement = Int32.MaxValue;      // Traverse the array     for (int i = 0; i < n; i++) {       maxElement = Math.Max(maxElement, arr[i]);       minElement = Math.Min(minElement, arr[i]);     }      // Now if both are equal return -1     if (maxElement == minElement) {       return -1;     }          // Now traverse the array and     // check if adjacent element     // of max element because     // maxelement of array always     // leader But if more than 1     // leader element so, check     // the adjacent elements of that     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;      // Function Call     int ans = LeaderElement(arr, N);     Console.Write(ans);   } }  // This code is contributed by Samim Hossain Mondal. 
JavaScript
<script> // Javascript program for above approach   // Function to find the leader element in array function LeaderElement(arr, n) {      // Initialize two variable     let maxElement = Number.MIN_SAFE_INTEGER,      minElement = Number.MIN_SAFE_INTEGER;      // Traverse the array     for (let i = 0; i < n; i++) {         maxElement = Math.max(maxElement, arr[i]);         minElement = Math.min(minElement, arr[i]);     }      // Now if both are equal return -1     if (maxElement == minElement) {         return -1;     }     // Now traverse the array and     // check if adjacent element     // of max element because     // maxelement of array always     // leader But if more than 1     // leader element so, check     // the adjacent elements of that     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; }  // Driver Code let arr = [4, 4, 3, 4, 4];  let N = 5;  // Function Call let ans = LeaderElement(arr, N); document.write(ans)  // This code is contributed by gfgking. </script> 

 
 


Output
2


 

Time Complexity: O(N) 
Auxiliary Space: O(1)


 


Next Article
Find maximum value of the last element after reducing the array with given operations
author
hrithikgarg03188
Improve
Article Tags :
  • Misc
  • Greedy
  • Mathematical
  • DSA
  • Arrays
Practice Tags :
  • Arrays
  • Greedy
  • Mathematical
  • Misc

Similar Reads

  • Find the index of the array elements after performing given operations K times
    Given an array arr[] and an integer K, the task is to print the position of the array elements, where the ith value in the result is the index of the ith element in the original array after applying following operations exactly K times: Remove the first array element and decrement it by 1.If it is g
    7 min read
  • Find maximum value of the last element after reducing the array with given operations
    Given an array arr[] of N elements, you have to perform the following operation on the given array until the array is reduced to a single elements, Choose two indices i and j such that i != j.Replace arr[i] with arr[i] - arr[j] and remove arr[j] from the array. The task is to maximize and print the
    7 min read
  • Make the array elements equal by performing given operations minimum number of times
    Given an array arr[] of size N, the task is to make all the array elements equal by performing following operations minimum number of times: Increase all array elements of any suffix array by 1.Decrease all the elements of any suffix array by 1.Replace any array element y another. Examples: Input: a
    7 min read
  • Find all indices of a given element in sorted form of given Array
    Given an array arr[] of integers of size N and a target value val. Your task is to find the indices of val in the array after sorting the array in increasing order. Note: The indices must be in increasing order. Examples: Input: arr = [1, 2, 5, 2, 3], val = 2Output: 1 2Explanation: After sorting, ar
    6 min read
  • Minimum operations of given type required to empty given array
    Given an array arr[] of size N, the task is to find the total count of operations required to remove all the array elements such that if the first element of the array is the smallest element, then remove that element, otherwise move the first element to the end of the array. Examples: Input: A[] =
    14 min read
  • Find position of an element in a sorted array of infinite numbers
    Given a sorted array arr[] of infinite numbers. The task is to search for an element k in the array. Examples: Input: arr[] = [3, 5, 7, 9, 10, 90, 100, 130, 140, 160, 170], k = 10Output: 4Explanation: 10 is at index 4 in array. Input: arr[] = [2, 5, 7, 9], k = 3Output: -1Explanation: 3 is not presen
    15+ min read
  • Find the first repeating element in an array of integers
    Given an array of integers arr[], The task is to find the index of first repeating element in it i.e. the element that occurs more than once and whose index of the first occurrence is the smallest. Examples: Input: arr[] = {10, 5, 3, 4, 3, 5, 6}Output: 5 Explanation: 5 is the first element that repe
    8 min read
  • Find the element having maximum premutiples in the array
    Given an array arr[], the task is to find the element which has the maximum number of pre-multiples present in the set. For any index i, pre-multiple is the number that is multiple of i and is present before the ith index of the array. Also, print the count of maximum multiples of that element in th
    8 min read
  • Find array elements with frequencies in range [l , r]
    Given an array of integers, find the elements from the array whose frequency lies in the range [l, r]. Examples: Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 } l = 2, r = 3 Output : 2 3 3 2 2 Approach : Take a hash map, which will store the frequency of all the elements in the array.Now, traverse once agai
    9 min read
  • Find the number of operations required to make all array elements Equal
    Given an array of N integers, the task is to find the number of operations required to make all elements in the array equal. In one operation we can distribute equal weights from the maximum element to the rest of the array elements. If it is not possible to make the array elements equal after perfo
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences