Guest User

Untitled

a guest
Oct 18th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. function searchElementInSortedMatrix(matrix, inputX)
  2. {
  3. let result = { row: -1, col: -1 };
  4. let indexInMiddle = Math.floor(matrix[0].length/2)-1;
  5.  
  6. for(let i=matrix.length-1; i>-1; i--)
  7. {
  8. if(inputX>matrix[i][0])
  9. {
  10. if(matrix[i][indexInMiddle]> inputX)
  11. {
  12. for(let y=0; y<=indexInMiddle; y++)
  13. {
  14. if(matrix[i][y] === inputX)
  15. {
  16. result.row=i;
  17. result.col=y;
  18. return result;
  19. }
  20. }
  21. }
  22. else
  23. {
  24. for(let y=indexInMiddle; y<matrix[i].length; y++)
  25. {
  26. if(matrix[i][y] === inputX)
  27. {
  28. result.row=i;
  29. result.col=y;
  30. return result;
  31. }
  32. }
  33. }
  34. }
  35. }
  36.  
  37. return result;
  38. }
  39. const matrix = [
  40. [ 1, 5, 10, 20 ],
  41. [ 7, 9, 15, 28 ],
  42. [ 12, 25, 30, 36 ],
  43. [ 22, 35, 45, 50 ]
  44. ];
  45. console.info(searchElementInSortedMatrix(matrix, 35));
Add Comment
Please, Sign In to add comment