📌  相关文章
📜  使用给定操作在给定数组中查找前导元素的位置

📅  最后修改于: 2022-05-13 01:56:06.970000             🧑  作者: Mango

使用给定操作在给定数组中查找前导元素的位置

给定一个数组arr[] 。任务是在arr[]中找到领导元素的位置。领导者元素是一个,它可以使用以下操作删除数组中的所有其他元素。

  • 如果arr[i] > arr[i + 1] ,它会删除第(i+1)个元素并将它们的值增加1并将数组的大小减少1
  • 如果arr[i] > arr[i – 1] ,它会删除第(i-1)个元素并将它们的值增加1并将数组的大小减少1

例子

方法:这个问题是基于实现的。请按照以下步骤解决给定的问题。

  • 查找数组arr的最大和最小元素。
  • 如果最小和最大元素相同,则意味着不存在领导元素。
  • 遍历数组。
    • 现在,检查arr[i]是否是最大元素。
    • 检查arr[i-1]arr[i+1]是否小于 max 。
    • 因此,该元素是数组中的领导元素。
  • 打印找到的领导元素的位置。

下面是上述方法的实现。

C++
// C++ program for above approach
#include 
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



输出
2

时间复杂度: O(N)
辅助空间: O(1)