Posts

Showing posts from August, 2020

Design a tiny URL or URL Shortner

Design a system that takes big URLs like “ http://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from-1-to-n/ ” and converts them into a short  6 character URL . It is given that URLs are stored in database and every URL has an associated integer   id .  So your program should take an integer id and generate a 6 character long URL.  A URL character can be one of the following A lower case alphabet [‘a’ to ‘z’], total 26 characters An upper case alphabet [‘A’ to ‘Z’], total 26 characters A digit [‘0′ to ‘9’], total 10 characters There are total 26 + 26 + 10 = 62 possible characters. So the task is to convert an integer (database id) to a base 62 number where digits of 62 base are "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" Input: The first line of input contains an integer T denoting the number of test cases.  The second line consists of a long integer. Output: For each testcase, in a new line, print the  shortened  string in the first line and conve

Boolean Matrix Problem

Given a boolean matrix   mat[M][N]   of size   M   X   N , modify it such that if a matrix cell   mat[i][j]   is   1   (or true) then make all the cells of ith row and jth column as   1 . Input: The first line of input contains an integer  T  denoting the number of test cases. The first line of each test case is r and c, r is the number of rows and c is the number of columns. The second line of each test case contains all the elements of the matrix in a single line separated by a single space. Output: Print the modified array. Constraints: 1 ≤ T ≤ 100 1 ≤ r, c ≤ 1000 0 ≤ A[i][j] ≤ 1 Example: Input: 3 2 2 1 0 0 0 2 3 0 0 0  0 0 1 4 3 1 0 0 1 0 0 1 0 0 0 0 0 Output: 1 1 1 0 0 0 1  1 1 1 1 1 1 1 1 1 1 0 0 Explanation: Test case 1:  Since only first element of matrix has 1 (at index 1,1) as value, so first row and first column are modified to 1. The link of this problem is  https://practice.geeksforgeeks.org/problems/boolean-matrix-problem/0 Solution: CPP #include<iostream> using nam

Floyd Warshall algorithm and it's applications.

The floyd warshall algorithm is for solving the All Pairs Shortest Path problem. The problem is to find shortest distances between every pair of vertices in a given edge weighted directed Graph. INPUT : Input will be a distance matrix (let say dis) , where dis[i][j] will represent the distance between the ith and jth node in the graph. and you have to input 2 nodes (a and b) between whom you want to find the shortest distance . OUTPUT: Output will be the shortest distance between the two node a and b. and it will also output the path between the two node through which you can achieve the shortest distance. ...................................................................................................................................................................... #include<bits/stdc++.h> using namespace std; #define inf 1000 void iter(int dis[][5],int seq[][5],int k) {     for(int i=0;i<5;i++)     {          for(int j=0;j<5;j++)          {               if(i<j

Squares in N*N Chessboard

  Find total number of Squares in a   N*N   chessboard. Input: The first line contains an integer  T , depicting total number of test cases. Then following T lines contains an integer N that is the side of the chessboard. Output: Each separate line showing the maximum number of squares possible. Constraints: 1 ≤ T ≤ 100 1 ≤ N ≤ 100 Example: Input: 2 1 2 Output: 1 5 Solution: CPP #include<iostream> using namespace std; int main() {      int t;      cin>>t;      while(t--)      {          int n;          cin>>n;          int sum=0;          for(int i=1;i<=n;i++)          {              sum=sum+i*i;          }          cout<<sum<<endl;      } return 0; }

Primality Test

  For a given number   N   check if it is prime or not. A prime number is a number which is only  divisible by 1 and itself. Example 1: Input: N = 5 Output: Yes Explanation: 5 is a prime number. Example 2: Input: N = 4 Output: No Explanation: 4 is not a prime number. Your Task: You don't need to read input or print anything. Your task is to complete the function   isPrime()  that takes  N  as  parameter  and returns  True  if  N  is prime else returns  false .  Expected Time Complexity  : O(N 1/2 ) Expected Auxilliary Space  :  O(1) Constraints: 1 <= N <= 10 9      The link of this problem is   https://practice.geeksforgeeks.org/problems/primality-test/1 Solution: Python3 import math def isPrime(N):     if N<2:         return False     for i in range(2,int(math.sqrt(N))+1):         if N%i==0:             return False     return True def main():     T=int(input())     while(T>0):         N=int(input())         if(isPrime(N)):             print("Yes")         el

Digits in Factorial

Problem Statement:   Given an integer   N . The task is to find the number of digits that appear in its factorial, where factorial is defined as, factorial(n) = 1*2*3*4……..*N and factorial(0) = 1. Example 1: Input: N = 5 Output: 3 Explanation: Factorial of 5 is 120. Number of digits in 120 is 3 (1, 2, and 0) Example 2: Input: N = 120 Output: 199 Explanation: The number of digits in 200! is 199 Your Task: You don't need to read input or print anything. Your task is to complete the function  digitsInFactorial()  that takes  N  as parameter and returns  number of digits  in factorial of  N . Expected Time Complexity  : O(N) Expected Auxilliary Space  : O(1) Constraints: 1 ≤ N ≤ 10 4  The link of this problem is  https://practice.geeksforgeeks.org/problems/digits-in-factorial/1 Solution: Python3 import math def digitsInFactorial(N):     sum1=0     for i in range(1,N+1):         sum1+=math.log10(i)     return math.floor(sum1)+1 def main():     T=int(input())     while(T>0):         N

Sum of Prime

Problem Statement :   Given a number (greater than 2), print two prime numbers whose sum will be equal to given number, else print -1 if no such number exists. NOTE :  A solution will always exist if the number is even. If [a, b] is one solution with a <= b, and [c, d] is another solution with c <= d, and a < c then print [a, b] only and not all possible solutions. Input: The first line contains an integer  T , depicting total number of test cases. Then following T lines contains an integer N. Output: Print the two prime numbers in a single line with space in between. Constraints: 1 ≤ T ≤ 50 2 ≤ N ≤ 1000000 Example: Input : 2 8 3 Output 3 5 -1 Explanation: Testcase 1:  two prime numbers from 1 to 8 are 3 and 5 whose sum is 8. This problem is available in Geeksforgeeks, the link of which is mentioned below. https://practice.geeksforgeeks.org/problems/sum-of-prime/0 Solution: CPP Solution #include<iostream> using namespace std; //Function to determine whether a number is p