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

NTA Declares JEE (Main)-2021 (February Session) NTA Scores for Paper 1 (B.E./B.Tech.) | JEE Mains 2021

The JEE (Main) Examination for B.E./B.Tech. was conducted by NTA from 24 to 26 February 2021. A total number of 6.52 lacs candidates were registered for Paper 1 (B.E. /B. Tech.) in this examination.  The Examination was conducted in 331 Cities [including 9 cities outside India in Colombo, Doha, Dubai, Kathmandu, Muscat, Riyadh, Sharjah, Singapore, and Kuwait] in 828 Centres.  A total number of 742 Observers, 261 City-Coordinators, 19 Regional Coordinators, 06 Special Coordinators & 02 National Coordinators were deployed at these centres to oversee the smooth and fair conduct of the examination.  The examination for Paper 1 (B.E. /B. Tech.) was conducted in a total of 6 shifts from 24 to 26 February 2021. The scores of Paper 1 (B.E./B.Tech.) are being declared today.   The Examination was conducted for the first time in 13 languages (English, Hindi, Gujarati along with Assamese, Bengali, Kannada, Malayalam, Marathi, Odia, Punjabi, Tamil, Telugu, and Urdu).  T...

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 ...

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 ...