Ad
Code
Diff
  • function pairs(num_elements, target_value, array) {
      let ret = 0;
      array.sort(function(a, b){return a-b});
      for(let i = 0; i < array.length; i++) {
        if(binaryIndexOf.call(array, array[i] - target_value) != -1) {
          ret++;
        }
      }
      return ret;
    }
    
    /**
     * Performs a binary search on the host array. This method can either be
     * injected into Array.prototype or called with a specified scope like this:
     * binaryIndexOf.call(someArray, searchElement);
     *
     * @param {*} searchElement The item to search for within the array.
     * @return {Number} The index of the element which defaults to -1 when not found.
     */
    function binaryIndexOf(searchElement) {
        'use strict';
     
        var minIndex = 0;
        var maxIndex = this.length - 1;
        var currentIndex;
        var currentElement;
     
        while (minIndex <= maxIndex) {
            currentIndex = (minIndex + maxIndex) / 2 | 0;
            currentElement = this[currentIndex];
     
            if (currentElement < searchElement) {
                minIndex = currentIndex + 1;
            }
            else if (currentElement > searchElement) {
                maxIndex = currentIndex - 1;
            }
            else {
                return currentIndex;
            }
        }
     
        return -1;
    }
    
    • function pairs(num_elements, target_value, array) {
    • return 0;
    • }
    • let ret = 0;
    • array.sort(function(a, b){return a-b});
    • for(let i = 0; i < array.length; i++) {
    • if(binaryIndexOf.call(array, array[i] - target_value) != -1) {
    • ret++;
    • }
    • }
    • return ret;
    • }
    • /**
    • * Performs a binary search on the host array. This method can either be
    • * injected into Array.prototype or called with a specified scope like this:
    • * binaryIndexOf.call(someArray, searchElement);
    • *
    • * @param {*} searchElement The item to search for within the array.
    • * @return {Number} The index of the element which defaults to -1 when not found.
    • */
    • function binaryIndexOf(searchElement) {
    • 'use strict';
    • var minIndex = 0;
    • var maxIndex = this.length - 1;
    • var currentIndex;
    • var currentElement;
    • while (minIndex <= maxIndex) {
    • currentIndex = (minIndex + maxIndex) / 2 | 0;
    • currentElement = this[currentIndex];
    • if (currentElement < searchElement) {
    • minIndex = currentIndex + 1;
    • }
    • else if (currentElement > searchElement) {
    • maxIndex = currentIndex - 1;
    • }
    • else {
    • return currentIndex;
    • }
    • }
    • return -1;
    • }
doomsbyFailed Tests

Pairs

You will be given an array of integers and a target value. Determine the number of pairs of array elements that have a difference equal to the target value.

For example, given an array of [1,2,3,4] and a target value of 1, we have three pairs meeting the condition. 2 - 1 = 1, 3 - 2 = 1, and 4 - 3 = 1.

Input Format:

The first line contains n and k, the number of array elements and the target value.
The second line contains n space-separated integers of the array.

Constraints:

2 ≤ n ≤ 105
0 < k < 109
each integer element arr[i], will conform to 0 < arr[i] < 2^31 - 1
each integer arr[i] will be unique
Output Format
An integer representing the number of pairs of integers whose difference is k.

Sample Input:

5, 2, [1, 5, 3, 4, 2]

Sample Output:

3

Explanation:

There are 3 pairs of integers in the set with a difference of 2: [5,3], [4,2], and [3,1].

function pairs(num_elements, target_value, array) {
  
  return 0;
}
Code
Diff
  • function throttle(wait, onLast, onFirst, interval, timestamps) {
      ret = [];
      if(onFirst) {
        ret.push(timestamps[0]); // account for first element
      }
      let clusterStart = timestamps[0];
      
      for(let i=1; i<timestamps.length; i++) {
        if(timestamps[i] - timestamps[i-1] > wait) { // 2.) new cluster
          clusterStart = timestamps[i];
          if(onFirst) {
            ret.push(clusterStart); // push cluster start
          }
        }
        if(interval !=0 && (timestamps[i+1] - timestamps[i] > wait || i == timestamps.length-1)) { // 1.) do interval shit
          let intervalVal = clusterStart + interval;
          let loopInvariant = timestamps[i-1];
          if(i == timestamps.length-1){
            loopInvariant = timestamps[i];
          }
          if(onLast) {
            loopInvariant += wait;
          }
      
          console.log('intervalVal: ' + intervalVal);
          console.log('loopInvariant: ' + loopInvariant);
          while(intervalVal <= loopInvariant) {
            console.log('pushing intervalVal: ' + intervalVal);
            ret.push(intervalVal);
            intervalVal += interval;
          }
        }
          
        if(timestamps[i+1] - timestamps[i] > wait) { // 2.) new cluster
          if(onLast) {
            ret.push(timestamps[i] + wait); // push cluster end
          }
        }
        
        if(i == timestamps.length-1 && onLast && interval == 0) { // 3.) Account for last element (needs updating)
          ret.push(timestamps[i] + wait);
        }
      }
      
      return ret;
    }
    
    • function throttle(wait, onLast, onFirst, interval, timestamps) {
    • let ret = [];
    • let cluster = [timestamps[0]];
    • ret = [];
    • if(onFirst) {
    • ret.push(timestamps[0]); // account for first element
    • }
    • let clusterStart = timestamps[0];
    • for(let i=1; i<timestamps.length; i++) {
    • if(timestamps[i] - timestamps[i-1] <= wait) {
    • cluster.push(timestamps[i]);
    • } else {
    • let clusterEventTimes = evaluateCluster(wait, onLast, onFirst, interval, cluster);
    • clusterEventTimes.forEach( function(el){ ret.push(el); });
    • cluster = [timestamps[i]];
    • if(timestamps[i] - timestamps[i-1] > wait) { // 2.) new cluster
    • clusterStart = timestamps[i];
    • if(onFirst) {
    • ret.push(clusterStart); // push cluster start
    • }
    • }
    • if(interval !=0 && (timestamps[i+1] - timestamps[i] > wait || i == timestamps.length-1)) { // 1.) do interval shit
    • let intervalVal = clusterStart + interval;
    • let loopInvariant = timestamps[i-1];
    • if(i == timestamps.length-1){
    • loopInvariant = timestamps[i];
    • }
    • if(onLast) {
    • loopInvariant += wait;
    • }
    • console.log('intervalVal: ' + intervalVal);
    • console.log('loopInvariant: ' + loopInvariant);
    • while(intervalVal <= loopInvariant) {
    • console.log('pushing intervalVal: ' + intervalVal);
    • ret.push(intervalVal);
    • intervalVal += interval;
    • }
    • }
    • if(timestamps[i+1] - timestamps[i] > wait) { // 2.) new cluster
    • if(onLast) {
    • ret.push(timestamps[i] + wait); // push cluster end
    • }
    • }
    • if(i == timestamps.length-1) {
    • let clusterEventTimes = evaluateCluster(wait, onLast, onFirst, interval, cluster);
    • clusterEventTimes.forEach( function(el){ ret.push(el); });
    • if(i == timestamps.length-1 && onLast && interval == 0) { // 3.) Account for last element (needs updating)
    • ret.push(timestamps[i] + wait);
    • }
    • }
    • return ret;
    • }
    • // Determines all times when an event needs to be fired
    • function evaluateCluster(wait, onLast, onFirst, interval, cluster){
    • let ret = [];
    • if(onFirst) {
    • ret.push(cluster[0]); // push cluster start
    • }
    • if(interval != 0) {
    • let maxInterval = cluster[cluster.length-1];
    • if(onLast) {
    • maxInterval += wait;
    • }
    • for(let intEv = cluster[0]+interval; intEv < maxInterval; intEv+=interval) {
    • ret.push(intEv);
    • }
    • }
    • if(onLast) {
    • ret.push(cluster[cluster.length-1]+wait); // push cluster end
    • }
    • return ret;
    • }