Skip to main content

Posts

Showing posts from September, 2022

LeetCode Solutions | 67. Add Binary | LeetCode C++ Solution

 LeetCode Question:  67. Add Binary   ( Level :  Easy ) Problem Statement: Given two binary strings a and b , return their sum as a binary string. Note :- Scroll horizontally to see the full line of code. class Solution { public : string addBinary ( string a , string b ) { int n = a . length () - 1 ; int m = b . length () - 1 ; string ans ; int c = 0 ; while ( m > - 1 && n > - 1 ) { int a1 = int ( a [ n ]) - 48 , b1 = int ( b [ m ]) - 48 ; int sum = a1 ^ b1 ^ c ; c = ( a1 & b1 ) | ( a1 & c ) | ( b1 & c ); ans . append ( to_string ( sum )); n -- ; m -- ; } while ( n > - 1 ) { int a1 = int ( a [ n ]) - 48 ; int sum = a1 ^ c ; c = a1 & c ; ans . append ( to_string ( sum )); ...

LeetCode Solutions | 622. Design Circular Queue | LeetCode C++ Solution

 LeetCode Question: 622. Design Circular Queue   (Level : Medium ) Note :- Scroll horizontally to see the full line of code. class MyCircularQueue { public : int size ; int * q ; int rear = - 1 ; MyCircularQueue ( int k ) { size = k ; q = new int [ size ]; } bool enQueue ( int value ) { if ( rear < size - 1 ) { q [ ++ rear ] = value ; return true ; } else { return false ; } } bool deQueue () { if ( rear >= 0 ) { for ( int i = 1 ; i <= rear ; i ++ ) { q [ i - 1 ] = q [ i ]; } rear -- ; return true ; } else { return false ; } } int Front () { if ( rear >= 0 ) { return q [ 0 ]; } else { ...

Factorial using Recursion in CPP | Factorial of a Number Program

 Problem Statement: Write a program in cpp to find the factorial of a number. Note :- Scroll horizontally to see the full line of code. // Program to find the factorial of a number using recursion #include < iostream > using namespace std ; int factorial ( int n ) { if ( n == 0 ) { return 1 ; } return n * factorial ( n - 1 ); } int main () { int n ; cout << " Enter n: " ; cin >> n ; cout << " Factorial of " << n << " is " << factorial ( n ); cout << endl ; return 0 ; }

Print 1 to n numbers using recursion in CPP | Print 1 to n numbers without using loop in CPP

 Problem Statement: Print 1 to n numbers using recursion in cpp (c++). Note :- Scroll horizontally to see the full line of code. // Program to print 1 to n numbers using recursion #include < iostream > using namespace std ; void print ( int n ) { if ( n == 0 ) { return ; } print ( n - 1 ); cout << n << " " ; } int main () { int n ; cout << " Enter n: " ; cin >> n ; print ( n ); cout << endl ; return 0 ; }