Saturday, May 8, 2021

GFG DSA SELF PACED SOLUTIONS + SOME NOTES

 GEEKSFORGEEKS DSA SELF PACED SOLUTIONS

SEC-1 INTRO



SEC-2 MATHS


- ABSOLUTE VALUE

You are given an integer I, find the absolute value of the interger I.

int absolute(int I) {

            if(I > 0)

                return I;

            else 

                return -I;


- CONVERT CELSIUS TO FAHRENHEIT

Given a temperature in celsius C. You need to convert the given temperature to Fahrenheit.

double cToF(int C)

    {

        double F = (C * 9/5) + 32;

        return F;

    }


QUADRATIC EQUATION ROOTS

vector<int> quadraticRoots(int a, int b, int c) {

        int d = b*b - 4*a*c;

        double sqrt_d = sqrt(abs(d));

        vector<int>ans;

        if(d < 0){

            ans.push_back(-1);

        }

        else if(d == 0){

            ans.push_back(floor(-(double)b/(2*a)));

            ans.push_back(floor(-(double)b/(2*a)));

        }

        else{

            ans.push_back(floor((double)(-b + sqrt_d)/(2*a)));

            ans.push_back(floor((double)(-b-sqrt_d)/(2*a)));

            if(ans[0] < ans[1])

                swap(ans[0], ans[1]);

        }

        return ans;

    }

REFER


FACTORIAL OF NUMBER

Given a positive integer N. The task is to find factorial of N.

N <= 18

long long factorial(int N) {

        long long ans = 1;

        for(int i = 2; i <= N; i++){

            ans = ans * i;

        }

        return ans;

    }

REFER


DIGITS OF FACTORIAL

Given an integer N. Find the number of digits that appear in its factorial. 

N <= 10^5

int digitsInFactorial(int N)

    {

        if(N <= 1)

            return 1;

        double digits = 0;

        for(int i = 2; i <= N; i++){

            digits += log10(i);

        }

        return floor(digits)+1;

    }

REFER


GP TERM

Given the first 2 terms A and B of a Geometric Series. The task is to find the Nth term of the series.

double termOfGP(int A,int B,int N)

    {

        double r = (double)B/A;

        double res = A * pow(r, N-1);

        return res;

    }

REFER


PRIMALITY TEST

A prime number is a number which is only divisible by 1 and itself.
Given number N check if it is prime or not.

bool isPrime(int N){

            //simple method

            for(int i = 2; i <= sqrt(N); i++){

                if(N % i == 0)

                    return false;

            }

            return true;

            

            //seive of eratosthenes remaining

        }


No comments:

Post a Comment

LEETCODE JUNE 2021 DAILY CODING CHALLENGE

 LEETCODE JUNE 2021 DAILY CODING CHALLENGE JUNE 1 JUNE 2 JUNE 3 JUNE 4 JUNE 5 Maximum Performance of a Team LINK You are given two integers ...