Skip to main content

Posts

Showing posts from September, 2020

two pointer technique which is used to solve some basic algorithms related question like 2sum, 3sum, and difference of 2 item in a vector equal to given target

the two-pointer technique is a very popular technique to solve some basic problem like just watch this nice video for more information https://www.youtube.com/watch?v=2wVjt3yhGwg&t=174s https://leetcode.com/articles/two-pointer-technique/ 1. Given a  vector  of integers and a target sum returns true if any two elements sum is equal to the target in the given vector bool checkSum(int sum, vector<int> &myVect) {     sort(myVect.begin(),myVect.end());     int i=0,j=myVect.size()-1;     while(j>=i)     {         if(myVect[i]+myVect[j]==sum)         {             return true;         }else if(myVect[i]+myVect[j]>sum)         {             j--;         }else if(myVect[j]+myVect[i]<sum)         {             i++; ...

Reverse a sentence and print each word in reverse order in Javascript

  Reverse a sentence and print each word in reverse order in Javascript working sample https://www.onlinegdb.com/edit/S1qrNPHEw #include <iostream> #include<string> #include <ctype.h> using namespace std; string reverse(string &name) {     string newStr;     int len=name.length()-1;     int track=0;     int count = 0;      for(int i=len;i>=0;i--)     {         int c=name[i];         if (isspace(c))         {                         int start=i;             int end=(name.length())-track;             string temp;             for(int i=start;i<end;i++)             {                 temp+=name[i];       ...

some common recursive problem in c++

//1 check a string is palindrome or not bool checkString(string &name,int start,int end) {     if(start>=end)     {         return true;     }     char a=name[start];     char b=name[end];     if(a==b)     {        return checkString(name,++start,--end);     }      return false;      } //2 get maximum element from a array int getMax(vector<int> &items,int index=0) {     if(index>=items.size())     {         return 0;     }     int current=items.at(index);     return max(current,getMax(items,++index)); } //3 Longest Common Subsequence int LCS(string &str1,string &str2,int a=0,int b=0) {     if(a>=str1.length() || b>=str2.length())     {         return 0;     }     if(str1[a]...