Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let first = [1,8,5];
- let second = [4,9,2];
- let target = 7;
- function solve(first,second,target){
- first.sort((a,b) => a-b);
- second.sort((a,b) => a-b);
- let x = first.length-1;
- let y = 0;
- let minDiff = Number.MAX_SAFE_INTEGER;
- let result = [];
- while(x>0 && y < second.length){
- let sum = first[x] + second[y];
- let diff = Math.abs(sum-target);
- if(diff < minDiff){
- result = [first[x],second[y]];
- minDiff = diff;
- }
- if(sum >= target){
- x--;
- }
- else{
- y++;
- }
- }
- console.log(result);
- }
- solve(first,second,target);
Add Comment
Please, Sign In to add comment