fork download
  1. /*
  2. Given an array of integers nums and an integer target, return indices of the two
  3. numbers such that they add up to target.
  4.  
  5. You may assume that each input would have exactly one solution, and you may not
  6. the same element twice.
  7.  
  8. You can return the answer in any order.
  9.  
  10.  
  11.  
  12. Example 1:
  13.  
  14. Input: nums = [2,7,11,15], target = 9
  15. Output: [0,1]
  16. Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
  17. Example 2:
  18.  
  19. Input: nums = [3,2,4], target = 6
  20. Output: [1,2]
  21. Example 3:
  22.  
  23. Input: nums = [3,3], target = 6
  24. Output: [0,1]
  25.  
  26.  
  27. Constraints:
  28.  
  29. 1. 2 <= nums.length <= 104
  30. 2. -109 <= nums[i] <= 109
  31. 3. -109 <= target <= 109
  32. 4. Only one valid answer exists.
  33. */
  34.  
  35.  
  36. function findIndexes(arr){
  37. let storeIndex=arr.map((value,index)=>{
  38. return {"value":value,"index":index};
  39. })
  40. storeIndex.sort((a,b)=>a["value"]-b["value"]);
  41. console.log(storeIndex);
  42. let ans=[];
  43.  
  44. let l=0;
  45. let r=storeIndex.length-1;
  46.  
  47. //while(l<r){
  48.  
  49. //}
  50.  
  51.  
  52. }
  53.  
  54. findIndexes([2,7,11,15]);
  55.  
Success #stdin #stdout 0.05s 43404KB
stdin
Standard input is empty
stdout
[
  { value: 2, index: 0 },
  { value: 7, index: 1 },
  { value: 11, index: 2 },
  { value: 15, index: 3 }
]