📌  相关文章
📜  从数组中找出三元组(i,j,k),使得i <j <k和arr [i] <arr [j]> arr [k]

📅  最后修改于: 2021-04-17 16:13:27             🧑  作者: Mango

给定一个数组arr [] ,其中包含前N个自然数的排列,任务是从给定数组中找到任何三元组(i,j,k) ,使得0≤i arr [i] arr [j]> arr [k] 。如果不存在这样的三元组,则打印“ -1”

例子:

天真的方法:最简单的方法是从给定数组arr []生成所有可能的三元组,如果存在满足给定条件的三元组,则打印该三元组。否则,打印“ -1”

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

高效方法:可以通过观察数组仅包含[1,N]范围内不同元素的事实来优化上述方法。如果存在具有给定标准的三元组,则该三元组必须彼此相邻。

因此,想法是遍历给定数组arr []的范围为[1,N – 2] ,如果存在索引i,则arr [i – 1] 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.


输出:
1 2 3

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