Advertisement
samimwebdev

DSA Exercise-1( Time and space complexity)

Aug 20th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Practice Time and space complexity of these examples
  2.  
  3. //Exercise 1: Sum of All Elements
  4. const sumArray = (arr) => {
  5.   let sum = 0;
  6.   for (let i = 0; i < arr.length; i++) {
  7.     sum += arr[i];
  8.   }
  9.   return sum;
  10. }
  11.  
  12. console.log(sumArray([1, 2, 3, 4, 5])); // Output: 15
  13.  
  14. //Exercise 2: Find the Maximum Element
  15. const findMax = (arr) => {
  16.   let max = arr[0];
  17.   for (let i = 1; i < arr.length; i++) {
  18.     if (arr[i] > max) {
  19.       max = arr[i];
  20.     }
  21.   }
  22.   return max;
  23. }
  24.  
  25. console.log(findMax([1, 2, 3, 4, 5])); // Output: 5
  26.  
  27.  
  28.  //Exercise 3: Reverse an Array
  29. const reverseArray = (arr) => {
  30.   let left = 0;
  31.   let right = arr.length - 1;
  32.   while (left < right) {
  33.     [arr[left], arr[right]] = [arr[right], arr[left]]; // Swap elements
  34.     left++;
  35.     right--;
  36.   }
  37.   return arr;
  38. }
  39.  
  40. console.log(reverseArray([1, 2, 3, 4, 5])); // Output: [5, 4, 3, 2, 1]
  41.  
  42.  
  43. //4. Exercise 4: Check if a String is a Palindrom
  44. //Write a function to check if a given string is a palindrome (reads the same forward and backward).
  45. const isPalindrome = (str) => {
  46.   let left = 0;
  47.   let right = str.length - 1;
  48.  
  49.   while (left < right) {
  50.     if (str[left] !== str[right]) {
  51.       return false;
  52.     }
  53.     left++;
  54.     right--;
  55.   }
  56.  
  57.   return true;
  58. }
  59.  
  60. console.log(isPalindrome("racecar")); // Output: true
  61. console.log(isPalindrome("hello"));   // Output: false
  62.  
  63.  
  64. //5. Exercise 5: Count Distinct Elements
  65.  
  66. const countDistinct = (arr) => {
  67.   const uniqueElements = new Set(arr);
  68.   return uniqueElements.size;
  69. }
  70.  
  71. console.log(countDistinct([1, 2, 2, 3, 4, 4, 5])); // Output: 5
  72.  
  73. //6. Exercise 6: Two Sum Problem
  74. Write a function that returns the indices of two numbers in an array that add up to a given target.
  75. const twoSum = (arr, target) => {
  76.   const map = new Map();
  77.  
  78.   for (let i = 0; i < arr.length; i++) {
  79.     const complement = target - arr[i];
  80.     if (map.has(complement)) {
  81.       return [map.get(complement), i];
  82.     }
  83.     map.set(arr[i], i);
  84.   }
  85.  
  86.   return [];
  87. }
  88.  
  89.  
  90. // 7. Exercise 7: Merge Two Sorted Arrays
  91. const mergeSortedArrays = (arr1, arr2) => {
  92.   let i = 0, j = 0;
  93.   const merged = [];
  94.  
  95.   while (i < arr1.length && j < arr2.length) {
  96.     if (arr1[i] < arr2[j]) {
  97.       merged.push(arr1[i]);
  98.       i++;
  99.     } else {
  100.       merged.push(arr2[j]);
  101.       j++;
  102.     }
  103.   }
  104.  
  105.   while (i < arr1.length) {
  106.     merged.push(arr1[i]);
  107.     i++;
  108.   }
  109.  
  110.   while (j < arr2.length) {
  111.     merged.push(arr2[j]);
  112.     j++;
  113.   }
  114.  
  115.   return merged;
  116. }
  117.  
  118. console.log(mergeSortedArrays([1, 3, 5], [2, 4, 6])); // Output: [1, 2, 3, 4, 5, 6]
  119.  
  120. //8. Exercise 8: Find All Pairs with a Given Sum
  121. Write a function that returns all unique pairs of numbers from an array that add up to a given target sum.
  122.  
  123. javascript
  124. Copy code
  125. const findPairs = (arr, target) => {
  126.   const pairs = [];
  127.   const seen = new Set();
  128.  
  129.   for (let num of arr) {
  130.     const complement = target - num;
  131.     if (seen.has(complement)) {
  132.       pairs.push([complement, num]);
  133.     }
  134.     seen.add(num);
  135.   }
  136.  
  137.   return pairs;
  138. }
  139.  
  140. console.log(findPairs([2, 4, 3, 5, 6, -1, 2, 5], 7)); // Output: [[3, 4], [5, 2], [6, 1]]
  141.  
  142. //9. Exercise 9: Move Zeroes
  143. //Write a function that moves all the zeroes in an array to the end while maintaining the relative order of the non-zero elements.
  144.  
  145. const moveZeroes = (arr) => {
  146.   let nonZeroIndex = 0;
  147.  
  148.   for (let i = 0; i < arr.length; i++) {
  149.     if (arr[i] !== 0) {
  150.       arr[nonZeroIndex++] = arr[i];
  151.     }
  152.   }
  153.  
  154.   for (let i = nonZeroIndex; i < arr.length; i++) {
  155.     arr[i] = 0;
  156.   }
  157.  
  158.   return arr;
  159. }
  160.  
  161. console.log(moveZeroes([0, 1, 0, 3, 12])); // Output: [1, 3, 12, 0, 0]
  162.  
  163. //10. Exercise 10: First Unique Character in a String
  164. //Write a function that returns the index of the first non-repeating character in a string. If all characters are repeated, return -1.
  165.  
  166.  
  167. const firstUniqueChar = (str) => {
  168.   const charCount = {};
  169.  
  170.   for (let char of str) {
  171.     charCount[char] = (charCount[char] || 0) + 1;
  172.   }
  173.  
  174.   for (let i = 0; i < str.length; i++) {
  175.     if (charCount[str[i]] === 1) {
  176.       return i;
  177.     }
  178.   }
  179.  
  180.   return -1;
  181. }
  182.  
  183. console.log(firstUniqueChar("leetcode")); // Output: 0
  184. console.log(firstUniqueChar("loveleetcode")); // Output: 2
  185. console.log(firstUniqueChar("aabb")); // Output: -1
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement