Skip to main content

Sorted triplet in an array | Naive Approach

Find the sorted triplet in an array

A, efficiently find a sorted triplet such that A[i] < A[j] < A[k] and 0 <= i < j < k < n, where n is the array size.

For example,

Input:  A[] = { 5, 4, 3, 7, 6, 1, 9 }
 
Output: Any one of the following triplets:
 
(5, 7, 9)
(4, 7, 9)
(3, 7, 9)
(5, 6, 9)
(4, 6, 9)
(3, 6, 9)

A simple solution would be to traverse the array and, for each index, check if at least one smaller and one larger element exists on its left and right, respectively. If any such index exists, we have found our triplet. The time complexity of this approach is O(n2)

import java.io.*;
import java.util.*;

/*

Given an integer array `nums`, efficiently find a sorted triplet such that `nums[i] < nums[j] < nums[k]` where `i < j < k`.

Input : [7, 3, 4, 2, 6]
Output: (3, 4, 6)

• If the input contains several sorted triplets, the solution can return any one of them.

Input : [5, 4, 3, 7, 6, 1, 9]
Output: (5, 7, 9) or (4, 7, 9) or (3, 7, 9) or (5, 6, 9) or (4, 6, 9) or (3, 6, 9)

• If no triplet exists, return null.

Input : [5, 4, 3]
Output: null

*/

public class SortedTripletNative {
   
        public static void main(String[] args) throws Exception {
            // pass the path to the file as a parameter
            File file = new File("assets/input.txt");
            Scanner sc = new Scanner(file);
           
            String[] str = sc.nextLine().split(" ");
            int arr[] = new int[str.length], i = 0;

            for(String s:str)
                arr[i++] = Integer.parseInt(s);
           
            System.out.println(getTriplet(arr));

            sc.close();
        }
   
        public static String getTriplet(int[] arr){
            String str = "";            
            int n = arr.length;

            if(n>=3){
                int i,j,k;
                for(i=1;i<n;i++){
                    for(j=i-1;j>=0;j--)
                        if(arr[i]>arr[j]){
                            for(k=i+1;k<n;k++)
                                if(arr[i]<arr[k])
                                    str += arr[j] + " " + arr[i] + " " + arr[k] + "\n";
                        }                    
                }
            }

            return str;
        }
}



Comments

Popular posts from this blog

Spot Round

Never ever give up !!! “Life is a journey with up and downs” Feeling happy when everything is right is natural, but feeling sad for something gone wrong can’t be justified. Many say miracle doesn’t happen, but a miracle is something that doesn’t have logic… Do you believe in miracles? So, stop seeing logic everywhere and then you can feel miracles. I want to share something… During my counselling for Bachelor’s degree till last round, I was unable to get a seat in college I want to study in. It felt like everything has gone. Then I heard about Spot Round. I was not sure to secure a seat. But I went there to try my luck, in a hope of a miracle. So, I kept ready all my necessary documents made a DD for this round. While I was going to reporting centre my mind was full of the variety of thoughts. I made a mistake and I was little late for reporting. So, I rushed up through check-in and register myself for this special round. Then I went to a hall where everyone ...

CSAB Special round for NITs, IIITs and CFTIS 2018

CSAB Special round for NITs, IIITs and CFTIS 2 018 After the  seat allotment of JoSAA 2018  is completed, CSAB will conduct a special round of counselling for NITs, IIITs and CFTIs through CSAB. The allotment will be open only for those candidates who have successfully completed their  JEE Main 2018   examination and secured valid scores. Exclusive:   JoSAA Counselling 2021, Documents Required... CSAB 2018 Special Round 1 Seat Allotment Result CSAB 2018 Special Round 1 Opening and Closing Rank Important Dates of CSAB Special round for NITs, IIITs and CFTIS 2018 S.No Events Important Dates (Tentative) 1 Registration and Choice Filling Fourth week of July 2018 2 Announcement of Seat Allotment Fourth week of July 2018 3 Reporting to Allotted Institutes First week of August 2018 click here to get full schedule... Eligibility Criteria of CSAB Special round ...

Certificates Format JoSAA 2018

Certificates Format JoSAA 2018 JoSAA certificate formats  required at time of documents verification at reporting centres are released and must download them from here... Click the  JoSAA certificate formats  and you will get the certificate you want for JoSAA. You can download required formats from this page. Do check for these documents before leaving your place... Click here to get the checklist for JoSAA counselling documents verification... Know more about first-round result 2018... Certificates Format JoSAA 2018 Format for Document Verification For admission to IITs and ISM The format for Document Verification For admission to IITs and ISM is given below. You will have to follow this pattern for JoSAA document Verification, click here to download... Format for Document Verification For admission to NITs, IIITs and Other-GFTIs For admission to NITs, IIITs and Other-GFTIs candidates will have to verify their doc...