Showing posts with label Number Theory. Show all posts
Showing posts with label Number Theory. Show all posts

Three Divisors

Given an integer n, return true if n has exactly three positive divisors. Otherwise, return false.

An integer m is a divisor of n if there exists an integer k such that n = k * m.

Example 1:

Input: n = 2
Output: false
Explantion: 2 has only two divisors: 1 and 2.

Example 2:

Input: n = 4
Output: true
Explantion: 4 has three divisors: 1, 2, and 4.

Approach

Java

public class ThreeDivisors {
    public static void main(String[] args) {
        int n = 4;

        System.out.println(isThree(n));

    }

    static boolean isThree(int n) {

        int cnt = 0;
        for (int i = 1; i * i <= n; i++) {

            // if i is the divisor of n
            if (n % i == 0) {

                // if n/i and i are same
                if (n / i == i) {
                    cnt++;
                } else
                    cnt += 2;
            }
        }
        if (cnt == 3)
            return true;
        return false;
    }

}

C++

#include <bits/stdc++.h>
using namespace std;

bool isThree(int n)
{

    int cnt = 0;
    for (int i = 1i * i <= ni++)
    {

        //if i is the divisor of n
        if (n % i == 0)
        {

            //if n/i and i are same
            if (n / i == i)
            {
                cnt++;
            }
            else
                cnt += 2;
        }
    }
    if (cnt == 3)
        return true;
    return false;
}
int main()
{
    int n = 4;

    if (isThree(n))
        cout << "true\n";

    else
        cout << "false\n";

    return 0;
}


Read Interview Questions

Exception Handling Interview Questions

DBMS Interview Questions Set -1

DBMS Interview Questions Set -2

SQL Interview Question Set -1

SQL Interview Question Set -2

JPA Interview Questions Set -1

JPA Interview Question Set -2

Hibernate Interview Questions

Spring Boot Interview Questions Set 1

Spring Boot Interview Questions Set 2


Sum of Digits in Base K

Given an integer n (in base 10) and a base k, return the sum of the digits of n after converting n from base 10 to base k.

After converting, each digit should be interpreted as a base 10 number and the sum should be returned in base 10.

Example 1:

Input: n = 34, k = 6
Output: 9
Explanation: 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.

Example 2:

Input: n = 10, k = 10
Output: 1
Explanation: n is already in base 10. 1 + 0 = 1.

Approach

C++

#include <bits/stdc++.h>
using namespace std;

string replaceDigits(string s)
{

    int n = s.size();
    string str = "";
    int i = 0;
    while (i < n)
    {
        str += s[i];
        i++;
        if (i < n)
            str += s[i - 1] + s[i] - '0';
        i++;
    }
    return str;
}

int main()
{
    string s = "a1c1e1";

    cout << replaceDigits(s<< "\n";

    return 0;
}


Small Factorials

You are asked to calculate factorials of some small positive integers.

Example:

Input:  n = 6
Output: 720

Approach

Java

public class SmallFactorials {
    public static void main(String[] args) {

        int n = 6;

        factorial(n);

        System.out.println();

    }

    static int factorial(int xint fact[], int size1) {
        int carry = 0;
        // iterate till the end of size
        for (int i = 0; i < size1; i++) {
            int prod = fact[i] * x + carry;
            fact[i] = prod % 10;
            carry = prod / 10;
        }

        // while carry>0 increment the size
        while (carry > 0) {
            fact[size1] = carry % 10;
            carry = carry / 10;
            size1++;
        }

        // return the new size
        return size1;
    }

    // method to find the factorial of a number
    static void factorial(int n) {
        int fact[] = new int[10000];

        fact[0] = 1;
        int size1 = 1;

        for (int x = 2; x <= n; x++) {
            size1 = factorial(x, fact, size1);
        }
        for (int i = size1 - 1; i >= 0; i--)
            System.out.print(fact[i]);
    }

}

C++

#include <bits/stdc++.h>
using namespace std;

//define the maximum
//number of digits
//that the factorail holds

#define MAX 10000
//function to numtiply the cuurent
//and update the result array
//and find the new size of the
//fatorial number
int factorial(int xint fact[], int size1)
{
    int carry = 0;
    //iterate till the end of size
    for (int i = 0i < size1i++)
    {
        int prod = fact[i] * x + carry;
        fact[i] = prod % 10;
        carry = prod / 10;
    }

    //while carry>0 increment the size
    while (carry)
    {
        fact[size1] = carry % 10;
        carry = carry / 10;
        size1++;
    }

    //return the new size
    return size1;
}

//function to find the factorial of a number
void factorial(int n)
{
    int fact[MAX];

    fact[0] = 1;
    int size1 = 1;

    for (int x = 2x <= nx++)
    {
        size1 = factorial(xfactsize1);
    }
    for (int i = size1 - 1i >= 0i--)
        cout << fact[i];
}

int main()
{

    int n = 6;

    factorial(n);
    cout << "\n";

    return 0;
}


Absolute Permutation

We define P to be a permutation of the first n natural numbers in the range . Let  denote the value at the position i in permutation P using -based indexing.

 is considered to be an absolute permutation if  holds true for every .

Given  and k, print the lexicographically smallest absolute permutation P. If no absolute permutation exists, print -1.

Example

Create an array of elements from  to . Using  based indexing, create a permutation where every . It can be rearranged to  so that all of the absolute differences equal :

pos[i]  i   |pos[i] - i|
  3     1        2
  4     2        2
  1     3        2
  2     4        2

Example:

Input:  n = 3, k = 0
Output: 1 2 3

Approach

C++

#include <bits/stdc++.h>
using namespace std;

vector<intabsolutePermutation(int nint k)
{
    vector<intres;
    if (k == 0)
    {
        for (int i = 1i <= ni++)
            res.push_back(i);
        return res;
    }
    else if (n % (2 * k) != 0 || 2 * k > n)
        return {-1};
    for (int i = 0i < ni++)
    {
        if ((i / k) % 2 == 0)
            res.push_back(i + 1 + k);
        else
            res.push_back(i + 1 - k);
    }
    return res;
}

int main()
{

    int n = 3k = 0;

    vector<intresult = absolutePermutation(nk);

    for (int i = 0i < result.size(); i++)
    {
        cout << result[i];

        if (i != result.size() - 1)
        {
            cout << " ";
        }
    }

    cout << "\n";

    return 0;
}