Math Flashcards

1
Q

Given an integer, return its base 7 string representation.

Example 1:
Input: 100
Output: "202"
Example 2:
Input: -7
Output: "-10"
A
/**
 * @param {number} num
 * @return {string}
 */
var convertToBase7 = function(num) {
    if (num === 0) {
        return "0";
    }
    if (num < 0) {
        return "-" + convertToBase7(-num);
    }
    let result = '';
    while (num > 0) {
        result += num % 7;
        num = Math.floor(num / 7);
    }
    return result.split('').reverse().join('');
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The following code converts an integer number to its binary representation. Note how we return result as a string, and how we treat non-positive numbers separately:

A
String convertToBinary(int x) {
    if (x == 0) return "0";
    if (x < 0)  return "-" + convertToBinary(-x);
    StringBuilder result = new StringBuilder();
    while (x > 0) {
        result.append(x % 2);
        x /= 2;
    }
    return result.reverse().toString();
}

System.out.println(convertToBinary(2)); // prints 10
System.out.println(convertToBinary(0)); // prints 0
System.out.println(convertToBinary(-7)); // prints -111
System.out.println(convertToBinary(13)); // prints 1101

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The following code converts binary number represented as a string to the base 10 integer. For simplicity here we assume that numbers are non-negative, and fit into an int type:

A
int convertFromBinary(String binary) {
    int result = 0;
    for (int i = 0; i < binary.length(); i++)
        result = result * 2 + binary.charAt(i) - '0';        
    return result;
}

System.out.println(convertFromBinary(“10”)); // prints 2
System.out.println(convertFromBinary(“0”)); // prints 0
System.out.println(convertFromBinary(“1101”)); // prints 13

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Count the number of prime numbers less than a non-negative number, n.

Example 1:

Input: n = 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
Example 2:

Input: n = 0
Output: 0
Example 3:

Input: n = 1
Output: 0

A

/**

  • @param {number} n
  • @return {number}
  • /
var isPrime = function(n) {
    if (n <= 1) return false;
    for (let i = 2; i * i <= n; i++)
        if (n % i == 0) 
            return false;
return true; }
var countPrimes = function(n) {
    let counter = 0;
    for (let i = 0; i < n; i++) {
        if (isPrime(i)) {
            counter++;
        }
    }
return counter; };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Initially on a notepad only one character ‘A’ is present. You can perform two operations on this notepad for each step:

Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
Paste: You can paste the characters which are copied last time.

Given a number n. You have to get exactly n ‘A’ on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n ‘A’.

Example 1:

Input: 3
Output: 3
Explanation:
Intitally, we have one character ‘A’.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get ‘AA’.
In step 3, we use Paste operation to get ‘AAA’.

A
/**
 * @param {number} n
 * @return {number}
 */
var minSteps = function(n) {
    let answer = 0;
    for (let i = 2; i <= n; i++) {
        while (n % i == 0) {
            answer += i;
            n = Math.floor(n/i);
        }
    }
    if (n > 1)
        answer += n;
return answer; };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Given an integer, write a function to determine if it is a power of three.

Example 1:

Input: 27
Output: true
Example 2:

Input: 0
Output: false

A
/**
 * @param {number} n
 * @return {boolean}
 */
var isPowerOfThree = function(n) {
     if (n < 1) {
            return false;
    }
while (n % 3 == 0) {
    n /= 3;
}

return n === 1; };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Given an integer n, write a function to determine if it is a power of two.

Example 1:

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

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

A
/**
 * @param {number} n
 * @return {boolean}
 */
var isPowerOfTwo = function(n) {
    if (n < 1) {
                return false;
        }
while (n % 2 == 0) {
    n /= 2;
}

return n === 1; };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given an integer n, return the number of trailing zeroes in n!.

Follow up: Could you write a solution that works in logarithmic time complexity?

Example 1:

Input: n = 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Example 2:

Input: n = 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Example 3:

Input: n = 0
Output: 0

A
/**
 * @param {number} n
 * @return {number}
 */
var trailingZeroes = function(n) {
    if (n < 0)
		return -1;
	let count = 0;
	for (let i = 5; Math.floor(n / i) >= 1; i *= 5) {
		count +=Math.floor( n / i);
	}
return count; };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

Example:

Input: 2
Output: 91
Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100,
excluding 11,22,33,44,55,66,77,88,99

A
/**
 * @param {number} n
 * @return {number}
 */
var countNumbersWithUniqueDigits = function(n) {
    let ans = 0;
     if(n == 0) return 1;
    var countNumbersWithUniqueDigitsR = function(n, index, used) {
    // Base case
        if(n === index) {
            return;
        }
        // Try 10 digits
        for(let i = 0; i < used.length; i++) {
            if(used[i]) continue;
        used[i] = true;

        ans++;

        if(i != 0 || index > 0) { // No recursion if current num is a single 0
            countNumbersWithUniqueDigitsR(n, index + 1, used);
        }

        used[i] = false;
    }
}
countNumbersWithUniqueDigitsR(n, 0, new Array(10).fill(0));
    return ans;
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

Example 1:

Input: “abab”
Output: True
Explanation: It’s the substring “ab” twice.
Example 2:

Input: “aba”
Output: False

A
/**
 * @param {string} s
 * @return {boolean}
 */
var repeatedSubstringPattern = function(str) {
    const ss = str + str;
    const sssub = ss.substring(1, ss.length - 1);
    return sssub.includes(str);
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly