├── .DS_Store ├── .gitignore ├── 01 Getting Started with C++ ├── .DS_Store ├── binary_numbers.cpp ├── handling_multiple_inputs.cpp ├── hello.cpp ├── hello_world.cpp ├── input_file.txt ├── output_file.txt ├── pattern_manmohan.cpp ├── simple_interest.cpp ├── sum_of_digits.cpp └── sum_of_numbers.cpp ├── 02 Programming Fundamentals-I ├── .DS_Store ├── data_type_modifiers.cpp ├── datatypes.cpp ├── do_while.cpp ├── fahrenheit_celsius_conversion.cpp ├── for_loop.cpp ├── if_else.cpp ├── min_max_number.cpp ├── pattern_traingle_number.cpp ├── square_root.cpp └── while_do_while.cpp ├── 03 Programming Fundamentals-II ├── .DS_Store ├── bitwise_operators.cpp ├── break.cpp ├── break_continue.cpp ├── calc_digits_spaces_alphabets.cpp ├── cinget.cpp ├── displacement.cpp ├── hello.cpp ├── pattern_alternate_0s_1s.cpp ├── prime_number.cpp ├── switch_case.cpp ├── test.cpp ├── unique_number.cpp ├── untitled.cpp └── variable_scope.cpp ├── 10DaysOfCode ├── 2_trailing_zeroes ├── 2_trailing_zeroes.cpp ├── div_subarrays ├── div_subarrays.cpp ├── ganesha ├── ganesha.cpp ├── magical_park ├── magical_park.cpp ├── park.cpp ├── prime_numbers_sieve ├── prime_numbers_sieve.cpp ├── prime_visits ├── prime_visits.cpp ├── rotate_image └── rotate_image.cpp ├── Arrays ├── 2_d_array ├── 2_d_array.cpp ├── 2d_character_array ├── 2d_character_array.cpp ├── array_basic ├── array_basic.cpp ├── binary_search ├── binary_search.cpp ├── bubble_sort ├── bubble_sort.cpp ├── bubble_sort_with_comparator ├── bubble_sort_with_comparator.cpp ├── character_arrays ├── character_arrays.cpp ├── help_rahul ├── help_rahul.cpp ├── inbuilt_sort ├── inbuilt_sort.cpp ├── insertion_sort ├── insertion_sort.cpp ├── largest_smallest ├── largest_smallest.cpp ├── largest_string ├── largest_string.cpp ├── length_of_string ├── length_of_string.cpp ├── linear_search ├── linear_search.cpp ├── pair_sum ├── pair_sum.cpp ├── palindrome ├── palindrome.cpp ├── read_a_list_of_strings ├── read_a_list_of_strings.cpp ├── read_paragraph ├── read_paragraph.cpp ├── remove_duplicates ├── remove_duplicates.cpp ├── selection_sort ├── selection_sort.cpp ├── sort_the_strings_challenge ├── sort_the_strings_challenge.cpp ├── spiral_print ├── spiral_print.cpp ├── string_class ├── string_class.cpp ├── test ├── test.cpp ├── wave_print └── wave_print.cpp ├── Bitmasking ├── .DS_Store ├── common_bit_tasks.cpp ├── decimal_to_binary.cpp ├── not_so_easy_math.cpp ├── power_fast.cpp ├── problem_replace_bits_n_m.cpp ├── set_bit_count.cpp ├── subsequences.cpp └── unique_number_2n_2.cpp ├── Functions ├── abcd ├── abcd.cpp ├── binary_search ├── binary_search.cpp ├── fibonacci_pattern ├── fibonacci_pattern.cpp ├── hello_function ├── hello_function.cpp ├── print_all_primes └── print_all_primes.cpp ├── Greedy Algorithms ├── acode.cpp ├── greedy_money_change_recursive ├── greedy_money_change_recursive.cpp ├── indian_money_counting └── indian_money_counting.cpp ├── README.md ├── STL ├── .DS_Store ├── Algorithms │ ├── .DS_Store │ ├── algo_stl_01_find.cpp │ ├── algo_stl_02_binary_search_lower_upper.cpp │ ├── algo_stl_06_next_permuation.cpp │ ├── algo_stl_money_change_problem.cpp │ └── pair_stl.cpp ├── Strings │ ├── .DS_Store │ ├── string_stl_01 2.cpp │ ├── string_stl_01.cpp │ ├── string_stl_02 2.cpp │ ├── string_stl_02.cpp │ ├── string_stl_03 2.cpp │ ├── string_stl_03.cpp │ ├── string_stl_04_tokneizer_implement 2.cpp │ └── string_stl_04_tokneizer_implement.cpp └── Vectors │ ├── .DS_Store │ ├── vector │ ├── vector.h │ ├── vector_demo.cpp │ ├── vector_stl_01.cpp │ ├── vector_stl_02.cpp │ └── vector_stl_1.cpp └── Webinar-01 ├── brackets ├── brackets.cpp ├── brackets_gen ├── brackets_gen.cpp ├── coin_greedy ├── coin_greedy.cpp ├── subsums └── subsums.cpp /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .exe -------------------------------------------------------------------------------- /01 Getting Started with C++/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/01 Getting Started with C++/.DS_Store -------------------------------------------------------------------------------- /01 Getting Started with C++/binary_numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Question Von Neuman Loves Binary 5 | 6 | 7 | int main() { 8 | 9 | int no; 10 | int N; 11 | cin>>N; 12 | 13 | while(N>0){ 14 | 15 | cin>>no; 16 | 17 | int p = 1; 18 | int ans = 0; 19 | 20 | while(no>0){ 21 | int r = no%10; 22 | ans = ans + r*p; 23 | p = p*2; 24 | no = no/10; 25 | } 26 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | // You are given N, followed by list of N numbers 7 | // Output : the squares of N Numbers 8 | /* 9 | 10 | Input File 11 | 3 N 12 | 1 13 | 2 No 14 | 5 15 | 16 | Output File 17 | 1 18 | 4 Ans 19 | 25 20 | */ 21 | int N; 22 | cin>>N; 23 | 24 | int no; 25 | while(N>0){ 26 | cin>>no; 27 | cout< //header file 2 | using namespace std; // standard c++ namespace 3 | 4 | 5 | 6 | int main(){ //start 7 | 8 | //Output 9 | cout<<"Hello Everyone!"< 2 | using namespace std; 3 | 4 | 5 | int main () { 6 | 7 | int n ; 8 | cin>>n; 9 | 10 | int row = 1; 11 | while(row<=n){ 12 | int col = 1; 13 | 14 | if(row%2!=0){ 15 | //Odd Row 16 | while(col<=row){ 17 | cout<<1; 18 | col = col + 1; 19 | } 20 | 21 | } 22 | else{ 23 | //Even Row 24 | cout<<1; 25 | while(col<=row-2){ 26 | cout<<0; 27 | col = col + 1; 28 | } 29 | cout<<1; 30 | } 31 | cout< 2 | using namespace std; 3 | 4 | 5 | 6 | int main(){ 7 | 8 | // Simple Interest 9 | //Declare a variable 10 | int p,r,t; 11 | float si; 12 | 13 | /* Assigns values to the buckets (storage in the memory) 14 | p = 10; 15 | r = 25; 16 | t = 1; 17 | */ 18 | cin>>p; 19 | cin>>r>>t; 20 | 21 | // 250/100 = 2.50 22 | // every statement should be terminated by a semi-colon 23 | 24 | si = p*r*t/100.0; 25 | 26 | // Typecasting -> Implicit 27 | //integer/integer = integer 28 | // float/integer = float 29 | // integer/float = float 30 | 31 | 32 | cout<< si < 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | int n; 8 | cin>>n; 9 | 10 | 11 | //Initialisation 12 | int sum = 0; 13 | 14 | //Stopping Criteria 15 | while(n>0){ 16 | int last_digit = n%10; 17 | sum = sum + last_digit; 18 | 19 | //Update Statement 20 | n = n/10; 21 | 22 | } 23 | 24 | //Print the ans 25 | cout<<"Sum of Digits is "< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | //Program to find sum of numbers from 1 to N 7 | 8 | int n; 9 | //Take Input 10 | cin>>n; 11 | 12 | //Init Condition 13 | int i = 1; 14 | int sum = 0; 15 | 16 | 17 | while(i<=n){ //Stopping Criteria 18 | sum = sum + i; 19 | 20 | i = i + 1; //Update Statement 21 | } 22 | //Output the Sum 23 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | //Data Type Modifiers + Data types 9 | // long, short, signed, unsigned 10 | 11 | int x; 12 | 13 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | 7 | //Primitive Datatypes 8 | //int , float, bool, double, char 9 | 10 | int x = 5000; 11 | float y = 10.141; 12 | bool weather_is_rainy = true; 13 | double pi = 3.141131212121; 14 | char ch = 'A'; 15 | 16 | // How much memory each is going to occupy! 17 | cout<< "Int " < 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | int cliff_end = 10; 8 | int x = 10; // start 9 | 10 | // do while is executed atleast once! 11 | // exit controlled doesnt check for the init condition is true or false! 12 | 13 | do{ 14 | x = x + 1; 15 | cout<<"Taking 1 step reaching :"<cliff_end){ 28 | cout<<"Falling from the cliff!"< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | int f = 0; 8 | int c; 9 | 10 | while(f<=300){ 11 | c = (5*(f-32))/9; 12 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | int cliff_end = 10; 8 | int x = 0; 9 | 10 | //for(start; stop; update){...} 11 | for( ; x 2 | using namespace std; 3 | 4 | //Conditional Statements in C++ 5 | 6 | int main(){ 7 | 8 | int n; 9 | cin>>n; 10 | 11 | //Program to check if a Number is divisble by 2, div by 3 and both 12 | // if else-if else 13 | 14 | // else-if block if you want only one of the blocks is executed 15 | /* 16 | if 17 | else if 18 | else if 19 | else if 20 | else 21 | 22 | */ 23 | 24 | if(n%2==0 and n%3==0){ 25 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | // Find the minimum and maximum of N Numbers 8 | int n; 9 | cin>>n; 10 | 11 | int min_so_far = INT_MAX; 12 | int max_so_far = INT_MIN; 13 | 14 | int no; 15 | // Without storing all the numbers, based upon the current number! 16 | for(int i=0;i>no; 18 | if(nomax_so_far){ 22 | max_so_far = no; 23 | } 24 | } 25 | 26 | cout<<"Max No "< 12 | using namespace std; 13 | 14 | 15 | int main(){ 16 | 17 | int n; 18 | cin>>n; 19 | //Loop for rows 1 to n 20 | for(int i=1;i<=n;i++){ 21 | 22 | //Spaces 23 | for(int space=1;space<=n-i;space++){ 24 | cout<<" "; 25 | } 26 | 27 | //Increasing Number 28 | int val = i; 29 | for(int cnt=1;cnt<=i;cnt++){ 30 | cout< 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | // Give a Number N 8 | // Square root of the number without using any predefined function 9 | int N; 10 | int P; 11 | cin >> N >> P; 12 | 13 | 14 | float ans = 0; 15 | float inc = 1.0; 16 | 17 | for (int times = 0; times <= P; times++) { 18 | 19 | // Finalise the correct digit for the current place 20 | while (ans * ans <= N) { 21 | ans = ans + inc; 22 | } 23 | ans = ans - inc ; 24 | // Updates the increment for the next position 25 | inc = inc / 10; 26 | } 27 | 28 | cout << ans << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /02 Programming Fundamentals-I/while_do_while.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | int cliff_end = 10; 8 | int x = 10; 9 | 10 | while (x < cliff_end) { 11 | x = x + 1; 12 | cout << "Taking 1 step reaching :" << x << endl; 13 | } 14 | 15 | cout << "Final X " << x << endl; 16 | 17 | if (x == cliff_end) { 18 | cout << "We are standing at cliff edge!" << endl; 19 | } 20 | 21 | else if (x > cliff_end) { 22 | cout << "Falling from the cliff!" << endl; 23 | } 24 | 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /03 Programming Fundamentals-II/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/03 Programming Fundamentals-II/.DS_Store -------------------------------------------------------------------------------- /03 Programming Fundamentals-II/bitwise_operators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | //Comma Operator 8 | int a, b, c; 9 | 10 | // Assignment Operator 11 | a = 10; 12 | b = 20; 13 | c = 30; 14 | 15 | //Logical Operator 16 | if (c > a and c > b) { 17 | cout << "C is largest" << endl; 18 | } 19 | 20 | 21 | // Ternary Operator 22 | int x = c % 2 == 0 ? 1 : 0; 23 | cout << x << endl; 24 | c % 2 == 0 ? cout << "Even" : cout << "Odd"; 25 | cout << endl; 26 | 27 | // Bitwise Operator 28 | x = 5; 29 | int y = 7; 30 | cout << "AND " << (x & y) << endl; 31 | cout << " OR " << (x | y) << endl; 32 | cout << "XOR " << (x ^ y) << endl; 33 | 34 | // Shift Operator 35 | x = x << 2; 36 | 37 | cout << x << endl; // 20 38 | cout << (y >> 1) << endl; //3 39 | // Unary Operator 40 | 41 | // Address Of 42 | cout << (&x) << endl; 43 | 44 | //Post Increment / Decrement Operator 45 | a = 10; 46 | int z = a++; //z = 10, a = 11 47 | cout << z << endl; // 10 48 | z = ++a; // a = 12, z = 12 49 | cout << z << endl; // 12 50 | 51 | // Compound Assingment Operator 52 | a = 10; 53 | a *= 3; 54 | cout << "A after multiply " << a << endl; // 30 55 | a %= 5; 56 | cout << "A after modulo " << a << endl; //0 57 | 58 | b = 5; 59 | b <<= 1 ; 60 | cout << "B after left shift" << b << endl; //10 61 | 62 | 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /03 Programming Fundamentals-II/break.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | int main(){ 7 | 8 | //Break - that is used to stop a loop based upon certain condition 9 | 10 | 11 | //Problem Statement - Read a stream of numbers until you get a mutiple of 7 12 | int no; 13 | 14 | while(true){ 15 | //No Stopping Condtion as of now 16 | cin>>no; 17 | if(no%7==0){ 18 | //Stop Processing More Numbers; 19 | //break // Take you line 21 20 | continue; //Take you out to line number 14, skip line 21 21 | //starting of the loop! 22 | } 23 | cout<<"No "< 2 | using namespace std; 3 | 4 | 5 | 6 | int main(){ 7 | 8 | //Break - that is used to stop a loop based upon certain condition 9 | 10 | 11 | //Problem Statement - Read a stream of numbers until you get a mutiple of 7 12 | int no; 13 | // Accept and Print Numbers until you get a number which is multiple of 7 14 | // and you will skip all numbers which are multiple of 13 and 7 15 | 16 | while(true){ 17 | //No Stopping Condtion as of now 18 | cin>>no; 19 | 20 | if(no%13==0){ 21 | continue; //multiple times 22 | } 23 | 24 | if(no%7==0){ 25 | break; // only once 26 | } 27 | cout<<"No "< 5 | using namespace std; 6 | 7 | 8 | int main() { 9 | 10 | int digits = 0; 11 | int alphabets = 0; 12 | int spaces = 0; 13 | int other = 0; 14 | 15 | char ch; 16 | ch = cin.get(); 17 | 18 | while (ch != '$') { 19 | 20 | if (ch >= '0' and ch <= '9') { 21 | digits++; 22 | } 23 | else if ( (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')) { 24 | alphabets++; 25 | } 26 | else if (ch == ' ' or ch == '\n' or ch == '\t') { 27 | spaces++; 28 | } 29 | else { 30 | other++; 31 | } 32 | ch = cin.get(); 33 | } 34 | 35 | cout << "Digits " << digits << endl; 36 | cout << "alphabets " << alphabets << endl; 37 | cout << "spaces " << spaces << endl; 38 | cout << "Others " << other << endl; 39 | 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /03 Programming Fundamentals-II/cinget.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | int main() { 7 | 8 | // Problem - Read a list of characters (sentence) 9 | // cin.get() 10 | 11 | char ch; 12 | //cin>>ch; 13 | //reads the first char 14 | ch = cin.get(); //this method reads any single char including spaces/newlines from the input buffer 15 | 16 | while (ch != '.') { 17 | //Print the last character that we have read 18 | cout << ch; 19 | // Update my char in the memory (read the next character) 20 | //reads the rest of the characters 21 | ch = cin.get(); 22 | } 23 | 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /03 Programming Fundamentals-II/displacement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | // Problem - Find the Displacement 8 | 9 | char ch; 10 | ch = cin.get(); 11 | 12 | int x = 0; 13 | int y = 0; 14 | 15 | while(ch!='\n'){ 16 | 17 | if(ch=='N' or ch=='n'){ 18 | y++; 19 | } 20 | else if(ch=='S' or ch=='s'){ 21 | y--; 22 | } 23 | else if(ch=='E' or ch=='e'){ 24 | x++; 25 | } 26 | else{ 27 | x--; 28 | } 29 | ch = cin.get(); 30 | } 31 | cout<<"Final Displacement is "< 18 | using namespace std; 19 | 20 | 21 | int main() { 22 | 23 | int n; 24 | cin >> n; 25 | 26 | for (int i = 1; i <= n; i++) { 27 | int val = i % 2 == 0 ? 1 : 0; 28 | 29 | //Print i values in ith line 30 | for (int cnt = 1; cnt <= i; cnt++) { 31 | cout << val; 32 | val = 1 - val; // val = 1, val = 0, val = 1, .... 33 | } 34 | cout << endl; 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /03 Programming Fundamentals-II/prime_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | //Check if a given number is Prime 8 | int n; 9 | cin >> n; 10 | 11 | int i; 12 | 13 | for (i = 2; i <= n - 1; i++) { 14 | if (n % i == 0) { 15 | cout << n << "is not Prime" << endl; 16 | break; 17 | } 18 | } 19 | //Remember this check is important 20 | if (i == n) { 21 | cout << n << " is prime!" << endl; 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /03 Programming Fundamentals-II/switch_case.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | //Switch Case 7 | /* Design a Menu - where pressing a button gives you the corresponding item 8 | 9 | B - Burger 10 | M - Maggi 11 | P - Pizza 12 | C - Coke 13 | D - Dosa 14 | . 15 | . 16 | and so on! 17 | */ 18 | char ch; 19 | cin >> ch; 20 | int pizza_orders = 0; 21 | 22 | switch (ch) { 23 | case 'B' : cout << "Burger" << endl; 24 | break; 25 | 26 | case 'm' : 27 | case 'M' : cout << "Maggi" << endl; 28 | break; 29 | 30 | case 'p' : 31 | case 'P' : cout << "Pizza" << endl; 32 | pizza_orders++; 33 | 34 | break; 35 | 36 | default : cout << "Item not available!" << endl; 37 | } 38 | 39 | 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /03 Programming Fundamentals-II/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/03 Programming Fundamentals-II/test.cpp -------------------------------------------------------------------------------- /03 Programming Fundamentals-II/unique_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | //Problem - Unique Number 2N + 1 8 | // Given a list of numbers where every numbers occurs twice except one, find that unique number 9 | int n; 10 | cin >> n; 11 | 12 | int no; 13 | int ans = 0; 14 | //Bitwise XOR Operator to solve, 15 | // Helped to not any storage 16 | 17 | for (int i = 0; i < n; i++) { 18 | cin >> no; 19 | ans = ans ^ no; 20 | } 21 | cout << ans << endl; 22 | 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /03 Programming Fundamentals-II/untitled.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | int main(){ 7 | 8 | //Break - that is used to stop a loop based upon certain condition 9 | 10 | 11 | //Problem Statement - Read a stream of numbers until you get a mutiple of 7 12 | int no; 13 | 14 | while(true){ 15 | //No Stopping Condtion as of now 16 | cin>>no; 17 | if(no%7==0){ 18 | //Stop Processing More Numbers; 19 | //break // Take you line 21 20 | continue; //Take you out to line number 14, skip line 21 21 | //starting of the loop! 22 | } 23 | cout<<"No "< 2 | using namespace std; 3 | 4 | 5 | // Scope -> defined for variables(acessible -> lifetime and visibility) 6 | // Local Scope 7 | // Global Scope 8 | 9 | //Global Scope 10 | int x = 100; 11 | 12 | int main(){ 13 | 14 | 15 | int x = 10; 16 | 17 | cout< 2 | using namespace std; 3 | 4 | int findZeroes(int n) { 5 | int ans = 0; 6 | for (int D = 5; n / D >= 1; D = D * 5) { 7 | ans += n / D; 8 | } 9 | return ans; 10 | } 11 | 12 | int main() { 13 | 14 | long long int n; 15 | cin >> n; 16 | 17 | cout << findZeroes(n) << endl; 18 | 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /10DaysOfCode/div_subarrays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/10DaysOfCode/div_subarrays -------------------------------------------------------------------------------- /10DaysOfCode/div_subarrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | #define ll long 6 | 7 | ll a[1000005], pre[1000005]; 8 | 9 | int main() { 10 | 11 | int t; 12 | cin >> t; 13 | while (t--) { 14 | int n; 15 | cin >> n; 16 | memset(pre, 0, sizeof(pre)); 17 | 18 | pre[0] = 1; 19 | //read the input 20 | int sum = 0; 21 | for (int i = 0; i < n; i++) { 22 | cin >> a[i]; 23 | sum += a[i]; 24 | sum %= n; 25 | sum = (sum + n) % n; 26 | pre[sum]++; 27 | } 28 | ll ans = 0; 29 | for (int i = 0; i < n; i++) { 30 | int m = pre[i]; 31 | ans += (m) * (m - 1) / 2; 32 | } di 33 | cout << ans << endl; 34 | } 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /10DaysOfCode/ganesha: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/10DaysOfCode/ganesha -------------------------------------------------------------------------------- /10DaysOfCode/ganesha.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | 7 | int main() { 8 | 9 | int n; 10 | cin >> n; 11 | 12 | //print pattern 13 | //First Component 14 | cout << "*"; 15 | for (int i = 1; i <= (n - 3) / 2; i++) { 16 | cout << " "; 17 | } 18 | for (int i = 1; i <= (n + 1) / 2; i++) { 19 | cout << "*"; 20 | } 21 | cout << endl; 22 | 23 | //Second Component 24 | for (int rows = 1; rows <= (n - 3) / 2; rows++) { 25 | cout << "*"; 26 | //spaces 27 | for (int i = 1; i <= (n - 3) / 2; i++) { 28 | cout << " "; 29 | } 30 | cout << "*" << endl; 31 | } 32 | 33 | // Third Component 34 | for (int i = 1; i <= n; i++) { 35 | cout << "*"; 36 | } 37 | cout << endl; 38 | 39 | //Fourth Component 40 | for (int rows = 1; rows <= (n - 3) / 2; rows++) { 41 | 42 | //spaces 43 | for (int i = 1; i <= (n - 3) / 2 + 1; i++) { 44 | cout << " "; 45 | } 46 | 47 | 48 | //star 49 | cout << "*"; 50 | 51 | //spaces 52 | for (int i = 1; i <= (n - 3) / 2; i++) { 53 | cout << " "; 54 | } 55 | 56 | //star 57 | cout << "*" << endl; 58 | 59 | } 60 | 61 | //fifth component 62 | //stars 63 | for (int i = 1; i <= (n + 1) / 2; i++) { 64 | cout << "*"; 65 | } 66 | 67 | //spaces 68 | for (int i = 1; i <= (n - 3) / 2; i++) { 69 | cout << " "; 70 | } 71 | 72 | 73 | //single star 74 | cout << "*" << endl; 75 | 76 | 77 | 78 | 79 | return 0; 80 | 81 | 82 | } -------------------------------------------------------------------------------- /10DaysOfCode/magical_park: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/10DaysOfCode/magical_park -------------------------------------------------------------------------------- /10DaysOfCode/magical_park.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void magical_park(char a[][100], int m, int n, int k, int s) { 5 | 6 | 7 | //piyush can get out of the mark 8 | bool success = true; 9 | 10 | for (int i = 0; i < m; i++) { 11 | for (int j = 0; j < n; j++) { 12 | char ch = a[i][j]; 13 | 14 | //check 15 | if (s < k) { 16 | success = false; 17 | break; 18 | } 19 | if (ch == '*') { 20 | s += 5; 21 | } 22 | else if (ch == '.') { 23 | s -= 2; 24 | } 25 | else { 26 | break; 27 | } 28 | //we also loose 1 point when we move right except for the last column 29 | if (j != n - 1) { 30 | s--; 31 | } 32 | } 33 | } 34 | if (success) { 35 | cout << "Yes" << endl; 36 | cout << s << endl; 37 | } 38 | else { 39 | cout << "No" << endl; 40 | } 41 | } 42 | 43 | 44 | int main() { 45 | 46 | int m, n, k, s; 47 | cin >> m >> n >> k >> s; 48 | 49 | 50 | char park[100][100]; 51 | //Take Input 52 | for (int i = 0; i < m; i++) { 53 | for (int j = 0; j < n; j++) { 54 | cin >> park[i][j]; 55 | } 56 | } 57 | magical_park(park, m, n, k, s); 58 | 59 | return 0; 60 | } -------------------------------------------------------------------------------- /10DaysOfCode/park.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | 8 | 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /10DaysOfCode/prime_numbers_sieve: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/10DaysOfCode/prime_numbers_sieve -------------------------------------------------------------------------------- /10DaysOfCode/prime_numbers_sieve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | //Naive Approach O(sqrtN) 7 | bool isPrime(int n) { 8 | if (n == 1) { 9 | return false; 10 | } 11 | if (n == 2) { 12 | return true; 13 | } 14 | for (int i = 2; i * i < n; i++) { 15 | if (n % i == 0) 16 | return false; 17 | } 18 | return true; 19 | } 20 | 21 | //Sieve Approach - Generate a array containing prime Numbers 22 | void prime_sieve(int *p) { 23 | 24 | //first mark all odd number's prime 25 | for (int i = 3; i <= 1000000; i += 2) { 26 | p[i] = 1; 27 | } 28 | 29 | // Sieve 30 | for (ll i = 3; i <= 1000000; i += 2) { 31 | //if the current number is not marked (it is prime) 32 | if (p[i] == 1) { 33 | //mark all the multiples of i as not prime 34 | for (ll j = i * i; j <= 1000000; j = j + i ) { 35 | p[j] = 0; 36 | } 37 | } 38 | 39 | } 40 | //special case 41 | p[2] = 1; 42 | p[1] = p[0] = 0; 43 | 44 | } 45 | 46 | 47 | 48 | int main() { 49 | 50 | int n; 51 | cin >> n; 52 | 53 | int p[1000005] = {0}; 54 | prime_sieve(p); 55 | 56 | //lets print primes upto range n 57 | for (int i = 0; i <= n; i++) { 58 | if (p[i] == 1) { 59 | cout << i << " "; 60 | } 61 | } 62 | 63 | 64 | 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /10DaysOfCode/prime_visits: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/10DaysOfCode/prime_visits -------------------------------------------------------------------------------- /10DaysOfCode/prime_visits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | 7 | //Sieve Approach - Generate a array containing prime Numbers 8 | void prime_sieve(int *p) { 9 | 10 | //first mark all odd number's prime 11 | for (int i = 3; i <= 1000000; i += 2) { 12 | p[i] = 1; 13 | } 14 | 15 | // Sieve 16 | for (ll i = 3; i <= 1000000; i += 2) { 17 | //if the current number is not marked (it is prime) 18 | if (p[i] == 1) { 19 | //mark all the multiples of i as not prime 20 | for (ll j = i * i; j <= 1000000; j = j + i ) { 21 | p[j] = 0; 22 | } 23 | } 24 | 25 | } 26 | //special case 27 | p[2] = 1; 28 | p[1] = p[0] = 0; 29 | 30 | } 31 | 32 | 33 | 34 | int main() { 35 | 36 | int p[1000005] = {0}; 37 | prime_sieve(p); 38 | 39 | 40 | int csum[1000005] = {0}; 41 | 42 | //precompute the primes upto an index i 43 | 44 | for (int i = 1; i <= 1000000; i++) { 45 | csum[i] = csum[i - 1] + p[i]; 46 | } 47 | int q; 48 | cin >> q; 49 | while (q--) { 50 | int a, b; 51 | cin >> a >> b; 52 | cout << csum[b] - csum[a - 1] << endl; 53 | 54 | } 55 | 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /10DaysOfCode/rotate_image: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/10DaysOfCode/rotate_image -------------------------------------------------------------------------------- /10DaysOfCode/rotate_image.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | void display(int a[][1000], int n) { 7 | for (int i = 0; i < n; i++) { 8 | for (int j = 0; j < n; j++) { 9 | cout << a[i][j] << " "; 10 | } 11 | cout << endl; 12 | } 13 | } 14 | 15 | 16 | void rotate(int a[][1000], int n) { 17 | 18 | //reverse each row 19 | for (int row = 0; row < n; row++) { 20 | int start_col = 0; 21 | int end_col = n - 1; 22 | while (start_col < end_col) { 23 | swap(a[row][start_col], a[row][end_col]); 24 | start_col++; 25 | end_col--; 26 | } 27 | } 28 | 29 | //to take transpose 30 | for (int i = 0; i < n; i++) { 31 | for (int j = 0; j < n; j++) { 32 | if (i < j) { 33 | swap(a[i][j], a[j][i]); 34 | } 35 | 36 | } 37 | } 38 | } 39 | 40 | void rotate_stl(int a[][1000], int n) { 41 | 42 | // Same Thing using STL reverse(start_container,end_container) methods 43 | for (int i = 0; i < n; i++) { 44 | reverse(a[i], a[i] + n); 45 | } 46 | 47 | //to take transpose 48 | for (int i = 0; i < n; i++) { 49 | for (int j = 0; j < n; j++) { 50 | if (i < j) { 51 | swap(a[i][j], a[j][i]); 52 | } 53 | 54 | } 55 | } 56 | } 57 | 58 | 59 | 60 | int main() { 61 | 62 | int a[1000][1000]; 63 | int n; 64 | cin >> n; 65 | 66 | for (int i = 0; i < n; i++) { 67 | for (int j = 0; j < n; j++) { 68 | cin >> a[i][j]; 69 | } 70 | } 71 | rotate_stl(a, n); 72 | display(a, n); 73 | 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /Arrays/2_d_array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/2_d_array -------------------------------------------------------------------------------- /Arrays/2_d_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | int a[1000][1000] = {0}; 8 | int m, n; 9 | cin >> m >> n; 10 | 11 | 12 | //Iterate Over the array 13 | int val = 1; 14 | 15 | for (int row = 0; row <= m - 1; row++) { 16 | for (int col = 0; col <= n - 1; col++) { 17 | a[row][col] = val; 18 | val = val + 1; 19 | cout << a[row][col] << " "; 20 | } 21 | cout << endl; 22 | } 23 | 24 | 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Arrays/2d_character_array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/2d_character_array -------------------------------------------------------------------------------- /Arrays/2d_character_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | char c[10][10]; 8 | c[0][0] = 'A'; 9 | 10 | char b[][10] = {{'a', 'b', '\0'}, {'d', 'e', 'f', '\0'}}; 11 | char a[][10] = {"ab", "def", "ghi", "hello"}; 12 | cout << a[2] << endl; 13 | cout << a[3] << endl; 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Arrays/array_basic: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/array_basic -------------------------------------------------------------------------------- /Arrays/array_basic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | //Init of an array 8 | int a[10] = {0}; 9 | 10 | //Size of 11 | cout << sizeof(a) << endl; 12 | int n = sizeof(a) / sizeof(int); 13 | cout << n << endl; 14 | 15 | //Input all the elements from the user 16 | for (int i = 0; i < 5; i++) { 17 | cin >> a[i]; 18 | } 19 | 20 | //Update a single index 21 | a[8] = 100; 22 | 23 | //Print all the first 10 elements 24 | for (int i = 0; i < n; i++) { 25 | cout << a[i] << " ,"; 26 | } 27 | 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Arrays/binary_search: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/binary_search -------------------------------------------------------------------------------- /Arrays/binary_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Binary Search 5 | //Efficient Way to Search in Sorted Arrays 6 | 7 | int binary_search(int a[], int n, int key) { 8 | 9 | int s = 0; 10 | int e = n - 1; 11 | 12 | while (s <= e) { 13 | int mid = (s + e) / 2; 14 | 15 | if (a[mid] == key) { 16 | return mid; 17 | } 18 | else if (a[mid] > key) { 19 | e = mid - 1; 20 | } 21 | else { 22 | s = mid + 1; 23 | } 24 | 25 | } 26 | return -1; 27 | 28 | } 29 | 30 | int main() { 31 | 32 | 33 | int n, key; 34 | cin >> n; 35 | 36 | int a[1000]; 37 | 38 | for (int i = 0; i < n; i++) { 39 | cin >> a[i]; 40 | } 41 | 42 | //Ask for the element we want to search 43 | cout << "Enter the element you want to search : "; 44 | cin >> key; 45 | 46 | cout << binary_search(a, n, key) << endl; 47 | 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Arrays/bubble_sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/bubble_sort -------------------------------------------------------------------------------- /Arrays/bubble_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | // Bubble Sort 6 | void bubble_sort(int a[], int n) { 7 | 8 | //N-1 large elements to the end 9 | for (int itr = 1; itr <= n - 1; itr++) { 10 | 11 | //Pairwise Swapping in the unsorted of the array 12 | for (int j = 0; j <= (n - itr - 1); j++) { 13 | if (a[j] > a[j + 1]) { 14 | swap(a[j], a[j + 1]); 15 | } 16 | } 17 | 18 | } 19 | } 20 | 21 | int main() { 22 | 23 | 24 | int n, key; 25 | cin >> n; 26 | 27 | int a[1000]; 28 | 29 | for (int i = 0; i < n; i++) { 30 | cin >> a[i]; 31 | } 32 | 33 | 34 | bubble_sort(a, n); 35 | for (int i = 0; i < n; i++) { 36 | cout << a[i] << ","; 37 | } 38 | 39 | 40 | return 0; 41 | 42 | } -------------------------------------------------------------------------------- /Arrays/bubble_sort_with_comparator: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/bubble_sort_with_comparator -------------------------------------------------------------------------------- /Arrays/bubble_sort_with_comparator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool compare(int a, int b) { 5 | cout << "Comparing " << a << " and " << b << endl; 6 | return a < b; 7 | } 8 | 9 | // Bubble Sort 10 | void bubble_sort(int a[], int n, bool (&cmp)(int a, int b)) { 11 | 12 | //N-1 large elements to the end 13 | for (int itr = 1; itr <= n - 1; itr++) { 14 | 15 | //Pairwise Swapping in the unsorted of the array 16 | for (int j = 0; j <= (n - itr - 1); j++) { 17 | if (cmp(a[j], a[j + 1])) { 18 | swap(a[j], a[j + 1]); 19 | } 20 | } 21 | 22 | } 23 | } 24 | 25 | int main() { 26 | 27 | 28 | int n, key; 29 | cin >> n; 30 | 31 | int a[1000]; 32 | 33 | for (int i = 0; i < n; i++) { 34 | cin >> a[i]; 35 | } 36 | 37 | 38 | bubble_sort(a, n, compare); 39 | for (int i = 0; i < n; i++) { 40 | cout << a[i] << ","; 41 | } 42 | 43 | 44 | return 0; 45 | 46 | } -------------------------------------------------------------------------------- /Arrays/character_arrays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/character_arrays -------------------------------------------------------------------------------- /Arrays/character_arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | 7 | int main() { 8 | 9 | int b[] = {1, 2, 3}; 10 | cout << b << endl; 11 | 12 | char a[] = {'a', 'b', 'c', 'd', 'e', '\0'}; 13 | cout << a << endl; 14 | 15 | //Input 16 | char s1[] = {'h', 'e', 'l', 'l', 'o'}; //doesnt terminate with null 17 | char s2[] = "hello"; //another way 18 | 19 | cout << s1 << " " << sizeof(s1) << endl; //5 bytes 20 | cout << s2 << " " << sizeof(s2) << endl; //6 bytes 21 | 22 | char s3[10] = "hello"; 23 | char s4[10]; 24 | cin >> s4; 25 | cout << s4 << endl; 26 | 27 | 28 | 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Arrays/help_rahul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/help_rahul -------------------------------------------------------------------------------- /Arrays/help_rahul.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int find_key(int a[], int n, int key) { 5 | int s = 0; 6 | int e = n - 1; 7 | while (s <= e) { 8 | int mid = (s + e) / 2; 9 | if (a[mid] == key) { 10 | return mid; 11 | } 12 | else if (a[s] <= a[mid]) { 13 | //2 cases -> element lies on the left or the right of the mid 14 | if (key >= a[s] and key <= a[mid]) { 15 | e = mid - 1; 16 | } 17 | else { 18 | s = mid + 1; 19 | } 20 | 21 | } 22 | else { 23 | if (key >= a[mid] and key <= a[e]) { 24 | s = mid + 1; 25 | } 26 | else { 27 | e = mid - 1; 28 | } 29 | } 30 | 31 | } 32 | return -1; 33 | 34 | } 35 | 36 | 37 | 38 | int main() { 39 | 40 | int n; 41 | cin >> n; 42 | int a[1000000]; 43 | for (int i = 0; i < n; i++) { 44 | cin >> a[i]; 45 | } 46 | int key; 47 | cin >> key; 48 | cout << find_key(a, n, key) << endl; 49 | 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Arrays/inbuilt_sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/inbuilt_sort -------------------------------------------------------------------------------- /Arrays/inbuilt_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | //Define A Comparator Function 6 | bool compare(int a, int b) { 7 | cout << "Comparing " << a << " and " << b << endl; 8 | return a > b; 9 | } 10 | 11 | 12 | int main() { 13 | 14 | int n, key; 15 | cin >> n; 16 | 17 | int a[1000]; 18 | 19 | for (int i = 0; i < n; i++) { 20 | cin >> a[i]; 21 | } 22 | //Sort an array using sort() function, more efficient 23 | sort(a, a + n, compare); 24 | 25 | for (int i = 0; i < n; i++) { 26 | cout << a[i] << ","; 27 | } 28 | 29 | 30 | 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Arrays/insertion_sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/insertion_sort -------------------------------------------------------------------------------- /Arrays/insertion_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Insertion Sort 5 | void insertion_sort(int arr[], int n) { 6 | 7 | for (int i = 1; i <= n - 1; i++) { 8 | 9 | int e = arr[i]; 10 | //place the current element at 'right' position in the sorted part 11 | int j = i - 1; 12 | while (j >= 0 and arr[j] > e) { 13 | arr[j + 1] = arr[j]; 14 | j = j - 1; 15 | } 16 | arr[j + 1] = e; 17 | } 18 | 19 | 20 | } 21 | 22 | int main() { 23 | 24 | 25 | int n, key; 26 | cin >> n; 27 | 28 | int a[1000]; 29 | 30 | for (int i = 0; i < n; i++) { 31 | cin >> a[i]; 32 | } 33 | 34 | 35 | insertion_sort(a, n); 36 | for (int i = 0; i < n; i++) { 37 | cout << a[i] << ","; 38 | } 39 | 40 | 41 | return 0; 42 | 43 | } -------------------------------------------------------------------------------- /Arrays/largest_smallest: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/largest_smallest -------------------------------------------------------------------------------- /Arrays/largest_smallest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | // Find the Largest and Smallest Number in array 5 | 6 | int main() { 7 | 8 | 9 | int n, key; 10 | cin >> n; 11 | 12 | int a[1000]; 13 | 14 | for (int i = 0; i < n; i++) { 15 | cin >> a[i]; 16 | } 17 | //Algorithm to find the largest and smallest number 18 | 19 | int largest = INT_MIN; 20 | int smallest = INT_MAX; 21 | 22 | for (int i = 0; i < n; i++) { 23 | largest = max(largest, a[i]); 24 | smallest = min(smallest, a[i]); 25 | 26 | } 27 | 28 | cout << "Largest " << largest << endl; 29 | cout << "Smallest " << smallest << endl; 30 | 31 | 32 | return 0; 33 | 34 | } -------------------------------------------------------------------------------- /Arrays/largest_string: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/largest_string -------------------------------------------------------------------------------- /Arrays/largest_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | /*Question - Read N, followed by N strings and print the largest string 5 | and its length 6 | */ 7 | 8 | int main() { 9 | 10 | 11 | int n; 12 | cin >> n; 13 | 14 | char a[1000]; 15 | char largest[1000]; 16 | 17 | int len = 0; 18 | int largest_len = 0; 19 | 20 | cin.get(); 21 | 22 | for (int i = 0; i < n; i++) { 23 | cin.getline(a, 1000); 24 | //cout << a << endl; 25 | len = strlen(a); 26 | if (len > largest_len) { 27 | largest_len = len; 28 | strcpy(largest, a); 29 | } 30 | } 31 | 32 | cout << largest << " and " << largest_len << endl; 33 | 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Arrays/length_of_string: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/length_of_string -------------------------------------------------------------------------------- /Arrays/length_of_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | //read a sentence/paragraph and find its length 5 | 6 | int findLen(char a[]) { 7 | int i = 0; 8 | while (a[i] != '\0') { 9 | i++; 10 | } 11 | return i; 12 | } 13 | 14 | int main() { 15 | 16 | char a[1000]; 17 | cin.getline(a, 1000); 18 | 19 | cout << findLen(a) << endl; 20 | cout << strlen(a) << endl; 21 | 22 | 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Arrays/linear_search: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/linear_search -------------------------------------------------------------------------------- /Arrays/linear_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | // Linear Search 6 | 7 | // A particular element in the array 8 | 9 | int main() { 10 | 11 | 12 | int n, key; 13 | cin >> n; 14 | 15 | int a[1000]; 16 | 17 | for (int i = 0; i < n; i++) { 18 | cin >> a[i]; 19 | } 20 | 21 | //Ask for the element we want to search 22 | cout << "Enter the element you want to search : "; 23 | cin >> key; 24 | 25 | //Find out the index of that element by traversing the array 26 | //Linear Search Algorithm 27 | int i; 28 | for ( i = 0; i <= n - 1; i++) { 29 | if (a[i] == key) { 30 | cout << key << " found at " << i << " index"; 31 | break; 32 | } 33 | } 34 | if (i == n) { 35 | cout << key << "is not present " << endl; 36 | } 37 | 38 | return 0; 39 | 40 | } -------------------------------------------------------------------------------- /Arrays/pair_sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/pair_sum -------------------------------------------------------------------------------- /Arrays/pair_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Given a Sorted Array, Find Pair of Elements that sum to K (Given) 5 | 6 | 7 | 8 | int main() { 9 | 10 | 11 | 12 | 13 | int a[] = {1, 3, 5, 7, 10, 11, 12, 13}; 14 | int s = 16; 15 | 16 | int i = 0; 17 | int j = sizeof(a) / sizeof(int) - 1; 18 | 19 | while (i < j) { 20 | int current_sum = a[i] + a[j]; 21 | if (current_sum > s) { 22 | j--; 23 | } 24 | else if (current_sum < s) { 25 | i++; 26 | 27 | } 28 | else if (current_sum == s) { 29 | cout << a[i] << " and " << a[j] << endl; 30 | i++; 31 | j--; 32 | } 33 | } 34 | 35 | 36 | return 0; 37 | 38 | } -------------------------------------------------------------------------------- /Arrays/palindrome: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/palindrome -------------------------------------------------------------------------------- /Arrays/palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | //read a sentence/paragraph and check if its palindrome or not 5 | 6 | bool isPalindrome(char a[]) { 7 | 8 | int i = 0; 9 | int j = strlen(a) - 1; 10 | 11 | while (i < j) { 12 | if (a[i] == a[j]) { 13 | i++; 14 | j--; 15 | } 16 | else { 17 | return false; 18 | } 19 | } 20 | return true; 21 | } 22 | 23 | int main() { 24 | 25 | char a[1000]; 26 | cin.getline(a, 1000); 27 | 28 | if (isPalindrome(a)) { 29 | cout << "Palindromic String"; 30 | } 31 | else { 32 | cout << "Not a Palindrome"; 33 | } 34 | 35 | 36 | 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Arrays/read_a_list_of_strings: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/read_a_list_of_strings -------------------------------------------------------------------------------- /Arrays/read_a_list_of_strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | char a[100][1000]; 8 | 9 | //Read a list of strings and we will store them in a 2d character array 10 | int n; 11 | cin >> n; 12 | 13 | cin.get(); 14 | for (int i = 0; i < n; i++) { 15 | cin.getline(a[i], 1000); 16 | } 17 | 18 | //Print out all the strings 19 | for (int i = 0; i < n; i++) { 20 | cout << a[i] << endl; 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Arrays/read_paragraph: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/read_paragraph -------------------------------------------------------------------------------- /Arrays/read_paragraph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //read a sentence/paragraph and store it 4 | //cin.get() reads a single character 5 | //put a loop to read characters till you get a new line '\n' 6 | 7 | void readline(char a[], int maxLen, char delim = '\n') { 8 | 9 | int i = 0; 10 | char ch = cin.get(); 11 | while (ch != delim) { 12 | a[i] = ch; 13 | i++; 14 | if (i == (maxLen - 1)) { 15 | break; 16 | } 17 | ch = cin.get(); 18 | } 19 | //once out of the loop 20 | a[i] = '\0'; 21 | return; 22 | 23 | } 24 | 25 | int main() { 26 | 27 | char a[1000]; 28 | //readline(a, 1000, '$'); 29 | cin.getline(a, 1000, '$'); 30 | cout << a << endl; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Arrays/remove_duplicates: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/remove_duplicates -------------------------------------------------------------------------------- /Arrays/remove_duplicates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //remove consecutive duplicate characters from a string 4 | //ccoooding -- > coding 5 | 6 | 7 | void removeDuplicates(char a[]) { 8 | 9 | int l = strlen(a); 10 | if (l == 1 or l == 0) { 11 | return; 12 | } 13 | 14 | int prev = 0; 15 | for (int current = 1; current < l; current++) { 16 | if (a[current] != a[prev]) { 17 | prev++; 18 | a[prev] = a[current]; 19 | } 20 | } 21 | a[prev + 1] = '\0'; 22 | return; 23 | } 24 | 25 | int main() { 26 | 27 | char a[1000]; 28 | cin.getline(a, 1000); 29 | removeDuplicates(a); 30 | cout << a << endl; 31 | 32 | 33 | 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Arrays/selection_sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/selection_sort -------------------------------------------------------------------------------- /Arrays/selection_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | // Selection Sort 6 | void selection_sort(int a[], int n) { 7 | 8 | for (int i = 0; i < n - 1; i++) { 9 | //find out the smallest element idx in the unsorted part 10 | int min_index = i; 11 | for (int j = i; j <= n - 1; j++) { 12 | if (a[j] < a[min_index]) { 13 | min_index = j; 14 | } 15 | } 16 | //After this loop we can do swap finally 17 | swap(a[i], a[min_index]); 18 | } 19 | } 20 | 21 | int main() { 22 | 23 | 24 | int n, key; 25 | cin >> n; 26 | 27 | int a[1000]; 28 | 29 | for (int i = 0; i < n; i++) { 30 | cin >> a[i]; 31 | } 32 | selection_sort(a, n); 33 | for (int i = 0; i < n; i++) { 34 | cout << a[i] << ","; 35 | } 36 | 37 | 38 | return 0; 39 | 40 | } -------------------------------------------------------------------------------- /Arrays/sort_the_strings_challenge: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/sort_the_strings_challenge -------------------------------------------------------------------------------- /Arrays/sort_the_strings_challenge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | string extractStringAtKey(string str, int key) { 7 | //strtok 8 | char *s = strtok((char *)str.c_str(), " "); 9 | while (key > 1) { 10 | s = strtok(NULL, " "); 11 | key--; 12 | } 13 | return (string)s; 14 | } 15 | int convertToInt(string s) { 16 | int ans = 0; 17 | int p = 1; 18 | for (int i = s.length() - 1; i >= 0; i--) { 19 | ans += ((s[i] - '0') * p); 20 | p = p * 10; 21 | } 22 | return ans; 23 | 24 | } 25 | 26 | bool numericCompare(pair s1, pair s2) { 27 | string key1, key2; 28 | key1 = s1.second; 29 | key2 = s2.second; 30 | 31 | return convertToInt(key1) < convertToInt(key2); 32 | 33 | } 34 | bool lexicoCompare(pair s1, pair s2) { 35 | string key1, key2; 36 | key1 = s1.second; 37 | key2 = s2.second; 38 | 39 | return key1 < key2; 40 | 41 | } 42 | 43 | int main() { 44 | 45 | 46 | 47 | int n; 48 | cin >> n; 49 | cin.get(); 50 | 51 | string a[100]; 52 | for (int i = 0; i < n; i++) { 53 | getline(cin, a[i]); 54 | } 55 | 56 | int key; 57 | string reversal, ordering; 58 | cin >> key >> reversal >> ordering; 59 | 60 | pair strPair[100]; 61 | 62 | 63 | 64 | for (int i = 0; i < n; i++) { 65 | 66 | strPair[i].first = a[i]; 67 | strPair[i].second = extractStringAtKey(a[i], key); 68 | } 69 | 70 | //Next = sorting 71 | 72 | if (ordering == "numeric") { 73 | sort(strPair, strPair + n, numericCompare); 74 | } 75 | else { 76 | sort(strPair, strPair + n, lexicoCompare); 77 | } 78 | 79 | //Reversal 80 | if (reversal == "true") { 81 | for (int i = 0; i < n / 2; i++) { 82 | swap(strPair[i], strPair[n - i - 1]); 83 | } 84 | } 85 | 86 | //Print the output 87 | for (int i = 0; i < n; i++) { 88 | cout << strPair[i].first << endl; 89 | } 90 | 91 | return 0; 92 | 93 | } -------------------------------------------------------------------------------- /Arrays/spiral_print: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/spiral_print -------------------------------------------------------------------------------- /Arrays/spiral_print.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void spiralprint(int a[][1000], int m, int n) { 5 | 6 | int startRow = 0; 7 | int startCol = 0; 8 | int endRow = m - 1; 9 | int endCol = n - 1; 10 | 11 | //Print it 12 | while (startRow <= endRow and startCol <= endCol) { 13 | //First Row 14 | 15 | for (int i = startCol; i <= endCol; i++) { 16 | cout << a[startRow][i] << " "; 17 | } 18 | startRow++; 19 | 20 | for (int i = startRow; i <= endRow; i++) { 21 | cout << a[i][endCol] << " "; 22 | } 23 | endCol--; 24 | 25 | //Bottom Row 26 | if (endRow > startRow) { 27 | for (int i = endCol; i >= startCol; i--) { 28 | cout << a[endRow][i] << " "; 29 | } 30 | endRow--; 31 | } 32 | 33 | 34 | //start col 35 | if (endCol > startCol) { 36 | for (int i = endRow; i >= startRow; i--) { 37 | cout << a[i][startCol] << " "; 38 | } 39 | startCol++; 40 | } 41 | } 42 | 43 | } 44 | 45 | 46 | int main() { 47 | 48 | int a[1000][1000] = {0}; 49 | int m, n; 50 | cin >> m >> n; 51 | 52 | 53 | //Iterate Over the array 54 | int val = 1; 55 | 56 | for (int row = 0; row <= m - 1; row++) { 57 | for (int col = 0; col <= n - 1; col++) { 58 | a[row][col] = val; 59 | val = val + 1; 60 | cout << a[row][col] << " "; 61 | } 62 | cout << endl; 63 | } 64 | 65 | spiralprint(a, m, n); 66 | 67 | 68 | 69 | 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /Arrays/string_class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/string_class -------------------------------------------------------------------------------- /Arrays/string_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | 7 | int main() { 8 | 9 | string s; 10 | 11 | getline(cin, s); 12 | 13 | cout << s; 14 | 15 | //Lets one function that is available 16 | cout << s.length() << endl; 17 | 18 | s.pop_back(); 19 | s.push_back('i'); 20 | 21 | for (int i = 0; i < s.length(); i++) { 22 | cout << s[i] << ","; 23 | } 24 | 25 | string s1 = "hello"; 26 | string s2 = "world"; 27 | cout << s1 + s2 << endl; 28 | 29 | 30 | 31 | 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Arrays/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/test -------------------------------------------------------------------------------- /Arrays/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | 9 | 10 | 11 | 12 | } -------------------------------------------------------------------------------- /Arrays/wave_print: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Arrays/wave_print -------------------------------------------------------------------------------- /Arrays/wave_print.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | int a[1000][1000] = {0}; 8 | int m, n; 9 | cin >> m >> n; 10 | 11 | 12 | //Iterate Over the array 13 | int val = 1; 14 | 15 | for (int row = 0; row <= m - 1; row++) { 16 | for (int col = 0; col <= n - 1; col++) { 17 | a[row][col] = val; 18 | val = val + 1; 19 | cout << a[row][col] << " "; 20 | } 21 | cout << endl; 22 | } 23 | 24 | //Wave Print 25 | for (int col = 0; col < n; col++) { 26 | 27 | if (col % 2 == 0) { 28 | //Even Col - Top Down 29 | for (int row = 0; row < m; row++) { 30 | cout << a[row][col] << " "; 31 | } 32 | } 33 | else { 34 | //Bottom Up Direction 35 | for (int row = m - 1; row >= 0; row--) { 36 | cout << a[row][col] << " "; 37 | } 38 | } 39 | 40 | } 41 | 42 | 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Bitmasking/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Bitmasking/.DS_Store -------------------------------------------------------------------------------- /Bitmasking/common_bit_tasks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isOdd(int n) { 5 | return (n & 1); 6 | } 7 | 8 | int getBit(int n, int i) { 9 | int mask = (1 << i); 10 | int bit = (n & mask) > 0 ? 1 : 0; 11 | return bit; 12 | } 13 | 14 | int setBit(int n, int i) { 15 | int mask = (1 << i); 16 | int ans = n | mask; 17 | return ans; 18 | 19 | } 20 | 21 | void clearBit(int &n, int i) { 22 | int mask = ~(1 << i); 23 | n = n & mask; 24 | } 25 | 26 | void updateBit(int &n, int i, int v) { 27 | int mask = ~(1 << i); 28 | int cleared_n = n & mask; 29 | n = cleared_n | (v << i); 30 | } 31 | 32 | int clearLastIBits(int n, int i) { 33 | int mask = (-1 << i); 34 | return n & mask; 35 | } 36 | int clearRangeItoJ(int n, int i, int j) { 37 | int ones = (~0); 38 | int a = ones << (j + 1); 39 | int b = (1 << i) - 1; 40 | int mask = a | b; 41 | int ans = n & mask; 42 | return ans; 43 | 44 | } 45 | 46 | 47 | int main() { 48 | 49 | int n = 31; 50 | int i = 1, j = 3; //from position i+1 to 0 51 | cout << clearRangeItoJ(n, i, j) << endl; 52 | //cout << clearLastIBits(n, i) << endl; 53 | 54 | 55 | 56 | //cout << (~0) << endl; 57 | 58 | 59 | /* 60 | cin >> i; 61 | clearBit(n, i); 62 | cout << n << endl; 63 | 64 | 65 | //Update the 4th bit in 5 66 | updateBit(n, 2, 0); 67 | updateBit(n, 3, 1); 68 | cout << n << endl; 69 | 70 | 71 | cout << getBit(n, i) << endl; 72 | n = setBit(n, i); 73 | cout << "Ans " << n << endl; 74 | //cout << isOdd(n) << endl; 75 | */ 76 | 77 | 78 | return 0; 79 | } -------------------------------------------------------------------------------- /Bitmasking/decimal_to_binary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int decimalToBinary(int n) { 5 | 6 | int ans = 0; 7 | int p = 1; 8 | 9 | while (n > 0) { 10 | int last_bit = (n & 1); 11 | ans += p * last_bit; 12 | 13 | p = p * 10; 14 | n = n >> 1; 15 | } 16 | 17 | return ans; 18 | } 19 | 20 | 21 | int main() { 22 | 23 | //Given a Number N, find number of set bits in binary rep. of it 24 | int n; 25 | cin >> n; 26 | cout << decimalToBinary(n); 27 | cout << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Bitmasking/not_so_easy_math.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define F(i,a,b) for(ll i = a; i <= b; i++) 5 | #define RF(i,a,b) for(ll i = a; i >= b; i--) 6 | #define pii pair 7 | #define PI 3.14159265358979323846264338327950288 8 | #define ll long long 9 | #define ff first 10 | #define ss second 11 | #define pb(x) push_back(x) 12 | #define mp(x,y) make_pair(x,y) 13 | #define debug(x) cout << #x << " = " << x << endl 14 | #define INF 1000000009 15 | #define mod 1000000007 16 | #define S(x) scanf("%d",&x) 17 | #define S2(x,y) scanf("%d%d",&x,&y) 18 | #define P(x) printf("%d\n",x) 19 | #define all(v) v.begin(),v.end() 20 | ll primes[] = {2, 3, 5, 7, 11, 13, 17, 19}; 21 | int main() 22 | { 23 | std::ios::sync_with_stdio(false); 24 | ll t; 25 | cin >> t; 26 | while (t--) 27 | { 28 | ll n, ans = 0; 29 | cin >> n; 30 | ll maximum_mask = ceil(pow(2ll, 8)) - 1ll; // 11111111 for 8 prime numbers (indexed from left to right) 31 | F(mask, 1, maximum_mask) // all possible combinations 32 | { 33 | ll denominator = 1ll; // number which will divide n in inclusion - exclusion principle 34 | ll setbits = __builtin_popcount(mask); 35 | F(j, 0, 7) // number of digits in mask is always 8 36 | { 37 | if (mask & (1 << j)) // if jth bit is set in mask then its contribution will count 38 | denominator = denominator * primes[j]; 39 | } 40 | if (setbits & 1) // if number of set bits is 1 then add this to answer 41 | ans += n / denominator; 42 | else 43 | ans -= n / denominator; 44 | } 45 | cout << ans << endl; 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Bitmasking/power_fast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | //Exponentiation/Power using Bitmasking 6 | int power_optimised(int a, int n) { 7 | int ans = 1; 8 | 9 | while (n > 0) { 10 | int last_bit = (n & 1); 11 | if (last_bit) { 12 | ans = ans * a; 13 | } 14 | a = a * a; //Square up 15 | n = n >> 1; //Discard the last bit of N 16 | } 17 | return ans; 18 | 19 | return 0; 20 | } 21 | 22 | 23 | int main() { 24 | 25 | int a, n; 26 | cin >> a >> n; 27 | 28 | cout << power_optimised(a, n) << endl; 29 | 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Bitmasking/problem_replace_bits_n_m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Replace Bits in N by M 5 | /* 6 | You are given two 32-bit numbers, N and M, and two bit positions, i and j. 7 | Write a method to set all bits between i and j in N equal 8 | to M (e.g., M becomes a substring of N located at i and starting at j). 9 | 10 | EXAMPLE: 11 | Input: N = 10000000000, 12 | M = 10101, i = 2, j = 6 13 | Output: N = 10001010100 14 | */ 15 | 16 | int clearRangeItoJ(int n, int i, int j) { 17 | int ones = (~0); 18 | int a = ones << (j + 1); 19 | int b = (1 << i) - 1; 20 | int mask = a | b; 21 | int ans = n & mask; 22 | return ans; 23 | 24 | } 25 | 26 | int replaceBits(int n, int m, int i, int j) { 27 | int n_ = clearRangeItoJ(n, i, j); 28 | int ans = n_ | (m << i); 29 | return ans; 30 | } 31 | 32 | int main() { 33 | 34 | int n = 15; 35 | int i = 1, j = 3; 36 | int m = 2; 37 | cout << replaceBits(n, m, i, j) << endl; 38 | 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Bitmasking/set_bit_count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //First Method 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 | 15 | //Second Method 16 | int countBitsFast(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 | int main() { 28 | 29 | //Given a Number N, find number of set bits in binary rep. of it 30 | // N = 13, Binary = 1101, Output = 3 31 | 32 | int n; 33 | cin >> n; 34 | cout << countBits(n) << endl; 35 | cout << countBitsFast(n) << endl; 36 | cout << __builtin_popcount(n) << endl; 37 | 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Bitmasking/subsequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | Finding Subsequences/Subsets of Given a String 7 | Input - "abc" 8 | Output - " ",a,ab,abc,ac,b,bc,c 9 | */ 10 | void filterChars(int n, char a[]) { 11 | 12 | int j = 0; 13 | 14 | while (n > 0) { 15 | int last_bit = (n & 1); 16 | if (last_bit == 1) { 17 | cout << a[j]; 18 | } 19 | j++; 20 | n = n >> 1; 21 | } 22 | cout << endl; 23 | } 24 | 25 | void printSubsets(char a[]) { 26 | 27 | int n = strlen(a); 28 | for (int i = 0; i < (1 << n); i++) { 29 | filterChars(i, a); 30 | } 31 | return; 32 | } 33 | 34 | int main() { 35 | 36 | char a[100]; 37 | cin >> a; 38 | printSubsets(a); 39 | 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Bitmasking/unique_number_2n_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | int main() { 7 | 8 | int n; 9 | int a[100005]; 10 | cin >> n; 11 | 12 | int no; 13 | int res = 0; 14 | 15 | 16 | for (int i = 0; i < n; i++) { 17 | cin >> no; 18 | a[i] = no; 19 | res = res ^ no; 20 | } 21 | // xor = a^b; 22 | int temp = res; 23 | int pos = 0; 24 | while ((temp & 1) != 1) { 25 | pos++; 26 | temp = temp >> 1; 27 | } 28 | 29 | //The first bit in xor is at position 'pos' 30 | int mask = (1 << pos); 31 | 32 | //find those numbers which contain set bit at position pos 33 | int x = 0; 34 | int y = 0; 35 | 36 | for (int i = 0; i < n; i++) { 37 | if ((a[i]&mask) > 0) { 38 | x = x ^ a[i]; 39 | } 40 | } 41 | y = res ^ x; 42 | 43 | cout << min(x, y) << " " << max(x, y) << endl; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Functions/abcd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Functions/abcd -------------------------------------------------------------------------------- /Functions/abcd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Print the following pattern for given N = 5 5 | /* 6 | ABCDE 7 | ABCD 8 | ABC 9 | AB 10 | A 11 | */ 12 | 13 | void printABCDPattern() { 14 | 15 | int n; 16 | cin >> n; 17 | 18 | for (int i = 1; i <= n; i++) { 19 | int cnt_alpabets = n - i + 1; 20 | char alphabet = 'A'; 21 | 22 | for (int j = 1; j <= cnt_alpabets; j++) { 23 | cout << alphabet; 24 | alphabet = alphabet + 1; 25 | } 26 | cout << endl; 27 | } 28 | 29 | } 30 | 31 | int main() { 32 | 33 | 34 | 35 | printABCDPattern(); 36 | 37 | 38 | 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Functions/binary_search: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Functions/binary_search -------------------------------------------------------------------------------- /Functions/binary_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Binary Search 5 | //Efficient Way to Search in Sorted Arrays 6 | 7 | int binary_search(int a[], int n, int key) { 8 | 9 | int s = 0; 10 | int e = n - 1; 11 | 12 | while (s <= e) { 13 | int mid = (s + e) / 2; 14 | 15 | if (a[mid] == key) { 16 | return mid; 17 | } 18 | else if (a[mid] > key) { 19 | e = mid - 1; 20 | } 21 | else { 22 | s = mid + 1; 23 | } 24 | 25 | } 26 | return -1; 27 | 28 | } 29 | 30 | int main() { 31 | 32 | 33 | int n, key; 34 | cin >> n; 35 | 36 | int a[1000]; 37 | 38 | for (int i = 0; i < n; i++) { 39 | cin >> a[i]; 40 | } 41 | 42 | //Ask for the element we want to search 43 | cout << "Enter the element you want to search : "; 44 | cin >> key; 45 | 46 | cout << binary_search(a, n, key) << endl; 47 | 48 | 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Functions/fibonacci_pattern: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Functions/fibonacci_pattern -------------------------------------------------------------------------------- /Functions/fibonacci_pattern.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Fiboacci Series - Nth Fibonacci Number 5 | 6 | //0 , 1, 1, 2, 3, 5, 8 , 13 .... 7 | 8 | int fibonacci(int n) { 9 | int a = 0; 10 | int b = 1; 11 | int c; 12 | 13 | for (int i = 1; i <= n - 1; i++) { 14 | c = a + b; 15 | a = b; 16 | b = c; 17 | } 18 | return c; 19 | 20 | } 21 | 22 | int main() { 23 | 24 | int n; 25 | cin >> n; 26 | cout << fibonacci(n) << endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Functions/hello_function: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Functions/hello_function -------------------------------------------------------------------------------- /Functions/hello_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void sayHello() { 5 | 6 | cout << "Hello Everyone!"; 7 | 8 | } 9 | 10 | int factorial(int n) { 11 | int ans = 1; 12 | for (int i = 2; i <= n; i++) { 13 | ans = ans * i; 14 | } 15 | return ans; 16 | } 17 | 18 | 19 | 20 | int main() { 21 | 22 | cout << "Before Function!" << endl; 23 | 24 | sayHello(); 25 | cout << endl << "After Function!" << endl; 26 | 27 | 28 | //2. part 29 | int n; 30 | cout << "Enter a number"; 31 | cin >> n; 32 | factorial(n); 33 | 34 | 35 | cout << factorial(n) << endl;; 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Functions/print_all_primes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Functions/print_all_primes -------------------------------------------------------------------------------- /Functions/print_all_primes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPrime(int n) { 5 | 6 | 7 | for (int i = 2; i <= n - 1; i++) { 8 | if (n % i == 0) { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | 15 | //write a function to print all primes upto n 16 | void printPrimes(int N) { 17 | 18 | for (int i = 2; i <= N; i++) { 19 | if (isPrime(i)) { 20 | cout << i << " "; 21 | } 22 | } 23 | } 24 | 25 | 26 | int main() { 27 | 28 | //Check if a given number is Prime 29 | int n; 30 | cin >> n; 31 | 32 | printPrimes(n); 33 | 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Greedy Algorithms/acode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | -------------------------------------------------------------------------------- /Greedy Algorithms/greedy_money_change_recursive: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Greedy Algorithms/greedy_money_change_recursive -------------------------------------------------------------------------------- /Greedy Algorithms/greedy_money_change_recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int indian_currency[] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}; 7 | int n = sizeof(indian_currency) / sizeof(int); 8 | 9 | 10 | bool compare(int a, int b) { 11 | return a <= b; 12 | } 13 | 14 | 15 | // Binary Search + Recursion + STL 16 | void count_notes(int money, vector &v) { 17 | 18 | if (money == 0) { 19 | return; 20 | } 21 | 22 | int idx = lower_bound(indian_currency, indian_currency + n, money, compare) - indian_currency - 1; 23 | int m = indian_currency[idx]; 24 | v.push_back(m); 25 | 26 | //Recursive Call 27 | count_notes(money - m, v); 28 | } 29 | int main() { 30 | //Indian Coin Change 31 | int money ; 32 | cin >> money; 33 | 34 | // Money Change 35 | vector v; 36 | 37 | //Make a Recursive Call 38 | count_notes(money, v); 39 | 40 | // For Each Loop 41 | for (int note : v) { 42 | cout << note << " + "; 43 | } 44 | cout << "0" << endl; 45 | 46 | cout << v.size() << " notes needed!" << endl; 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Greedy Algorithms/indian_money_counting: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Greedy Algorithms/indian_money_counting -------------------------------------------------------------------------------- /Greedy Algorithms/indian_money_counting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool compare(int a, int b) { 7 | return a <= b; 8 | } 9 | 10 | vector count_notes(int money) { 11 | 12 | int indian_currency[] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}; 13 | int n = sizeof(indian_currency) / sizeof(int); 14 | 15 | int ans = 0; 16 | vector notes; 17 | 18 | while (money > 0) { 19 | int idx = lower_bound(indian_currency, indian_currency + n, money, compare) - indian_currency - 1; 20 | int m = indian_currency[idx]; 21 | notes.push_back(m); 22 | money = money - m; 23 | ans++; 24 | } 25 | return notes; 26 | } 27 | 28 | int main() { 29 | //Indian Coin Change 30 | int money ; 31 | cin >> money; 32 | vector notes = count_notes(money); 33 | for (int note : notes) { 34 | cout << note << " + "; 35 | } 36 | cout << "0" << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cplusplus-datastructures-algorithms-coding-blocks 2 | Coding Blocks | Data Structures & Algorithms Online Track 3 | 4 | Course Link - [https://online.codingblocks.com/app/courses/c-plus-plus-online-course-for-beginners](https://online.codingblocks.com/app/courses/c-plus-plus-online-course-for-beginners) 5 | 6 | Sections 7 | - Basics of Problem Solving 8 | - Getting started with C++ 9 | - Programming Fundamentals 10 | - Programming Fundamentals-II 11 | 12 | 13 | -------------------------------------------------------------------------------- /STL/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/STL/.DS_Store -------------------------------------------------------------------------------- /STL/Algorithms/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/STL/Algorithms/.DS_Store -------------------------------------------------------------------------------- /STL/Algorithms/algo_stl_01_find.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | 8 | int arr[] = {1, 10, 11, 9, 100}; 9 | int n = sizeof(arr) / sizeof(int); 10 | //Search --> find 11 | 12 | int key ; 13 | cin >> key; 14 | auto it = find(arr, arr + n, key); 15 | int index = it - arr; 16 | if (index == n) { 17 | cout << key << " not present"; 18 | } 19 | else { 20 | cout << "Present at index " << index; 21 | } 22 | 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /STL/Algorithms/algo_stl_02_binary_search_lower_upper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | 8 | int arr[] = {20, 30, 40, 40, 40, 50, 100, 1100}; 9 | int n = sizeof(arr) / sizeof(int); 10 | //Search in a sorted array 11 | int key ; 12 | cin >> key; 13 | 14 | bool present = binary_search(arr, arr + n, key); 15 | if (present) { 16 | cout << "Present"; 17 | } 18 | else { 19 | cout << "Absent!"; 20 | } 21 | 22 | //Two more things 23 | // Get the index of the element 24 | // lower_bound(s,e,key) and upper_bound(s,e,key) 25 | 26 | auto lb = lower_bound(arr, arr + n, 41); 27 | cout << endl << "Lower bound of 40 is" << (lb - arr) << endl; 28 | 29 | 30 | //uppper bound method 31 | auto ub = upper_bound(arr, arr + n, 40); 32 | cout << endl << "Upper bound of 40 is" << (ub - arr) << endl; 33 | 34 | cout << "Occ Freq of 40 is " << (ub - lb) << endl; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /STL/Algorithms/algo_stl_06_next_permuation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int main() { 6 | 7 | int arr[] = {10, 20, 30, 40, 50}; 8 | int n = sizeof(arr) / sizeof(int); 9 | rotate(arr, arr + 2, arr + n); 10 | 11 | for (int i = 0; i < n; i++) { 12 | cout << arr[i] << " "; 13 | } 14 | cout << endl; 15 | //Apply the same on a vector 16 | vector v{1, 2, 3}; 17 | 18 | //rotate(v.begin(),v.begin()+3,v.end()); 19 | 20 | for (int i = 0; i < v.size(); i++) { 21 | cout << v[i] << " "; 22 | } 23 | 24 | //Next_permuation 25 | next_permutation(v.begin(), v.end()); 26 | next_permutation(v.begin(), v.end()); 27 | cout << endl; 28 | //for each loop 29 | for (int x : v) { 30 | cout << x << " "; 31 | } 32 | 33 | 34 | 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /STL/Algorithms/algo_stl_money_change_problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a = 10; 8 | int b = 20; 9 | 10 | swap(a, b); 11 | cout << a << " and " << b << endl; 12 | 13 | cout << max(a, b) << endl; 14 | cout << min(a, b) << endl; 15 | 16 | int arr[] = {1, 2, 3, 4, 4, 5}; 17 | reverse(arr, arr + 4); 18 | 19 | swap(arr[0], arr[1]); 20 | 21 | int n = sizeof(arr) / sizeof(int); 22 | for (int i = 0; i < n; i++) { 23 | cout << arr[i] << " "; 24 | } 25 | next_permutation(arr, arr + n); 26 | cout << endl; 27 | for (int i = 0; i < n; i++) { 28 | cout << arr[i] << " "; 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /STL/Algorithms/pair_stl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | 8 | //Pair 9 | pair p; 10 | p.first = 10; 11 | p.second = 'B'; 12 | 13 | //Another way 14 | pair p2(p); 15 | 16 | cout<< p2.first< p3 = make_pair(100,"Audi"); 20 | cout<>a>>b; 25 | 26 | pair p4 = make_pair(a,b); 27 | //Array of Pairs 28 | //Vector of Pairs 29 | pair, string> car; 30 | car.second = "Audi"; 31 | car.first.first = 10; 32 | car.first.second = 20; 33 | 34 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | //String Init 8 | string s0; 9 | string s1("Hello"); 10 | 11 | string s2 = "Hello World!"; 12 | string s3(s2); 13 | 14 | string s4 = s3; 15 | 16 | char a[] = {'a','b','c','\0'}; 17 | string s5(a); 18 | 19 | cout< 0 or < 0 45 | 46 | 47 | //Overloaded Operator operators 48 | if(s1 > s0){ 49 | cout<<"Mango is lexi greater than Apple"< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | //String Init 8 | string s0; 9 | string s1("Hello"); 10 | 11 | string s2 = "Hello World!"; 12 | string s3(s2); 13 | 14 | string s4 = s3; 15 | 16 | char a[] = {'a','b','c','\0'}; 17 | string s5(a); 18 | 19 | cout< 0 or < 0 45 | 46 | 47 | //Overloaded Operator operators 48 | if(s1 > s0){ 49 | cout<<"Mango is lexi greater than Apple"< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool compare(string a,string b){ 7 | if(a.length()==b.length()){ 8 | return ab.length(); 11 | } 12 | 13 | int main() { 14 | int n; 15 | cin>>n; 16 | cin.get(); 17 | 18 | string s[100]; //Vector 19 | 20 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool compare(string a,string b){ 7 | if(a.length()==b.length()){ 8 | return ab.length(); 11 | } 12 | 13 | int main() { 14 | int n; 15 | cin>>n; 16 | cin.get(); 17 | 18 | string s[100]; //Vector 19 | 20 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | //char *strtok(char *s,char *delimiters) 6 | // returns a token on each subsequent call 7 | // on the first call function should be passed with string argument for 's' 8 | // on subsequent calls we should pass the string argument as null 9 | 10 | int main() { 11 | 12 | char s[100] = "Today, is a rainy, day"; 13 | 14 | char *ptr = strtok(s," "); 15 | cout< 2 | #include 3 | using namespace std; 4 | 5 | //char *strtok(char *s,char *delimiters) 6 | // returns a token on each subsequent call 7 | // on the first call function should be passed with string argument for 's' 8 | // on subsequent calls we should pass the string argument as null 9 | 10 | int main() { 11 | 12 | char s[100] = "Today, is a rainy, day"; 13 | 14 | char *ptr = strtok(s," "); 15 | cout< 2 | #include 3 | using namespace std; 4 | 5 | char *mystrtok(char *str,char delim){ 6 | //string and single char acts as delimiter 7 | static char*input = NULL; 8 | if(str!=NULL){ 9 | //we are maing the first call 10 | input = str; 11 | } 12 | //check here -base case after the final token has been returned 13 | if(input==NULL){ 14 | return NULL; 15 | } 16 | 17 | //start extracting tokens and store them in an array 18 | char *output = new char[strlen(input)+1]; 19 | int i=0; 20 | for( ;input[i]!='\0';i++){ 21 | if(input[i]!=delim){ 22 | output[i] = input[i]; 23 | } 24 | else{ 25 | output[i] = '\0'; 26 | input = input + i + 1; 27 | return output; 28 | } 29 | } 30 | //corner case 31 | output[i] = '\0'; 32 | input = NULL; 33 | return output; 34 | } 35 | 36 | 37 | 38 | int main() { 39 | 40 | char s[100] = "Today, is a rainy, day"; 41 | 42 | char *ptr = mystrtok(s,' '); 43 | cout< 2 | #include 3 | using namespace std; 4 | 5 | char *mystrtok(char *str,char delim){ 6 | //string and single char acts as delimiter 7 | static char*input = NULL; 8 | if(str!=NULL){ 9 | //we are maing the first call 10 | input = str; 11 | } 12 | //check here -base case after the final token has been returned 13 | if(input==NULL){ 14 | return NULL; 15 | } 16 | 17 | //start extracting tokens and store them in an array 18 | char *output = new char[strlen(input)+1]; 19 | int i=0; 20 | for( ;input[i]!='\0';i++){ 21 | if(input[i]!=delim){ 22 | output[i] = input[i]; 23 | } 24 | else{ 25 | output[i] = '\0'; 26 | input = input + i + 1; 27 | return output; 28 | } 29 | } 30 | //corner case 31 | output[i] = '\0'; 32 | input = NULL; 33 | return output; 34 | } 35 | 36 | 37 | 38 | int main() { 39 | 40 | char s[100] = "Today, is a rainy, day"; 41 | 42 | char *ptr = mystrtok(s,' '); 43 | cout< 4 | class Vector { 5 | int cs; 6 | int ms; 7 | T *arr; 8 | 9 | public: 10 | Vector() { 11 | cs = 0; 12 | ms = 1; 13 | arr = new T[ms]; 14 | } 15 | void push_back(const T d) { 16 | if (cs == ms) { 17 | //Array if full 18 | T *oldArr = arr; 19 | arr = new T[2 * ms]; 20 | ms = 2 * ms; 21 | for (int i = 0; i < cs; i++) { 22 | arr[i] = oldArr[i]; 23 | } 24 | //clear the old memory 25 | delete [] oldArr; 26 | } 27 | arr[cs] = d; 28 | cs++; 29 | } 30 | void pop_back() { 31 | cs--; 32 | } 33 | 34 | T front() const { 35 | return arr[0]; 36 | } 37 | T back() const { 38 | return arr[cs - 1]; 39 | } 40 | bool empty() const { 41 | return cs == 0; 42 | } 43 | 44 | int capacity() const { 45 | return ms; 46 | } 47 | T at(const int i) { 48 | return arr[i]; 49 | } 50 | int size() const { 51 | return cs; 52 | } 53 | 54 | //operator overloading 55 | T operator[](const int i) { 56 | return arr[i]; 57 | } 58 | 59 | }; -------------------------------------------------------------------------------- /STL/Vectors/vector_demo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "vector.h" 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | Vector v; 8 | 9 | v.push_back(71); 10 | 11 | v.push_back(72); 12 | 13 | v.push_back(73); 14 | 15 | v.push_back(74); 16 | v.pop_back(); 17 | v.push_back(76); 18 | v.push_back(80); 19 | 20 | cout << "Capacity " << v.capacity() << endl; 21 | 22 | for (int i = 0; i < v.size(); i++) { 23 | cout << v[i] << " "; 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /STL/Vectors/vector_stl_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main() { 6 | 7 | //You can create and initalise a vector 8 | vector a; 9 | vector b(5,10); //five int with value 10 - init a vector of zeros (n,0) 10 | vector c(b.begin(),b.end()); 11 | vector d{1,2,3,10,14}; 12 | 13 | //Look at how we can iterate over the vector 14 | for(int i=0;i v; 31 | int n; 32 | cin>>n; 33 | for(int i=0;i>no; 36 | v.push_back(no); 37 | } 38 | 39 | for(int x:v){ 40 | cout< 2 | #include 3 | 4 | using namespace std; 5 | int main() { 6 | 7 | 8 | vector d{1,2,3,10,14}; 9 | //Push Back O(1) for most of the time 10 | d.push_back(16); 11 | 12 | //Pop Back / Removes the last element O(1) 13 | d.pop_back(); 14 | 15 | //Insert some elements in the middle O(N) 16 | d.insert(d.begin() + 3,4,100); 17 | 18 | //erase some elements in the middle 19 | d.erase(d.begin()+2); 20 | d.erase(d.begin()+2,d.begin()+5); 21 | 22 | //size 23 | cout<>n; 53 | vector v; 54 | //To avoid doubling, we will use reserve function 55 | v.reserve(1000); 56 | 57 | for(int i=0;i>no; 60 | v.push_back(no); 61 | cout<<"Changing capacity "< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | vector a; 7 | vector b(5,10); //five ints with value 10 8 | vector c(b.begin(),b.end()); 9 | vector d{1,2,3,4,5}; 10 | 11 | int A[] = {1,2,3,4,5}; 12 | vector v; 13 | for(int i=0;i<5;i++){ 14 | v.push_back(A[i]); 15 | } 16 | 17 | for(int x:v){ 18 | cout< 2 | using namespace std; 3 | 4 | 5 | void findStrings(char *a, int i, int j, char *out) { 6 | //base case 7 | if (a[i] == '\0') { 8 | out[j] = '\0'; 9 | cout << out << endl; 10 | return; 11 | } 12 | //rec case 13 | //1 . include single char 14 | int digit = a[i] - '0'; 15 | char ch = digit + 'A' - 1; //generate 16 | out[j] = ch; 17 | //remaining call 18 | findStrings(a, i + 1, j + 1, out); 19 | 20 | // 2. include two char 21 | if (a[i + 1] != '\0') { 22 | int second_digit = a[i + 1] - '0'; 23 | int no = 10 * digit + second_digit; 24 | if (no <= 26) { 25 | ch = no + 'A' - 1; 26 | out[j] = ch; 27 | findStrings(a, i + 2, j + 1, out); 28 | } 29 | } 30 | } 31 | 32 | int main() { 33 | 34 | char a[10]; 35 | char output[10]; 36 | 37 | cin >> a; 38 | findStrings(a, 0, 0, output); 39 | 40 | 41 | 42 | 43 | } -------------------------------------------------------------------------------- /Webinar-01/brackets_gen: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Webinar-01/brackets_gen -------------------------------------------------------------------------------- /Webinar-01/brackets_gen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void generate_paranthesis(char *a, int n, int open = 0, int close = 0, int i = 0) { 5 | //base case 6 | if (i == 2 * n) { 7 | a[i] = '\0'; 8 | cout << a << endl; 9 | return; 10 | } 11 | 12 | //open 13 | if (open < n) { 14 | a[i] = '('; 15 | generate_paranthesis(a, n, open + 1, close, i + 1); 16 | } 17 | 18 | //closing 19 | if (close < open) { 20 | a[i] = ')'; 21 | generate_paranthesis(a, n, open, close + 1, i + 1); 22 | } 23 | return; 24 | } 25 | 26 | 27 | int main() { 28 | 29 | 30 | char output[100]; 31 | int n; 32 | cin >> n; 33 | generate_paranthesis(output, n); 34 | 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Webinar-01/coin_greedy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Webinar-01/coin_greedy -------------------------------------------------------------------------------- /Webinar-01/coin_greedy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | 7 | int indian_currency[] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}; 8 | int n = sizeof(indian_currency) / sizeof(int); 9 | 10 | bool compare(int a, int b) { 11 | return a <= b; 12 | } 13 | 14 | void count_notes(int money) { 15 | 16 | // Base Case 17 | if (money == 0) { 18 | return; 19 | } 20 | //Recursive Case 21 | // lower_bound will return an iterator 22 | // Log (T) where T is the type of coins you have! 23 | 24 | int largest_idx = lower_bound(indian_currency, indian_currency + n, money, compare) - indian_currency - 1; 25 | int m = indian_currency[largest_idx]; 26 | cout << m << " " << endl; 27 | 28 | //Recursive Call 29 | count_notes(money - m); 30 | 31 | return; 32 | 33 | } 34 | 35 | int main() { 36 | 37 | int money; 38 | cin >> money; 39 | 40 | count_notes(money); 41 | 42 | 43 | 44 | 45 | 46 | return 0 ; 47 | } -------------------------------------------------------------------------------- /Webinar-01/subsums: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supershivam13/cplusplus-datastructures-algorithms-coding-blocks/b83bdc3b4c9c3aeb531dce1d029d64646e3f4d9a/Webinar-01/subsums -------------------------------------------------------------------------------- /Webinar-01/subsums.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define ll long long 5 | using namespace std; 6 | 7 | //Input Vector is from Q 8 | // Output is Vector of possible subsets sums 9 | vector subsets(vector v) { 10 | vector ans; 11 | 12 | int n = v.size(); // n <= 17 <= 10^6 13 | //Bitmasking - Subset Finding 14 | for (int i = 0; i < (1 << n); i++ ) { 15 | int j = 0; 16 | ll sum = 0 ; 17 | for (int no = i; no > 0; no = no >> 1) { 18 | if (no & 1) { 19 | sum += v[j]; 20 | } 21 | j++; 22 | } 23 | ans.push_back(sum); 24 | 25 | } 26 | return ans; 27 | } 28 | 29 | 30 | int main() { 31 | 32 | vector v1, v2; //17,17 33 | int n, a, b; 34 | cin >> n >> a >> b; 35 | 36 | //input 37 | int n1 = n / 2; 38 | int n2 = n - n1; 39 | 40 | 41 | for (int i = 0; i < n1; i++) { 42 | ll x; 43 | cin >> x; 44 | v1.push_back(x); 45 | } 46 | 47 | for (int i = 0; i < n2; i++) { 48 | ll x; 49 | cin >> x; 50 | v2.push_back(x); 51 | } 52 | //subsets sums of v1 and v2 53 | vector sv1, sv2; 54 | 55 | sv1 = subsets(v1); 56 | sv2 = subsets(v2); 57 | 58 | // sort atleast sv2 sorted 59 | sort(sv2.begin(), sv2.end()); 60 | 61 | 62 | //matching using binary array 63 | int ans = 0; 64 | for (int i = 0; i < sv1.size(); i++) { 65 | ll lo = a - sv1[i]; 66 | ll hi = b - sv1[i]; 67 | 68 | int m1 = lower_bound(sv2.begin(), sv2.end(), lo) - sv2.begin(); 69 | int m2 = upper_bound(sv2.begin(), sv2.end(), hi) - sv2.begin(); 70 | 71 | ans += (m2 - m1); 72 | 73 | } 74 | cout << ans << endl; 75 | 76 | return 0; 77 | } --------------------------------------------------------------------------------