Posts

Showing posts from July, 2021

Add binary numbers and output binary numbers (no. of digits> 10^4)

IMPLEMENTED WITH HELP OF LOOK AHEAD CARRY ADDER Input: a = "11", b = "1" Output: "100" Input: a = "1010", b = "1011" Output: "10101"   string   addBinary ( string   a ,  string   b ) {      int   carry  =  0 ,  sum  =  0 ,  i ,  j ,  x ,  y ;     string  s  =  "" ;      i  =  a . size () -  1 ;      j  =  b . size () -  1 ;      while  ( i  >=  0  &&  j  >=  0 )     {          sum  = (( a [ i ] -  '0' ) ^ ( b [ j ] -  '0' )) ^ ( carry );          s  = ( char )( sum  +  '0' ) +  s ;          x ...

Increment the array LSB digit by one

Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Input: digits = [9,9,9] Output: [1,0,0,0] Explanation: The array represents the integer 999. vector < int >  plusOne ( vector < int >  & d ) {      int  i =  d . size () -  1 , sum =  0 , carry =  0 ;     sum =  d [ d . size () -  1 ] +  1 ;     carry = sum /  10 ;      d [ d . size () -  1 ] = sum %  10 ;     i -=  1 ;      while  (i >=  0  && carry !=  0 )     {         sum =  d [i] + carry;          d [i] = sum %  10 ; ...

Find the decimal number from the linked list representation of its binary form !

Image
  Input: head = [1,0,1] Output: 5 Explanation: (101) in base 2 = (5) in base 10 /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     ListNode *next;  *     ListNode() : val(0), next(nullptr) {}  *     ListNode(int x) : val(x), next(nullptr) {}  *     ListNode(int x, ListNode *next) : val(x), next(next) {}  * };  */ class   Solution { public:      void   rev ( ListNode   * head ,  vector < int >  & v )     {          if  ( head  ==  0 )              return ;  ...

Add numbers present in strings

Return the output string. Input: num1 = "11", num2 = "123" Output: "134" Input: num1 = "456", num2 = "77" Output: "533" string   addStrings ( string   num1 ,  string   num2 ) {      int   i ,  j ,  sum ,  carry ,  x ,  y ;      if  ( num1  ==  "0" )          return   num2 ;      else   if  ( num2  ==  "0" )          return   num1 ;      else     {         string  s  =  "" ;          carry  =  0 ;          sum  =  0 ;          i  =  num1 . size () -  1 ,  j  =  num2 . size () - ...

Add integers from a number and an array !!

Here we have two numbers, one is normally an integer number while the digits of other number are arranged in the array in the same order they appear in number. Input: num = [1,2,0,0], k = 34 Output: [1,2,3,4] Explanation: 1200 + 34 = 1234 vector < int >  addToArrayForm ( vector < int >  & a ,  int   k ) {      int  i =  a . size () -  1 , carry =  0 , sum =  0 ;     vector< int > v;      while  (k !=  0  && i >=  0 )     {         sum = k %  10  +  a [i] + carry;         carry = sum /  10 ;          v . push_back (sum %  10 );        ...

link list binary to decimal

  /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     ListNode *next;  *     ListNode() : val(0), next(nullptr) {}  *     ListNode(int x) : val(x), next(nullptr) {}  *     ListNode(int x, ListNode *next) : val(x), next(next) {}  * };  */ class Solution { public:      void rev(ListNode* head, vector<int>&v)     {         if(head==0)             return;         else             rev(head->next,v);         v.push_back(head->val);     }     int getDecimalValue(ListNode* head)      {         vector<int>v;         rev(head,v);         int sum=0,i=1;         for...

Check if Given string is palindrome after removing at most 1 character !!

#include   <bits/stdc++.h> using   namespace   std ; bool   ispalindrome ( int   i ,  int   j ,  string   s ) {      while  (i < j)     {          if  ( s [i] ==  s [j])         {             i++;             j--;         }          else              return   0 ;     }      return   1 ; } bool   validPalindrome ( string   s ) {      int  i =  0 , j =  s . size () -  1 , flag =  1 ;   ...

Valid Bracket sequence

  bool   isValid ( string   s )      {         stack< char >stk;          int   f = 1 , i ;          for ( i = 0 ; i < s . size () &&  f == 1 ; i ++)         {              if ( stk . empty ())             {                  if (( s [ i ]== '{'  ||  s [ i ]== '['  ||  s [ i ]== '(' ))                      stk . push ( s [ i ]);                  else   ...

Different approaches of merge function of Merge sort(linked list)

// 1.Least time consuming and memory efficient // 1A. ListNode   * merge ( ListNode   * l1 ,  ListNode   * l2 ) {     cout <<  "l1 " ;      printList (l1);     cout <<  "l2 " ;      printList (l2);     cout << endl          << endl;     // base case if (a == NULL)      if  (l1 ==  0 )          return  l2;      if  (l2 ==  0 )          return  l1;     //take a head pointer node *c;     ListNode *c;      if  ( l1 -> val ...

Merge sort linked list ( Fast, less memory)

// This approach is faster //because in its merging part same lists are altered, //by changing the address of the remaining lists. #include   <iostream> using   namespace   std ; struct   ListNode {      int   val ;      ListNode  * next ;      ListNode ( int   val )     {          this -> val  =  val ;          this -> next  =  nullptr ;     } }; ListNode   * midNode ( ListNode   * head ) {      if  ( head  ==  0  ||  head -> next  ==  0 )          return   head ;      ListNode  * slow  =  head ;  ...

Merge sort linked list ( Slow, more memory)

//This approach is bit slower and memory consuming //because of its merging function part. //each time while merging a new list is created, //that occupies more memory. #include   <bits/stdc++.h> using   namespace   std ; struct   ListNode {      int   val ;      ListNode  * next ;      ListNode ( int   val )     {          this -> val  =  val ;          this -> next  =  nullptr ;     } }; void   printList ( ListNode   * head ) {      while  ( head  !=  0 )     {          cout   <<   head -> val   <...