vector<pair<int,int>>pairDifference(vector<int>arr){// store all the numbers in a set for ease of searching// cpp set stores all numbers in ascending order by defaultset<int>numbers(arr.begin(),arr.end());// stores the pairs which have difference of twovector<pair<int,int>>numPairs;for(intnum:numbers){// for any number num if num+2 is also in the set// then include it in the vectorif(numbers.find(num+2)!=numbers.end()){numPairs.push_back(make_pair(num,num+2));}}returnnumPairs;}
Something like this should do it in JavaScript. I'm not entirely sure if this sort is okay, though. I can never remember which way it's ascendent and which descendent.
defpair_difference(lst)lst.sort# sort array.combination(2)# get all posible combinations of pairs.select{|arr|(arr.max-arr.min)==2}# only combinations that have difference of 2.sort_by{|arr|arr[0]}# sort by the first numberend
constpairDifference=(nums)=>{constresults=[]if(!nums||nums.length<2)returnresultsnums.sort()for(leti=0;i<nums.length-1;i++){// 2nd num can only be 1 or 2 positions away when sorted and no duplicatesif(nums[i+1]-nums[i]===2){results.push([nums[i],nums[i+1]])}elseif(i<nums.length-2&&nums[i+2]-nums[i]===2){results.push([nums[i],nums[i+2]])}}returnresults}constpairsEqual=(p1,p2)=>p1[0]===p2[0]&&p1[1]===p2[1]consttest=(nums,expected)=>{constresults=pairDifference(nums)constpassed=results.length===expected.length&&!results.find((p,i)=>!pairsEqual(p,expected[i]))console.log(`${passed?'Pass':'FAIL'} - input: ${JSON.stringify(nums)} `+`expected: ${JSON.stringify(expected)} results: ${JSON.stringify(results)}`)}test([1,2,3,4],[[1,3],[2,4]])test([1,3,4,6],[[1,3],[4,6]])test([4,1,2,3],[[1,3],[2,4]])test([1,23,3,4,7],[[1,3]])test([4,3,1,5,6],[[1,3],[3,5],[4,6]])test(undefined,[])test([],[])test([1],[])test([2,-1,0,1],[[-1,1],[0,2]])test([2000,-100,-98,1999],[[-100,-98]])
Assuming the input should be an array of unsigned integers.
"use strict";/**
* Find the pairs that have a difference of two.
*
* @param {number[]} An array of unsigned integers.
*
* @throws {Error} If the function is not called with one argument.
* @throws {TypeError} If the first argument is not an array.
* @throws {TypeError} If the first argument is not an array of integers.
* @throws {TypeError} If the first argument is not an array of unsigned integers.
*
* @return {number[][]} An array of array of unsigned integer pairs
*
* @example
* pairDifference([1, 2, 3, 4]); // [ [ 1, 3 ], [ 2, 4 ] ]
* pairDifference([4, 1, 2, 3]); // [ [ 4, 2 ], [ 1, 3 ] ]
* pairDifference([1, 23, 3, 4, 7]); // [ [ 1, 3 ] ]
* pairDifference([4, 3, 1, 5, 6]); // [ [ 4, 6 ], [ 3, 1 ], [ 3, 5 ] ]
*/functionpairDifference(numbers){if(arguments.length!==1){thrownewError("expected one argument");}if(!Array.isArray(numbers)){thrownewTypeError("expected first argument to be an array");}constlength=numbers.length;constpairs=[];for(letfirst=0;first<length;first++){constfirstNumber=numbers[first];if(!Number.isInteger(firstNumber)){thrownewTypeError("expected first argument to be an array of integers");}if(firstNumber<0){thrownewTypeError("expected first argument to be an array of unsigned integers");}for(letsecond=first+1;second<length;second++){constsecondNumber=numbers[second];if(!Number.isInteger(secondNumber)){thrownewTypeError("expected first argument to be an array of integers");}if(secondNumber<0){thrownewTypeError("expected first argument to be an array of unsigned integers");}if(Math.abs(firstNumber-secondNumber)===2){pairs.push(firstNumber,secondNumber);}}}returnpairs;}
We're a place where coders share, stay up-to-date and grow their careers.
Python solution
Whoa, that reads almost as if it were English!
Thanks!
C++ solution
Something like this should do it in JavaScript. I'm not entirely sure if this sort is okay, though. I can never remember which way it's ascendent and which descendent.
This really is Savage
Ruby solution
JS Solution
output:
JS solution with reduce
My Swift solution :
JS solution
Took some iterations to get to this solution, but here it is.
Haskell:
JavaScript
Assuming the input should be an array of unsigned integers.