Write a java program to find if the array has any triplets

Write a java program to find if the array has any triplets. Given an integer array, Write a program to find if the array has any triplets. A triplet is a value if it appears 3 consecutive times in the array.

Input and Output Format:

  • Input consists of n+1 integers. The first integer would represent the size of the array and the next n integers would have the values.
  • Output consists of a string stating TRUE or FALSE.

Refer sample output for formatting specifications

Sample Input 1:
7
3
3
5
5
5
2
3

Sample Output 1:
TRUE

Sample Input 2:
7
5
3
5
1
5
2
3

Sample Output 2:
FALSE

Following are the steps to find the triplets:

  • Input the size of the array. Create an array of input sizes.
  • Add elements to it. Now pass the array to the checkTripplets() method.
  • Inside the method, declare a boolean variable false. Iterate over the numbers and check for triplets. Compare each element with its next element and so on. If triplets are found return true else false.
package com.demo;
 
import java.util.*;
 
public class Main {
 
private static Scanner sc;
 
public static void main(String[] args) {
int n;
int[] a = new int[10];
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
boolean s = checkTripplets(a);
if (s)
System.out.println("TRUE");
else
System.out.println("FALSE");
}
 
public static boolean checkTripplets(int[] a) {
boolean b = false;
for (int i = 0; i < a.length - 3; i++) {
if ((a[i] == a[i + 1]) && (a[i + 1] == a[i + 2])) {
b = true;
}
}
return b;
}
 
}

Output

Program to find Array Triplets whose sum is equal to a given number

Write a Java program to find triplets in the given array whose sum is equal to the given number. For example,  [1, 5, 9, 6, 2, 3, 7] is the given array and 10 is the given number then array triplets whose sum is equal to 10 are:

  1. [1, 9, 0]
  2. [1, 6, 3]
  3. [1, 2, 7]
  4. [5, 2, 3]
  5. [3, 7, 0]

Input and Output Format:

  • Input consists of n+1 integers. The first integer would represent the size of the array and the next n integers would have the values. Also, input a given number.
  • Output consists of a string stating the sum of the triplets is a given number.

Refer sample output for formatting specifications

Sample Input 1:
7
1
5
9
6
2
3
7

Input number
10

Sample Output 1:
Sum of Triplets are
[1, 9, 0]
[1, 6, 3]
[1, 2, 7]
[5, 2, 3]
[3, 7, 0]

We will be using the Brute Force Method.

Brute Force Method:
  • Here, each element of triplets is compared for sum equality with the given number.
  • Use three for loops to check the sum of three numbers is equal to the given number.
    • Start the ith index from 0 to array length -2.
    • The jth index should start from i+1 to array length -1.
    • At last, the kth index should start from j+1 to array length.
  • Thus, we will get the triplets whose sum is equal to the given number.

Following are the steps we will use:

  • Input the size of the array. Create an array of input sizes. Input the given number.
  • Add elements to it. Now pass the array and given number to the sumTripplets() method.
  • Inside the method, use the Brute Force Method. (Explained above)
  • For each iteration print the triplets whose sum is equal to the given number.
package com.demo;

import java.util.Scanner;

public class Triplets {
    public static void main(String[] args) {
        int n;
        int[] a = new int[10];
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        System.out.println("Input number");
        int num = sc.nextInt();
        sumTripplets(a,num);
    }

    public static void sumTripplets(int[] a, int num) {
        System.out.println("Sum of Triplets are ");
        for (int i = 0; i < a.length-2; i++)
        {
            for (int j = i+1; j < a.length-1; j++)
            {
                for (int k = j+1; k < a.length; k++)
                {
                    if (a[i]+a[j]+a[k] == num)
                    {
                        System.out.println("["+a[i]+", "+a[j]+", "+a[k]+"]");
                        break;
                    }
                }
            }
        }
    }
}

Output

Thus, in this way, we learn how to find the triplets in Java in an array.