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.

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;
}
 
}

Input and Output Format:

Input consists of n+1 integers. The first integer would represent the size of 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

Add a Comment

Your email address will not be published. Required fields are marked *

4 × five =