Menu Close

Find out the absolute difference of values of two array integers

Rakesh has given an array of integers and you need to find out if the absolute difference of values of any two consecutive array integers is at-most D.

Input:
First line of input contains an integer T, denoting the number of test cases. 

First line of each test case contains two integers N and D, denoting the size of the array and value of D respectively.

Second line of each test case contains N integers, which are separated by one space and are the integers for the array.


Output:
Print YES if the absolute difference between any two consecutive integer is at-most D, otherwise print NO
#include <stdio.h>
#include <stdlib.h>
int main()
{
 int j=0,t,i,n,k;
 scanf("%d",&t);
 while(j<t)
 {
 scanf("%d %d",&n,&k);
 int integers[n];
 int flag=0;
 for(i=0;i<n;i++)
 scanf("%d",&integers[i]);
 for(i=0;i<n;i++)
 if(abs(integers[i]-integers[i+1])<=k)
 flag++;
 if(flag==(n-1))
 printf("\nYES");
 else
 printf("\nNO");
 j++;
 }

return 0;
}

INPUT_1:
2
5  2
11  12  14  13  15
3  4
11  17  26

OUTPUT:
YES
NO


INPUT_2:
3
6  3
5  3  2  1  5  8
2  2
13  12
2  1
21  26

OUTPUT:
NO
YES
NO


ILLUSTRATION:

EXECUTED USING GCC LINUX

Morae! Q