Input:
The first line 2 space-separated integers 'n' and 'd', the length of the sequence and the beautiful difference.
The second line 'n' space-separated integers arr[i].
Output:
Print a single line denoting the number of beautiful triplets in the sequence.
Explanation:
Sample Input
STDIN Function
7 3 arr[] size n = 7, d = 3
1 2 4 5 7 8 10 arr = [1, 2, 4, 5, 7, 8, 10]
Sample Output
3
There are many possible triplets (arr[i], arr[j], arr[k]), but our only beautiful triplets are (1,4,7), (4,7,10) and (2,5,8) by value, not index. Please see the equations below:
7-4 = 4-1 =3 =d
10-7 = 7-4 =3 =d
8-5 = 5-2 =3 =d
Recall that a beautiful triplet satisfies the following equivalence relation:
arr[j] – arr[i] = arr [k] – arr [j] = d where I<j<k.
#include <stdio.h> #include<stdlib.h> int main() { int str[100]; int n,d,s=0,i,j,k; scanf("%d %d",&n,&d); int *arr; arr=(int *)malloc(n*sizeof(int)); *arr=n; for( i=0;i<n;i++) {scanf("%d",&str[i]); } for( i=0;i<n;i++){ for( j=i+1;j<n;j++){ if(str[j]-str[i]!=d) continue; for( k=j+1;k<n;k++){ if(str[j]-str[i]==str[k]-str[j] && str[k]-str[j]==d)s++; } } } printf("%d",s); return 0; }
INPUT_1:
6 3
2 4 5 7 8 10
OUTPUT:
2
INPUT_2:
5 3
4 5 7 8 10
OUTPUT:
1
ILLUSTRATION
Morae Q!
- Find how long will it take till the next fortune year from the current year.
- Find if it’s possible to distribute all the crayons into boxes and satisfy all the conditions.
- Compute sum of numbers using call by reference.
- Compute the amount of money to be handed over with conditions.
- Find the total number of beautiful triplets in the sequence.
- Find the total amount each worker has to pay individually.
- Find the minimum distance between any pair of equal elements in the array.
- Compute the maximum possible value of the given equation.
- Find the age of the student at the time of joining.
- Compute the total number of photos at each restaurant .
- Find the sum of digits of the number using Union datatype.
- Generate the details of the novels in the expected format.
- Sort the student details based on their names in ascending order.
- Find a way to move the king from current position to different square on chessboard.
- Find the time differences in the two different time zones.
- Identifying the greatest number with a numerator and denominator.
- Find the Minimum number of packages to supply all the bases.
- Tower of Hanoi, A puzzle.
- Compute the sum of array of elements using recursion method.
- Find the super digit of an integer using the rules.