Posts

Showing posts from June, 2020

Absolute difference

// the absolute difference will be |a-b|; M1> int a,b; cin>>a>>b; e=(a>b)?(a-b):(b-a); cout<<e; M2> #include<cstdlib>        //this Headerfile must be included in the file int a,b; cin>>a>>b; e=abs(a-b);               //the inbuilt func. to find absolute difference cout<<e; MM

note

1) The symbols like {,[<@ are not characters, so they can't be stored in string. 2) If you need to enter the some elements in vector or set and you don't know the number of elements tha follow: ex for integers; set <int> a; char b; while(cin>>b) { if(any condition if you need) a.insert(b); } if you now the number of elements set <int> a; char b; int n; cin>>n; for(int i=0;i<n;i++) { if(any condition if you need) a.insert(b); }

Sets

Sets are the associative containers. There are mainly four types of associative containers: set, multi set, map and multi map. Unlike the vectors, when elements are putted in sets and maps, the elements have association with each other. They contain a sorted set of unique objects of any type. If we want to store user defined datatype (like any class's object) in set than we will have to provide compare function also, so that the data can be stored in set in sorted order. we can pass the order of sorting the elements in set(ascending or descending), but by default the order is ascending. set <datatype >  set_name;  set_name.insert (any number);     //inserts elements in default ascending order. set_name.erase (any number);    // Erases an integer val from the set . set <datatype , greater<datatype> >  set_name;    set_name.insert(any number);    // to store elements in descending order set <datatype , less...

CP

Basic Programming basic input and outputs every topic in a day Code monk hacker rank. Online IDE Telegram CP community groups https://t.me/competitive_programming https://t.me/codeforces_community https://t.me/CodeForcesGroup https://t.me/CP_Tuf https://t.me/sdeinterviewpreperation https://t.me/coding_FAANGM String class  Member function and other details Hackerearth STL notes All classes and details Y outube playlists Data Structures and Algorithms  << important. https://www.youtube.com/watch?v=X-2OUqnUH6U&list=PLfBJlB6T2eOup9gVZLAMjEV50YPKcPXuO&index=4 https://www.youtube.com/watch?v=8SEzicEJyYo&list=PLfBJlB6T2eOup9gVZLAMjEV50YPKcPXuO&index=5 https://www.youtube.com/watch?v=LAU4VrW60Sc&list=PLfBJlB6T2eOup9gVZLAMjEV50YPKcPXuO&index=6 What is CP competetive programming Codeforces beginner problems Google code Sheet Most important sheet

Vectors

VECTORS First of all include the header #include<vector> in file; The capacity of the vector at the time it is initialised is 0, when value is stored in it, its capacity first increases by 1 and then gets doubled each time the capacity of the vector lacks. Vector <datatype> vector_name; //creates vector of size zero. Vector <datatype> vector_name(n); //creates vector of size n. vector_name.push_back(any number); //to insert any number in the vector and the value gets inserted in the one by one order. vector_name.pop_back() ;   // pops out the last element from the vector. This function does not reduces the size of the vector once increased by push_back() function. To enter the elmts in vector with help of loop. for(i=0; i<n; i++) { cin>>a; vector_name.push_back(a); } vector_name.size();  // returns the number of elements filled in vector. vector_name.capacity(); //returns the capacity of the vector. vector_name.clear(); // is used to empty the vec...

To get precision of n digits in decimal value.

First of all use the header #include<iomanip> Write cout<<fixed;   ///// to get the same precision of zeros also in decimal points. Then cout<<setprecision(n);   ----(A)          cout<<variable;   //variable which you want with n decimal precision points. Note: the value n should be in the decimal ranging of datatype. You can also use type casting to get more decimal point values. Now all the values printed after the above statement A   will have the same precision. In order to disable the precision use the statement cout.unsetf (ios::fixed);

Dynamically Allocating Arrays

The major use of the concept of dynamic memory allocation is for allocating memory to an array when we have to declare it by specifying its size but are not sure about it. Let’s see examples to understand its usage. #include <iostream> using namespace std; int main() { int len, sum = 0; cout << "Enter the no. of students in the class" << endl; cin >> len; int *marks= new int [len]; //Dynamic memory allocation OR int *marks = (int*)malloc(len * sizeof(int)); //Dynamic memory allocation cout << "Enter the marks of each student" << endl; for( int i = 0; i < len; i++ ) { cin >> *(marks+i); } for( int i = 0; i < len; i++ )        { sum += *(marks+i); } cout << "sum is " << sum << endl; return 0; }

String class member functions

string a; a.size() or a.length();              //       to find the length of string. reverse(a.begin(),a.end());     //        to reverse the string a.swap(b)         //        to swap string a with b. To enter a multi word string like"my name is ABC"; string s; getline(cin,s); and it can simply be printed on screen using cout. To enter a multiple line string; #include <bits/stdc++.h> using namespace std; int main() {     int t;     string s,a;     cin>>t;     while(t--) {  cin>>a ;  s+=a+ "\n" ;     // this  (\n)  make a multi line string. }    cout<<s; } INPUT 1: 5 XO|XX XX|XX XO|OX XO|OO OX|XO OUTPUT 1: XO|XX XX|XX XO|OX XO|OO OX|XO INPUT 2: 3 RAM Rajesh Ramesh OUTPUT 2: RAM Rajesh  Ramesh To find the position of character and replace it with oth...