给定一个由前N 个自然数排列组成的数组arr[] ,任务是从给定数组中找到任何三元组(i, j, k)使得0 ≤ i < j < k ≤ (N – 1)和arr[i] < arr[j]和arr[j] > arr[k] 。如果不存在这样的三元组,则打印“-1” 。
例子:
Input: arr[] = {4, 3, 5, 2, 1, 6}
Output: 1 2 3
Explanation: For the triplet (1, 2, 3), arr[2] > arr[1]( i.e. 5 > 3) and arr[2] > arr[3]( i.e. 5 > 2).
Input: arr[] = {3, 2, 1}
Output: -1
朴素的方法:最简单的方法是从给定的数组arr[]生成所有可能的三元组,如果存在满足给定条件的任何三元组,则打印该三元组。否则,打印“-1” 。
时间复杂度: O(N 3 )
辅助空间: O(1)
有效的方法:可以通过观察数组仅包含范围[1, N] 中的不同元素这一事实来优化上述方法。如果存在具有给定标准的任何三元组,则该三元组必须彼此相邻。
因此,想法是在范围[1, N – 2]上遍历给定数组arr[]并且如果存在任何索引 i 使得arr[i – 1] < arr[i]和arr[i] > arr [i + 1] ,然后打印三元组(i – 1, i, i + 1)作为结果。否则,打印“-1” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find a triplet such
// that i < j < k and arr[i] < arr[j]
// and arr[j] > arr[k]
void print_triplet(int arr[], int n)
{
// Traverse the array
for (int i = 1; i <= n - 2; i++) {
// Condition to satisfy for
// the resultant triplet
if (arr[i - 1] < arr[i]
&& arr[i] > arr[i + 1]) {
cout << i - 1 << " "
<< i << " " << i + 1;
return;
}
}
// Otherwise, triplet doesn't exist
cout << -1;
}
// Driver Code
int main()
{
int arr[] = { 4, 3, 5, 2, 1, 6 };
int N = sizeof(arr) / sizeof(int);
print_triplet(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find a triplet such
// that i < j < k and arr[i] < arr[j]
// and arr[j] > arr[k]
static void print_triplet(int arr[], int n)
{
// Traverse the array
for(int i = 1; i <= n - 2; i++)
{
// Condition to satisfy for
// the resultant triplet
if (arr[i - 1] < arr[i] &&
arr[i] > arr[i + 1])
{
System.out.print(i - 1 + " " + i + " " +
(i + 1));
return;
}
}
// Otherwise, triplet doesn't exist
System.out.print(-1);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 4, 3, 5, 2, 1, 6 };
int N = arr.length;
print_triplet(arr, N);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to find a triplet such
# that i < j < k and arr[i] < arr[j]
# and arr[j] > arr[k]
def print_triplet(arr, n):
# Traverse the array
for i in range(1, n - 1):
# Condition to satisfy for
# the resultant triplet
if (arr[i - 1] < arr[i] and
arr[i] > arr[i + 1]):
print(i - 1, i, i + 1)
return
# Otherwise, triplet doesn't exist
print(-1)
# Driver Code
if __name__ == "__main__":
arr = [ 4, 3, 5, 2, 1, 6 ]
N = len(arr)
print_triplet(arr, N)
# This code is contributed by chitranayal
C#
// C# program to implement
// the above approach
using System;
public class GFG
{
// Function to find a triplet such
// that i < j < k and arr[i] < arr[j]
// and arr[j] > arr[k]
static void print_triplet(int[] arr, int n)
{
// Traverse the array
for(int i = 1; i <= n - 2; i++)
{
// Condition to satisfy for
// the resultant triplet
if (arr[i - 1] < arr[i] &&
arr[i] > arr[i + 1])
{
Console.Write(i - 1 + " " + i + " " +
(i + 1));
return;
}
}
// Otherwise, triplet doesn't exist
Console.Write(-1);
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 4, 3, 5, 2, 1, 6 };
int N = arr.Length;
print_triplet(arr, N);
}
}
// This code is contributed by splevel62.
Javascript
1 2 3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live