Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. function findMaxNumberOfConsecOnes(dec){
  2. const countConsecOnes = (acc, x) => [acc[0] < acc[1]
  3. ? acc[1]
  4. : acc[0],
  5. x === "1"
  6. ? acc[1] + 1
  7. : 0 ];
  8.  
  9. return [...(dec >>> 0).toString(2)]
  10. .reduce(countConsecOnes, [0,0]);
  11. }
  12.  
  13. function findMaxNumberOfConsecOnes(dec){
  14. const countConsecOnes = (acc, x) => [acc[0] < acc[1] ? acc[1] : acc[0],
  15. x === "1" ? acc[1] + 1 : 0 ];
  16.  
  17. return [...(dec >>> 0).toString(2)]
  18. .reduce(countConsecOnes, [0,0]);
  19.  
  20. function findMaxNumberOfConsecOnes(dec){
  21. return [...(dec >>> 0).toString(2)]
  22. .reduce((acc, x) => {
  23. const current = x === "1" ? acc[1] + 1 : 0;
  24. const max = Math.max(acc[0], current);
  25. return [max, current];
  26. }, [0,0]);
  27. }
  28.  
  29. function findMaxNumberOfConsecOnes(dec){
  30. const binStr = (dec >>> 0).toString(2);
  31. let max = 0, current = 0;
  32. for (const s of binStr) {
  33. current = s === "1" ? current + 1 : 0;
  34. max = Math.max(max, current);
  35. }
  36. return max;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement