├── Lecture-01 STL ├── a.exe ├── btw_menu.txt ├── btw_menu_2.txt ├── char_array.cpp ├── compartors.cpp ├── functional_objects.cpp ├── hashmap.cpp ├── in_file.cpp ├── input.txt ├── output.txt ├── priority_queue.cpp ├── reading_lot_of_inputs.cpp ├── s ├── scanf_return_value.cpp ├── search.cpp ├── set.cpp ├── string_token_complete.cpp ├── tokenization.cpp └── vector_string_int.cpp ├── Lecture-02 Maths ├── a.exe └── birthday_paradox.cpp ├── Lecture-03 Binary Search ├── a.exe ├── cows.cpp ├── set_bits.cpp ├── square_root.cpp ├── subsets.cpp └── upper_lower.cpp └── README.md /Lecture-01 STL/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/competitive-pitampura-dec-2018/04187a0042598933cac8ea046a97e933999c6650/Lecture-01 STL/a.exe -------------------------------------------------------------------------------- /Lecture-01 STL/btw_menu.txt: -------------------------------------------------------------------------------- 1 | 3 2 | golgappa 3 | 30 4 | pawbhaji 5 | 40 6 | icecream-shakes 7 | 60 8 | -------------------------------------------------------------------------------- /Lecture-01 STL/btw_menu_2.txt: -------------------------------------------------------------------------------- 1 | 3 2 | golgappa 30 3 | pawbhaji 40 4 | icecream shakes 60 5 | 60 grapes wines 70 6 | -------------------------------------------------------------------------------- /Lecture-01 STL/char_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | int main(){ 9 | 10 | char s[100]; 11 | //cin>>s; 12 | 13 | //cin.getline(s,100,'.'); //delimiter 14 | //cout<>n; 18 | 19 | int price; 20 | string name; 21 | 22 | for(int i=0;i>price; 26 | 27 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool compare(int a,int b){ 7 | cout<<"Comparing "<b; 9 | } 10 | 11 | int main(){ 12 | 13 | vector v; 14 | 15 | 16 | 17 | for(int i=0;i<10;i++){ 18 | v.push_back(i*i); 19 | cout< 2 | using namespace std; 3 | 4 | 5 | class Person{ 6 | string name; 7 | string category; 8 | int package; 9 | public: 10 | Person(){ 11 | 12 | } 13 | Person(string n,string catg,int p){ 14 | name = n; 15 | category = catg; 16 | package = p; 17 | } 18 | bool operator()(string lang){ 19 | //cout<<"Having Fun!"; 20 | cout<, PersonCompare> pq; 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Lecture-01 STL/hashmap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | map phonebook; 9 | phonebook["Prateek"] = "10012"; 10 | phonebook.insert(make_pair("Rohan","12003")); 11 | 12 | pair p3; 13 | p3.first = "Lolkit"; 14 | p3.second = "7009"; 15 | 16 | phonebook.insert(p3); 17 | 18 | //Search 19 | string name; 20 | cin>>name; 21 | /* 22 | if(phonebook.count(name)){ 23 | cout<second<<" is the desired no!"; 35 | } 36 | 37 | //phonebook.erase("Pulkit"); 38 | //Lets Print everything which is inside hashmap 39 | 40 | /* 41 | for(unordered_map::iterator it = phonebook.begin();it!=phonebook.end();it++){ 42 | //it->first; 43 | //(*it).first; 44 | cout<first<<" and "<second< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Compare{ 7 | public: 8 | bool operator()(int a,int b){ 9 | return a>b; 10 | } 11 | 12 | }; 13 | 14 | int main(){ 15 | 16 | //priority_queue maxHeap; 17 | 18 | priority_queue,Compare> q; //Min Heap 19 | 20 | 21 | int a[] = {1,4,5,3,2,0}; 22 | 23 | for(int i=0;i<6;i++){ 24 | q.push(a[i]); 25 | } 26 | 27 | //Remove 28 | while(!q.empty()){ 29 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | int no; 9 | while(scanf("%d ",&no)!=EOF){ 10 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | int no; 9 | auto x = scanf("%d",&no); 10 | 11 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | //Linear Search 9 | int a[] = {1,2,3,3,3,3,0,4,5,9}; 10 | int n = sizeof(a)/sizeof(a[0]); 11 | int idx = find(a,a+n,3)-a; 12 | cout<>key; 22 | 23 | bool present = binary_search(a,a+n,key); 24 | if(present){ 25 | cout<<"Yes "; 26 | int lb = lower_bound(a,a+n,key) - a; 27 | cout<<"Lower Bound "< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | int a[] = {5,4,1,2,1,1,0}; 9 | int n = sizeof(a)/sizeof(int); 10 | 11 | set s; 12 | 13 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | char* myStrtok(char *str,char delim){ 6 | 7 | static char*input = NULL; 8 | if(str!=NULL){ 9 | input = str; 10 | } 11 | if(input==NULL){ 12 | return NULL; 13 | } 14 | 15 | 16 | char*output = new char[strlen(input)+1]; 17 | int i; 18 | for(i=0;input[i]!='\0';i++){ 19 | if(input[i]!=delim){ 20 | output[i] = input[i]; 21 | } 22 | else{ 23 | ///Delim 24 | output[i] ='\0'; 25 | input = input + i + 1; 26 | return output; 27 | } 28 | } 29 | output[i] = NULL; 30 | input = NULL; 31 | return output; 32 | 33 | 34 | 35 | 36 | } 37 | 38 | int main(){ 39 | 40 | char input[100] = "Hey, this is something new"; 41 | 42 | char*token = strtok(input," "); 43 | 44 | while(token!=NULL){ 45 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | char s[1000]; 9 | cin.getline(s,1000); 10 | 11 | char *ptr = strtok(s," "); 12 | 13 | while(ptr!=NULL){ 14 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | bool compare(pair p1, pair p2){ 8 | 9 | int l1 = p1.first.length(); 10 | int l2 = p2.first.length(); 11 | if(l1==l2){ 12 | return p1.second < p2.second; 13 | } 14 | return l1 p; 21 | p.first = "Apple"; 22 | p.second = 100; 23 | 24 | vector > v; 25 | int n; 26 | cin>>n; 27 | 28 | for(int i=0;i>item>>price; 32 | v.push_back(make_pair(item,price)); 33 | } 34 | 35 | sort(v.begin(),v.end(),compare); 36 | 37 | /* 38 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | float p=1.0; 8 | int cnt = 0; 9 | 10 | float threshold; 11 | cin>>threshold; 12 | int num = 365; 13 | float denom = 365.0; 14 | 15 | while(p>1-threshold){ 16 | p *= num/denom; 17 | cnt++; 18 | cout<<"Count "< 2 | using namespace std; 3 | 4 | bool canPlace(int cows,int *stalls,int n,int sep){ 5 | 6 | int c = 1; 7 | int last_cow = stalls[0]; 8 | 9 | for(int i=1;i=sep){ 11 | c++; 12 | last_cow = stalls[i]; 13 | } 14 | if(c==cows){ 15 | return true; 16 | } 17 | } 18 | return false; 19 | } 20 | 21 | int main(){ 22 | 23 | int stalls[] = {1,2,4,8,9}; //after sorting 24 | int cows = 3; 25 | int n = 5; 26 | 27 | int s = 0; 28 | int e = stalls[n-1] - stalls[0]; 29 | int ans = 0; 30 | while(s<=e){ 31 | 32 | int mid = (s+e)/2; 33 | 34 | if(canPlace(cows,stalls,n,mid)){ 35 | ans = mid; 36 | s = mid + 1; 37 | 38 | } 39 | else{ 40 | e = mid-1; 41 | } 42 | } 43 | cout<<"Ans "< 2 | using namespace std; 3 | 4 | //Complexity O(LogN) 5 | int countBits(int n){ 6 | 7 | int ans = 0; 8 | while(n>0){ 9 | ans += (n&1); 10 | n = n>>1; 11 | } 12 | return ans; 13 | } 14 | // Hack N & (N-1) 15 | //===> No of set bits 16 | int countBits2(int n){ 17 | 18 | int ans = 0; 19 | while(n>0){ 20 | n = n&(n-1); 21 | ans++; 22 | } 23 | return ans; 24 | } 25 | 26 | 27 | 28 | 29 | int main(){ 30 | 31 | 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Lecture-03 Binary Search/square_root.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | float squareRoot(int n,int p){ 6 | 7 | int s = 0; 8 | int e = n; 9 | 10 | int ans = 0; 11 | 12 | while(s<=e){ 13 | int mid = (s+e)/2; 14 | 15 | if(mid*mid==n){ 16 | return mid; 17 | } 18 | else if(mid*mid>n; 47 | 48 | cout< 2 | using namespace std; 3 | 4 | void filterChar(char *a,int i){ 5 | 6 | int j = 0; 7 | while(i>0){ 8 | char ch = a[j]; 9 | int last_bit = (i&1); 10 | if(last_bit){ 11 | cout<>1; 14 | j++; 15 | } 16 | cout< 2 | using namespace std; 3 | 4 | int lowerBound(int *a,int n,int key){ 5 | int s = 0; 6 | int e = n-1; 7 | 8 | int ans = -1; 9 | 10 | while(s<=e){ 11 | int mid = (s+e)/2; 12 | if(a[mid]==key){ 13 | ans = mid; 14 | e = mid - 1; 15 | } 16 | else if(a[mid]