Advertisement
samimwebdev

problem solving-1

Dec 20th, 2021
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //competitive programming (specializes in problem solving)
  2.  
  3. //data structure , algorithm(steps of solving problem)
  4.  
  5. //memory complexity, time complexity
  6.  
  7. //logic development
  8. //=============================
  9. //Tools - hammer
  10. //problem - pin
  11.  
  12. //problem solving
  13. // breakdown into small steps
  14.  
  15. //receiving array
  16.  
  17. //percentage 90/100 = 0.9
  18. // function getAvgPercentage(arr) {
  19. //   //get total number after addition
  20. //   //looping
  21. //   let total = 0
  22. //   for (let num of arr) {
  23. //     // total = total + num
  24. //     total += num
  25. //   }
  26. //   //average (total number/ count of  element)
  27. //   const avg = total / arr.length
  28. //   //percentage
  29. //   const percentage = avg / 100
  30. //   return percentage
  31. // }
  32.  
  33. // const result = getAvgPercentage([60, 70, 90, 50])
  34.  
  35. // console.log(result)
  36.  
  37. //get maximum number
  38. // receiving arr
  39.  
  40. // function getMaxNum(arr) {
  41. //looping array
  42. //approach-1
  43. //  let maxNum = arr[0]
  44. //  for(let num of arr){
  45. //     if(num > maxNum){
  46. //         maxNum = num
  47. //     }
  48. //  }
  49. //  console.log(maxNum)
  50. //approach - 2
  51. //   console.log(Math.max(...arr))
  52. //approach-3
  53. //   console.log(Math.max.apply(null, arr))
  54. // }
  55.  
  56. // getMaxNum([1, 7, 9, 3, 10, 2])
  57.  
  58. // function diffMaxMin(arr) {
  59. //   //approach-1
  60. //   let maxNum = arr[0]
  61. //   for (let num of arr) {
  62. //     if (num > maxNum) {
  63. //       maxNum = num
  64. //     }
  65. //   }
  66.  
  67. //   let minNum = arr[0]
  68. //   for (let number of arr) {
  69. //     if (number < minNum) {
  70. //       minNum = number
  71. //     }
  72. //   }
  73.  
  74. //   console.log(maxNum - minNum)
  75. // }
  76.  
  77. // diffMaxMin([1, 7, 9, 3, 10, 2])
  78.  
  79. //finding a element
  80. // function findElm(arr, searchElm) {
  81. //   let isFound = false
  82. //   for (let num of arr) {
  83. //     console.log(num)
  84. //     if (num === searchElm) {
  85. //       isFound = true
  86. //       break
  87. //     }
  88. //   }
  89.  
  90. //   return isFound
  91. // }
  92.  
  93. // console.log(findElm([1, 7, 9, 3, 10, 2], 10))
  94.  
  95. //return unique value based on searchArr
  96. // [1, 7, 9, 3]
  97. function excludeVal(arr, searchArr) {
  98.   const unique = []
  99.   //approach -1
  100.   //   for (let num1 of arr) {
  101.   //     let found = false
  102.   //     for (let num2 of searchArr) {
  103.   //      if(num1 === num2){
  104.   //         found = true
  105.   //      }
  106.   //     }
  107.   //     if(found == false){
  108.   //       unique.push(num1)
  109.   //     }
  110.   //   }
  111.  
  112.   //approach-2
  113.  
  114.   for (let number of arr) {
  115.     if (!searchArr.includes(number)) {
  116.       unique.push(number)
  117.     }
  118.   }
  119.  
  120.   console.log(unique)
  121. }
  122.  
  123. console.log(excludeVal([1, 7, 9, 3, 10, 2], [10, 2]))
  124.  
  125.  
  126.  
  127. console.log([1,2, 3].includes(10))
  128.  
  129. //effect application performance n2
  130. for (let i = 0; i < 2; i++) {
  131.   console.log(i) //2
  132.   for (let z = 0; z < 2; z++) {
  133.     console.log(z)
  134.     ////4
  135.   }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement