Posts

Showing posts from February, 2021

problem no 54:convert an integer to roman.

 question:                         Convert integer to Roman No Basic Accuracy: 49.9% Submissions: 14164 Points: 1 Given an integer n, your task is to complete the function convertToRoman which prints the corresponding roman number of n. Various symbols and their values are given below. I 1 V 5 X 10 L 50 C 100 D 500 M 1000

problem no 53:(problem on binary tree): counting the number of the leaf nodes in the binary tree.

Question:

problem no 52:(problem on linked list):find the number of occurances of the given integer inside the linked list.

 QUESTION: Occurence of an integer in a Linked List Easy Accuracy: 56.23% Submissions: 31590 Points: 2 Given a singly linked list and a key, count the number of occurrences of given key in the linked list. Example 1: Input: N = 7 Link List = 1->2->1->2->1->3->1 search_for = 1 Output: 4 Explanation: 1 appears 4 times. Example 2: Input: N = 5 Link List = 1->2->1->2->1 search_for = 3 Output: 0 Explanation: 3 appears 0 times.  

problem no 51:find the remainder when the string is divided by 7.

 QUESTION: Remainder with 7 Basic Accuracy: 46.43% Submissions: 9141 Points: 1 Given a number as string(n) , find the remainder of the number whe it is divided by 7 Example 1: Input: 5 Output: 5   Example 2: Input: 8 Output: 1      // IS FOR COMMENT. ANSWER: int remainderWith7(string num) {     // This series is used to find remainder with 7     int series[] = {1, 3, 2, -1, -3, -2};       // Index of next element in series    ...

problem no 50:josephus problem:(recursion problem).

 QUESTION: Josephus problem Easy Accuracy: 52.47% Submissions: 34010 Points: 2 Given the total number of persons n and a number k which indicates that k-1 persons are skipped and k th person is killed in circle in a fixed direction. The task is to choose the safe place in the circle so that when you perform these operations starting from 1 st place in the circle, you are the last one remaining and survive. Example 1: Input: n = 3 k = 2 Output: 3 Explanation: There are 3 persons so skipping 1 person i.e 1s...

problem no 49:sorting problem:sort the array if the first half and the second half of the array is sorted.

QUESTION:

problem no 48:sorting problem:find the triplet inside the array that sum up to the given particular value.

 QUESTION: Triplet Sum in Array Given an array arr of size arr_size and an integer sum. Find if there's a triplet in the array which sums up to the given integer sum. Example 1: Input : N = 6, X = 13 arr[] = [1 4 45 6 10 8] Output : 1 Explanation : The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input : N = 5, X = 10 arr[] = [1 2 4 3 6] Output : 1 Explanation : The triplet {1, 3, 6} in the array sums up to 10.    

problem no 47:sorting problem:bubble sort .

 QUESTION:

problem no 46:sorting problem:sorting the array which only contains 0,1,2.

 QUESTION: Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.

problem no 45:(sorting problem):binary array sorting .

 QUESTIONS: Given a binary array A[] of size N . The task is to arrange the array in increasing order. Note: The binary array contains only 0  and 1.  

problem no 44:(sorting problem):check whether the array is sorted or not.

 QUESTIONS:

problem no 43:(array problem):Remove duplicate elements from sorted Array .

QUESTION: Given a sorted array A of size N, delete all the duplicates elements from A.

problem no 42:(array problem):return the count of the elements which are less than or equal to x.

 QUESTION: Given an sorted array A of size N . Find number of elements which are less than or equal to given element X .  

problem no 41:(array problem):rotate the array of size n by d in clockwise direction.

 QUESTION: Given an unsorted array arr[] of size N , rotate it by D elements (clockwise). 

problem no 40:(array problem): finding the second largest element in the array.

 QUESTION:

problem no 39: (linked list problem): printing the node at the particular index.

 QUESTION:

problem no 38:(linked list problem):printing whole linked list.

 QUESTION: Print Linked List elements  .  

problem no 37:(string problem):checks whether s1 is substring of s2 or not ,if yes then at what position?

 QUESTION:

problem no 36:(array problem):finding minimum and maximum element inside array.

 QUESTION: Find minimum and maximum element in an array Basic Accuracy: 81.18% Submissions: 1632 Points: 1 Given an array A of size N of integers. Your task is to find the minimum and maximum elements in the array.   Example 1: Input: N = 6 A[] = {3, 2, 1, 56, 10000, 167} Output: min = 1, max = 10000   Example 2: Input: N = 5 A[] = {1, 345, 234, 21, 56789} Output: min = 1, max = 56789  

problem no 35:(array problem):alternate array printing.

QUESTION: 

problem no 34:(problem on string): merge two strings.

 QUESTIONS:

problem no 33:string problem:sort the string in the decreasing order.

 QUESTION:

problem no 32: string problem:Upper case conversion

 QUESTION:

problem no 31: string problem:Anagram palindrome (VIP).

 QUESTION:

problem no 30:(string problem):Anagram.

 QUESTION: Anagram  Given two strings  a  and  b  consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other. Example 1: Input: a = geeksforgeeks, b = forgeeksgeeks Output: YES Explanation: Both the string have same characters with same frequency. So, both are anagrams. Example 2: Input: a = allergy, b = allergic Output: NO Explanation: Characters in both the strings are not same, so they are not anagrams.  

problem no 29:(string problem):palindrome string.

 QUESTION: Given a string S , check if it is palindrome or not. Example 1: Input: S = "abba" Output: 1 Explanation: S is a palindrome Example 2: Input: S = "abc" Output: 0 Explanation: S is not a palindrome  

problem no 28:(linked list),print the number of nodes in the linked list.

 QUESTION:   Given a singly linked list. The task is to find the length of the linked list, where length is defined as the number of nodes in the linked list. Example 1: Input: LinkedList: 1->2->3->4->5 Output: 5 Explanation: Count of nodes in the linked list is 5, which is its length. Example 2: Input: LinkedList: 2->4->6->7->5->1->0 Output: 7 Explanation: Count of nodes in the linked list is 7. Hence, the output is 7.  

problem no 27: peak element in the array ,peak element is the one who is greater than the left as well as right element.

 QUESION: Peak element A peak element in an array is the one that is not smaller than its neighbours. Given an array of size N, find the index of any one of its peak elements.   Example 1: Input: N = 3 arr[] = {1,2,3} Output: 2 Explanation: index 2 is 3. It is the peak element as it is greater than its neighbour 2.   Example 2: Input: N = 2 arr[] = {3,4} Output: 1 Explanation: 4 (at index 1) is the peak element as it is greater than its only neighbour element 3.      

problem no 26: Given a string S consisting only '0's and '1's, find the last index of the '1' present in it.

QUESTION: Last index of One Basic Accuracy: 65.74% Submissions: 1347 Points: 1 Given a string S consisting only ' 0 's and ' 1 's,  find the last index of the '1' present in it.   Example 1: Input: S = 00001 Output: 4 Explanation: Last index of  1 in given string is 4.   Example 2: Input: 0 Output: -1 Explanation: Since, 1 is not present, so output is -1.  

problem no 25: finding the transition point in the sorted binary array.

 QUESTION: Find Transition Point Given a sorted array containing only 0s and 1s, find the transition point.  Example 1: Input: N = 5 arr[] = {0,0,0,1,1} Output: 3 Explanation: index 3 is the transition point where 1 begins. Example 2: Input: N = 4 arr[] = {0,0,0,0} Output: -1 Explanation: Since, there is no "1", the answer is -1.  

problem no 24: square root of the number.

 QUESTION: Square root of a number Given an integer x,  find the square root of x. If x is not a perfect square, then return floor(√x).   Example 1: Input: x = 5 Output: 2 Explanation: Since, 5 is not a perfect square, floor of square_root of 5 is 2. Example 2: Input: x = 4 Output: 2 Explanation: Since, 4 is a perfect square, so its square root is 2.  

problem no 23: Missing number inside the array.

QUESTION: Vaibhav likes to play with numbers and he has n numbers. One day he was placing the numbers on the playing board just to count that how many numbers he have. He was placing the numbers in increasing order i.e. from 1 to n . But when he was putting the numbers back into his bag, some numbers fell down onto the floor. He picked up all the numbers but one number, he couldn't find. Now he have to go somewhere urgently, so he asks you to find the missing number. Input: The first of input contains an integer T , then T test cases follow. Each test case contains an integer n i.e. numbers of integers he placed on the board and the second line of each test case contains the array  a[] which represents the numbers, he successfully picked up from the floor.  Output: For each test case in a new line print the missing number. Constraints: 1 <= T <= 100 1 <= n <= 10 4 1 <= a[i] <= 10 4 Example: Input :     ...

problem no 22: Search in a Rotated Array

 QUESTION: Given a sorted and rotated array A of N distinct elements which is rotated at some point, and given an element K . The task is to find the index of the given element K in the array A. Input: The first line of the input contains an integer T , denoting the total number of test cases. Then T test cases follow. Each test case consists of three lines. First line of each test case contains an integer N denoting the size of the given array. Second line of each test case contains N space separated integers denoting the elements of the array A. Third line of each test case contains an integer K denoting the element to be searched in the array. Output: Corresponding to each test case, output the index of the element found in the array.  If element is not present, then output -1. Constraints: 1 ≤ T ≤ 100 1 ≤ N ≤ 10 7 0 ≤ A i ≤ 10 8 1 ≤ K ≤ 10 8 Example: Input: 3 9 5 6 7 8 9 10 1 2 3 10 3 3 1 2 1 4 3 5 1 2 6 Output: 5 1 -1 Explanat...

problem no 21: count the number of occurances of the number X in array.

 QUESTION: Number of occurrence Given a sorted array Arr  of size N and a number X , you need to find the number of occurrences of X in Arr . Example 1: Input: N = 7, X = 2 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 4 Explanation: 2 occurs 4 times in the given array. Example 2: Input: N = 7, X = 4 Arr[] = {1, 1, 2, 2, 2, 2, 3} Output: 0 Explanation:  4 is not present in the given array.  

Problem no 20: finding largest element in the array which is less than x.

 QUESTION:   Floor in a Sorted Array Given a sorted array arr [] of size N without duplicates, and given a value x . Floor of x is defined as the largest element K in arr[] such that K is smaller than or equal to x. Find the index of K(0-based indexing). Example 1: Input: N = 7, x = 0 arr[] = {1,2,8,10,11,12,19} Output: -1 Explanation: No element less than 0 is found. So output is " -1 ". Example 2: Input: N = 7, x = 5 arr[] = {1,2,8,10,11,12,19} Output: 1 Explanation: Largest Number less than 5 is 2 (i.e K = 2) , whose index is 1 (0-based indexing).      

problem no 19: Binary search.

 QUESTION: Binary Search Basic Accuracy: 38.43% Submissions: 69401 Points: 1 Given a sorted array of size N and an integer K, find the position at which K is present in the array using binary search. Example 1: Input: N = 5 arr[] = {1 2 3 4 5} K = 4 Output: 3 Explanation: 4 appears at index 3. Example 2: Input: N = 5 arr[] = {11 22 33 44 55} K = 445 Output: -1 Explanation: 445 is not present.       ANSWER: //the below function will be called when needed. int binsearch(int array[],int start_index,i...

problem no 18: number of buildings that can see the sun . ( or facing the sun.)

Image
 QUESTION: Given an array H representing heights of buildings. You have to count the buildings which will see the sunrise (Assume : Sun rise on the side of array starting point). Example 1: Input: N = 5 H[] = {7, 4, 8, 2, 9} Output: 3 Explanation: As 7 is the first element, it can see the sunrise. 4 can't see the sunrise as 7 is hiding it. 8 can see. 2 can't see the sunrise. 9 also can see the sunrise. Example 2: Input: N = 4 H[] = {2, 3, 4, 5} Output: 4 Explanation: As 2 is the first element, it can see the sunrise. 3 can see the sunrise as 2 is not hiding it. Same for 4 and 5, they also can see the sunrise.        ANSWER:   int countBuildings(int h[], int n) { int count=0; int max=INT_MIN; for(int i=0;i<n;i++) { if(h[i]>max) { count=count+1; max=h[i]; } } return count; }  

problem no 17: Linear search.

 QUESTION: Given an array Arr of N elements and a integer K . Your task is to return the position of first occurence of K in the given array. Note: Position of first element is considered as 1. Example 1: Input: N = 5, K = 16 Arr[] = {9, 7, 2, 16, 4} Output: 4 Explanation: K = 16 is found in the given array at position 4. Example 2: Input: N = 7, K = 98 Arr[] = {1, 22, 57, 47, 34, 18, 66} Output: -1 Explanation:  K = 98 isn't found in the given array.   ANSWER: int search(int arr[], int n, int k) {         int found=0;         for(int i=0;i<n;i++)         {             if(arr[i]==k)             {                 return i+1;              ...

Problem no 16: count the number of squares below N.

QUESTION: Consider a sample space S consisting of all perfect squares starting from 1, 4, 9 and so on. You are given a number N , you have to output the number of integers less than N in the sample space S. Example 1: Input : N = 9 Output: 2 Explanation: 1 and 4 are the only Perfect Squares less than 9. So, the Output is 2. Example 2: Input : N = 3 Output: 1 Explanation: 1 is the only Perfect Square less than 3. So, the Output is 1.   ANSWER:   int countSquares(int N) { int c=0; for(int i=1;i*i<N;i++) { c=c+1; } return c; }

Problem no 15: Calculate npr.

 QUESTION :   Write a program to calculate n P r .  n P r represents n permutation r and value of n P r is (n!) / (n-r)!. Example 1: Input: n = 2, r = 1 Output: 2 Explaination: 2!/(2-1)! = 2!/1! = (2*1)/1 = 2. Example 2: Input: n = 3, r = 3 Output: 6 Explaination: 3!/(3-3)! = 3!/0! = 6/1 = 6.   ANSWER: long long int factorial(int N){ long long int result=1; for(int i=1;i<=N;i++) { result*=i; } return result; } long long nPr(int n, int r){ int diff=n-r; long long int x=factorial(n); ..calling factorial long long int y=factorial(diff); ..calling factorial long long z=x/y; return z; }

Problem no 14: Largest prime Factor.

 QUESTION: Given a number N , the task is to find the largest prime factor of that number.   Example 1: Input: N = 5 Output: 5 Explanation: 5 has 1 prime factor i.e 5 only. Example 2: Input: N = 24 Output: 3 Explanation: 24 has 2 prime factors 3 and 2 in which 3 is greater    ANSWER:   long long int largestPrimeFactor(int n){ int num=n; while(true) { if(n%num==0) { int yes= isPrime(num); ..calling isPrime func. if(yes==1) { return num; } } num--; } }   int isPrime(int n){ if(n==1) { return 0; } else { for(int i=2;i<=n/2;i++) { if(n%i==0) { return 0; } } return 1; } }  

Problem no 13: check whether the number is prime or not.

 QUESTION:   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: 1 Explanation: 5 has 2 factors 1 and 5 only. Example 2: Input: N = 25 Output: 0 Explanation: 25 has 3 factors 1, 5, 25    ANSWER:   int isPrime(int n){ if(n==1) { return 0; } else { for(int i=2;i<=n/2;i++) { if(n%i==0) { return 0; } } return 1; }  }

Problem no 12: Factorial .

 QUESTION: Factorial Basic Accuracy: 54.57% Submissions: 4751 Points: 1 Given a positive integer,  N . Find the factorial of N .   Example 1: Input: N = 5 Output: 120 Explanation: 5*4*3*2*1 = 120 Example 2: Input: N = 4 Output: 24 Explanation: 4*3*2*1 = 24 Your Task: You don't need to read input or print anything. Your task is to complete the function factorial()  which takes an integer N  as input parameters and returns an integer, the factorial of N.   ANSWER: long long int factorial(int N){      long long int re...

Problem no 11: GCD of the entire array.

QUESTION: GCD of Array Given an array of N positive integers, find GCD of all the array elements. Example 1: Input: N = 3, arr[] = {2, 4, 6} Output: 2 Explanation: GCD of 2,4,6 is 2. Example 2: Input: N = 1, arr[] = {1} Output: 1 Explanation: Greatest common divisor of all the numbers is 1. ANSWER:        int gcd(int N, int arr[])     {         if(N==1)         {             return arr[0];        ...

Problem no 10: LCM and GCD.

 QUESTION: LCM And GCD Given two numbers A and B. The task is to find out their LCM and GCD.   Example 1: Input: A = 5 , B = 10 Output: 10 5 Explanation: LCM of 5 and 10 is 10, while thier GCD is 5. Example 1: Input: A = 14 , B = 8 Output: 56 2 Explanation: LCM of 14 and 8 is 56, while thier GCD is 2.   ANSWER:     vector<long long> lcmAndGcd(long long a , long long b) {         vector<long long> vec;          long long lcmm=lcm(a,b);   ......   CALLING ...

problem no 9: GCD of A and B.

         QUESTION:   GCD of two numbers Given two positive integers A and B, find GCD of A and B. Example 1: Input: A = 3, B = 6 Output: 3 Explanation: GCD of 3 and 6 is 3 Example 2: Input: A = 1, B = 1 Output: 1 Explanation: GCD of 1 and 1 is 1           Inefficient code for gcd:   int gcd(int A, int B)     {         int small;         if(A>B)         {             small=B; ...

problem no 8:Binary number to decimal number

 QUESTION: Given a Binary Number in the string format B , find its decimal equivalent.   Example 1: Input: B = 10001000 Output: 136 Example 2: Input: B = 101100 Output: 44     ANSWER:       int binary_to_decimal(string str)         {             vector<char> vec;             int result;             for(int i=str.size()-1;i>=0;i--)             {                 vec.push_back(str[i]);             }             for(int i=0;i<vec.size();i++)             {             ...

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

 QUESTION: Given two numbers A and B , find K th digit from right of A B .   Example 1: Input: A = 3 B = 3 K = 1 Output: 7 Explanation: 3 3 = 27 and 1st digit from right is 7 Example 2: Input: A = 5 B = 2 K = 2 Output: 2 Explanation: 5 2 = 25 and second digit from right is 2. ANSWER: int kthDigit(int A,int B,int K){      long long int num=pow(A,B);      int count=0;      int rem;      while(num>0)      {                    rem=num%10;                    num=num/10;          count=count+1;          if(count!=K)          {         ...

problem no 6: Reverse the number.

 QUESTION: Given N,  reverse the digits of N.   Example 1: Input: 200 Output: 2 Explanation: By reversing the digts of number, number will change into 2. Example 2: Input : 122 Output: 221 Explanation: By reversing the digits of number, number will change into 221.     ANSWER: long long int reverse_digit(long long int n)         {           long long int result=0;           while(n>0)           {               int rem=n%10;                             result=result*10+rem;               n=n/10;           }           return result; ...

Problem no 5:Sum of Digit is Pallindrome or not

QUESTION: Given a number N.Find if the digit sum(or sum of digits) of N is a Palindrome number or not. Note:A Palindrome number is a number which stays the same when reversed.Example- 121,131,7 etc. Example 1: Input: N=56 Output: 1 Explanation: The digit sum of 56 is 5+6=11. Since, 11 is a palindrome number.Thus, answer is 1. Example 2: Input: N=98 Output: 0 Explanation: The digit sum of 98 is 9+8=17. Since 17 is not a palindrome,thus, answer is 0.  ANSWER: int isDigitSumPalindrome(int N) { int sum=0;           while(N>0)           {               int rem=N%10;               sum=sum+rem;               N=N/10;           }    ...

Problem no 4: Armstrong number.

 QUESTION:   For a given 3 digit number, find whether it is armstrong number or not. An  Armstrong number  of three digits is an integer such that the sum of the cubes of its digits is equal to the  number  itself. Return " Yes " if it is a armstrong number else return " No ". NOTE:  371 is an  Armstrong number  since 3 3 + 7 3 + 1 3 = 371 ANSWER: string armstrongNumber(int n){         int x=n;         int result=0;         while(n>0)         {                         int rem =n%10;             result=result+pow(rem,3);             n=n/10;   ...

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

  Closest Number Given non-zero two integers N and M . The problem is to find the number closest to N and divisible by M . If there are more than one such number, then output the one having maximum absolute value .   Example 1: Input: N = 13 , M = 4 Output: 12 Explanation: 12 is the Closest Number to 13 which is divisible by 4. Example 2: Input: N = -15 , M = 6 Output: -18 Explanation: -12 and -18 are both similarly close to -15 and divisible by 6. but -18 has the maximum absolute value. So, Output is -18   ANSWER:   int closestNumber(int n , int m) { int q = n / ...

problem 2:Given the first 2 terms A1 and A2 of an Arithmetic Series.Find the Nth term of the series.

 QUESTION:   Series AP School Given the first 2 terms A1 and A2 of an Arithmetic Series.Find the N th term of the series.  Example 1: Input: A1=2 A2=3 N=4 Output: 5 Explanation: The series is 2,3,4,5,6.... Thus,4th term is 5.   ANSWER:   int nthTermOfAP(int A1, int A2, int N) {       int result;       int diff=A2-A1;       result=A1+(N-1)*diff;       return result;     }

problem 1: print the table of N.

  Multiplication Table School Print the multiplication table of a given number N.   Example 1: Input: N = 9 Output: 9 18 27 36 45 54 63 72 81 90     ANSWER:   vector<int> getTable(int N) { vector<int> vec; for(int i=1;i<11;i++) { vec.push_back(N*i); } return vec; }