Skip to main content

Posts

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 ; }

Sequential File Handling using CPP to store data of studenets

  Problem Statement:- Department maintains student information. The file contains the roll number, name, division and address. Allow user to add, and delete information of student. Display information about a particular student. If the record of the student does not exist an appropriate message is displayed. If it is, then the system displays the student details. Use the sequential file to maintain the data. Note :- Scroll horizontally to see the full line of code. #include < iostream > #include < fstream > #include < cstring > #include < iomanip > using namespace std ; const int MAX = 20 ; class Student {     int rollno ;     char name [ 20 ], city [ 30 ];     int div ;     int year ; public :     Student ()     {         strcpy ( name , "" );         strcpy ( city , "" );         rollno = year = div = 0 ;    ...

Implementation of Heap Sort using CPP | Heapify Function

  Problem Statement:- Implement the Heap sort algorithm implemented in CPP demonstrating heap data structure with the modularity of programming language. Note :- Scroll horizontally to see the full line of code. #include < iostream > using namespace std ; class heap {     int n ;     int * array ; public :     heap ( int n ) {         this -> n = n ;         array = new int [ n ];     }     void inputheap ();     void swap ( int i , int j );     void heapify ( int size , int i );     void heapSort ();     void printheap (); }; void heap :: inputheap () {     cout << " Enter elements: " << endl ;     for ( int i = 0 ; i < n ; i ++ ) {         cout << i << " : " ;         cin >> array [ i ];     } } void he...

Topological Sort of Graph using CPP | Representation of Graph using Adjacency List

  Problem Statement:- Write a C++ program to implement topological sorting on graph using object-oriented programming features. Design necessary class (Use of Graph). Note :- Scroll horizontally to see the full line of code. #include < iostream > using namespace std ; class flagarray {     char data ;     int flag ; public :     flagarray () {         this -> data = ' * ' ;         this -> flag = 0 ;     }     flagarray ( char data , int flag ) {         this -> data = data ;         this -> flag = flag ;     }     friend class node ;     friend class graph ;     friend class stack ; }; class stack {     int top = - 1 ;     char arr [ 30 ]; public :     bool isFull ();     bool isEmpty ();     void push ( char data );...