给定一个由前N 个自然数排列组成的数组arr[] ,任务是从给定的数组中找到一个三元组(i, j, k) ,使得arr[i] < arr[j] > arr[k] , 其中(i < j < k) 。如果存在多个三元组,则打印任何有效的索引三元组。否则,打印-1 。
例子:
Input: arr[] = {2, 1, 4, 3}
Output: 1 2 3
Explanation: Triplet that satisfy the given condition is (arr[1], arr[2], arr[3])
Therefore, the required output is 1 2 3.
Input: arr[] = {1, 2, 3, 4, 5}
Output: -1
朴素的方法:解决这个问题的最简单的方法是遍历数组并生成给定数组的所有可能的三元组,对于每个三元组,检查它是否满足给定的条件。如果发现是真的,则打印该三元组。否则,打印-1 。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效的方法:为了优化上述方法,该想法基于以下观察:
- 如果给定数组从索引范围[1, N – 2] 开始按升序或降序排序,则解不存在。
- 否则,给定数组中至少存在一个索引,使得该索引前后的元素小于当前元素。
请按照以下步骤解决问题:
- 遍历给定的数组,对于每个数组索引,检查当前索引前后的元素是否小于当前元素。如果发现为真,则打印三元组(i – 1, i, i + 1) 。
- 否则,打印-1 。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find a triplet
// that satisfy the conditions
void FindTrip(int arr[], int N)
{
// Traverse the given array
for(int i = 1; i < N - 1; i++)
{
// Stores current element
int p = arr[i - 1];
// Stores element just before
// the current element
int q = arr[i];
// Stores element just after
// the current element
int r = arr[i + 1];
// Check the given conditions
if (p < q && q > r)
{
// Print a triplet
cout << i - 1 << " "
<< i << " " << i + 1;
return;
}
}
// If no triplet found
cout << -1;
}
// Driver Code
int main()
{
int arr[] = { 2, 1, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
FindTrip(arr, N);
return 0;
}
// This code is contributed by jyoti369
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG {
// Function to find a triplet
// that satisfy the conditions
static void FindTrip(int arr[],
int N)
{
// Traverse the given array
for (int i = 1; i < N - 1;
i++) {
// Stores current element
int p = arr[i - 1];
// Stores element just before
// the current element
int q = arr[i];
// Stores element just after
// the current element
int r = arr[i + 1];
// Check the given conditions
if (p < q && q > r) {
// Print a triplet
System.out.println(
(i - 1) + " "
+ (i) + " "
+ (i + 1));
return;
}
}
// If no triplet found
System.out.println(-1);
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 2, 1, 4, 3 };
int N = arr.length;
FindTrip(arr, N);
}
}
Python3
# Python3 program to implement
# the above approach
# Function to find a triplet
# that satisfy the conditions
def FindTrip(arr, N):
# Traverse the given array
for i in range(1, N - 1):
# Stores current element
p = arr[i - 1]
# Stores element just before
# the current element
q = arr[i]
# Stores element just after
# the current element
r = arr[i + 1]
# Check the given conditions
if (p < q and q > r):
# Print a triplet
print(i - 1, i, i + 1)
return
# If no triplet found
print(-1)
# Driver Code
if __name__ == '__main__':
arr = [ 2, 1, 4, 3 ]
N = len(arr)
FindTrip(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find a triplet
// that satisfy the conditions
static void FindTrip(int[] arr, int N)
{
// Traverse the given array
for(int i = 1; i < N - 1; i++)
{
// Stores current element
int p = arr[i - 1];
// Stores element just before
// the current element
int q = arr[i];
// Stores element just after
// the current element
int r = arr[i + 1];
// Check the given conditions
if (p < q && q > r)
{
// Print a triplet
Console.WriteLine((i - 1) + " " +
(i) + " " + (i + 1));
return;
}
}
// If no triplet found
Console.WriteLine(-1);
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 1, 4, 3 };
int N = arr.Length;
FindTrip(arr, N);
}
}
// This code is contributed by code_hunt
Javascript
输出:
1 2 3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live