给定一个整数的数组arr [] 。任务是找到给定数组中所有局部最小值和局部最大值的索引。
例子:
Input: arr = [100, 180, 260, 310, 40, 535, 695]
Output:
Points of local minima: 0 4
Points of local maxima: 3 6
Explanation:
Given array can be break as below sub-arrays:
1. first sub array
[100, 180, 260, 310]
index of local minima = 0
index of local maxima = 3
2. second sub array
[40, 535, 695]
index of local minima = 4
index of local maxima = 6
Input: arr = [23, 13, 25, 29, 33, 19, 34, 45, 65, 67]
Output:
Points of local minima: 1 5
Points of local maxima: 0 4 9
方法:想法是遍历给定的数组arr []并检查数组中的每个元素在其相邻元素中是最小还是最大。如果最小,则为局部最小值;如果最大,则为局部最大值。步骤如下:
- 创建两个数组max []和min []来存储所有局部最大值和局部最小值。
- 遍历给定的数组,并根据以下条件将数组的索引附加到数组max []和min []中:
- 如果arr [i – 1]> arr [i]
则将该索引附加到min []上。 - 如果arr [i – 1]
arr [i + 1], 则将该索引附加到max [] 。
- 如果arr [i – 1]> arr [i]
- 分别检查第一个和最后一个元素的局部最大和最小条件。
- 打印存储在min []和max []中的索引。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find all the local maxima
// and minima in the given array arr[]
void findLocalMaximaMinima(int n, int arr[])
{
// Empty vector to store points of
// local maxima and minima
vector mx, mn;
// Checking whether the first point is
// local maxima or minima or none
if (arr[0] > arr[1])
mx.push_back(0);
else if (arr[0] < arr[1])
mn.push_back(0);
// Iterating over all points to check
// local maxima and local minima
for(int i = 1; i < n - 1; i++)
{
// Condition for local minima
if ((arr[i - 1] > arr[i]) and
(arr[i] < arr[i + 1]))
mn.push_back(i);
// Condition for local maxima
else if ((arr[i - 1] < arr[i]) and
(arr[i] > arr[i + 1]))
mx.push_back(i);
}
// Checking whether the last point is
// local maxima or minima or none
if (arr[n - 1] > arr[n - 2])
mx.push_back(n - 1);
else if (arr[n - 1] < arr[n - 2])
mn.push_back(n - 1);
// Print all the local maxima and
// local minima indexes stored
if (mx.size() > 0)
{
cout << "Points of Local maxima are : ";
for(int a : mx)
cout << a << " ";
cout << endl;
}
else
cout << "There are no points of "
<< "Local Maxima \n";
if (mn.size() > 0)
{
cout << "Points of Local minima are : ";
for(int a : mn)
cout << a << " ";
cout << endl;
}
else
cout << "There are no points of "
<< "Local Minima \n";
}
// Driver Code
int main()
{
int N = 9;
// Given array arr[]
int arr[] = { 10, 20, 15, 14, 13,
25, 5, 4, 3 };
// Function call
findLocalMaximaMinima(N, arr);
return 0;
}
// This code is contributed by himanshu77
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find all the local maxima
// and minima in the given array arr[]
public static void findLocalMaximaMinima(int n,
int[] arr)
{
// Empty vector to store points of
// local maxima and minima
Vector mx = new Vector();
Vector mn = new Vector();
// Checking whether the first point is
// local maxima or minima or none
if (arr[0] > arr[1])
mx.add(0);
else if (arr[0] < arr[1])
mn.add(0);
// Iterating over all points to check
// local maxima and local minima
for(int i = 1; i < n - 1; i++)
{
// Condition for local minima
if ((arr[i - 1] > arr[i]) &&
(arr[i] < arr[i + 1]))
mn.add(i);
// Condition for local maxima
else if ((arr[i - 1] < arr[i]) &&
(arr[i] > arr[i + 1]))
mx.add(i);
}
// Checking whether the last point is
// local maxima or minima or none
if (arr[n - 1] > arr[n - 2])
mx.add(n - 1);
else if (arr[n - 1] < arr[n - 2])
mn.add(n - 1);
// Print all the local maxima and
// local minima indexes stored
if (mx.size() > 0)
{
System.out.print("Points of Local " +
"maxima are : ");
for(Integer a : mx)
System.out.print(a + " ");
System.out.println();
}
else
System.out.println("There are no points " +
"of Local Maxima ");
if (mn.size() > 0)
{
System.out.print("Points of Local " +
"minima are : ");
for(Integer a : mn)
System.out.print(a + " ");
System.out.println();
}
else
System.out.println("There are no points of " +
"Local Maxima ");
}
// Driver code
public static void main(String[] args)
{
int N = 9;
// Given array arr[]
int arr[] = { 10, 20, 15, 14, 13,
25, 5, 4, 3 };
// Function call
findLocalMaximaMinima(N, arr);
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program for the above approach
# Function to find all the local maxima
# and minima in the given array arr[]
def findLocalMaximaMinima(n, arr):
# Empty lists to store points of
# local maxima and minima
mx = []
mn = []
# Checking whether the first point is
# local maxima or minima or neither
if(arr[0] > arr[1]):
mx.append(0)
elif(arr[0] < arr[1]):
mn.append(0)
# Iterating over all points to check
# local maxima and local minima
for i in range(1, n-1):
# Condition for local minima
if(arr[i-1] > arr[i] < arr[i + 1]):
mn.append(i)
# Condition for local maxima
elif(arr[i-1] < arr[i] > arr[i + 1]):
mx.append(i)
# Checking whether the last point is
# local maxima or minima or neither
if(arr[-1] > arr[-2]):
mx.append(n-1)
elif(arr[-1] < arr[-2]):
mn.append(n-1)
# Print all the local maxima and
# local minima indexes stored
if(len(mx) > 0):
print("Points of Local maxima"\
" are : ", end ='')
print(*mx)
else:
print("There are no points of"\
" Local maxima.")
if(len(mn) > 0):
print("Points of Local minima"\
" are : ", end ='')
print(*mn)
else:
print("There are no points"\
" of Local minima.")
# Driver Code
if __name__ == '__main__':
N = 9
# Given array arr[]
arr = [10, 20, 15, 14, 13, 25, 5, 4, 3]
# Function Call
findLocalMaximaMinima(N, arr)
C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
// Function to find all the local maxima
// and minima in the given array arr[]
public static void findLocalMaximaMinima(int n,
int[] arr)
{
// Empty vector to store points of
// local maxima and minima
ArrayList mx = new ArrayList();
ArrayList mn = new ArrayList();
// Checking whether the first point is
// local maxima or minima or none
if (arr[0] > arr[1])
mx.Add(0);
else if (arr[0] < arr[1])
mn.Add(0);
// Iterating over all points to check
// local maxima and local minima
for(int i = 1; i < n - 1; i++)
{
// Condition for local minima
if ((arr[i - 1] > arr[i]) &&
(arr[i] < arr[i + 1]))
mn.Add(i);
// Condition for local maxima
else if ((arr[i - 1] < arr[i]) &&
(arr[i] > arr[i + 1]))
mx.Add(i);
}
// Checking whether the last point is
// local maxima or minima or none
if (arr[n - 1] > arr[n - 2])
mx.Add(n - 1);
else if (arr[n - 1] < arr[n - 2])
mn.Add(n - 1);
// Print all the local maxima and
// local minima indexes stored
if (mx.Count > 0)
{
Console.Write("Points of Local " +
"maxima are : ");
foreach(int a in mx)
Console.Write(a + " ");
Console.Write("\n");
}
else
Console.Write("There are no points " +
"of Local Maxima ");
if (mn.Count > 0)
{
Console.Write("Points of Local " +
"minima are : ");
foreach(int a in mn)
Console.Write(a + " ");
Console.Write("\n");
}
else
Console.Write("There are no points of " +
"Local Maxima ");
}
// Driver code
public static void Main(string[] args)
{
int N = 9;
// Given array arr[]
int []arr = { 10, 20, 15, 14, 13,
25, 5, 4, 3 };
// Function call
findLocalMaximaMinima(N, arr);
}
}
// This code is contributed by rutvik_56
输出:
Points of Local maxima are : 1 5
Points of Local minima are : 0 4 8
时间复杂度: O(N)
辅助空间: O(N)