Typing Of Words / Crazy Computer

ZS the Coder is coding on a crazy computer. If you don’t type in a word for a c consecutive seconds, everything you typed disappear!

More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen.

For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen.

You’re given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.

Input

The first line contains two integers c and n (1 ≤ c ≤ 109, 1 ≤ n ≤ 100 000 ) — the number of words ZS the Coder typed and the crazy computer delay respectively.

The next line contains n integers t1, t2, …, tn (1 ≤ t1 < t2 < … < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word.


Output

Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the secondtn.

Examples

Input:

5    6
1  3  8  14  19  20

Output:

3

Input:

1   6
1  3  5  7  9  10

Output:

2

Program:

import java.util.Scanner;

public class CountOfWordsOnScreen {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter c : ");
int c = sc.nextInt();
System.out.print("Enter n : ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.print("Enter time values : ");
for(int i=0; i<n; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Count of Words on Screen " +countOfWordsOnScreen(c, arr));
}
public static int countOfWordsOnScreen(int c, int[] arr) {
int count = 1;
for(int i=1; i<arr.length; i++) {
if(arr[i] - arr[i-1] <= c)
count ++;
else
count = 1;
}
return count;
}
}


Output 1:

Enter c : 5
Enter n : 6
Enter time values : 1 3 8 14 19 20
Count of Words on Screen 3

Output 2:

Enter c : 1
Enter n : 6
Enter time values : 1 3 5 7 9 10
Count of Words on Screen 2



No comments:

Post a Comment