# HackerRank Beautiful Triplets problem solution

In this HackerRank Beautiful Triplets problem you have Given an increasing sequence of integers and the value of d, count the number of beautiful triplets in the sequence.

## Problem solution in Python programming.

```n, d = [int(r) for r in input().split()]
a = [int(r) for r in input().split()]

triplets = 0
for i in range(n-2):
for j in range(i + 1, n-1):
if a[j] - a[i] == d:
foundTrip = False
for k in range(j + 1, n):
if a[k] - a[j] == d:
triplets += 1
foundTrip = True
break
if foundTrip == True:
break

print(triplets)
```

## Problem solution in Java Programming.

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

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int d = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int ans = solve(n, d, a);
System.out.println(ans);
}

private static int solve(int n, int d, int[] a) {
Map<Integer, Integer> m1 = new HashMap<>();
Map<Integer, Integer> m2 = new HashMap<>();

int r = 0;
for (int i = 0; i < n; i++) {
if (m2.containsKey(a[i])) {
int c = m2.remove(a[i]);
r += c;
}
if (m1.containsKey(a[i])) {
int c = m1.remove(a[i]);
m2.put(a[i] + d, c);
}
}
return r;
}

private static void add(Map<Integer, Integer> map, int key) {
Integer old = map.get(key);
if (old == null) {
old = 0;
}
old++;
map.put(key, old);
}
}```

### Problem solution in C++ programming.

```#include <bits/stdc++.h>
#define pb push_back
#define sqr(x) (x)*(x)
#define sz(a) int(a.size())
#define reset(a,b) memset(a,b,sizeof(a))
#define oo 1000000007

using namespace std;

typedef pair<int,int> pii;
typedef long long ll;

int a[111111],n,d;
set<int> mys;

int main(){
//    freopen("input.txt","r",stdin);
cin>>n>>d;
for(int i=1; i<=n; ++i){
cin>>a[i];
mys.insert(a[i]);
}
int res=0;
for(int i=2; i<n; ++i) if(mys.count(a[i]-d) && mys.count(a[i]+d)) ++res;
cout<<res<<endl;
}
```

### Problem solution in C programming.

```#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int n, mid, d, i, j, k, *a, count = 0;

scanf("%d %d", &n, &d);
mid = n / 2;
a = (int *) malloc(n * sizeof(int));

//scanf("%d %d", &a[0], &a[1]);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

for (i = 0; i < n - 2; i++) {
j = 1;
while (a[j] - a[i] <= d) {
if (a[j] - a[i] == d) {
k = j + 1;
while (a[k] - a[j] <= d) {
if (a[k] - a[j] == d) {
count++;
break;
}
k++;
}
}
j++;
}
}

printf("%d", count);

return 0;
}```

### Problem solution in JavaScript programming.

```function processData(input) {
var data = input.trim().split(/\n/);
var arr = (data[1].split(" ")).sort(function(a,b){return Number(a)-Number(b);});
var cut = data[0].split(" ");
var n = Number(cut[0]);
var d = Number(cut[1]);
var a;
var b;
var c;
var trips = 0;

if(n < 3){
console.log(0);
}
else{
for(var h = 0; h < n - 2; h++){
a = arr[h];
for(var i = h+1; i < n-1;i++){
b = arr[i];
if(b-a !== d){
continue;
}
for(var k = i+1; k < n; k++){

c = arr[k];
//console.log("round: "+h +"b-a=" + (b-a)+ "c-b=" + (c-b) + "c=" + c + "b = " + b);
if(c-b ===d){
trips++;
}
}
}
}
console.log(trips);
}
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});

process.stdin.on("end", function () {
processData(_input);
});```