📌  相关文章
📜  查找给定数组的峰值索引

📅  最后修改于: 2021-05-18 00:21:35             🧑  作者: Mango

给定一个由N ( > 2 )个整数组成的数组arr [] ,任务是找到该数组的峰值索引。如果数组不包含任何峰值索引,则输出-1

例子:

天真的方法:最简单的方法是遍历数组并检查[1,N – 2]范围内的每个索引,例如idx ,索引idx是否可以是数组的峰值索引。可以通过检查此索引idx左侧和右侧的所有元素是否必须严格按照递增和严格递减的顺序来完成此操作。检查所有索引之后,如果存在任何这样的索引,则打印该索引。否则,打印“ -1”

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

高效方法:也可以基于以下事实来优化上述方法仅当数组包含严格增加的前缀后跟严格减少的后缀时,峰索引才会存在。
请按照以下步骤解决问题:

  • 初始化两个变量,例如ans ,以存储数组的峰值元素的索引。
  • 使用变量i遍历索引[1,N – 2]范围内的给定数组。如果arr [i]的值大于或等于arr [i + 1] ,则将ans更新为i并退出循环。
  • 如果ans的值为0(N – 1) ,则打印“ -1” ,因为对于给定的数组不存在这样的峰值索引。
  • 现在,使用变量i遍历[ans,N – 2]范围内的给定数组,如果arr [i]的值小于或等于arr [i + 1] ,则跳出循环。
  • 完成上述步骤后,如果i的值为(N – 1),则打印ans的值作为结果峰指数。否则,打印“ -1”,因为对于给定的数组不存在这样的峰值索引。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the peak
// index for the given array
int peakIndex(int arr[], int N)
{
     
    // Base Case
    if (N < 3)
        return -1;
 
    int i = 0;
 
    // Check for strictly
    // increasing array
    while (i + 1 < N)
    {
         
        // If the strictly increasing
        // condition is violated, then break
        if (arr[i + 1] < arr[i] ||
            arr[i] == arr[i + 1])
            break;
             
        i++;
    }
 
    if (i == 0 || i == N - 1)
        return -1;
 
    // Stores the value of i, which
    // is a potential peak index
    int ans = i;
 
    // Second traversal, for
    // strictly decreasing array
    while (i < N - 1)
    {
         
        // When the strictly
        // decreasing condition is
        // violated, then break
        if (arr[i] < arr[i + 1] ||
            arr[i] == arr[i + 1])
            break;
             
        i++;
    }
 
    // If i = N - 1, it means that
    // ans is the peak index
    if (i == N - 1)
        return ans;
 
    // Otherwise, peak index doesn't exist
    return -1;
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 1, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << peakIndex(arr, N) << "\n";
 
    return 0;
}
 
// This code is contributed by Kingash


Java
// Java program for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the peak
    // index for the given array
    public static int peakIndex(int[] arr)
    {
        int N = arr.length;
 
        // Base Case
        if (arr.length < 3)
            return -1;
 
        int i = 0;
 
        // Check for strictly
        // increasing array
        while (i + 1 < N) {
 
            // If the strictly increasing
            // condition is violated, then break
            if (arr[i + 1] < arr[i]
                || arr[i] == arr[i + 1])
                break;
            i++;
        }
 
        if (i == 0 || i == N - 1)
            return -1;
 
        // Stores the value of i, which
        // is a potential peak index
        int ans = i;
 
        // Second traversal, for
        // strictly decreasing array
        while (i < N - 1) {
 
            // When the strictly
            // decreasing condition is
            // violated, then break
            if (arr[i] < arr[i + 1]
                || arr[i] == arr[i + 1])
                break;
            i++;
        }
 
        // If i = N - 1, it means that
        // ans is the peak index
        if (i == N - 1)
            return ans;
 
        // Otherwise, peak index doesn't exist
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 0, 1, 0 };
        System.out.println(peakIndex(arr));
    }
}


Python3
# Python3 program for the above approach
 
# Function to find the peak
# index for the given array
def peakIndex(arr):
     
    N = len(arr)
 
    # Base Case
    if (len(arr) < 3):
        return -1
 
    i = 0
 
    # Check for strictly
    # increasing array
    while (i + 1 < N):
 
        # If the strictly increasing
        # condition is violated, then break
        if (arr[i + 1] < arr[i] or
            arr[i] == arr[i + 1]):
            break
         
        i += 1
 
    if (i == 0 or i == N - 1):
        return -1
 
    # Stores the value of i, which
    # is a potential peak index
    ans = i
 
    # Second traversal, for
    # strictly decreasing array
    while (i < N - 1):
 
        # When the strictly
        # decreasing condition is
        # violated, then break
        if (arr[i] < arr[i + 1] or
            arr[i] == arr[i + 1]):
            break
         
        i += 1
 
    # If i = N - 1, it means that
    # ans is the peak index
    if (i == N - 1):
        return ans
 
    # Otherwise, peak index doesn't exist
    return -1
 
# Driver Code
if __name__ == '__main__':
     
    arr = [0, 1, 0]
     
    print(peakIndex(arr))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find the peak
// index for the given array
public static int peakIndex(int[] arr)
{
    int N = arr.Length;
 
    // Base Case
    if (arr.Length < 3)
        return -1;
 
    int i = 0;
 
    // Check for strictly
    // increasing array
    while (i + 1 < N)
    {
         
        // If the strictly increasing
        // condition is violated, then break
        if (arr[i + 1] < arr[i] ||
            arr[i] == arr[i + 1])
            break;
             
        i++;
    }
 
    if (i == 0 || i == N - 1)
        return -1;
 
    // Stores the value of i, which
    // is a potential peak index
    int ans = i;
 
    // Second traversal, for
    // strictly decreasing array
    while (i < N - 1)
    {
         
        // When the strictly
        // decreasing condition is
        // violated, then break
        if (arr[i] < arr[i + 1] ||
            arr[i] == arr[i + 1])
            break;
             
        i++;
    }
 
    // If i = N - 1, it means that
    // ans is the peak index
    if (i == N - 1)
        return ans;
 
    // Otherwise, peak index doesn't exist
    return -1;
}
 
// Driver Code
static public void Main()
{
    int[] arr = { 0, 1, 0 };
     
    Console.WriteLine(peakIndex(arr));
}
}
 
// This code is contributed by splevel62


输出:
1

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