Posts

Showing posts from March, 2021

get set oops

 #include <iostream> using namespace std; class student{     public:     int roll_no;     private :     int age;     public :     void display(){         cout<<age<<" "<<roll_no<<endl;     }     int getage(){         return age;     }     void setage(int a, int password){         if (password!=123){             return;         }         age =a;     }      }; int main() {     student s1,s2,s3;     student* s4= new student;     s1.roll_no=12;     s1.setage(-2,123);     s1.setage(23,124);     s4->setage(25,123);     s4->roll_no=101;          s1.getage();          cout<<s1.rol...

SUPER IMPORTANT: MERGESORT - O(nlogn)

 #include <iostream> using namespace std; void merge(int* arr, int first, int mid, int last){     int b[50];     int i,j,k;     i=first;     j=mid+1;     k=first;     while(i<=mid && j<=last){                  if (arr[i]<=arr[j]){             b[k++]=arr[i++];         }         else             b[k++]=arr[j++];     }     if(i>mid){         while(j<=last)             b[k++]=arr[j++];     }     else {         while(i<=mid)             b[k++]=arr[i++];     }                   for (int i=first;i<=last;i++){         arr[i]=b[i];   ...

VERY IMPORTANT : MERGE TWO SORTED ARRAYS

 #include <iostream> using namespace std; void merge(int*arr1,int* arr2,int m,int n){     int out[m+n];     int i=0,j=0,k=0;     while(i<m && j< n){         if(arr1[i]<arr2[j]){             out[k++]=arr1[i++];         }         else{             out[k++]=arr2[j++];         }              }     while(i<m){         out[k++]=arr1[i++];     }     while(j<n){         out[k++]=arr2[j++];     }     for(int i=0;i<m+n;i++){         cout<<out[i]<<endl;     }      } int main() {     int arr1[3]={2,5,8};     int arr2[3]={1,3,10};     merge(arr1,arr2,3,3);        ...

Duplicate in array

  You have been given an integer array/list(ARR) of size N which contains numbers from 0 to (N - 2). Each number is present at least once. That is, if N = 5, the array/list constitutes values ranging from 0 to 3, and among these, there is a single integer value that is present twice. You need to find and return that duplicate number present in the array. Note : Duplicate number is always present in the given array/list. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. First line of each test case or query contains an integer 'N' representing the size of the array/list. Second line contains 'N' single space separated integers representing the elements in the array/list. Output Format : For each test case, print the duplicate element in the array/list. Output for every test case will be printed in a separate line. Constraints : 1 <= t <= 10^2 0 <= N <= 10^6...

unique in array

  You have been given an integer array/list(ARR) of size N. Where N is equal to [2M + 1]. Now, in the given array/list, 'M' numbers are present twice and one number is present only once. You need to find and return that number which is unique in the array/list.  Note: Unique element is always present in the array/list according to the given condition. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. First line of each test case or query contains an integer 'N' representing the size of the array/list. Second line contains 'N' single space separated integers representing the elements in the array/list. Output Format : For each test case, print the unique element present in the array. Output for every test case will be printed in a separate line. Constraints : 1 <= t <= 10^2 0 <= N <= 10^6 Time Limit: 1 sec Sample Input 1: 1 7 2 3 1 6 3 6 2 Sample O...

remove duplicate recursion

 #include <iostream> #include <string> using namespace std; string removed(string s){     if(s.length()==0){         return "";              }     char ch=s[0];     string ans=removed(s.substr(1));          if(ch==ans[0]){         return ans;     }          return ch+ans;      } int main() {     cout<<removed("aabbccddeee");     return 0; }

keypad

 #include <iostream> #include <string> using namespace std; string keypadarr[]={"","./","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; void keypad(string s, string ans) {     if(s.length()==0){         cout<<ans<<endl;         return ;     }     char ch=s[0];     string code=keypadarr[ch-'0'];     string ros=s.substr(1);          for (int i=0;i<code.length();i++){         keypad(ros,ans+code[i]);     }           } int main() {     keypad("923","");     return 0; }

replace pi

 #include <iostream> #include <string> using namespace std; void replacepi(string s){     if(s.length()==0){         return;     }     if (s[0]=='p' && s[1]=='i'){         cout<<"3.14";         replacepi(s.substr(2));     }     else {         cout<<s[0];         replacepi(s.substr(1));     } } int main() {          string s="piipiiipi";     replacepi(s);     return 0; }

reverse string recursion

 #include <iostream> #include <string> using namespace std; void reverse(string s){     if(s.length()==0){         return;     }     string ros=s.substr(1);     reverse(ros);     cout<<s[0]; } int main() {          string s="binod";     reverse(s);     return 0; }

inc / dec order till n recursion

 #include <iostream> using namespace std; void decreasing(int n){     if (n==0){         return;     }          cout<<n<<" ";     decreasing(n-1); } void inc(int n){     if(n==0){         return;     }     inc(n-1);     cout<<n<<" "; } int main() {     int n;     cin>>n;     decreasing(n);     cout<<endl;     inc(n);          return 0; }

check array is sorted recursion

 #include <iostream> using namespace std; bool sorted(int *arr, int n){          if (n==1){         return true;     }          bool restarr=sorted(arr+1, n-1);      if (arr[0]<arr[1] && restarr){         return true;     }     return false;      } int xch(int*arr, int a,int b){     int temp;     temp=arr[a];     arr[a]=arr[b];     arr[b]=temp; } int main() {     int arr[]={1,2,6,4,5};     cout<<sorted(arr,5);          return 0; }

Recursion matrix: no of paths

 #include<iostream> using namespace std; int path(int m, int n); int main() {     int a,b;          cin >>a>>b;    cout<<path(a,b);     return 0; } int path(int m,int n) {     if(m == 1 || n==1)         return 1;     return path(m,n-1)+path(m-1,n); }