Tuesday, November 19, 2024
Google search engine
HomeData Modelling & AIFind a triplet in an array such that arr arr and...

Find a triplet in an array such that arr[i] arr[k] and i < j < k

Given an array arr[] consisting of a permutation of first N natural numbers, the task is to find a triplet (i, j, k) from the given array such that arr[i] < arr[j] > arr[k], where (i < j < k). If multiple triplets exist, then print any valid triplet of indices. Otherwise, print -1.

Examples:

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

 

Naive Approach: The simplest approach to solve this problem is to traverse the array and generate all possible triplets of the given array and for each triplet, check if it satisfies the given conditions or not. If found to be true, then print that triplet. Otherwise, print -1

Time Complexity: O(N3)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach the idea is based on the following observations: 

  • If the given array is sorted in ascending order or descending order from the index range [1, N – 2], then the solution does not exist.
  • Otherwise, at least one index exists in the given array such that the element just before and after that concerning index is less than the current element.

Follow the steps below to solve the problem:

  • Traverse the given array and for each array index, check if the element just before and after the current index is less than the current element or not. If found to be true, then print that the triplet (i – 1, i, i + 1).
  • Otherwise, print -1.

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
#include <iostream>
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




<script>
// Javascript program to implement
// the above approach
 
    // Function to find a triplet
    // that satisfy the conditions
    function FindTrip(arr, N)
    {
        // Traverse the given array
        for (let i = 1; i < N - 1;
            i++) {
   
            // Stores current element
            let p = arr[i - 1];
   
            // Stores element just before
            // the current element
            let q = arr[i];
   
            // Stores element just after
            // the current element
            let r = arr[i + 1];
   
            // Check the given conditions
            if (p < q && q > r) {
   
                // Print a triplet
                document.write(
                    (i - 1) + " "
                    + (i) + " "
                    + (i + 1));
                return;
            }
        }
   
        // If no triplet found
        document.write(-1);
    }
     
    // Driver Code
      
           let arr = [2, 1, 4, 3 ];
   
        let N = arr.length;
        FindTrip(arr, N);
   
</script>


Output:

1 2 3

Time Complexity: O(N)

Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments