Saturday, May 7, 2022

509. Fibonacci Number

 The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.

Given n, calculate F(n).

 

Example 1:

Input: n = 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: n = 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

 

Constraints:

  • 0 <= n <= 30
Solution:

In JS:

var fib = function(n) {
        function fib(n) {
        if(n==0){
            return 0;
        }
        if(n==1){
            return 1;
        }
        let small=fib(n-1)+fib(n-2);
        return small;
    }
    let result = fib(n);
    return result;
};

2119. A Number After a Double Reversal

Reversing an integer means to reverse all its digits.

  • For example, reversing 2021 gives 1202. Reversing 12300 gives 321 as the leading zeros are not retained.

Given an integer numreverse num to get reversed1then reverse reversed1 to get reversed2. Return true if reversed2 equals num. Otherwise return false.

 

Example 1:

Input: num = 526
Output: true
Explanation: Reverse num to get 625, then reverse 625 to get 526, which equals num.

Example 2:

Input: num = 1800
Output: false
Explanation: Reverse num to get 81, then reverse 81 to get 18, which does not equal num.

Example 3:

Input: num = 0
Output: true
Explanation: Reverse num to get 0, then reverse 0 to get 0, which equals num.

 

Constraints:

  • 0 <= num <= 106

 Solution:

In JS:

var isSameAfterReversals = function(num) {
    let reverseFirst = reverseNumber(num);
    let reverseSecond = reverseNumber(reverseFirst);
    if(num == reverseSecond) return true
    else return false;

    function reverseNumber(num){
        let reverse = 0;
        let remainder;
        while(num != 0){
        remainder = num % 10;
        reverse = reverse  * 10 + remainder;
        num = parseInt(num / 10);
      }
      return reverse;
    }
};

728. Self Dividing Numbers

self-dividing number is a number that is divisible by every digit it contains.

  • For example, 128 is a self-dividing number because 128 % 1 == 0128 % 2 == 0, and 128 % 8 == 0.

self-dividing number is not allowed to contain the digit zero.

Given two integers left and right, return a list of all the self-dividing numbers in the range [left, right].

 

Example 1:

Input: left = 1, right = 22
Output: [1,2,3,4,5,6,7,8,9,11,12,15,22]

Example 2:

Input: left = 47, right = 85
Output: [48,55,66,77]

 

Constraints:

  • 1 <= left <= right <= 104
 Solution:

In JS:

var selfDividingNumbers = function(left, right) {
    const arr = [];
    for(let index = left; index <= right; index++){
      let num = index;
      let count = 0;
      let numOfDigit = 0;
      while(num != 0){
        let digit = num%10;
        numOfDigit++;
        if(index%digit == 0) {
          count++;
        }
        num = parseInt(num/10);
      }
      if(numOfDigit === count){
        arr.push(index);
      }
    }
return arr;
};

367. Valid Perfect Square

 Given a positive integer num, write a function which returns True if num is a perfect square else False.

Follow up: Do not use any built-in library function such as sqrt.

 

Example 1:

Input: num = 16
Output: true

Example 2:

Input: num = 14
Output: false

 

Constraints:

  • 1 <= num <= 2^31 - 1
Solution:

In JS:

var isPerfectSquare = function(num) {
   for(let index = 0; index <= parseInt(Math.sqrt(num))+1; index++){
      let perfectSqrt = index * index;
      if(perfectSqrt === num) return true
    }
    return false;

};

69. Sqrt(x)

Given a non-negative integer x, compute and return the square root of x.

Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.

Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5.

 

Example 1:

Input: x = 4
Output: 2

Example 2:

Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.

 

Constraints:

  • 0 <= x <= 231 - 1

Solution:

In JS:

var mySqrt = function(x) {

    let numOfSqrt = parseInt(Math.sqrt(x));

    return numOfSqrt;

};

507. Perfect Number

perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.

Given an integer n, return true if n is a perfect number, otherwise return false.

 

Example 1:

Input: num = 28
Output: true
Explanation: 28 = 1 + 2 + 4 + 7 + 14
1, 2, 4, 7, and 14 are all divisors of 28.

Example 2:

Input: num = 7
Output: false

 

Constraints:

  • 1 <= num <= 108

 Solution:

In JS:

var checkPerfectNumber = function(num) {

    let sumOfDiv = 0;

    for(let index = 1; index < num; index++){

      if(num%index == 0) sumOfDiv += index;

    }

    if(sumOfDiv == num) return true;

    else return false;

};

326. Power of Three

Given an integer n, return true if it is a power of three. Otherwise, return false.

An integer n is a power of three, if there exists an integer x such that n == 3x.

 

Example 1:

Input: n = 27
Output: true

Example 2:

Input: n = 0
Output: false

Example 3:

Input: n = 9
Output: true

 

Constraints:

  • -231 <= n <= 231 - 1

 

Follow up: Could you solve it without loops/recursion?

Solution:

In JS:

var isPowerOfThree = function(n) {

    let result = false;

    let numOfSqrt = parseInt(Math.sqrt(n))+1;

    for(let index = 0; index<=numOfSqrt; index++){

      let power = Math.pow(3, index);

      if(n === power) result = true;

      else result;

    }

    return result;

};

342. Power of Four

Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four, if there exists an integer x such that n == 4x.

 

Example 1:

Input: n = 16
Output: true

Example 2:

Input: n = 5
Output: false

Example 3:

Input: n = 1
Output: true

 

Constraints:

  • -231 <= n <= 231 - 1

 

Follow up: Could you solve it without loops/recursion?

Solution:

In JS:

var isPowerOfFour = function(n) {

    let result = false;

    let numOfSqrt = parseInt(Math.sqrt(n))+1;

    for(let index = 0; index<=numOfSqrt; index++){

      let power = Math.pow(4, index);

      if(n === power) result = true;

      else result;

    }   

    return result;

}; 

231. Power of Two

 Given an integer n, return true if it is a power of two. Otherwise, return false.

An integer n is a power of two, if there exists an integer x such that n == 2x.

 

Example 1:

Input: n = 1
Output: true
Explanation: 20 = 1

Example 2:

Input: n = 16
Output: true
Explanation: 24 = 16

Example 3:

Input: n = 3
Output: false

 

Constraints:

  • -231 <= n <= 231 - 1

 

Follow up: Could you solve it without loops/recursion?

Solution:

In JS:

var isPowerOfTwo = function(n) {
    if (n <= 0) {
        return false;
    }
    while (n != 1) {
        if(n % 2 == 1) {
            return false;
        }
        n /= 2;
    }
    return true;
};

2235. Add Two Integers

 Given two integers num1 and num2, return the sum of the two integers.

 

Example 1:

Input: num1 = 12, num2 = 5
Output: 17
Explanation: num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned.

Example 2:

Input: num1 = -10, num2 = 4
Output: -6
Explanation: num1 + num2 = -6, so -6 is returned.

 

Constraints:

  • -100 <= num1, num2 <= 100
Soluation:

In JS:

var sum = function(num1, num2) {
    let result = num1+num2;
    return result;
};

908. Smallest Range I

You are given an integer array nums and an integer k.

In one operation, you can choose any index i where 0 <= i < nums.length and change nums[i] to nums[i] + x where x is an integer from the range [-k, k]. You can apply this operation at most once for each index i.

The score of nums is the difference between the maximum and minimum elements in nums.

Return the minimum score of nums after applying the mentioned operation at most once for each index in it.

 

Example 1:

Input: nums = [1], k = 0
Output: 0
Explanation: The score is max(nums) - min(nums) = 1 - 1 = 0.

Example 2:

Input: nums = [0,10], k = 2
Output: 6
Explanation: Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.

Example 3:

Input: nums = [1,3,6], k = 3
Output: 0
Explanation: Change nums to be [4, 4, 4]. The score is max(nums) - min(nums) = 4 - 4 = 0.

 

Constraints:

  • 1 <= nums.length <= 104
  • 0 <= nums[i] <= 104
  • 0 <= k <= 104

Soluation:

In JS:

var smallestRangeI = function(nums, k) {

    let range = Math.max(...nums) - Math.min(...nums);

    return range > 2 * k ? range - 2 * k : 0;

};