给定一个数组arr [] ,该数组包含前N个自然数的排列,任务是从给定数组中找到一个三元组(i,j,k) ,以使arr [i]
例子:
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
输出:
1 2 3
时间复杂度: O(N)
辅助空间: O(1)