Skip to content

Instantly share code, notes, and snippets.

@QuocCao-dev
Last active October 18, 2022 18:28
Show Gist options
  • Save QuocCao-dev/cd009855e9d0b7a1483b4c17edd31e87 to your computer and use it in GitHub Desktop.
Save QuocCao-dev/cd009855e9d0b7a1483b4c17edd31e87 to your computer and use it in GitHub Desktop.
Array

Merge Two Sorted Arrays

Implement a function that merges two sorted arrays into another sorted array. Name it mergeArrays(arr1, arr2)

Input: An array and a number value Output: An array with two integers a and b ([a,b]) that add up to value

Input
arr1 = [1,3,4,5]  
arr2 = [2,6,7,8]

Output
arr = [1,2,3,4,5,6,7,8]

image

function mergeArrays(array1: number[], array2: number[]): number[] {
  return [];
}

// Test Cases
console.log(mergeArrays([1, 3, 4, 5], [2, 6, 7, 8])); // [1, 2, 3, 4, 5, 6, 7, 8]
console.log(mergeArrays([], [])); // []
console.log(mergeArrays([], [1, 2, 3, 4, 5])); // [1,2,3,4,5]
console.log(mergeArrays([1, 4, 45, 63], [])); // [1,4,45,63]
console.log(mergeArrays([4, 4, 4, 4, 4, 4, 4], [4, 4, 4, 4, 4, 4, 4])); // [4,4,4,4,4,4,4,4,4,4,4,4,4,4]
console.log(mergeArrays([-133, -100, 0, 4], [-2000, 2000])); // [-2000,-133,-100,0,4,2000]

Find Two Numbers that Add up to "k"

In this problem, you have to implement the find_sum(lst,k) function which will take a number k as input and return two numbers that add up to k.

Input: lst is a list of numbers and k is a number. Output: A list with two integers a and b that add up to k

Sample Input
arr1 = [1,3,4,5]  
arr2 = [2,6,7,8]

Sample Output
arr = [1,2,3,4,5,6,7,8]

image

function findSum(arr: number[], value: number): number[] {
  return [];
}
// Test Cases
findSum([1,21,3,14,5,60,7,6], 81) // [21,60]
findSum([1,2,3], 5) // [2,3]
findSum([1,2,3,4], 10) // [] 

Array of Products of All Elements

Implement a function, findProduct(arr), which modifies an array so that each index has a product of all the numbers present in the array except the number stored at that index.

Input An array of numbers (can even be floats, integers, and negative!)

Output An array such that each index has a product of all the numbers in the array except the number stored at that index.

Sample Input
arr = [1,2,3,4]

Sample Output
arr = [24,12,8,6]

image

function findProduct(...arr:number[]): number[]{
  return []
}

// Test Cases
findProduct(1,2,3,4) // [24,12,8,6]
findProduct(2,5,9,3,6) // [810,324,180,540,270]

Find First Unique Integer in an Array

Implement a function, findFirstUnique(arr), which takes an array as input and returns the first unique integer in the array

Input An array of integers

Output The first unique element in the array

image

function findFirstUnique(array: number[]): number {
  return 0
}

Find Second Maximum Value in an Array

Implement a function findSecondMaximum(arr), which returns the second largest element in the array.

Input An array of integers

Output The second largest element in the array

image

function findSecondMaximum(arr: number): number {
  return 0;
}

// Test Cases
findSecondMaximum(9,2,3,6) // 6
findSecondMaximum(2,3,3,3,3) // 2

Right Rotate an Array by n

Implement a function rightRotate(arr,n) that will rotate the given array by n.

image

function rightRotate(arr: number[], n: number): number[] {
  return [];
}

rightRotate([1,2,3,4,5], 2) // [4,5,1,2,3]
rightRotate([300,-1,3,0], 2) // [3,0,300,-1]
@QuocCao-dev
Copy link
Author

function mergeArrays(array1: number[], array2: number[]): number[] {
  let idxArr1 = 0;
  let idxArr2 = 0;
  let idxResult = 0;
  let result: number[] = [];

  while (idxArr1 < array1.length && idxArr2 < array2.length) {
    if (array1[idxArr1] < array2[idxArr2]) {
      result[idxResult] = array1[idxArr1];
      idxArr1++;
      idxResult++;
    } else {
      result[idxResult] = array2[idxArr2];
      idxArr2++;
      idxResult++;
    }
  }

  while (idxArr1 < array1.length) {
    result[idxResult] = array1[idxArr1];
    idxArr1++;
    idxResult++;
  }

  while (idxArr2 < array2.length) {
    result[idxResult] = array2[idxArr2];
    idxArr2++;
    idxResult++;
  }

  return result;
}

@QuocCao-dev
Copy link
Author

Merge Sort Cách 2
function mergeArrays(array1: number[], array2: number[]): number[] {
  let idxArr1 = 0;
  let idxArr2 = 0;

  while (idxArr1 < array1.length && idxArr2 < array2.length) {
    if (array1[idxArr1] > array2[idxArr2]) {
      array1.splice(idxArr1, 0, array2[idxArr2]);
      idxArr1++;
      idxArr2++;
    } else {
      idxArr1++;
    }
  }

  while (idxArr2 < array2.length) {
    array1.push(array2[idxArr2]);
    idxArr2++;
  }

  return array1;
}

@QuocCao-dev
Copy link
Author

function findSum(arr: number[], value: number): number[] {
  let i = 0;
  let j = arr.length - 1;

  arr.sort((a, b) => a - b); // O(n * log n)

  while (i < j) {
    let sum = arr[i] + arr[j];
    if (sum === value) {
      return [arr[i], arr[j]];
    } else if (sum < value) {
      i++;
    } else {
      j--;
    }
  }

  return [];
}

@QuocCao-dev
Copy link
Author

function findProduct(...arr: number[]): number[] {
  let rt: number[] = [];
  let temp = 1;
  for (let i = 0; i < arr.length; i++) {
    rt[i] = temp;
    temp *= arr[i];
  }

  temp = 1;
  for (let j = arr.length - 1; j >= 0; j--) {
    rt[j] *= temp;
    temp *= arr[j];
  }
  return rt;
}

@QuocCao-dev
Copy link
Author

function findFirstUnique(array: number[]): number {
  for (let id1 = 0; id1 < array.length; id1++) {
    let id2 = id1 + 1;
    while (id2 < array.length) {
      if (id1 !== id2 && array[id1] === array[id2]) {
        break;
      }
      id2++;
    }
    if (id2 === array.length) {
      return array[id1];
    }
  }
  return -1;
}

@QuocCao-dev
Copy link
Author

function findSecondMaximum(...arr: number[]): number {
  if (arr.length < 2) {
    return -1;
  }

  let firstMax = -Infinity;
  let secondMax = -Infinity;

  for (let i = 0; i < arr.length; i++) {
    if (arr[i] > firstMax) {
      secondMax = firstMax;
      firstMax = arr[i];
    }

    if (arr[i] > secondMax && arr[i] !== firstMax) {
      secondMax = arr[i];
    }
  }

  if (secondMax === -Infinity) return -1;

  return secondMax;
}

@QuocCao-dev
Copy link
Author

function rightRotate(arr: number[], n: number): number[] {
  if (arr.length === 0) {
    n = 0;
  } else {
    n = n % arr.length;
  }

  const result: number[] = [];

  for (let num = arr.length - n; num < arr.length; num++) {
    result.push(arr[num]);
  }

  for (let num = 0; num < arr.length - n; num++) {
    result.push(arr[num]);
  }

  return result;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment