Knowee
Questions
Features
Study Tools

Given an array of integers A, and an integer K find number of happy elements. Element X is happy if there exists at least 1 element whose difference is less than K i.e. an element X is happy if there is another element in the range [X-K, X+K] other than X itself. Constraints 1 <= N <= 10^5 0 <= K <= 10^5 0 <= A[i] <= 10^9 Input First line contains two integers N and K where N is size of the array and K is a number as described above. Second line contains N integers separated by space. Output Print a single integer denoting the total number of happy elements. Example Input 3 2 1 3 5 Output 3 Explanation All numbers have at least 1 element in the range [X-2, X+2]. Hence they are all happy elements. Since these three are in number, the output is 3. give java code

Question

Given an array of integers A, and an integer K find number of happy elements.

Element X is happy if there exists at least 1 element whose difference is less than K i.e. an element X is happy if there is another element in the range [X-K, X+K] other than X itself.

Constraints

1 <= N <= 10^5

0 <= K <= 10^5

0 <= A[i] <= 10^9

Input

First line contains two integers N and K where N is size of the array and K is a number as described above. Second line contains N integers separated by space.

Output

Print a single integer denoting the total number of happy elements.

Example

Input

3 2

1 3 5

Output

3

Explanation

All numbers have at least 1 element in the range [X-2, X+2]. Hence they are all happy elements. Since these three are in number, the output is 3. give java code

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Java code snippet that solves the problem:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int K = sc.nextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = sc.nextInt();
        }
        Arrays.sort(A);
        int happyCount = 0;
        int j = 0;
        for (int i = 0; i < N; i++) {
            while (j < N && A[j] - A[i] < K) {
                j++;
            }
            if (j > i + 1) {
                happyCount++;
            }
        }
        System.out.println(happyCount);
    }
}

This code works by first reading the input, then sorting the array. It then iterates over the array, and for each element, it finds the first element that is not in the range [X-K, X+K]. If there is at least one element in the range, the current element is happy, and the happy count is incremented. Finally, it prints the total number of happy elements.

This problem has been solved

Similar Questions

You are playing a game with your friend Ajay where Ajay gave you an array 'A' consisting of 'N' integers such that all the elements of the array does not exceed the value 'K'.Ajay has asked you to replace the minimum number of elements (replacement is the following operation: choose some index i from 1 to n and replace a[i] with some integer in range [1;k] ) to satisfy the following conditions:after all replacements, all a[i] are positive integers not greater than k;for all i from 1 to n/2 the following equation is true: a[i]+a[n]−i+1=x, where x should be the same for all n/2 pairs of elements.Note: It is guaranteed that 'N' is even.Input FormatThe first line contains two integers n and k — the length of a and the maximum possible value of some a[i] correspondingly. It is guratanteed that n is even .The second line of the test case contains n integers a1,a2,…,an, where ai is the i-th element of a.Constraints2≤n≤2⋅10^51≤k≤2⋅10^51≤a[i]≤kOutput FormatPrint the integer — the minimum number of elements you have to replace in a to satisfy the conditions from the problem statement.Sample Input 04 41 1 3 1Sample Output 01Sample Input 16 71 1 1 1 2 2Sample Output 11

You have been given an array 'A' of N integers. You need to find the maximum value of j - i subjected to the constraint of A[i] <= A[j], where ‘i’ and ‘j’ are the indices of the array.For example :If 'A' = {3, 5, 4, 1}then the output will be 2.Maximum value occurs for the pair (3, 4)Detailed explanation ( Input/output format, Notes, Images )Constraints:1 <= T <= 1001 <= N <= 10 ^ 4-10 ^ 5 <= A[i] <= 10 ^ 5Time limit: 1 sec.Sample Input 1:1934 8 10 3 2 80 30 33 1Sample Output 1:6Explanation:Maximum value occurs for the pair (8, 33)Sample Input 2:1109 2 3 4 5 6 7 8 18 0Sample Output 2:8Explanation:Maximum value occurs for the pair (9, 18)

Given a sorted array A of size N, along with an integer target K, implement Binary Search to find the target K in the given array.Input FormatThe first line of input contains an integer N - the size of an array and target K. The second line contains the elements of the array.Output FormatFor each iteration of Binary Search, print the values of low, high, and mid. At the end, if the target K is found in the array, print "True" otherwise, print "False".Constraints1 <= N <= 201 <= A[i] <= 103ExampleInput 19 121 4 6 7 10 11 12 20 23Output 10 8 45 8 6TrueInput 210 213 5 8 11 15 17 19 23 26 30Output 20 9 45 9 75 6 56 6 6False

First index of elementSend FeedbackTake an array with n elements with possibly duplicate elements as the input. The task is to find the index of the first occurrences of the element x in the array and, if it is not present, return -1.Input Format:The first line contains an integer N representing the size of the array.The next line contains N space-separated integers representing the elements of the array.The last line contains an integer 'x' whose index has to be found.Output Format:The only line of the output prints the Index or -1.Constraints:1 <= N <= 10^31 <= arr[i] <= 10^91 <= x < NSample Input 1 :87 5 2 11 2 43 1 12Sample Output 1 :2Explanation of Sample Input 1:2 is present twice in the input array and the first time it appears is at index 2.Sample Input 2 :87 5 2 11 2 43 1 110Sample Output 2 :-1Explanation of Sample Input 2:10 is not present in the array so the output is -1.

Given an array of integers and a window size K, find the number of distinct elements in every window of size K of the given array.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines. The first line of each test case contains the N-size of the array and the K-size of the window. The second line contains N integers - elements of the array.Output FormatFor each test case, print the number of distinct elements in every window of size K, separated by a new line.Constraints30 points1 <= N <= 1001 <= K <= N70 points1 <= N <= 100001 <= K <= NGeneral Constraints1 <= T <= 1000-100 <= ar[i] <= 100ExampleInput35 3-2 -4 -2 4 -210 73 -4 -3 -4 -2 0 2 -2 6 017 13-5 -1 4 8 -5 -3 -4 7 4 -4 0 8 0 -2 3 2 5Output2 3 26 5 6 58 9 9 10 11

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.