šŸ“˜
Placecom Question
  • āœ…ABC? (Contest)
  • āœ…Activity Selection (Contest)
  • āœ…Absolute value discrepancy (Contest)
  • āœ…Akash's Contest (Contest)
  • āœ…Alternate Matrix Addition(Contest)
  • āœ…Anti clockwise(Contest)
  • āœ…Arpit's toy (Contest)
  • āœ…Array Frequency Paradigm (Contest)
  • āœ…Array Games (Contest)
  • āœ…Arranging Students (Contest)
  • āœ…Array Proximity (Contest)
  • āœ…ATM Machine (Contest)
  • āœ…Awesome Numbers(Contest)
  • āœ…Bad Dish (Contest)
  • āœ…Balanced Neighborhood (Contest)
  • āœ…Birthday Gift (Contest)
  • āœ…Boundary Traversal of Matrix(Contest)
  • āœ…Buy and Sell Stock(Contest)
  • āœ…Cakewalk? (Contest)
  • āœ…Can we sort? (Contest)
  • āœ…Candy Love (Contest)
  • āœ…Candy Shopping (Contest)
  • āœ…Choose Card optimally(Contest)
  • āœ…Candy Store Earnings (Contest)
  • āœ…Choose points (Contest)
  • āœ…Concatenate Strings (Contest)
  • āœ…Common digits in two numbers (Contest)
  • āœ…Count 1's in binary array(Contest)
  • āœ…Count Numbers (Contest)
  • āœ…Count Total Digits in a Number (Contest)
  • āœ…Counting Zeroes to Ones (Contest)
  • āœ…Cyclic Rotation Paradigm (Contest)
  • āœ…Derrangement Exercise (Contest)
  • āœ…Diagonal Sum(Contest)
  • āœ…Diet planning (Contest)
  • āœ…Difference Array (Contest)
  • āœ…Digits Rearrangement (Contest)
  • āŒDivisible by 8 (Contest)
  • āœ…Door problem (Contest)
  • āœ…Duplicates at a distance k (Contest)
  • āœ…Easy - Peasy (Contest)
  • āœ…Eat or be Eaten(Contest)
  • āœ…Factorial - Recursion(Contest)
  • āœ…Find element in 2D array (Contest)
  • āœ…Generate all parentheses(Contest)
  • āœ…The gabba test(Contest)
  • āœ…Fruit Market (Contest)
  • āœ…Frequency Sort (Contest)
  • āœ…Floor in a Sorted Array(Contest)
  • āœ…First non-repeating character in a String(Contest)
  • āœ…Find unique(Contest)
  • āœ…Get the Shadow (Contest)
  • āœ…Good cells (Contest)
  • āœ…Good circular array (Contest)
  • āœ…Grid Magic (Contest)
  • āœ…Guardians of Galaxy(Contest)
  • āœ…Happy Balloons (Contest)
  • āœ…Help James (Contest)
  • āœ…Help Samar with Chopsticks (Contest)
  • āœ…Hip Hip Array (Contest)
  • āœ…Increment- Decrement Philosophy (Contest)
  • āœ…Infinity Stones : Form Black Order - The Army of Thanos (Contest)
  • āœ…Insert Operator(Contest)
  • āœ…Integer Modification (contest)
  • āœ…Inversion of array(Contest)
  • āœ…Iso Lexo String (Contest)
  • āœ…Jumping Numbers (Contest)
  • āœ…K closest points(Contest)
  • āœ…Knight game(Contest)
  • āœ…K-Pairs (Contest)
  • āœ…Kth Row of Pascal's Triangle(Contest)
  • āœ…Largest Bitonic Subarray(Contest)
  • āœ…Least Subarrays(Contest)
  • āœ…Lexographical Rotation (Contest)
  • āœ…Lone Sum Supremacy (Contest)
  • āœ…Lucky Boys(Contest)
  • āŒMajority Element(Contest)
  • āœ…Matrix problem(Contest)
  • āœ…Matrix ZigZag Traversal(Contest)
  • āœ…Max Candies(Contest)
  • āœ…Max permute (Contest)
  • āœ…Max Score in Quiz (Contest)
  • āœ…Max sum column (Contest)
  • āœ…Max XOR(Contest)
  • āŒMaximize Strength (Contest)
  • āœ…Maximizing Difference(Contest)
  • āœ…Maximum Area(Contest)
  • āŒMaximizing Difference(Contest)
  • āœ…Maximum Force(Contest)
  • āœ…Longest subarray not having more than K distinct elements(Contest)
  • āœ…Maximum value of difference of a pair of elements and their Index(Contest)
  • āœ…Minimum adjacent difference in a circular array easy(Contest)
  • āœ…Minimum Element in Sorted and Rotated Array(Contest)
  • āœ…Minimum operation - II(Contest)
  • āœ…Missing two(Contest)
  • āœ…Mohit and array(Contest)
  • āœ…Most occurring elements(Contest)
  • āœ…Move Zeros(Contest)
  • āœ…Moving right (Contest)
  • āœ…Max Score in Quiz (Contest)
  • āœ…N integers: Easy (Contest)
  • āœ…Nth node from end of linked list(Contest)
  • āœ…Number of distinct numbers(Contest)
  • āœ…Odd Sum Array (Contest)
  • āœ…Optimal Goodies (Contest)
  • āœ…Orange or Chocolate Candy? (Contest)
  • āœ…Packing Rectangles (Contest)
  • āœ…Pair sum (Contest)
  • āœ…Passcode (Contest)
  • āœ…Permutation - 2 (Contest)
  • āœ…Polynomial equation(Contest)
  • āœ…Power function(Contest)
  • āœ…Power of Three(Contest)
  • āœ…Print Pattern (Contest)
  • āœ…Reduce to 1 (Contest)
  • āœ…Remove Duplicates Inplace(Contest)
  • āœ…Repeating numbers (Contest)
  • āœ…Replace element(Contest)
  • āœ…Ropes (Contest)
  • āœ…Row Index Identification (Contest)
  • āœ…Row with maximum 1's(Contest)
  • āœ…Sara and Monsters (Contest)
  • āœ…Science Camp (contest)
  • āœ…Score bar(Contest)
  • āœ…Search in rotated sorted array(Contest)
  • āœ…Searching an element in a sorted array(Contest)
  • āœ…Separating Negative and Positive numbers(Contest)
  • āŒSequence Formation (Contest)
  • āŒSetwet's Fish Pond(Contest)
  • āœ…Shopping (Contest)
  • āœ…Simple Circle (Simple Contest)
  • āœ…Simple Pairs(Contest)
  • āœ…Simple Transpose (Contest)
  • āœ…Skit Video (Contest)
  • āœ…Smaller elements (Contest)
  • āœ…Sort it (Contest)
  • āœ…Special digit sum (Contest)
  • āœ…Spiral rotation(Contest)
  • āœ…Squiggly brackets (Contest)
  • āœ…Strange element (Easy-Version)
  • āœ…Subarray with given sum(Contest)
  • āœ…Sum of largest elements(Contest)
  • āœ…Sum Up(Contest)
  • āœ…Swapping Matrix (Contest)
  • āœ…Sweet Bunty (Contest)
  • āœ…Teacher(Contest)
  • āœ…The EndGame : Concatenated Words(Contest)
  • āœ…The high median paradigm (Contest)
  • āœ…Tower of Hanoi(Contest)
  • āœ…Transpose of a matrix(Contest)
  • āœ…Triangular matrix (Contest)
  • āœ…Two Sum Maximization(Contest)
  • āœ…Ultron : Vibranium Quest(Contest)
  • āœ…Wakandan Point in Unsorted Array(Contest)
  • āœ…Yet again Partition sort problem (Contest)
  • āœ…Yet Another Array Rearrangement Problem (Contest)
  • āœ…Zero Padding(Contest)
  • āœ…First & Last (Contest)
  • āŒFast Search (Contest)
Powered by GitBook
On this page

Choose points (Contest)

Choose points easy Time Limit: 2 sec Memory Limit: 128000 kB

PreviousCandy Store Earnings (Contest)NextConcatenate Strings (Contest)

Last updated 2 years ago

Problem Statement :

You are given N points on a horizontal axis. Find the number of ways you can choose 3 points such that maximum distance between those points is not greater than d. Input The first line contains two integers: N and d. The next line contains N space-separated integers x1, x2, ..., xN, — the x-coordinates of the points that Petya has got.

Constraints: 1 ≤ N ≤ 1e5 1 ≤ d ≤ 1e9 1 ≤ |x[i]| ≤ 1e9

It is guaranteed that the coordinates of the points in the input strictly increase. Output Print the number of ways to choose 3 points. Example Sample Input: 4 3 1 2 3 4

Sample Output: 4

Explanation: 1 2 3 1 2 4 2 3 4 1 3 4 are the required triplets

Sample Input: 4 2 -3 -2 -1 0

Sample Output: 2

link:

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args)throws Exception {
        // Your code here
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String line1=br.readLine();
        String []line1Arr=line1.split(" ");
        long n=Long.parseLong(line1Arr[0]);
        long d=Long.parseLong(line1Arr[1]);
        long[]valueArr=new long[(int)n];
        String line2=br.readLine();
        String[]line2Arr=line2.split(" ");
        for(int i=0;i<n;i++){
            valueArr[i]=Long.parseLong(line2Arr[i]);
        }
        Arrays.sort(valueArr);
        System.out.print(choosePoint(valueArr,n,d));
    }
 static long choosePoint(long[]a,long n,long d){
        long ways=0;
        if(d==n){
            return 0;
        }
        for(int i=0;i<n;i++){
            long higherIndex=upperLimit(a,0,n,a[i]+d);
            long numberOfEelements=higherIndex-(i+1);
            if(numberOfEelements >=2){
                ways +=(numberOfEelements*(numberOfEelements-1)/2);
            }
        }
            return ways;
        }
        static long upperLimit(long[]a,long low,long high,long d){
            while(low<high){
                long middle=(long)low+(high-low)/2;
                if(a[(int)middle]>d)
                high=middle;
                else
                    low=middle+1;
            }
            return low;
        }
    }
āœ…
https://my.newtonschool.co/playground/code/339bd7ujll9h/