├── .vscode ├── c_cpp_properties.json ├── launch.json └── settings.json ├── 2048 problem recursion.cpp ├── 2Sum.cpp ├── 3Sum.cpp ├── 4sum.cpp ├── 890. Find and Replace Pattern ├── Add BiNumbers_using_string_Data_Type.cpp.cpp ├── Adding-Floyd-cycle-detection-algorithm.cpp ├── Append_Data_in_a_File.cpp ├── Banking.java ├── Binary Tree - Preorder build and print.cpp ├── Binary Tree BFS traversal.cpp ├── Binary Tree Height Balanced Tree.cpp ├── Binary Tree Level Order Traversal.cpp ├── Binary Tree Sum replacement.cpp ├── Binary Tree count Nodes.cpp ├── Binary Tree diameter of tree.cpp ├── Binary search tree - Is BST.cpp ├── Binary search tree deletion.cpp ├── Binary search tree search.cpp ├── Binary tree left view.cpp ├── Bucket Sort.cpp ├── Capitalize_First_Letter_of_All_Word.cpp ├── Capitalize_First_Letter_of_All_Words.cpp ├── Codeforces ├── A_Jellyfish_and_Undertale.cpp ├── B_Chips_on_the_Board.cpp ├── B_Keep_it_Beautiful.cpp ├── C_Ski_Resort.cpp ├── C_Vasilije_in_Cacak.cpp ├── Fedor_and_New_Game.cpp ├── Queue_At_the_School.cpp ├── Random_Teams.cpp ├── Xenia_and_Ringroad.cpp └── stopwatch.py ├── Cont.md ├── Convert_Celsius_To_Fahrenheit.cpp ├── Customer_Billing_System.c ├── DNF Sort.cpp ├── Decimal to Binary using bitwise.cpp ├── Deletion _in_Linked_List ├── Design Circular Queue Leetcode ├── Digital_Clock.c ├── Digital_clock.c ├── Doubly Link List DS.c ├── Factorial ├── Find_SecLargest_Element_using_for_Loop ├── Generate_10_Random_Numbers.cpp ├── IPV4_DecimalIP.py ├── Implement_of_Graph ├── Implementation_of_Graph ├── InsertNodeAtBeginningOfLinkedList.c ├── Insertion_Sort.cpp ├── Interchange_First_and_Last_Digit_of_a_Number.cpp ├── K-closest-points-to-origin.cpp ├── Largestrectangleinhistogram.cpp ├── Linearsearch.c ├── Linked list Deletion.cpp ├── Linked list take input.cpp ├── Mapped strings.cpp ├── Mat_chain_multiplication_dp.cpp ├── Maximize_Toys.cpp ├── Merge sort algo.cpp ├── Minimum Depth of Binary Tree.cpp ├── Minimum └── Maximum Falling Path Sum ├── Octal_Hexadecimal_without_Function.cpp ├── Possible Victory.cpp ├── Print ArmstrongNumbers_between_1_to_500.cpp ├── Print vertical order of tree.cpp ├── Print_Fibonacci_Series_of_N_Terms ├── Print_Floyd_Triangle.cpp ├── Print_Pascal_Triangle.cpp ├── Program_to_find_the_value_of_nPr.cpp ├── Program_to_generate_and_print_Armstrong_numbers.cpp ├── Programming_Code_to_Get_IP_Address.cpp ├── Python ├── Countdown Timer.py ├── Dots-and-Boxes │ ├── images │ │ ├── icon.ico │ │ ├── movie.mov │ │ ├── preview.gif │ │ └── screenshot.png │ └── main.py ├── Fibonacci.py ├── Flower Design in Python ├── Hi ├── Merge Mails.py ├── PongGame.py ├── Quad Eq.py ├── Rock_Paper_Scissor_Game.py ├── Shuffle Cards.py ├── WordGame.py ├── animal quiz.py ├── armstrong using python.py ├── calender.py ├── dice roll simulator.py ├── fidget spinner.py ├── first-letter-capatalisation.py ├── flappy bird .py ├── guess_game.py ├── hangman.py ├── leap year.py ├── love_calculator.py ├── matrix using loop.py ├── pallindrome or not.py ├── popular_course.py ├── prime number in an interval.py ├── quick sort.py ├── quizGame.c ├── rockpaperscissors.py ├── square of natural number.py ├── stderr.txt ├── stdout.txt └── sudoku solver │ ├── icon.png │ └── solver.py ├── Queue using LinkedList.c ├── Radix_Sort ├── Ransom Note Leetcode ├── ReverseStringQuestion.class ├── ReverseStringQuestion.java ├── Searching_element_inarray.cpp ├── Second-Smallest&Second-Largest-Element-in-array.cpp ├── Sorted Array to BST ├── Sorting_Algorithms └── RadixSort.c ├── Spiral_Matrix.cpp ├── Tower of hanoi.cpp ├── Tower_of_Hanoi.cpp ├── Tower_of_hanoi.cpp ├── TrapRainwater.cpp ├── Treesort.cpp ├── Unique number 3.cpp ├── Validparentheses.cpp ├── Wave Sort.cpp ├── Xor profit problem.cpp ├── adjacency list for weigted graph.cpp ├── array rotation.py ├── binary search tree insertion and build.cpp ├── binary search.cpp ├── binary tree-preorder,inorder,postorder traversal.cpp ├── binarySearchTreeOperations.c ├── binarysearch.c ├── binaryserach.cpp ├── build balanced tree from array.cpp ├── build tree from pre and inorder.cpp ├── check string using python.py ├── check subarray with sum zero.cpp ├── check_foldable_tree.cpp ├── christmas_tree_pattarn.c ├── circular linked list deletion.cpp ├── circular linked list insertion.cpp ├── codechef_solutions ├── BWF.cpp ├── Cheaper_cab.cpp ├── Dominant_army.cpp ├── FINALSUM,cpp ├── First.cpp ├── Football_cup.cpp ├── LongestANDSubarray.cpp ├── MOnthly_budget.cpp ├── Mango.cpp ├── RC.cpp ├── ROT.cpp ├── TV_discount.cpp ├── Vol_control.cpp ├── assignments.cpp ├── aud.rng.cpp ├── best_coupon.cpp ├── bird_farm.cpp ├── blackjack.cpp ├── bottles.cpp ├── candy.cpp ├── chef_and_masks.cpp ├── chef_n_candies.cpp ├── chef_n_choco.cpp ├── chef_office.cpp ├── chess.cpp ├── chess_dist.cpp ├── chfspl.cpp ├── cooler.cpp ├── course_reg.cpp ├── dcc.cpp ├── discus.cpp ├── dishes.cpp ├── exit.cpp ├── exp_setter.cpp ├── expiry.cpp ├── filcandy.cpp ├── footbal.cpp ├── greater_avg.cpp ├── high_accuracy.cpp ├── hoop.cpp ├── insurance.cpp ├── jenga.cpp ├── level.cpp ├── mahasena.cpp ├── mb.cpp ├── miami_GP.cpp ├── min_car.cpp ├── motivation.cpp ├── mt.cpp ├── osp.cpp ├── pizza.cpp ├── playlist.cpp ├── practice.cpp ├── proglang.cpp ├── qual.cpp ├── rank.cpp ├── reach_target.cpp ├── rich.cpp ├── sale.cpp ├── shoe.cpp ├── speciality.cpp ├── stock.cpp ├── subscriptions.cpp ├── taller.cpp ├── tastiness.cpp ├── taxes.cpp ├── tea.cpp ├── test_avg.cpp ├── todolist.cpp ├── vaccine.cpp ├── weightbal.cpp └── winner.cpp ├── count set bits and optimisations.cpp ├── count_no_of_set_bits_5_methods.cpp ├── counting sort.cpp ├── cpp_programs ├── Cpp_Pattrens │ ├── TwetlthPattren.cpp │ ├── eightPattren.cpp │ ├── elventhPattren.cpp │ ├── fifthPattren.cpp │ ├── fourthpattren.cpp │ ├── fristPattren.cpp │ ├── ninthPattren.cpp │ ├── secondPattren.cpp │ ├── sevenPattren.cpp │ ├── sixthPattren.cpp │ ├── tempCodeRunnerFile.cpp │ ├── tenthPattren.cpp │ └── thirdpattren.cpp ├── Josephus.cpp ├── heap_sort.cpp ├── linkedList │ ├── JosephusCircleImplementation.cpp │ ├── loop_detection.cpp │ ├── reverse.cpp │ └── rotate.cpp ├── quickSort.cpp └── sorting.cpp ├── csv_files ├── customize_multiplication_matrix_finder.c ├── doubly linked list insertion.cpp ├── dsa-code ├── Doubly Linked List ├── LinkedListH1.cpp ├── Sort-an-Array.cpp ├── Sparse_Matrix.c ├── Tries&Huffman_1.cpp ├── binaryTree_1.cpp ├── binaryTree_2.cpp ├── binaryTree_3.cpp ├── binaryTree_4.cpp ├── binarysearchTree_1.cpp ├── binarysearchTree_2.cpp ├── binarysearchTree_3.cpp ├── carRunner.cpp ├── code1.cpp ├── demo.cpp ├── factorial.cpp ├── graph_1.cpp ├── graph_2.cpp ├── graph_3.cpp ├── graph_4.cpp ├── hashmap_1.cpp ├── hashmap_2.cpp ├── hashmap_3.cpp ├── hashmap_4.cpp ├── header │ ├── binarySearchTree.h │ ├── binaryTree.h │ ├── car.h │ ├── demoheader.h │ ├── linkedList.h │ └── tree.h ├── linked_list_1.cpp ├── linked_list_2.cpp ├── linked_list_3.cpp ├── linked_list_4.cpp ├── priorityQueue_1.cpp ├── priorityQueue_2.cpp ├── recursion1.cpp ├── recursion2.cpp ├── snippet.cpp ├── stack_and_queue_1.cpp ├── stack_and_queue_2.cpp ├── stack_and_queue_3.cpp ├── stack_and_queue_4.cpp ├── stack_and_queue_5.cpp ├── stack_and_queue_6.cpp ├── treeH1.cpp ├── tree_1.cpp ├── tree_2.cpp └── vector_1.cpp ├── empty list 1.py ├── encryption_decryption.c ├── factorial using recursion.cpp ├── fast exponentiation.cpp ├── fast power using recursion.cpp ├── fibonacci series ├── find upper bound and lower bound.cpp ├── freinds problem recursion.cpp ├── generate subsets using bitsmasking.cpp ├── height of binary tree.cpp ├── insertion of node ├── insertion sort.cpp ├── interest_calculator.c ├── interpolation.c ├── interpolation_search.c ├── intersection of two linked list.cpp ├── ipaddress.c ├── java ├── AddDigitSum.java ├── AddElementsinArray.java ├── AddTwoBinaryNumbers.java ├── Age Calculator.java ├── AllCharactersToLowerCase.java ├── Area of Circle.java ├── ArrayElementBinarySearch.java ├── ArrayList.java ├── ArrayListExample.java ├── ArrayOpsBubbleSort.java ├── ArrayinReverseOrder.java ├── ArraysofArrays.java ├── AverageandPercentage.java ├── factorial-trailing-zeroes └── house-robber ├── jumpsearch.c ├── left shift.c ├── length of longest subarray with sum k.cpp ├── level-order-traversal.cpp ├── lfu_cache.cpp ├── linear search.cpp ├── linearSearch.cpp ├── linked list - merge two sorted linked lists.cpp ├── linked list K append.cpp ├── linked list insertion at head.cpp ├── linked list insertion insertion at head,last,midle.cpp ├── linked list kth node from last.cpp ├── linked list merge sort.cpp ├── linked list mid point.cpp ├── linked list palindrome.cpp ├── linked list reverse recursively.cpp ├── linked list reverse.cpp ├── linked list search.cpp ├── linked list-even after odd.cpp ├── lowest common ancestor.cpp ├── lru_cache.cpp ├── nQueenproblem.cpp ├── nth fibonacci using recursion.cpp ├── nth_root_using_binary_search.cpp ├── optimal binary strings recursion.cpp ├── optimal game strategy.cpp ├── palindromicarray.cpp ├── paper.cpp ├── permutation.cpp ├── pong.py ├── print pascal triangle using function in c++ language ├── quick sort algo.cpp ├── quicksort.cpp ├── readme ├── recursion dictionary order.cpp ├── recursion sorted array.cpp ├── recursive binary search.py ├── replace bits in N by M.cpp ├── replace pi using recursion.cpp ├── replacechar.cpp ├── reversal algorithm.py ├── reverseString.cpp ├── rockpaperscissors.c ├── search-element-in-rotated-array.cpp ├── searching a user entered number in an array.cpp ├── selection sort.cpp ├── smart keypad.cpp ├── snakes.py ├── sort the string.cpp ├── sorting elements of an array by frequency.cpp ├── string remove duplicates.cpp ├── string to int using recursion.cpp ├── subsequences generation recursion.cpp ├── subset.cpp ├── sum of array.py ├── trailingzeroes.cpp ├── unique number 2.cpp ├── unique number1.cpp └── unique values.py /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "windows-gcc-x64", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "compilerPath": "C:/MinGW/bin/gcc.exe", 9 | "cStandard": "${default}", 10 | "cppStandard": "${default}", 11 | "intelliSenseMode": "windows-gcc-x64", 12 | "compilerArgs": [ 13 | "" 14 | ] 15 | } 16 | ], 17 | "version": 4 18 | } -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "C/C++ Runner: Debug Session", 6 | "type": "cppdbg", 7 | "request": "launch", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "externalConsole": true, 11 | "cwd": "c:/Users/Anubhav/OneDrive/Desktop/New folder/hactoberfest2023", 12 | "program": "c:/Users/Anubhav/OneDrive/Desktop/New folder/hactoberfest2023/build/Debug/outDebug", 13 | "MIMode": "gdb", 14 | "miDebuggerPath": "gdb", 15 | "setupCommands": [ 16 | { 17 | "description": "Enable pretty-printing for gdb", 18 | "text": "-enable-pretty-printing", 19 | "ignoreFailures": true 20 | } 21 | ] 22 | } 23 | ] 24 | } -------------------------------------------------------------------------------- /2048 problem recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | char words[][10]={"zero","one","two","three","four","five","six","seven","eight","nine"}; 5 | void print_spelling(int n){ 6 | //base case 7 | if(n==0){ 8 | return; 9 | } 10 | //recursive case first print the spellings of (204) 11 | print_spelling(n/10); 12 | //after the function call 13 | int digit=n%10; 14 | cout<>n; 21 | print_spelling(n); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /890. Find and Replace Pattern: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void createMapping(string &str){ 4 | char start = 'a'; 5 | char mapping[300] = {0}; 6 | for(auto ch : str){ 7 | if(mapping[ch] == 0){ 8 | mapping[ch] = start ; 9 | start++; 10 | } 11 | } 12 | for(int i=0 ; i findAndReplacePattern(vector& words, string pattern) { 18 | vector ans; 19 | createMapping(pattern); 20 | for(string s : words){ 21 | string tempstr = s; 22 | createMapping(tempstr); 23 | if(tempstr == pattern) 24 | ans.push_back(s); 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Add BiNumbers_using_string_Data_Type.cpp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string bin1, bin2, addRes; 7 | int len, carry, re, i; 8 | cout<<"Enter First Binary Number: "; 9 | cin>>bin1; 10 | cout<<"Enter Second Binary Number: "; 11 | cin>>bin2; 12 | len = bin1.size(); 13 | addRes = ""; 14 | carry = 0; 15 | for(i=(len-1); i>-1; i--) 16 | { 17 | re = carry; 18 | if(bin1[i] == '1') 19 | re = re+1; 20 | else 21 | re = re+0; 22 | if(bin2[i] == '1') 23 | re = re+1; 24 | else 25 | re = re+0; 26 | if(re%2==1) 27 | addRes = '1' + addRes; 28 | else 29 | addRes = '0' + addRes; 30 | if(re<2) 31 | carry = 0; 32 | else 33 | carry = 1; 34 | } 35 | if(carry!=0) 36 | addRes = '1' + addRes; 37 | cout<<"\nAddition Result = "< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | int main() 7 | { 8 | char filename[20], str[500]; 9 | fstream fp; 10 | cout<<"Enter File's Name: "; 11 | gets(filename); 12 | fp.open(filename, fstream::app); 13 | if(!fp) 14 | { 15 | cout<<"\nFile doesn't exist or Access denied!"; 16 | return 0; 17 | } 18 | cout<<"Enter the Data: "; 19 | gets(str); 20 | while(strlen(str)>0) 21 | { 22 | fp<<"\n"; 23 | fp< 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node*left; 8 | node*right; 9 | 10 | node(int d){ 11 | data=d; 12 | left=NULL; 13 | right=NULL; 14 | } 15 | }; 16 | node* buildTree(){ 17 | int d; 18 | cin>>d; 19 | if(d==-1){ 20 | return NULL; 21 | } 22 | node* root=new node(d); 23 | root->left=buildTree(); 24 | root->right=buildTree(); 25 | return root; 26 | } 27 | void print(node* root){ 28 | if(root==NULL){ 29 | return; 30 | } 31 | //otherwise , print the root first followed by subtrees 32 | cout<data<<" "; 33 | print(root->left); 34 | print(root->right); 35 | } 36 | int main(){ 37 | node* root=buildTree(); 38 | print(root); 39 | } 40 | -------------------------------------------------------------------------------- /Binary Tree BFS traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class node{ 6 | public: 7 | int data; 8 | node*left; 9 | node*right; 10 | 11 | node(int d){ 12 | data=d; 13 | left=NULL; 14 | right=NULL; 15 | } 16 | }; 17 | node* buildTree(){ 18 | int d; 19 | cin>>d; 20 | if(d==-1){ 21 | return NULL; 22 | } 23 | node* root=new node(d); 24 | root->left=buildTree(); 25 | root->right=buildTree(); 26 | return root; 27 | } 28 | 29 | int height(node* root){ 30 | if(root==NULL){ 31 | return 0; 32 | } 33 | int ls=height(root->left); 34 | int rs=height(root->right); 35 | return max(ls,rs)+1; 36 | } 37 | void bfs(node* root){ 38 | queueq; 39 | q.push(root); 40 | while(!q.empty()){ 41 | node* f=q.front(); 42 | cout<data<<" "; 43 | q.pop(); 44 | if(f->left){ 45 | q.push(f->left); 46 | 47 | } 48 | if(f->right){ 49 | q.push(f->right); 50 | } 51 | 52 | } 53 | return; 54 | } 55 | int main(){ 56 | node* root=buildTree(); 57 | bfs(root); 58 | 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Binary Tree Level Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node*left; 8 | node*right; 9 | 10 | node(int d){ 11 | data=d; 12 | left=NULL; 13 | right=NULL; 14 | } 15 | }; 16 | node* buildTree(){ 17 | int d; 18 | cin>>d; 19 | if(d==-1){ 20 | return NULL; 21 | } 22 | node* root=new node(d); 23 | root->left=buildTree(); 24 | root->right=buildTree(); 25 | return root; 26 | } 27 | 28 | int height(node* root){ 29 | if(root==NULL){ 30 | return 0; 31 | } 32 | int ls=height(root->left); 33 | int rs=height(root->right); 34 | return max(ls,rs)+1; 35 | } 36 | //print kth level 37 | void print_kth_level(node* root,int k){ 38 | if(root==NULL){ 39 | return; 40 | } 41 | if(k==1){ 42 | cout<data<<" "; 43 | return; 44 | } 45 | print_kth_level(root->left,k-1); 46 | print_kth_level(root->right,k-1); 47 | return; 48 | } 49 | //print all levels 50 | void print_all_levels(node* root){ 51 | int h=height(root); 52 | for(int i=1;i<=h;i++){ 53 | print_kth_level(root,i); 54 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | class node { 6 | public: 7 | int data; 8 | node* left; 9 | node* right; 10 | node(int d) 11 | { 12 | data = d; 13 | left = NULL; 14 | right = NULL; 15 | } 16 | }; 17 | node* buildtreeLevel() 18 | { 19 | queueq; 20 | int d; 21 | cin >> d; 22 | node* root = new node(d); 23 | q.push(root); 24 | int c1, c2; 25 | while (!q.empty()) 26 | { 27 | node* f = q.front(); 28 | q.pop(); 29 | cin >> c1 >> c2; 30 | if (c1 != -1) 31 | { 32 | f->left = new node(c1); 33 | q.push(f->left); 34 | } 35 | if (c2 != -1) 36 | { 37 | f->right = new node(c2); 38 | q.push(f->right); 39 | } 40 | } 41 | return root; 42 | } 43 | 44 | void leftView(node *root){ 45 | if(root==NULL){ 46 | return; 47 | } 48 | queueq; 49 | q.push(root); 50 | while(!q.empty()){ 51 | int n=q.size(); 52 | for(int i=1;i<=n;i++){ 53 | node* temp=q.front(); 54 | q.pop(); 55 | if(i==1){ 56 | cout<data<<" "; 57 | } 58 | if(temp->left!=NULL){ 59 | q.push(temp->left); 60 | } 61 | if(temp->right!=NULL){ 62 | q.push(temp->right); 63 | } 64 | } 65 | } 66 | } 67 | int main(){ 68 | node* root = buildtreeLevel(); 69 | leftView(root); 70 | } 71 | -------------------------------------------------------------------------------- /Bucket Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class student{ 5 | public: 6 | int marks; 7 | string name; 8 | }; 9 | //bucket sort to sort an array of students 10 | void bucket_sort(student s[],int n){ 11 | //assuming marks in range 0-100 12 | vector v[101]; 13 | for(int i=0;i=0;i--){ 20 | for(vector::iterator it=v[i].begin();it!=v[i].end();it++)//iterate over the vector 21 | 22 | cout<<(*it).marks<<" "<<(*it).name<>n; 35 | for(int i=0;i>s[i].marks>>s[i].name; //s.marks to access the marks and s.name is to access the name of the student s 37 | } 38 | bucket_sort(s,n); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Capitalize_First_Letter_of_All_Word.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | int main() 7 | { 8 | char str[200], ch; 9 | int len, i, asc_val; 10 | cout<<"Enter the String: "; 11 | gets(str); 12 | len = strlen(str); 13 | for(i=0; i=97 && asc_val<=122) 20 | { 21 | asc_val = asc_val-32; 22 | ch = asc_val; 23 | str[i] = ch; 24 | } 25 | } 26 | if(ch==' ') 27 | { 28 | ch = str[i+1]; 29 | asc_val = ch; 30 | if(asc_val>=97 && asc_val<=122) 31 | { 32 | asc_val = asc_val-32; 33 | ch = asc_val; 34 | str[i+1] = ch; 35 | } 36 | } 37 | } 38 | cout<<"\nAll words are capitalized successfully!"; 39 | cout<<"\nThe new string is:\n\n"; 40 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | int main() 7 | { 8 | char str[200], ch; 9 | int len, i, asc_val; 10 | cout<<"Enter the String: "; 11 | gets(str); 12 | len = strlen(str); 13 | for(i=0; i=97 && asc_val<=122) 20 | { 21 | asc_val = asc_val-32; 22 | ch = asc_val; 23 | str[i] = ch; 24 | } 25 | } 26 | if(ch==' ') 27 | { 28 | ch = str[i+1]; 29 | asc_val = ch; 30 | if(asc_val>=97 && asc_val<=122) 31 | { 32 | asc_val = asc_val-32; 33 | ch = asc_val; 34 | str[i+1] = ch; 35 | } 36 | } 37 | } 38 | cout<<"\nAll words are capitalized successfully!"; 39 | cout<<"\nThe new string is:\n\n"; 40 | cout< 2 | #define ll long long int 3 | #define MOD 1000000007 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>> t; 9 | while(t--) 10 | { 11 | ll a, b, n; 12 | cin>> a >> b >> n; 13 | ll ans = b; 14 | ll x; 15 | for(int i=0;i> x; 17 | ans += min(a-1, x); 18 | } 19 | 20 | cout << ans << endl; 21 | 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /Codeforces/B_Chips_on_the_Board.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long int 3 | #define MOD 1000000007 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>> t; 9 | while(t--) 10 | { 11 | int n; 12 | cin>> n; 13 | vector a(n), b(n); 14 | for(int i=0;i> a[i]; 17 | } 18 | for(int i=0;i> b[i]; 21 | } 22 | 23 | ll asum = 0, bsum = 0; 24 | ll amin = *min_element(a.begin(), a.end()); 25 | ll bmin = *min_element(b.begin(), b.end()); 26 | for(int i=0;ibsum) cout<< bsum << endl; 33 | else cout<< asum << endl; 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /Codeforces/B_Keep_it_Beautiful.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long int 3 | #define MOD 1000000007 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>> t; 9 | while(t--) 10 | { 11 | int q; 12 | cin>> q; 13 | vector query(n), temp; 14 | 15 | for(int i=0;i> query[i]; 18 | } 19 | 20 | string ans = ""; 21 | ll cnt = 0; 22 | for(int i=0;i= 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Codeforces/C_Ski_Resort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long int 3 | #define MOD 1000000007 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>> t; 9 | while(t--) 10 | { 11 | ll n, k, q; 12 | cin>> n >> k >> q; 13 | 14 | vector v(n); 15 | for(ll &i: v) cin>> i; 16 | 17 | ll ans = 0, length = 0; 18 | for(ll &i: v) { 19 | if(i<=q) { 20 | length++; 21 | } 22 | else { 23 | if(length >= k) { 24 | ans += (length - k + 1) * (length - k + 2) / 2; 25 | } 26 | length = 0; 27 | } 28 | } 29 | if (length >= k) 30 | { 31 | ans += (length - k + 1) * (length - k + 2) / 2; 32 | } 33 | 34 | cout<< ans < 2 | #define ll long long int 3 | #define MOD 1000000007 4 | using namespace std; 5 | int main() 6 | { 7 | 8 | int t; 9 | cin>> t; 10 | while(t--) 11 | { 12 | ll n, k, x; 13 | cin>> n >> x >> k; 14 | 15 | if(2*k>=x*(x+1) && 2*k<=n*(n+1)-(n-x)*(n-x+1)) { 16 | cout << "YES" << endl; 17 | } 18 | else cout << "NO" << endl; 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /Codeforces/Fedor_and_New_Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int bitdiff(int x,int y,int n){ 5 | int ct=0; 6 | int i=0; 7 | while(i<=n){ 8 | int a=x&1; 9 | int b=y&1; 10 | if(a!=b) ct++; 11 | x=x>>1; 12 | y=y>>1; 13 | i++; 14 | } 15 | return ct; 16 | } 17 | int main(){ 18 | int ct=0; 19 | int n,m,k; 20 | cin>>n>>m>>k; 21 | int a[m+1]; 22 | for(int i=0;i>a[i]; 24 | } 25 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int n,k; 7 | cin>>n>>k; 8 | string s; 9 | cin>>s; 10 | int hhh=223; 11 | int ct=0; 12 | for(int j=0;j 2 | using namespace std; 3 | int minS(long long int a,long long int b){ 4 | if(a>b) return b; 5 | else return a; 6 | } 7 | int main(){ 8 | long long int n,m; 9 | cin>>n>>m; 10 | 11 | 12 | long long int d=n-(m-1)-1; 13 | 14 | long long int max=d*(d+1)/2; 15 | 16 | long long int a=n/m; 17 | long long int b=n%m; 18 | 19 | long long int min=(a*(a+1)/2)*(n%m)+ ((a-1)*a/2)*(m-b); 20 | 21 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | long long int n,k; 7 | cin>>n>>k; 8 | long long int a[k]; 9 | long long int c[k]; 10 | for(long long int i=0;i>a[i]; 12 | } 13 | long long int sum=0; 14 | for(long long int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | #define ll long long int 9 | #define lt long long 10 | #define pi (3.141592653589) 11 | #define mod 1000000007 12 | 13 | #define tc \ 14 | int t; \ 15 | cin >> t; \ 16 | while (t--) 17 | 18 | #define lull NULL 19 | 20 | #define float double 21 | 22 | #define pb push_back 23 | 24 | #define mp make_pair 25 | 26 | #define ff first 27 | 28 | #define ss second 29 | 30 | #define all(c) c.begin(), c.end() 31 | 32 | #define min3(a, b, c) min(c, min(a, b)) 33 | 34 | #define min4(a, b, c, d) min(d, min(c, min(a, b))) 35 | 36 | #define fir(i, n) for (int i = n - 1; i >= 0; i--) 37 | 38 | #define fi(i, n) for (int i = 0; i < n; i++) 39 | #define fin(j, n) for (int i = j; i <= n; i++) 40 | 41 | #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); 42 | 43 | // function to convert Celsius 44 | // scale to Fahrenheit scale 45 | float Cel_To_Fah(float N) 46 | { 47 | return ((N * 9.0 / 5.0) + 32.0); 48 | } 49 | 50 | // Driver code 51 | int main() 52 | { 53 | float N = 20.0; 54 | 55 | // Function call 56 | cout << Cel_To_Fah(N); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /DNF Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void dnf_sort(int a[],int n){ 4 | //sort the array of 0's , 1's and 2's 5 | //initially 6 | int low=0; 7 | int high=n-1; 8 | int mid=0; 9 | while(mid<=high){ 10 | if(a[mid]==0){ 11 | swap(a[mid],a[low]); 12 | low++; 13 | mid++; 14 | } 15 | else if(a[mid]==1){ 16 | mid++; 17 | } 18 | else if(a[mid]==2){ 19 | swap(a[mid],a[high]); 20 | high--; 21 | 22 | } 23 | } 24 | return; 25 | 26 | } 27 | int main(){ 28 | int n; 29 | cin>>n; 30 | int a[100000]; 31 | for(int i=0;i>a[i]; 33 | } 34 | dnf_sort(a,n); 35 | 36 | for(int i=0;i 3 | using namespace std; 4 | int binary(int n){ 5 | int ans=0; 6 | int p=1; 7 | while(n>0){ 8 | int last_bit=(n&1); 9 | ans+=p*last_bit; 10 | p=p*10; 11 | n=n>>1; 12 | } 13 | return ans; 14 | 15 | } 16 | int main(){ 17 | int n; 18 | cin>>n; 19 | cout< cq; 42 | }; 43 | -------------------------------------------------------------------------------- /Digital_Clock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int h,m,s; 7 | 8 | printf("set time: \n"); 9 | scanf("%d%d%d",&h,&m,&s); 10 | if(h>12||m>60||s>60) 11 | { 12 | printf("error! \n"); 13 | } 14 | while(1) 15 | { 16 | s++; 17 | if(s>59) 18 | { 19 | m++; 20 | s=0; 21 | } 22 | if(m>59) 23 | { 24 | h++; 25 | m=0; 26 | } 27 | if(h>12) 28 | { 29 | h=1; 30 | } 31 | printf("Clock :"); 32 | printf("%02d:%02d:%02d",h,m,s); 33 | Sleep(1000); 34 | system("cls"); 35 | 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Digital_clock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int h,m,s; 7 | 8 | printf("set time: \n"); 9 | scanf("%d%d%d",&h,&m,&s); 10 | if(h>12||m>60||s>60) 11 | { 12 | printf("error! \n"); 13 | } 14 | while(1) 15 | { 16 | s++; 17 | if(s>59) 18 | { 19 | m++; 20 | s=0; 21 | } 22 | if(m>59) 23 | { 24 | h++; 25 | m=0; 26 | } 27 | if(h>12) 28 | { 29 | h=1; 30 | } 31 | printf("Clock :"); 32 | printf("%02d:%02d:%02d",h,m,s); 33 | Sleep(1000); 34 | system("cls"); 35 | 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Doubly Link List DS.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int info; 6 | struct node *lptr; 7 | struct node *rptr; 8 | }; 9 | struct node FIRST; 10 | void insertbeg() 11 | { 12 | struct node *newnode; 13 | printf("Enter Value to Insert: "); 14 | scanf("%d",&newnode->info); 15 | newnode->lptr; 16 | if(FIRST==NULL) 17 | { 18 | newnode->rptr=NULL; 19 | FIRST=newnode; 20 | } 21 | else 22 | { 23 | newnode->rptr=FIRST; 24 | FIRST=newnode; 25 | } 26 | printf("Node Inserted.") 27 | } 28 | void insertend() 29 | { 30 | struct node *newnode,*temp; 31 | printf("Enter Value to Insert: "); 32 | scanf("%d",&newnode->info); 33 | newnode->rptr=NULL; 34 | if(FIRST==NULL) 35 | { 36 | newnode->lptr=NULL; 37 | FIRST=newnode; 38 | } 39 | else 40 | { 41 | temp=FIRST; 42 | while(temp->rptr!=NULL) 43 | temp=temp->rptr; 44 | temp->rptr=newnode; 45 | newnode->lptr=temp; 46 | } 47 | printf("Node Inserted.s") 48 | } 49 | void deletebeg() 50 | { 51 | if(FIRST==NULL) 52 | printf("List is Empty.") 53 | else if(FIRST->rptr==NULL) 54 | { 55 | printf("Deleted node: %d",FIRST->info); 56 | FIRST=NULL; 57 | } 58 | else 59 | { 60 | printf("Deleted node: %d",FIRST->info); 61 | FIRST=FIRST->rptr; 62 | FIRST->lptr=NULL 63 | } 64 | } 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /Factorial: -------------------------------------------------------------------------------- 1 | # Python program to find the factorial of a number provided by the user. 2 | 3 | # change the value for a different result 4 | num = 7 5 | 6 | # To take input from the user 7 | #num = int(input("Enter a number: ")) 8 | 9 | factorial = 1 10 | 11 | # check if the number is negative, positive or zero 12 | if num < 0: 13 | print("Sorry, factorial does not exist for negative numbers") 14 | elif num == 0: 15 | print("The factorial of 0 is 1") 16 | else: 17 | for i in range(1,num + 1): 18 | factorial = factorial*i 19 | print("The factorial of",num,"is",factorial) 20 | -------------------------------------------------------------------------------- /Find_SecLargest_Element_using_for_Loop: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main() 5 | { 6 | int i, arr[10], large, slarge; 7 | cout<<"Enter 10 Array Elements: "; 8 | for(i=0; i<10; i++) 9 | cin>>arr[i]; 10 | large = arr[0]; 11 | for(i=0; i<10; i++) 12 | { 13 | if(largedata); 22 | } 23 | } 24 | int main() 25 | { 26 | int a; 27 | struct node *head=NULL,*n1=NULL,*n2=NULL,*n3=NULL; 28 | head=(struct node *)malloc(sizeof(struct node*)); 29 | n1=(struct node *)malloc(sizeof(struct node)); 30 | n2=(struct node *)malloc(sizeof(struct node)); 31 | n3=(struct node *)malloc(sizeof(struct node)); 32 | if(head==NULL) 33 | { 34 | printf("no memory available"); 35 | } 36 | else 37 | { 38 | head->link=n1; 39 | n1->data=5; 40 | n1->link=n2; 41 | n2->data=6; 42 | n2->link=n3; 43 | n3->data=7; 44 | n3->link=NULL; 45 | insertb(head,43); 46 | display(head); 47 | return 0; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Insertion_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int arr[50], tot, i, j, k, elem, index; 6 | cout<<"Enter the Size for Array: "; 7 | cin>>tot; 8 | cout<<"Enter "<>arr[i]; 11 | for(i=1; ij; k--) 22 | arr[k] = arr[k-1]; 23 | break; 24 | } 25 | } 26 | } 27 | else 28 | continue; 29 | arr[index] = elem; 30 | } 31 | cout<<"\nThe New Array (Sorted Array):\n"; 32 | for(i=0; i &arr) { 4 | int sq = arr[0]*arr[0] + arr[1]*arr[1]; 5 | return sqrt(sq); 6 | } 7 | public: 8 | vector> kClosest(vector> &points, int k) { 9 | int n = points.size(); 10 | if (k == n) return points; 11 | vector> res; 12 | map> mp; 13 | // O(n * log(n)) time 14 | for (int i = 0; i < n; i++) { 15 | double d = dist(points[i]); 16 | mp[d].push_back(i); 17 | } 18 | // O(k) time 19 | for (auto it = mp.begin(); it != mp.end(); it++) { 20 | vector vec = it->second; 21 | for (int &x : vec) { 22 | res.push_back(points[x]); 23 | if (k == res.size()) return res; 24 | } 25 | } 26 | return {}; 27 | } 28 | }; 29 | 30 | 31 | /* 32 | Time Complexity : O(n * log(n)) + O(k) 33 | Space Complexity : O(n) + O(k) 34 | */ 35 | -------------------------------------------------------------------------------- /Largestrectangleinhistogram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestRectangleArea(vector& heights) { 4 | stack st; 5 | int ans=0; 6 | 7 | heights.push_back(0); 8 | 9 | for(int i=0;iheights[i]){ 11 | 12 | any 13 | 14 | int top=heights[st.top()]; 15 | st.pop(); 16 | int ran=st.empty()?-1:st.top(); 17 | 18 | ans=max(ans,top*(i-ran-1)); 19 | 20 | } 21 | st.push(i); 22 | 23 | } 24 | return ans; 25 | 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Linearsearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | /* 6 | 7 | Time Complexity : O(N) 8 | 9 | Space Complexity : O(1) 10 | 11 | */ 12 | 13 | 14 | 15 | void main() 16 | 17 | { 18 | 19 | float s=clock(); 20 | int arr[100],c,search,n; 21 | 22 | printf("Enter the size of array"); 23 | 24 | scanf("%d",&n); 25 | 26 | printf("Enter %d element in array",n); 27 | 28 | for(c=0;c 2 | using namespace std; 3 | class node{ 4 | public: 5 | int data; 6 | node *next; 7 | node(int d){ 8 | data=d; 9 | next=NULL; 10 | } 11 | }; 12 | 13 | 14 | 15 | void insert_at_head(node *&head,int d){ 16 | if(head==NULL){ 17 | head=new node(d); 18 | return; 19 | } 20 | node *n=new node(d); 21 | n->next=head; 22 | head=n; 23 | 24 | } 25 | void print(node *head){ 26 | while(head!=NULL){ 27 | cout<data<<"--->"; 28 | head=head->next; 29 | } 30 | } 31 | //taking input 32 | node* take_input(){ 33 | int d; 34 | cin>>d; 35 | node *head=NULL; 36 | while(d!=-1){ 37 | insert_at_head(head,d); 38 | cin>>d; 39 | 40 | } 41 | return head; 42 | } 43 | int main(){ 44 | node *head=take_input(); 45 | 46 | 47 | print(head); 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Mapped strings.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ravya1108/hactoberfest2023/1c1375739dce28f02f945279349091b28df46506/Mapped strings.cpp -------------------------------------------------------------------------------- /Merge sort algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | void merge(ll int a[],int s,ll int e){ 5 | ll int mid=(s+e)/2; 6 | ll int i=s; 7 | ll int j=mid+1; 8 | ll int k=s; 9 | ll int temp[e+1]; 10 | while(i<=mid && j<=e){ 11 | 12 | 13 | if(a[i]=e){ 34 | return; 35 | } 36 | //follow 3 steps 37 | //1. divide 38 | ll int mid=(s+e)/2; 39 | //2. recursively sort two arrays - s,mid and mid+1,e 40 | merge_sort(a,s,mid); 41 | merge_sort(a,mid+1,e); 42 | //3. merge the two parts 43 | merge(a,s,e); 44 | 45 | 46 | } 47 | int main(){ 48 | ll int n; 49 | cin>>n; 50 | ll int a[n]; 51 | for(ll int i=0;i>a[i]; 53 | } 54 | merge_sort(a,0,n-1); 55 | for(ll int i=0;i q; 19 | q.push(root); 20 | while(!q.empty()){ 21 | int s=q.size(); 22 | ans++; 23 | while(s--){ 24 | TreeNode* node=q.front(); 25 | q.pop(); 26 | if(!node->left && !node->right) 27 | return ans; 28 | if(node->left) 29 | q.push(node->left); 30 | if(node->right) 31 | q.push(node->right); 32 | } 33 | } 34 | return ans; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /Minimum/Maximum Falling Path Sum: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int getMaxUtil(int i, int j, int m, vector> &matrix, 6 | vector > &dp){ 7 | 8 | // Base Conditions 9 | if(j<0 || j>=m) 10 | return -1e9; 11 | if(i==0) 12 | return matrix[0][j]; 13 | 14 | if(dp[i][j]!=-1) return dp[i][j]; 15 | 16 | int up = matrix[i][j] + getMaxUtil(i-1,j,m,matrix,dp); 17 | int leftDiagonal = matrix[i][j] + getMaxUtil(i-1,j-1,m,matrix,dp); 18 | int rightDiagonal = matrix[i][j] + getMaxUtil(i-1,j+1,m,matrix,dp); 19 | 20 | return dp[i][j]= max(up,max(leftDiagonal,rightDiagonal)); 21 | 22 | } 23 | 24 | int getMaxPathSum(vector > &matrix){ 25 | 26 | int n = matrix.size(); 27 | int m = matrix[0].size(); 28 | 29 | vector> dp(n,vector(m,-1)); 30 | 31 | int maxi = INT_MIN; 32 | 33 | for(int j=0; j > matrix{{1,2,10,4}, 44 | {100,3,2,1}, 45 | {1,1,20,2}, 46 | {1,2,2,1}}; 47 | 48 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int r,o,c,score,max; 6 | cin>>r>>o>>c; 7 | score=r-c+1; 8 | max=(20-o)*36; 9 | if(max 2 | using namespace std; 3 | int main() 4 | { 5 | int first, last, temp, noOfDigit, num, res=0, rem, pow, i; 6 | first = 1; 7 | last = 500; 8 | cout<<"Armstrong Numbers Between 1 and 500\n"; 9 | while(first<=last) 10 | { 11 | temp = first; 12 | noOfDigit = 0; 13 | while(temp>0) 14 | { 15 | temp = temp/10; 16 | noOfDigit++; 17 | } 18 | num = first; 19 | while(num>0) 20 | { 21 | rem = num%10; 22 | pow = 1; 23 | i = 0; 24 | while(i 2 | using namespace std; 3 | int main() 4 | { 5 | int a=0, b=1, c, tot, temp, i; 6 | cout<<"Enter the Size of Fibonacci Sequence: "; 7 | cin>>tot; 8 | cout<<"\nFibonacci Series of "< 2 | using namespace std; 3 | int main() 4 | { 5 | int i, j, num=1; 6 | for(i=0; i<5; i++) 7 | { 8 | for(j=0; j<=i; j++) 9 | { 10 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int row, col, i=1, j=0, arr[5], arrTemp[5]; 6 | arr[0] = 1; 7 | arr[1] = 1; 8 | for(row=0; row<5; row++) 9 | { 10 | for(col=4; col>row; col--) 11 | cout<<" "; 12 | for(col=0; col<=row; col++) 13 | { 14 | if(row==0) 15 | cout<<"1"; 16 | else 17 | { 18 | if(col==0 || col==row) 19 | cout<<"1 "; 20 | else 21 | { 22 | arrTemp[i] = arr[j]+arr[j+1]; 23 | cout<1) 32 | { 33 | j=0; 34 | arr[j]=1; 35 | for(j=1, i=1; j<=row; j++, i++) 36 | arr[j] = arrTemp[i]; 37 | i=1; 38 | j=0; 39 | } 40 | } 41 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | long long fact=1, numerator, denominator; 6 | int perm, n, r, i=1, sub; 7 | cout<<"Enter the Value of n: "; 8 | cin>>n; 9 | cout<<"Enter the Value of r: "; 10 | cin>>r; 11 | while(i<=n) 12 | { 13 | fact = i*fact; 14 | i++; 15 | } 16 | numerator = fact; // n! 17 | sub = n-r; 18 | fact = 1; 19 | i = 1; 20 | while(i<=sub) 21 | { 22 | fact = i*fact; 23 | i++; 24 | } 25 | denominator = fact; // (n-r)! 26 | perm = numerator/denominator; 27 | cout<<"\nPermutation (nPr) = "< 2 | using namespace std; 3 | int main() 4 | { 5 | int first, last, temp, noOfDigit, num, res=0, rem, pow, i; 6 | first = 1; 7 | last = 500; 8 | cout<<"Armstrong Numbers Between 1 and 500\n"; 9 | while(first<=last) 10 | { 11 | temp = first; 12 | noOfDigit = 0; 13 | while(temp>0) 14 | { 15 | temp = temp/10; 16 | noOfDigit++; 17 | } 18 | num = first; 19 | while(num>0) 20 | { 21 | rem = num%10; 22 | pow = 1; 23 | i = 0; 24 | while(i 2 | #include 3 | void main() 4 | { 5 | clrscr(); 6 | 7 | system("C:\\Windows\\System32\\ipconfig"); 8 | 9 | getch(); 10 | } 11 | -------------------------------------------------------------------------------- /Python/Countdown Timer.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | def countdown(time_sec): 4 | while time_sec: 5 | mins, secs = divmod(time_sec, 60) 6 | timeformat = '{:02d}:{:02d}'.format(mins, secs) 7 | print(timeformat, end='\r') 8 | time.sleep(1) 9 | time_sec -= 1 10 | 11 | print("stop") 12 | 13 | countdown(5) -------------------------------------------------------------------------------- /Python/Dots-and-Boxes/images/icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ravya1108/hactoberfest2023/1c1375739dce28f02f945279349091b28df46506/Python/Dots-and-Boxes/images/icon.ico -------------------------------------------------------------------------------- /Python/Dots-and-Boxes/images/movie.mov: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ravya1108/hactoberfest2023/1c1375739dce28f02f945279349091b28df46506/Python/Dots-and-Boxes/images/movie.mov -------------------------------------------------------------------------------- /Python/Dots-and-Boxes/images/preview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ravya1108/hactoberfest2023/1c1375739dce28f02f945279349091b28df46506/Python/Dots-and-Boxes/images/preview.gif -------------------------------------------------------------------------------- /Python/Dots-and-Boxes/images/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ravya1108/hactoberfest2023/1c1375739dce28f02f945279349091b28df46506/Python/Dots-and-Boxes/images/screenshot.png -------------------------------------------------------------------------------- /Python/Fibonacci.py: -------------------------------------------------------------------------------- 1 | # Program to display the Fibonacci sequence up to n-th term 2 | 3 | nterms = int(input("How many terms? ")) 4 | 5 | # first two terms 6 | n1, n2 = 0, 1 7 | count = 0 8 | 9 | # check if the number of terms is valid 10 | if nterms <= 0: 11 | print("Please enter a positive integer") 12 | # if there is only one term, return n1 13 | elif nterms == 1: 14 | print("Fibonacci sequence upto",nterms,":") 15 | print(n1) 16 | # generate fibonacci sequence 17 | else: 18 | print("Fibonacci sequence:") 19 | while count < nterms: 20 | print(n1) 21 | nth = n1 + n2 22 | # update values 23 | n1 = n2 24 | n2 = nth 25 | count += 1 -------------------------------------------------------------------------------- /Python/Flower Design in Python: -------------------------------------------------------------------------------- 1 | import turtle 2 | 3 | 4 | tur = turtle.Turtle() 5 | tur.speed(20) 6 | tur.color("black", "orange") 7 | tur.begin_fill() 8 | 9 | for i in range(50): 10 | tur.forward(300) 11 | tur.left(170) 12 | 13 | tur.end_fill() 14 | turtle.done() 15 | -------------------------------------------------------------------------------- /Python/Hi: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Python/Merge Mails.py: -------------------------------------------------------------------------------- 1 | # Python program to mail merger 2 | # Names are in the file names.txt 3 | # Body of the mail is in body.txt 4 | 5 | # open names.txt for reading 6 | with open("names.txt", 'r', encoding='utf-8') as names_file: 7 | 8 | # open body.txt for reading 9 | with open("body.txt", 'r', encoding='utf-8') as body_file: 10 | 11 | # read entire content of the body 12 | body = body_file.read() 13 | 14 | # iterate over names 15 | for name in names_file: 16 | mail = "Hello " + name.strip() + "\n" + body 17 | 18 | # write the mails to individual files 19 | with open(name.strip()+".txt", 'w', encoding='utf-8') as mail_file: 20 | mail_file.write(mail) -------------------------------------------------------------------------------- /Python/Quad Eq.py: -------------------------------------------------------------------------------- 1 | # Solve the quadratic equation ax**2 + bx + c = 0 2 | 3 | # import complex math module 4 | import cmath 5 | 6 | a = 1 7 | b = 5 8 | c = 6 9 | 10 | # calculate the discriminant 11 | d = (b**2) - (4*a*c) 12 | 13 | # find two solutions 14 | sol1 = (-b-cmath.sqrt(d))/(2*a) 15 | sol2 = (-b+cmath.sqrt(d))/(2*a) 16 | 17 | print('The solution are {0} and {1}'.format(sol1,sol2)) -------------------------------------------------------------------------------- /Python/Rock_Paper_Scissor_Game.py: -------------------------------------------------------------------------------- 1 | def gameWin(comp, you): 2 | if comp == you: 3 | return None 4 | elif comp == 's': 5 | if you == 'p': 6 | return False 7 | elif you == 'r': 8 | return True 9 | elif comp == 'p': 10 | if you == 'r': 11 | return False 12 | elif you == 's': 13 | return True 14 | elif comp == 'r': 15 | if you == 's': 16 | return False 17 | elif you == 'p': 18 | return True 19 | print("Comp Turn: Rock(r) Paper(p) Scissor(s) ?") 20 | import random 21 | randNo = random.randint(1, 3) 22 | if randNo == 1: 23 | comp = 'r' 24 | elif randNo == 2: 25 | comp = 'p' 26 | elif randNo == 3: 27 | comp = 's' 28 | print("Computer has chosen") 29 | you = input("Your Turn: Rock(r) Paper(p) Scissor(s) ?\n") 30 | a = gameWin(comp, you) 31 | print(f"Computer chose {comp}") 32 | print(f"You chose {you}") 33 | if a == None: 34 | print("The game is a tie!") 35 | elif a == True: 36 | print("You Win!") 37 | else: 38 | print("You Lose!") 39 | -------------------------------------------------------------------------------- /Python/Shuffle Cards.py: -------------------------------------------------------------------------------- 1 | # Python program to shuffle a deck of card 2 | 3 | # importing modules 4 | import itertools, random 5 | 6 | # make a deck of cards 7 | deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club'])) 8 | 9 | # shuffle the cards 10 | random.shuffle(deck) 11 | 12 | # draw five cards 13 | print("You got:") 14 | for i in range(5): 15 | print(deck[i][0], "of", deck[i][1]) -------------------------------------------------------------------------------- /Python/animal quiz.py: -------------------------------------------------------------------------------- 1 | def check_guess(guess, answer): 2 | global score 3 | still_guessing = True 4 | attempt = 0 5 | while still_guessing and attempt < 3: 6 | if guess.lower() == answer.lower(): 7 | print("Correct Answer") 8 | score = score + 1 9 | still_guessing = False 10 | else: 11 | if attempt < 2: 12 | guess = input("Sorry Wrong Answer, try again") 13 | attempt = attempt + 1 14 | if attempt == 3: 15 | print("The Correct answer is ",answer ) 16 | 17 | score = 0 18 | print("Guess the Animal") 19 | guess1 = input("Which bear lives at the North Pole? ") 20 | check_guess(guess1, "polar bear") 21 | guess2 = input("Which is the fastest land animal? ") 22 | check_guess(guess2, "Cheetah") 23 | guess3 = input("Which is the larget animal? ") 24 | check_guess(guess3, "Blue Whale") 25 | print("Your Score is "+ str(score)) 26 | 27 | -------------------------------------------------------------------------------- /Python/armstrong using python.py: -------------------------------------------------------------------------------- 1 | # Python program to determine whether 2 | # the number is Armstrong number or not 3 | 4 | # Function to calculate x raised to 5 | # the power y 6 | def power(x, y): 7 | 8 | if y == 0: 9 | return 1 10 | if y % 2 == 0: 11 | return power(x, y // 2) * power(x, y // 2) 12 | 13 | return x * power(x, y // 2) * power(x, y // 2) 14 | 15 | # Function to calculate order of the number 16 | def order(x): 17 | 18 | # Variable to store of the number 19 | n = 0 20 | while (x != 0): 21 | n = n + 1 22 | x = x // 10 23 | 24 | return n 25 | 26 | # Function to check whether the given 27 | # number is Armstrong number or not 28 | def isArmstrong(x): 29 | 30 | n = order(x) 31 | temp = x 32 | sum1 = 0 33 | 34 | while (temp != 0): 35 | r = temp % 10 36 | sum1 = sum1 + power(r, n) 37 | temp = temp // 10 38 | 39 | # If condition satisfies 40 | return (sum1 == x) 41 | 42 | # Driver code 43 | x = 153 44 | print(isArmstrong(x)) 45 | 46 | x = 1253 47 | print(isArmstrong(x)) -------------------------------------------------------------------------------- /Python/calender.py: -------------------------------------------------------------------------------- 1 | import calendar 2 | year =int( input("Enter the year of the required calendar ")) 3 | month = int( input("Enter the month of the required calendar ")) 4 | print(calendar.month(year,month)) 5 | -------------------------------------------------------------------------------- /Python/dice roll simulator.py: -------------------------------------------------------------------------------- 1 | 2 | #importing module for random number generation 3 | import random 4 | 5 | #range of the values of a dice 6 | min_val = 1 7 | max_val = 6 8 | 9 | #to loop the rolling through user input 10 | roll_again = "yes" 11 | 12 | #loop 13 | while roll_again == "yes" or roll_again == "y": 14 | print("Rolling The Dices...") 15 | print("The Values are :") 16 | 17 | #generating and printing 1st random integer from 1 to 6 18 | print(random.randint(min_val, max_val)) 19 | 20 | #generating and printing 2nd random integer from 1 to 6 21 | print(random.randint(min_val, max_val)) 22 | 23 | #asking user to roll the dice again. Any input other than yes or y will terminate the loop 24 | roll_again = input("Roll the Dices Again?") 25 | -------------------------------------------------------------------------------- /Python/fidget spinner.py: -------------------------------------------------------------------------------- 1 | from turtle import * 2 | state = {'turn': 0} 3 | def spinner(): 4 | clear() 5 | angle = state['turn']/10 6 | right(angle) 7 | forward(100) 8 | dot(120, 'yellow') 9 | back(100) 10 | right(120) 11 | forward(100) 12 | dot(120, 'green') 13 | back(100) 14 | right(120) 15 | forward(100) 16 | dot(120, 'purple') 17 | back(100) 18 | right(120) 19 | update() 20 | def animate(): 21 | if state['turn']>0: 22 | state['turn']-=1 23 | 24 | spinner() 25 | ontimer(animate, 20) 26 | def flick(): 27 | state['turn']+=10 28 | 29 | setup(420, 420, 370, 0) 30 | hideturtle() 31 | tracer(False) 32 | width(20) 33 | onkey(flick, 'space') 34 | listen() 35 | animate() 36 | done() 37 | -------------------------------------------------------------------------------- /Python/first-letter-capatalisation.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | def First_Letter_Capatilise(ls): 4 | ls = ls.split(' ') 5 | final = '' 6 | b = 1 7 | for i in range(len(ls)): 8 | if b!=1: 9 | final+=' ' 10 | a = 1 11 | b+=1 12 | for j in ls[i]: 13 | if a==1: 14 | final+=j.upper() 15 | else: 16 | final+=j 17 | a+=1 18 | return final 19 | 20 | a = input('Enter File Whole Path:') 21 | a = a[1:len(a)-1] 22 | final = [] 23 | 24 | with open(fr"{a}") as t: 25 | temp = t.readlines() 26 | for i in temp: 27 | _ = First_Letter_Capatilise(i) 28 | final.append(_) 29 | 30 | with open(fr"{a}", 'w') as t: 31 | t.write('') 32 | 33 | with open(fr"{a}", 'a') as t: 34 | for i in final: 35 | t.write(i) 36 | -------------------------------------------------------------------------------- /Python/guess_game.py: -------------------------------------------------------------------------------- 1 | 2 | '''Welcome to guess game 3 | Here you have to guess 4 | a 4 digit number and you 5 | will be given 10 chances 6 | Good Luck''' 7 | 8 | import random 9 | def computer_choice(): 10 | choice1 = str(random_num) 11 | choice2 = map(int, choice1) # function to convert the random no generated to list 12 | choice_list = list(choice2) 13 | return choice_list 14 | 15 | def userinput(): 16 | input1 = int(input("Enter Your Guess")) 17 | input2 = str(input1) 18 | input3 = map(int, input2) 19 | input_list = list(input3) 20 | return input_list 21 | 22 | random_num = random .randint(1000, 9999) # 4 digit code generated 23 | 24 | a = computer_choice() 25 | i = 0 26 | while (i < 10): 27 | result = "" 28 | b = userinput() 29 | 30 | if len(b) != 4: 31 | print("As it is already asked to enter only 4 digit number") 32 | continue 33 | if (b == a): 34 | print("You guessed right !", a) 35 | break 36 | for elements in b: 37 | if elements in a: 38 | if b.index(elements) == a.index(elements): 39 | result += "R" 40 | else: 41 | result += "A" 42 | else: 43 | result += "W" 44 | print(result) 45 | i += 1 46 | 47 | else: 48 | print("You are out of choices", a) 49 | 50 | -------------------------------------------------------------------------------- /Python/leap year.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | if year % 4 == 0: 3 | if year % 100 == 0: 4 | if year % 400 == 0: 5 | return True 6 | else: 7 | return False 8 | else: 9 | return True 10 | else: 11 | return False 12 | 13 | 14 | def days_in_month(year, month): 15 | month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 16 | 17 | if is_leap(year) and month == 2: 18 | return 29 19 | return month_days[month - 1] 20 | 21 | 22 | # 🚨 Do NOT change any of the code below 23 | year = int(input("Enter a year: ")) 24 | month = int(input("Enter a month: ")) 25 | days = days_in_month(year, month) 26 | print(days) -------------------------------------------------------------------------------- /Python/love_calculator.py: -------------------------------------------------------------------------------- 1 | # 🚨 Don't change the code below 👇 2 | print("Welcome to the Love Calculator!") 3 | name1 = input("What is your name? \n") 4 | name2 = input("What is their name? \n") 5 | # 🚨 Don't change the code above 👆 6 | 7 | #Write your code below this line 👇 8 | 9 | 10 | final_name1 = name1.lower() 11 | final_name2 = name2.lower() 12 | concate = final_name1 + final_name2 13 | 14 | #for true 15 | t = int(concate.count("t")) 16 | r = int(concate.count("r")) 17 | u = int(concate.count("u")) 18 | e = int(concate.count("e")) 19 | true = t + r + u + e 20 | 21 | #for love 22 | l = int(concate.count("l")) 23 | o = int(concate.count("o")) 24 | v = int(concate.count("v")) 25 | e = int(concate.count("e")) 26 | love = l + o + v + e 27 | 28 | love_score = str(true) + str(love) 29 | 30 | if int(love_score) > 90 or int(love_score) < 10: 31 | print(f"Your score is {love_score}, you go together like coke and mentos.") 32 | elif int(love_score) > 40 and int(love_score) < 50: 33 | print(f"Your score is {love_score}, you are alright together.") 34 | else: 35 | print(f"Your score is {love_score}.") -------------------------------------------------------------------------------- /Python/matrix using loop.py: -------------------------------------------------------------------------------- 1 | # Python3 code to demonstrate working of 2 | # Vertical Concatenation in Matrix 3 | # Using loop 4 | 5 | # initializing lists 6 | test_list = [["Gfg", "good"], ["is", "for"], ["Best"]] 7 | 8 | # printing original list 9 | print("The original list : " + str(test_list)) 10 | 11 | # using loop for iteration 12 | res = [] 13 | N = 0 14 | while N != len(test_list): 15 | temp = '' 16 | for idx in test_list: 17 | 18 | # checking for valid index / column 19 | try: temp = temp + idx[N] 20 | except IndexError: pass 21 | res.append(temp) 22 | N = N + 1 23 | 24 | res = [ele for ele in res if ele] 25 | 26 | # printing result 27 | print("List after column Concatenation : " + str(res)) -------------------------------------------------------------------------------- /Python/pallindrome or not.py: -------------------------------------------------------------------------------- 1 | # function to check string is 2 | # palindrome or not 3 | def isPalindrome(str): 4 | 5 | # Run loop from 0 to len/2 6 | for i in range(0, int(len(str)/2)): 7 | if str[i] != str[len(str)-i-1]: 8 | return False 9 | return True 10 | 11 | # main function 12 | s = "malayalam" 13 | ans = isPalindrome(s) 14 | 15 | if (ans): 16 | print("Yes") 17 | else: 18 | print("No") -------------------------------------------------------------------------------- /Python/popular_course.py: -------------------------------------------------------------------------------- 1 | #a) 2 | 3 | def numbers(Students): 4 | list = [] 5 | for i in range(len(Students)): 6 | for j in range(len(Students[i])): 7 | list.append(Students[i][j]) 8 | 9 | n = {} 10 | for elements in list: 11 | 12 | n = {elements:list.count(elements) for elements in list} 13 | 14 | return n 15 | 16 | 17 | #b) 18 | 19 | def popular(n): 20 | popular_course = max(n , key = n.get) 21 | return popular_course 22 | 23 | 24 | 25 | Students = [['math', 'phy', 'chem', 'cs'], ['math', 'phy'], ['math', 'chem'], ['history', 'eco']] 26 | a = numbers(Students) 27 | print(a) 28 | b = popular(a) 29 | print("The most popular course is: ",b) -------------------------------------------------------------------------------- /Python/prime number in an interval.py: -------------------------------------------------------------------------------- 1 | def prime(x, y): 2 | prime_list = [] 3 | for i in range(x, y): 4 | if i == 0 or i == 1: 5 | continue 6 | else: 7 | for j in range(2, int(i/2)+1): 8 | if i % j == 0: 9 | break 10 | else: 11 | prime_list.append(i) 12 | return prime_list 13 | 14 | # Driver program 15 | starting_range = 2 16 | ending_range = 7 17 | lst = prime(starting_range, ending_range) 18 | if len(lst) == 0: 19 | print("There are no prime numbers in this range") 20 | else: 21 | print("The prime numbers in this range are: ", lst) -------------------------------------------------------------------------------- /Python/rockpaperscissors.py: -------------------------------------------------------------------------------- 1 | import random 2 | a=['Rock','Paper','Scissors'] 3 | print("Enter your choice") 4 | inp=input() 5 | res=random.choice(a) 6 | print("Computer Choice is: ",res) 7 | print("THEREFORE:") 8 | if(res==inp): 9 | print("DRAW") 10 | else: 11 | if((res.upper()=='ROCK' and inp.upper()=='PAPER') or (res.upper()=='SCISSORS' and inp.upper()=='ROCK') or (res.upper()=='PAPER' and inp.upper()=='SCISSORS')): 12 | print("YOU WIN") 13 | else: 14 | print("YOU LOSE") -------------------------------------------------------------------------------- /Python/square of natural number.py: -------------------------------------------------------------------------------- 1 | # Python3 Program to 2 | # find sum of square 3 | # of first n natural 4 | # numbers 5 | 6 | 7 | # Return the sum of 8 | # square of first n 9 | # natural numbers 10 | def squaresum(n) : 11 | 12 | # Iterate i from 1 13 | # and n finding 14 | # square of i and 15 | # add to sum. 16 | sm = 0 17 | for i in range(1, n+1) : 18 | sm = sm + (i * i) 19 | 20 | return sm 21 | 22 | # Driven Program 23 | n = 4 24 | print(squaresum(n)) -------------------------------------------------------------------------------- /Python/stderr.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ravya1108/hactoberfest2023/1c1375739dce28f02f945279349091b28df46506/Python/stderr.txt -------------------------------------------------------------------------------- /Python/stdout.txt: -------------------------------------------------------------------------------- 1 | DOSBox version 0.74 2 | Copyright 2002-2010 DOSBox Team, published under GNU GPL. 3 | --- 4 | CONFIG:Loading primary settings from config file C:\TURBOC3\dosbox-2.0.conf 5 | Memory sizes above 31 MB are NOT recommended. 6 | Stick with the default values unless you are absolutely certain. 7 | MIDI:Opened device:win32 8 | MAPPER: Loading mapper settings from C:\TURBOC3\mapper-2.0.map 9 | -------------------------------------------------------------------------------- /Python/sudoku solver/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ravya1108/hactoberfest2023/1c1375739dce28f02f945279349091b28df46506/Python/sudoku solver/icon.png -------------------------------------------------------------------------------- /Radix_Sort: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | int getMax(int array[], int n) { 6 | int max = array[0]; 7 | for (int i = 1; i < n; i++) 8 | if (array[i] > max) 9 | max = array[i]; 10 | return max; 11 | } 12 | 13 | void countingSort(int array[], int size, int place) { 14 | int output[size + 1]; 15 | int max = (array[0] / place) % 10; 16 | 17 | for (int i = 1; i < size; i++) { 18 | if (((array[i] / place) % 10) > max) 19 | max = array[i]; 20 | } 21 | int count[max + 1]; 22 | 23 | for (int i = 0; i < max; ++i) 24 | count[i] = 0; 25 | 26 | for (int i = 0; i < size; i++) 27 | count[(array[i] / place) % 10]++; 28 | 29 | for (int i = 1; i < 10; i++) 30 | count[i] += count[i - 1]; 31 | 32 | for (int i = size - 1; i >= 0; i--) { 33 | output[count[(array[i] / place) % 10] - 1] = array[i]; 34 | count[(array[i] / place) % 10]--; 35 | } 36 | 37 | for (int i = 0; i < size; i++) 38 | array[i] = output[i]; 39 | } 40 | 41 | void radixsort(int array[], int size) { 42 | int max = getMax(array, size); 43 | 44 | for (int place = 1; max / place > 0; place *= 10) 45 | countingSort(array, size, place); 46 | } 47 | void printArray(int array[], int size) { 48 | for (int i = 0; i < size; ++i) { 49 | printf("%d ", array[i]); 50 | } 51 | printf("\n"); 52 | } 53 | 54 | int main() { 55 | int array[] = {121, 432, 564, 23, 1, 45, 788}; 56 | int n = sizeof(array) / sizeof(array[0]); 57 | radixsort(array, n); 58 | printArray(array, n); 59 | } 60 | -------------------------------------------------------------------------------- /Ransom Note Leetcode: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canConstruct(string ransomNote, string magazine) { 4 | sort(ransomNote.begin(),ransomNote.end()); 5 | sort(magazine.begin(),magazine.end()); 6 | int i=0,j=0,c=0; 7 | while(i 2 | using namespace std; 3 | void search_array(float arr[],int no_elements,float element) 4 | { int flag=0; 5 | if(no_elements==0) 6 | { cout<<"Invalid, connot Search\n"; 7 | exit; 8 | } 9 | else 10 | { for(int i=0;i>s; 28 | cout<<"Enter the elements of the array: "<>arr[i]; 31 | cout<<"Enter the element you want to search : "; 32 | cin>>f; 33 | search_array(arr,s,f); 34 | cout< 2 | using namespace std; 3 | int secondSmallest(int arr[],int n) 4 | { 5 | if(n<2) 6 | return -1; 7 | int small = INT_MAX; 8 | int second_small = INT_MAX; 9 | int i; 10 | for(i = 0; i < n; i++) 11 | { 12 | if(arr[i] < small) 13 | { 14 | second_small = small; 15 | small = arr[i]; 16 | } 17 | else if(arr[i] < second_small && arr[i] != small) 18 | { 19 | second_small = arr[i]; 20 | } 21 | } 22 | return second_small; 23 | } 24 | int secondLargest(int arr[],int n) 25 | { 26 | if(n<2) 27 | return -1; 28 | int large=INT_MIN,second_large=INT_MIN; 29 | int i; 30 | for (i = 0; i < n; i++) 31 | { 32 | if (arr[i] > large) 33 | { 34 | second_large = large; 35 | large = arr[i]; 36 | } 37 | 38 | else if (arr[i] > second_large && arr[i] != large) 39 | { 40 | second_large = arr[i]; 41 | } 42 | } 43 | return second_large; 44 | } 45 | 46 | int main() { 47 | int arr[]={1,2,4,7,7,5}; 48 | int n=sizeof(arr)/sizeof(arr[0]); 49 | int sS=secondSmallest(arr,n); 50 | int sL=secondLargest(arr,n); 51 | cout<<"Second smallest is "<& nums) { 15 | return helper(nums, 0, nums.size()-1); 16 | } 17 | TreeNode* helper(vector& nums, int left, int right){ 18 | 19 | if(left > right){ 20 | return NULL; 21 | } 22 | int mid = (left + right) / 2; 23 | TreeNode* temp = new TreeNode(nums[mid]); 24 | temp->left = helper(nums, left, mid-1); 25 | temp->right = helper(nums, mid+1 , right); 26 | return temp; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Spiral_Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n,m; 6 | cin>>n>>m; 7 | int a[n][m]; 8 | for(int i=0;i>a[i][j]; 13 | } 14 | } 15 | int row_start=0,col_start=0,row_end=n-1,col_end=m-1; 16 | while(row_start<=row_end && col_start<=col_end) 17 | { 18 | for(int col=col_start;col<=col_end;col++) 19 | { 20 | cout<=col_start;col--) 33 | { 34 | cout<=row_start;row--) 43 | { 44 | cout< 2 | using namespace std; 3 | 4 | void move(int n,char src,char helper,char destination){ 5 | //base case 6 | if(n==0){ 7 | return; 8 | } 9 | //recursive case 10 | //move n-1 disks from source to helper 11 | move(n-1,src,destination,helper); 12 | cout<<"shift disk "<>n; 20 | 21 | move(n,'A','B','C'); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Tower_of_Hanoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //tower of HANOI function implementation 5 | void TOH(int n,char Sour, char Aux,char Des) 6 | { 7 | if(n==1) 8 | { 9 | cout<<"Move Disk "<>n; 25 | //calling the TOH 26 | TOH(n,'A','B','C'); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Tower_of_hanoi.cpp: -------------------------------------------------------------------------------- 1 | // C++ recursive function to 2 | // solve tower of hanoi puzzle 3 | #include 4 | using namespace std; 5 | 6 | void towerOfHanoi(int n, char from_rod, char to_rod, 7 | char aux_rod) 8 | { 9 | if (n == 0) { 10 | return; 11 | } 12 | towerOfHanoi(n - 1, from_rod, aux_rod, to_rod); 13 | cout << "Move disk " << n << " from rod " << from_rod 14 | << " to rod " << to_rod << endl; 15 | towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); 16 | } 17 | 18 | // Driver code 19 | int main() 20 | { 21 | int N = 3; 22 | 23 | // A, B and C are names of rods 24 | towerOfHanoi(N, 'A', 'C', 'B'); 25 | return 0; 26 | } 27 | 28 | // This is code is contributed by abhishek rohit 29 | -------------------------------------------------------------------------------- /TrapRainwater.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int trapwater(vector &height) 6 | { 7 | int n=height.size(); 8 | if(n<=2) 9 | { 10 | return 0; 11 | } 12 | int maxleft=height[0]; 13 | int maxright=height[n-1]; 14 | int left=1; 15 | int right=n-2; 16 | int trapwater=0; 17 | 18 | while(left<=right) 19 | { 20 | if(maxleft<=maxright) 21 | { 22 | if(height[left]>=maxleft) 23 | { 24 | maxleft=height[left]; 25 | } 26 | else 27 | { 28 | trapwater+=maxleft-height[left]; 29 | } 30 | left++; 31 | } 32 | else 33 | { 34 | if(height[right]>=maxright) 35 | { 36 | maxright=height[right]; 37 | } 38 | else 39 | { 40 | trapwater+=maxright-height[right]; 41 | } 42 | right--; 43 | } 44 | } 45 | return trapwater; 46 | } 47 | 48 | int main() 49 | { 50 | vector height; 51 | height.push_back(4); 52 | height.push_back(2); 53 | height.push_back(0); 54 | height.push_back(3); 55 | height.push_back(2); 56 | height.push_back(5); 57 | cout< 22 | using namespace std; 23 | int main(){ 24 | int n,no; 25 | int cnt[64]={0}; //maximum we can have 64 bits 26 | cin>>n; 27 | for(int i=0;i>no; 29 | //update the cnt array by extracting bits 30 | int j=0; 31 | while(no>0){ 32 | int last_bit=(no&1); 33 | cnt[j]+=last_bit; 34 | j++; 35 | no=no>>1; 36 | } 37 | 38 | } 39 | //iterate over the array and form the answer by converting binary to decimal number 40 | int p=1; 41 | int ans=0; 42 | for(int i=0;i<64;i++){ 43 | cnt[i]%=3; 44 | ans+=(cnt[i]*p); 45 | p=p<<1; 46 | 47 | } 48 | //print the final answer 49 | cout< st; 5 | for(auto i:s) 6 | { 7 | if(i=='(' or i=='{' or i=='[') st.push(i); 8 | else 9 | { 10 | if(st.empty() or (st.top()=='(' and i!=')') or (st.top()=='{' and i!='}') or (st.top()=='[' and i!=']')) return false; 11 | st.pop(); 12 | } 13 | } 14 | return st.empty(); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Wave Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int a[]={1,3,4,2,7,8}; 5 | //sort the array in wave form in o(N) time 6 | int n=sizeof(a)/sizeof(int); 7 | for(int i=0;i 22 | #include 23 | using namespace std; 24 | int XOR(int x,int y){ 25 | int maxi=INT_MIN; 26 | int ans; 27 | for(int a=x;a<=y;a++){ 28 | for(int b=y;b>=a;b--){ 29 | int ans=a^b; 30 | } 31 | maxi=max(maxi,ans); 32 | 33 | } 34 | return maxi; 35 | } 36 | int main () { 37 | int x,y; 38 | cin>>x>>y; 39 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | list> *l; 7 | int n; 8 | cin>>n; 9 | l=new list>[n]; 10 | int e; 11 | cin>>e; 12 | for(int i=0;i>x>>y>>wt; 15 | l[x].push_back(make_pair(y,wt)); 16 | l[y].push_back(make_pair(x,wt)); 17 | } 18 | for(int i=0;i"; 20 | for(auto xp:l[i]){ 21 | cout<<"("< 2 | using namespace std; 3 | int binarysearch(int a[],int n,int key){ 4 | int s=0; 5 | int e=n-1; 6 | 7 | while(s<=e){ 8 | int mid=(s+e)/2; 9 | 10 | if(a[mid]==key){ 11 | return mid; 12 | 13 | } 14 | else if(a[mid]>key){ 15 | e=mid-1; 16 | } 17 | else{ 18 | s=mid+1; 19 | } 20 | 21 | } 22 | return -1; 23 | 24 | } 25 | int main(){ 26 | int n,key; 27 | cout<<"enter the no. of elements in an array:"<>n; 29 | int a[1000]; 30 | 31 | for(int i=0;i>a[i]; 33 | } 34 | cout<<"enter the value to be found"<>key; 36 | cout< 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node*left; 8 | node*right; 9 | 10 | node(int d){ 11 | data=d; 12 | left=NULL; 13 | right=NULL; 14 | } 15 | }; 16 | node* buildTree(){ 17 | int d; 18 | cin>>d; 19 | if(d==-1){ 20 | return NULL; 21 | } 22 | node* root=new node(d); 23 | root->left=buildTree(); 24 | root->right=buildTree(); 25 | return root; 26 | } 27 | void print(node* root){ 28 | if(root==NULL){ 29 | return; 30 | } 31 | //otherwise , print the root first followed by subtrees 32 | cout<data<<" "; 33 | print(root->left); 34 | print(root->right); 35 | } 36 | void print_inorder(node* root){ 37 | if(root==NULL){ 38 | return; 39 | } 40 | print_inorder(root->left); 41 | cout<data<<" "; 42 | print_inorder(root->right); 43 | } 44 | void print_postorder(node* root){ 45 | if(root==NULL){ 46 | return; 47 | } 48 | print_postorder(root->left); 49 | print_postorder(root->right); 50 | cout<data<<" "; 51 | } 52 | int main(){ 53 | node* root=buildTree(); 54 | print(root); 55 | cout< 2 | 3 | /* 4 | 5 | Time Complexity : O(logN) 6 | 7 | Space Complexity : O(1) 8 | 9 | */ 10 | 11 | int binarySearch(int a[], int n, int target) { // function to find element 12 | 13 | int low = 0, high = n - 1; 14 | 15 | while (low <= high) { 16 | 17 | int mid = (low + high) / 2; 18 | 19 | if (a[mid] < target) { 20 | 21 | low = mid + 1; 22 | 23 | } else if (a[mid] == target) { 24 | 25 | return mid; 26 | 27 | } else { // a[mid] > target 28 | 29 | high = mid - 1; 30 | 31 | } 32 | 33 | } 34 | 35 | return -1; // if target element not found in array 36 | 37 | } 38 | 39 | int main() { 40 | 41 | int n = 5, found = -1, target = 6; 42 | 43 | int a[] = {1, 2, 3, 4, 5}; // array needs to be sorted in ascending order 44 | 45 | 46 | 47 | found = binarySearch(a, n, target); 48 | 49 | if (found == -1) { 50 | 51 | printf("Element not present in array\n"); 52 | 53 | } else { 54 | 55 | printf("Element found at index %d\n", found); 56 | 57 | } 58 | 59 | return 0; 60 | 61 | } 62 | -------------------------------------------------------------------------------- /binaryserach.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int binarySearch(int arr[],int n,int key) 4 | { 5 | int first=0; 6 | int last=n; 7 | while(first<=last) 8 | { 9 | int mid=(first+last)/2; 10 | if(arr[mid]==key) 11 | { 12 | return mid; 13 | } 14 | else if(arr[mid]>key) 15 | { 16 | last=mid-1; 17 | } 18 | else{ 19 | first=mid-1; 20 | } 21 | } 22 | return -1; 23 | 24 | 25 | } 26 | int main() 27 | { 28 | int n; 29 | cout<<"Enter the value of n"; 30 | cin>>n; 31 | int arr[n]; 32 | for( int i=0;i>arr[i]; 35 | 36 | } 37 | int key; 38 | cout<<"Enter the value of key"; 39 | cin>>key; 40 | cout< 22 | #include 23 | using namespace std; 24 | bool check_sum(int arr[],int n){ 25 | unordered_sets; 26 | //first compute prefix sum 27 | int pre=0; 28 | for(int i=0;i>n; 42 | int arr[n]; 43 | for(int i=0;i>arr[i]; 45 | } 46 | if(check_sum(arr,n)){ 47 | cout<<"true"; 48 | } 49 | else{ 50 | cout<<"false"; 51 | } 52 | return 0; 53 | } 54 | 55 | 56 | -------------------------------------------------------------------------------- /circular linked list insertion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class node{ 4 | public: 5 | int data; 6 | node *next; 7 | 8 | node(int d){ 9 | data=d; 10 | next=NULL; 11 | } 12 | }; 13 | void insertion_at_head(node *&head,int data){ 14 | //1.create a new node 15 | node *n=new node(data); 16 | node *temp=head; 17 | n->next=head; 18 | //2.if list is not empty than traverse the list till last node and updat the pointer of last node to the new node 19 | if(temp!=NULL){ 20 | while(temp->next!=head){ 21 | 22 | temp=temp->next; 23 | } 24 | temp->next=n; 25 | } 26 | //if list is empty then new node poins to itself 27 | else{ 28 | n->next=n; 29 | } 30 | //update head to new node 31 | head=n; 32 | } 33 | void print(node *head){ 34 | node *temp=head; 35 | while(temp->next!=head){ 36 | cout<data<<" "; 37 | temp=temp->next; 38 | } 39 | cout<data<<" "; 40 | } 41 | int main(){ 42 | node *head=NULL; 43 | insertion_at_head(head,1); 44 | insertion_at_head(head,2); 45 | insertion_at_head(head,3); 46 | insertion_at_head(head,4); 47 | print(head); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /codechef_solutions/BWF.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,w,x,y,z; 6 | cin>>t; 7 | for(i=0;i>w>>x>>y>>z; 10 | if(x-w > y*z) 11 | { 12 | cout<<"Unfilled"< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int i,k,m,n; 6 | string str[1000]; 7 | cin>>k; 8 | for(i=0;i>m>>n; 11 | 12 | if(mn) 17 | { 18 | str[i] = "SECOND"; 19 | } 20 | else 21 | { 22 | str[i] = "ANY"; 23 | } 24 | } 25 | 26 | for(i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,z; 6 | cin>>t; 7 | for(i=0;i>x>>y>>z; 10 | if(x>y+z || y>x+z || z>x+y) 11 | cout<<"YES"< 2 | 3 | using namespace std; 4 | int main() 5 | { 6 | cout<<"this is the first file of this repository"; 7 | } -------------------------------------------------------------------------------- /codechef_solutions/Football_cup.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int i,k,m,n; 6 | string str[1000]; 7 | cin>>k; 8 | for(i=0;i>m>>n; 11 | 12 | if(m==n && m>0 && n>0) 13 | { 14 | str[i] = "YES"; 15 | } 16 | else 17 | { 18 | str[i] = "NO"; 19 | } 20 | } 21 | 22 | for(i=0;i 2 | using namespace std; 3 | #define ll long long 4 | ll MAX = 1e9+7; 5 | int main(){ 6 | ll t; 7 | cin>>t; 8 | while(t--){ 9 | ll n; 10 | cin>>n; 11 | double ans=log2((double)n); 12 | ll ans2=log2(n); 13 | if(ans-ans2>0){ 14 | ll power=pow(2,ans2); 15 | ll power2=pow(2,ans2-1); 16 | ll sol1 = n+1- power%MAX; 17 | ll sol2 = power - power2%MAX; 18 | if(sol2>sol1){ 19 | cout<<(sol2)%MAX< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,m,n; 6 | cin>>t; 7 | for(i=0;i>m>>n; 10 | if(m 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,x,y,z; 6 | cin>>t; 7 | for(i=0;i>x>>y>>z; 10 | cout<<(z-y)/x< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c,d; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c>>d; 10 | if(a!=c && b!=d) 11 | { 12 | cout<<"1"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x>=30) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,a,b,c,d,i; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c>>d; 10 | if(a-c>b-d) 11 | { 12 | cout<<"Second"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,j,k,m,n; 6 | int arr[1000]; 7 | cin>>k; 8 | for(i=0;i>m>>n; 11 | if(m>n) 12 | { 13 | j = m-n; 14 | } 15 | else if(n>m) 16 | { 17 | j = n-m; 18 | } 19 | else 20 | { 21 | j = m-n; 22 | } 23 | arr[i] = j; 24 | } 25 | for(i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(10-x>=3) 11 | cout<<"Yes"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x>=67 && x<=45000) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x/10 > 100) 11 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,z; 6 | cin>>t; 7 | for(i=0;i>x>>y>>z; 10 | if(z%x==0 && z%y==0) 11 | { 12 | cout<<"ANY"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b; 6 | cin>>t; 7 | for(i=0;i>a>>b; 10 | if((21-(a+b))<=10) 11 | { 12 | cout<<21-(a+b)< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x,k; 6 | cin>>t; 7 | for(i=0;i>n>>x>>k; 10 | if(x>k) 11 | { 12 | cout<<"0"<k/x) 17 | { 18 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,m; 6 | cin>>t; 7 | for(i=0;i>n>>m; 10 | if(n%m==0 && (n/m)%2==0) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,m,n; 6 | long int d,c; 7 | string str[100]; 8 | cin>>t; 9 | for(i=0;i>m>>n; 12 | d = m*100; 13 | c = n*10; 14 | if(c<=d) 15 | { 16 | cout<<"Cloth"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x; 6 | cin>>t; 7 | for(i=0;i>n>>x; 10 | if(n<=x) 11 | { 12 | cout<<"0"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,c,x,y; 6 | cin>>t; 7 | for(i=0;i>c>>x>>y; 10 | if(c<=x) 11 | cout<<0< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | cout<<4*x+y< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | if(x>=y) 11 | { 12 | cout<<"0"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c,d; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c>>d; 10 | if(a>c) 11 | { 12 | if(b>d) 13 | { 14 | if((a-c)>(b-d)) 15 | cout<(d-b)) 22 | cout<d) 30 | { 31 | if((c-a)>(b-d)) 32 | cout<(d-b)) 39 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c; 10 | if(a>=c && b>=c) 11 | { 12 | cout<=a && c>=a) 15 | { 16 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,m; 6 | cin>>t; 7 | for(i=0;i>x>>y>>m; 10 | if(x*m 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,m,n,k; 6 | cin>>t; 7 | for(i=0;i>n>>m>>k; 10 | if(m-k>=n) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | int fun(int n) 4 | { 5 | int j; 6 | int count=0; 7 | char str[10000]; 8 | for(j=0;j>str[j]; 11 | } 12 | for(j=0;j>t; 27 | for(i=0;i>n; 30 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c; 10 | if(a>=b && a>=c) 11 | { 12 | cout<=a && b>=c) 15 | { 16 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,a,b,c; 6 | cin>>t; 7 | for(i=0;i>n>>a>>b>>c; 10 | if((a+c)>=n && b>=n) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x>50) 11 | cout<<"RIGHT"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,m; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | if(x%2==0) 11 | { 12 | m = x/2; 13 | if(y>=m) 14 | cout<<"YES"<=m) 22 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,n,m,k; 6 | cin>>t; 7 | for(i=0;i>n>>m>>k; 10 | if(n<=m*k) 11 | { 12 | cout<<"yes"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,k,m; 6 | cin>>t; 7 | for(i=0;i>n>>k>>m; 10 | if(n%(k*m)==0) 11 | { 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c,d; 6 | cin>>t; 7 | for(i=0;i>a>>b; 10 | cin>>c>>d; 11 | if(a<=c && b<=d) 12 | { 13 | cout<<"POSSIBLE"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c; 6 | float s; 7 | cin>>t; 8 | for(i=0;i>a>>b>>c; 11 | s = ((float)a + (float)b)/2; 12 | if(s>c) 13 | { 14 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x!=0 && x%3 != 0) 11 | { 12 | cout<<3-x%3< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n; 6 | cin>>t; 7 | for(i=0;i>n; 10 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,m,n; 6 | cin>>t; 7 | for(i=0;i>m>>n; 10 | if(m>n) 11 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x; 6 | cin>>t; 7 | for(i=0;i>n>>x; 10 | if(x%n==0 && x>=n) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,z; 6 | cin>>t; 7 | for(i=0;i>x>>y>>z; 10 | if(x<3) 11 | { 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t; 6 | cin>>t; 7 | int arr[100]; 8 | int count = 0; 9 | for(i=0;i>arr[i]; 12 | if(arr[i]%2==0) 13 | { 14 | count = count+1; 15 | } 16 | } 17 | if(count>t/2) 18 | cout<<"READY FOR BATTLE"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,z; 6 | 7 | cin>>t; 8 | for(i=0;i>x>>y>>z; 11 | if(z>y/x) 12 | { 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,x,y; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | if(y<=x*1.07) 11 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n; 6 | cin>>t; 7 | for(i=0;i>n; 10 | if(n%4==0) 11 | { 12 | cout< 2 | using namespace std; 3 | int fun(int n, int x) 4 | { 5 | int j,a[1000],b[1000],count=0; 6 | for(j=0;j>a[j]>>b[j]; 9 | } 10 | for(j=0;j=a[j]) 13 | { 14 | if(b[j]>count) 15 | { 16 | count = b[j]; 17 | } 18 | } 19 | } 20 | return count; 21 | 22 | } 23 | int main() { 24 | int i,t,n,x; 25 | cin>>t; 26 | for(i=0;i>n>>x; 29 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x%3==0) 11 | { 12 | cout<<"NORMAL"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c; 10 | if((a%2==0 && b%2==0 && c%2==0) || (a%2!=0 && b%2!=0 && c%2!=0)) 11 | { 12 | cout<<"NO"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x; 6 | cin>>t; 7 | for(i=0;i>n>>x; 10 | if((n*x)%4==0) 11 | cout<<(n*x)/4< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x; 6 | cin>>t; 7 | for(i=0;i>n>>x; 10 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int arr[100]; 6 | int i,c=0; 7 | for(i=0;i<4;i++) 8 | { 9 | cin>>arr[i]; 10 | if(arr[i]>=10) 11 | { 12 | c+=1; 13 | } 14 | } 15 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,a1,b1,a2,b2; 6 | cin>>t; 7 | for(i=0;i>a>>b>>a1>>b1>>a2>>b2; 10 | if((a==a1 && b==b1)||(a==b1 && b==a1)) 11 | { 12 | cout<<"1"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,a,b; 6 | cin>>t; 7 | for(i=0;i>n>>a>>b; 10 | if(n<=a+2*b) 11 | { 12 | cout<<"Qualify"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x/25==0) 11 | { 12 | cout<<"1"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,x; 6 | cin>>t; 7 | for(i=0;i>a>>b>>x; 10 | cout<<(b-a)/x< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x<=100) 11 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,m; 6 | cin>>t; 7 | for(i=0;i>n>>m; 10 | if(n>m) 11 | { 12 | cout<<(2*n-m)< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,k,l,m; 6 | cin>>t; 7 | for(i=0;i>k>>l>>m; 10 | if(k>l && k>m) 11 | { 12 | cout<<"Setter"<k && l>m) 15 | { 16 | cout<<"Tester"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t; 6 | double s,a,b,c; 7 | cin>>t; 8 | for(i=0;i>s>>a>>b>>c; 11 | if((s+s*c/100)>=a && (s+s*c/100)<=b) 12 | { 13 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x; 6 | cin>>t; 7 | for(i=0;i>n>>x; 10 | if(n%6!=0) 11 | { 12 | cout<<(n/6 + 1)*x< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | if(x>y) 11 | { 12 | cout<<"A"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c,d,p,q,r,s; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c>>d; 10 | p=a+c; 11 | q=a+d; 12 | r=b+c; 13 | s=b+d; 14 | if(p>=q && p>=r && p>=s) 15 | { 16 | cout<=p && q>=r && q>=s) 20 | { 21 | cout<=p && r>=q && r>=s) 24 | { 25 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x>100) 11 | { 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,z; 6 | cin>>t; 7 | for(i=0;i>x>>y>>z; 10 | if(x%y==0) 11 | { 12 | cout<<(x/y)*z< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c; 10 | if((a+b)/2 >= 35 && (a+c)/2 >= 35 && (c+b)/2 >= 35) 11 | cout<<"PASS"< 2 | using namespace std; 3 | int loop(int n) 4 | { 5 | int j; 6 | int arr[1000]; 7 | int count = 0; 8 | for(j=0;j>arr[j]; 11 | if(arr[j]>=1000) 12 | { 13 | count+=1; 14 | } 15 | } 16 | return count; 17 | } 18 | 19 | int main() { 20 | int i,t,n; 21 | cin>>t; 22 | for(i=0;i>n; 26 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,d,l,r; 6 | cin>>t; 7 | for(i=0;i>d>>l>>r; 10 | if(d>=l && d<=r) 11 | { 12 | cout<<"Take second dose now"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,w1,w2,x1,x2,m; 6 | cin>>t; 7 | for(i=0;i>w1>>w2>>x1>>x2>>m; 10 | if((w2-w1)>=m*x1 && (w2-w1)<=m*x2) 11 | { 12 | cout<<"1"< 2 | using namespace std; 3 | //given a number n , find the number of set bits in its binary representation 4 | //ex. N=13 , binary representation = 1101, no of set bits =3 5 | //first method 6 | int count_set_bits(int n){ 7 | int ans=0; 8 | while(n>0){ 9 | ans+=(n&1); 10 | n=n>>1; 11 | } 12 | return ans; 13 | } 14 | //second method 15 | int count_bits_fast(int n){ //more efficient 16 | int ans=0; 17 | while(n>0){ 18 | n=n&(n-1); 19 | ans++; 20 | } 21 | return ans; 22 | } 23 | int main(){ 24 | int n; 25 | cin>>n; 26 | cout< 2 | using namespace std; 3 | void counting_sort(int a[],int n){ 4 | //first find the largest element of the array to find range 5 | int largest=-1; //assume a array of positive numbers 6 | for(int i=0;i0){ 19 | a[j]=i; 20 | freq[i]--; 21 | j++; 22 | 23 | } 24 | 25 | 26 | } 27 | } 28 | int main(){ 29 | int a[]={88,97,10,12,15,1,5,6,12,5,8}; 30 | int n=sizeof(a)/sizeof(int); 31 | counting_sort(a,n); 32 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int size=6; 6 | for(int i=1;i<=size;i++) 7 | { 8 | 9 | if(i==size) 10 | { 11 | for(int j=0;j 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=1;i<=5;i++) 6 | { 7 | for(int j=1;j<=5-i;j++) 8 | { 9 | cout<<" "; 10 | } 11 | 12 | for(int j=1;j<=(2*i-1);j++) 13 | { 14 | cout<<"*"; 15 | } 16 | 17 | 18 | cout<<"\n"; 19 | 20 | } 21 | 22 | for(int i=5;i>=1;i--) 23 | { 24 | for(int j=5-i;j>0;j--) 25 | { 26 | cout<<" "; 27 | } 28 | 29 | for(int j=(2*i-1);j>=1;j--) 30 | { 31 | cout<<"*"; 32 | } 33 | 34 | 35 | cout<<"\n"; 36 | 37 | } 38 | 39 | return 0; 40 | } 41 | 42 | /* 43 | 44 | * 45 | *** 46 | ***** 47 | ******* 48 | ********* 49 | ********* 50 | ******* 51 | ***** 52 | *** 53 | * 54 | */ -------------------------------------------------------------------------------- /cpp_programs/Cpp_Pattrens/elventhPattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int size=5; 6 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=1;i<=5;i++) 6 | { 7 | for(int j=5;j>=i;j--) 8 | { 9 | cout<<"* "; 10 | } 11 | cout<<"\n"; 12 | 13 | } 14 | return 0; 15 | } 16 | 17 | /* 18 | * * * * * 19 | * * * * 20 | * * * 21 | * * 22 | * 23 | 24 | */ 25 | -------------------------------------------------------------------------------- /cpp_programs/Cpp_Pattrens/fourthpattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=1;i<=5;i++) 6 | { 7 | for(int j=1;j<=i;j++) 8 | { 9 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=0;i<5;i++) 6 | { 7 | for(int j=0;j<5;j++) 8 | { 9 | cout<<"* "; 10 | } 11 | cout<<"\n"; 12 | 13 | } 14 | return 0; 15 | } 16 | 17 | /** 18 | * ***** 19 | * ***** 20 | * ***** 21 | * ***** 22 | * ***** 23 | * 24 | */ 25 | -------------------------------------------------------------------------------- /cpp_programs/Cpp_Pattrens/ninthPattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=0;i<5;i++) 6 | { 7 | for(int j=0;j<=i;j++) 8 | { 9 | cout<<"*"; 10 | } 11 | cout<<"\n"; 12 | } 13 | 14 | for(int i=0;i<=4;i++) 15 | { 16 | for(int j=4-i;j>0;j--) 17 | { 18 | cout<<"*"; 19 | } 20 | cout<<"\n"; 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | /* 27 | 28 | * 29 | ** 30 | *** 31 | **** 32 | ***** 33 | **** 34 | *** 35 | ** 36 | * 37 | 38 | */ -------------------------------------------------------------------------------- /cpp_programs/Cpp_Pattrens/secondPattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=0;i<5;i++) 6 | { 7 | for(int j=0;j<=i;j++) 8 | { 9 | cout<<"* "; 10 | } 11 | cout<<"\n"; 12 | 13 | } 14 | return 0; 15 | } 16 | 17 | /** 18 | * * 19 | * ** 20 | * *** 21 | * **** 22 | * ***** 23 | * 24 | */ 25 | -------------------------------------------------------------------------------- /cpp_programs/Cpp_Pattrens/sevenPattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=1;i<=5;i++) 6 | { 7 | for(int j=1;j<=5-i;j++) 8 | { 9 | cout<<" "; 10 | } 11 | 12 | for(int j=1;j<=(2*i-1);j++) 13 | { 14 | cout<<"*"; 15 | } 16 | 17 | 18 | cout<<"\n"; 19 | 20 | } 21 | return 0; 22 | } 23 | 24 | // Reverse of this can be done easily do it yourself. 25 | /* 26 | 27 | * 28 | *** 29 | ***** 30 | ******* 31 | ********* 32 | 33 | */ -------------------------------------------------------------------------------- /cpp_programs/Cpp_Pattrens/sixthPattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=0;i<5;i++) 6 | { 7 | for(int j=1;j<=5-i;j++) 8 | { 9 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=0;i<5;i++) 6 | { 7 | for(int j=5-i;j>=1;j--) 8 | { 9 | cout<<"*"; 10 | } 11 | 12 | for( int j=1;j<=i*2;j++) 13 | { 14 | cout<<" "; 15 | } 16 | 17 | for(int j=5-i;j>=1;j--) 18 | { 19 | cout<<"*"; 20 | } 21 | cout<<"\n"; 22 | } 23 | 24 | for(int i=1;i<=5;i++) 25 | { 26 | for(int j=1;j<=i;j++) 27 | { 28 | cout<<"*"; 29 | } 30 | 31 | for( int j=1;j<=10-i*2;j++) 32 | { 33 | cout<<" "; 34 | } 35 | 36 | for(int j=1;j<=i;j++) 37 | { 38 | cout<<"*"; 39 | } 40 | cout<<"\n"; 41 | } 42 | return 0; 43 | } 44 | 45 | /* 46 | 47 | ********** 48 | **** **** 49 | *** *** 50 | ** ** 51 | * * 52 | * * 53 | ** ** 54 | *** *** 55 | **** **** 56 | ********** 57 | 58 | */ -------------------------------------------------------------------------------- /cpp_programs/Cpp_Pattrens/thirdpattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=1;i<=5;i++) 6 | { 7 | for(int j=1;j<=i;j++) 8 | { 9 | cout< 2 | using namespace std; 3 | 4 | void heapify(int arr[], int n, int i) 5 | { 6 | int largest = i; 7 | int l = 2*i + 1; 8 | int r = 2*i + 2; 9 | if (l < n && arr[l] > arr[largest]) 10 | largest = l; 11 | 12 | if (r < n && arr[r] > arr[largest]) 13 | largest = r; 14 | 15 | if (largest != i) 16 | { 17 | swap(arr[i], arr[largest]); 18 | heapify(arr, n, largest); 19 | } 20 | } 21 | 22 | void buildheap(int arr[],int n){ 23 | for (int i = n / 2 - 1; i >= 0; i--) 24 | heapify(arr, n, i); 25 | } 26 | 27 | void heapSort(int arr[], int n) 28 | { 29 | buildheap(arr,n); 30 | 31 | for (int i=n-1; i>0; i--) 32 | { 33 | swap(arr[0], arr[i]); 34 | heapify(arr, i, 0); 35 | } 36 | } 37 | 38 | int main(void) 39 | { 40 | int a[10]; 41 | cout << "Enter 10 elements of array: " << endl; 42 | for (int i=0; i<10; i++) 43 | cin >> a[i]; 44 | cout << "Original Array is: "; 45 | for (int i=0; i<10; i++) 46 | cout << a[i] << " "; 47 | int n=sizeof(a)/sizeof(a[0]); 48 | cout <<"\nArray after sorting is: "; 49 | heapSort(a,n); 50 | for(int x: a) 51 | cout< 3 | using namespace std; 4 | 5 | int josephusCircle(int n, int k){ 6 | listl; //creates a doubly linked list using stl container// 7 | for(int i=0;i1){ 12 | 13 | for(int i=1;i 2 | 3 | int main() { 4 | int n, first = 0, second = 1, next; 5 | 6 | printf("Enter the number of terms: "); 7 | scanf("%d", &n); 8 | 9 | printf("Fibonacci Series: %d, %d, ", first, second); 10 | 11 | for (int i = 2; i < n; i++) { 12 | next = first + second; 13 | printf("%d, ", next); 14 | first = second; 15 | second = next; 16 | } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /customize_multiplication_matrix_finder.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | void run(int r1,int c1,int r2, int c2); 5 | int main() 6 | { 7 | int r1,r2,c1,c2; 8 | printf("1st size of matrix\nr1="); 9 | scanf("%d",&r1); 10 | printf("c1="); 11 | scanf("%d",&c1); 12 | printf("2nd size of matrix\n"); 13 | printf("r2="); 14 | scanf("%d",&r2); 15 | printf("c2="); 16 | scanf("%d",&c2); 17 | 18 | if(c1== r2 ){ 19 | run(r1,c1,r2,c2); 20 | }else{ 21 | printf("invalid size: In multiplication of matrix c1 and r2 must be equal..."); 22 | } 23 | 24 | 25 | return 0; 26 | } 27 | 28 | 29 | void run(int r1,int c1,int r2, int c2){ 30 | 31 | 32 | int a[r1][c1],b[r2][c2]; 33 | int mul[r1][c2]; 34 | printf("\n1st matrix\n\n"); 35 | for(int i=0;i 2 | using namespace std; 3 | class node{ 4 | public: 5 | int data; 6 | node* prev; 7 | node* next; 8 | 9 | node(int d){ 10 | data=d; 11 | prev=NULL; 12 | next=NULL; 13 | } 14 | 15 | 16 | }; 17 | void insert_at_head(node *&head, int data){ 18 | if(head==NULL){ 19 | head=new node(data); 20 | return; 21 | } 22 | node *n=new node(data); 23 | n->next=head; 24 | head->prev=NULL; 25 | 26 | head=n; 27 | } 28 | /*void insert_at_tail(node *head,int data){ 29 | if(head==NULL){ 30 | head=new node(data); 31 | return; 32 | } 33 | node *n=new node(data); 34 | 35 | 36 | }*/ 37 | 38 | void deletion_at_head(node *head){ 39 | if(head==NULL){ 40 | return; 41 | } 42 | delete head; 43 | node *temp=head->next; 44 | head=temp; 45 | } 46 | int main(){ 47 | node *head=NULL; 48 | insert_at_head(head,5); 49 | insert_at_head(head,4); 50 | insert_at_head(head,3); 51 | insert_at_head(head,2); 52 | insert_at_head(head,1); 53 | 54 | print(head); 55 | 56 | deletion_at_head(head); 57 | print(head); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /dsa-code/Sort-an-Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector&nums,int s,int m,int e) 4 | { 5 | int s1=s; 6 | int e1=m; 7 | int s2=m+1; 8 | int e2=e; 9 | int *arr=new int[e-s+1]; 10 | int st=0; 11 | while(s1<=e1&&s2<=e2) 12 | { 13 | if(nums[s1]>nums[s2]) 14 | { 15 | arr[st]=nums[s2]; 16 | s2++; 17 | st++; 18 | } 19 | else 20 | { 21 | arr[st]=nums[s1]; 22 | s1++; 23 | st++; 24 | } 25 | } 26 | while(s1<=e1) 27 | { 28 | arr[st]=nums[s1]; 29 | s1++; 30 | st++; 31 | } 32 | while(s2<=e2) 33 | { 34 | arr[st]=nums[s2]; 35 | s2++; 36 | st++; 37 | } 38 | for(int i=0;i&nums,int s,int e) 42 | { 43 | if(s>=e) 44 | return; 45 | int mid=(s+e)/2; 46 | mergeSort(nums,s,mid); 47 | mergeSort(nums,mid+1,e); 48 | merge(nums,s,mid,e); 49 | 50 | 51 | } 52 | vector sortArray(vector& nums) { 53 | mergeSort(nums,0,nums.size()-1); 54 | return nums; 55 | 56 | } 57 | }; 58 | -------------------------------------------------------------------------------- /dsa-code/Tries&Huffman_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | cout<<"Program started"< 2 | using namespace std; 3 | 4 | template 5 | 6 | 7 | class BinaryTreeNode 8 | { 9 | public: 10 | T data; 11 | BinaryTreeNode* left; 12 | BinaryTreeNode* right; 13 | 14 | BinaryTreeNode(T data){ 15 | this->data=data; 16 | left=NULL; 17 | right=NULL; 18 | } 19 | 20 | ~BinaryTreeNode(){ 21 | delete left; 22 | delete right; 23 | } 24 | }; 25 | 26 | //printing binary tree 27 | void printTree(BinaryTreeNode* root) 28 | { 29 | if(root==NULL) 30 | return; 31 | cout<data<<":"; 32 | if(root->left){ 33 | cout<<"L"<left->data<<" "; 34 | } 35 | if(root->right){ 36 | cout<<"R"<right->data<<" "; 37 | } 38 | cout<left); 40 | printTree(root->right); 41 | } 42 | 43 | int main() 44 | { 45 | BinaryTreeNode* root=new BinaryTreeNode(1); 46 | BinaryTreeNode* node1=new BinaryTreeNode(2); 47 | BinaryTreeNode* node2=new BinaryTreeNode(3); 48 | root->left=node1; 49 | root->right=node2; 50 | 51 | printTree(root); 52 | delete root; 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /dsa-code/carRunner.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include "header/car.h" 4 | 5 | 6 | int main() { 7 | // Write C++ code here 8 | cout<<"---------program started---------"< 2 | #include "header/demoheader.h" 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | // Write C++ code here 8 | cout<<"---------program started---------"< 7 | using namespace std; 8 | 9 | void solve() 10 | { 11 | cout<<"test case running"<>testcase; 19 | while(testcase){ 20 | testcase--; 21 | solve(); 22 | } 23 | 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /dsa-code/factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int ni){ 5 | 6 | 7 | if( ni == 1){ return 1;} 8 | return ni * factorial(ni-1); 9 | } 10 | 11 | void main(){ 12 | int n =2; 13 | int fact = factorial(n); 14 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | //remove duplicates program 8 | vector removeDuplicates(int *a, int size){ 9 | /*ek vector bna lenge output numbers ko store krne ke liye 10 | ek unordered map bala lenge jo int and bool ko as a key value 11 | pair rakhega isme jo bhi elements aayega usko as a key manke 12 | uske value me true rakh denge and us element ko output vector me push back ke denge 13 | */ 14 | vector output; 15 | unordered_map seen; 16 | for(int i=0; i0){ 18 | continue; 19 | } 20 | seen[a[i]]=true; //if a[i]=4 then seen[4] as a key and value me true daal diye 21 | output.push_back(a[i]); //output vector me push back kr diye element ko 22 | } 23 | return output; 24 | } 25 | 26 | int main() 27 | { 28 | int a[]={1,2,3,3,2,1,4,3,6,5,5}; 29 | vector output = removeDuplicates(a,11); 30 | for(int i=0; i 2 | 3 | class BinaryTreeNode 4 | { 5 | public: 6 | T data; 7 | BinaryTreeNode* left; 8 | BinaryTreeNode* right; 9 | 10 | BinaryTreeNode(T data){ 11 | this->data=data; 12 | left=NULL; 13 | right=NULL; 14 | } 15 | 16 | ~BinaryTreeNode(){ 17 | delete left; 18 | delete right; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /dsa-code/header/binaryTree.h: -------------------------------------------------------------------------------- 1 | template 2 | 3 | 4 | class BinaryTreeNode 5 | { 6 | public: 7 | T data; 8 | BinaryTreeNode* left; 9 | BinaryTreeNode* right; 10 | 11 | BinaryTreeNode(T data){ 12 | this->data=data; 13 | left=NULL; 14 | right=NULL; 15 | } 16 | 17 | ~BinaryTreeNode(){ 18 | delete left; 19 | delete right; 20 | } 21 | }; 22 | 23 | -------------------------------------------------------------------------------- /dsa-code/header/car.h: -------------------------------------------------------------------------------- 1 | class Car{ 2 | 3 | //state 4 | string make; 5 | string model; 6 | bool isOn; 7 | 8 | //constructors 9 | Car(string make,string model,bool isOn){ 10 | this.make=make; 11 | this.model=model; 12 | this.isOn=isOn; 13 | } 14 | 15 | //getters and setters 16 | 17 | 18 | //methods 19 | 20 | 21 | }; 22 | -------------------------------------------------------------------------------- /dsa-code/header/demoheader.h: -------------------------------------------------------------------------------- 1 | class Rectangle 2 | { 3 | public: 4 | int length; 5 | int breadth; 6 | 7 | Rectangle(int length,int breadth){ 8 | this->length=length; 9 | this->breadth=breadth; 10 | } 11 | 12 | int area(){ 13 | return length*breadth; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /dsa-code/header/linkedList.h: -------------------------------------------------------------------------------- 1 | class Node 2 | { 3 | public: 4 | int data; 5 | Node* next; 6 | 7 | Node(int data){ 8 | this->data=data; 9 | next=NULL; 10 | } 11 | 12 | }; 13 | -------------------------------------------------------------------------------- /dsa-code/header/tree.h: -------------------------------------------------------------------------------- 1 | template 2 | 3 | class TreeNode 4 | { 5 | public: 6 | T data; 7 | vector children; 8 | 9 | TreeNode(T data){ 10 | this->data=data; 11 | } 12 | 13 | ~TreeNode(){} 14 | }; 15 | -------------------------------------------------------------------------------- /dsa-code/priorityQueue_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /*inbuilt priority queue bhi queue me hi rehta hai 4 | bs class me hm priority_queue is trah initiate krenge 5 | by default max priority queue bnta hai means 6 | functions: 7 | empty() 8 | size() 9 | push(element) 10 | T top() 11 | pop() - lekin isse element return nhi hoga 12 | */ 13 | 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | priority_queue pq; //normal max heap 19 | pq.push(167); 20 | pq.push(78); 21 | pq.push(87); 22 | pq.push(56); 23 | pq.push(45); 24 | pq.push(20); 25 | 26 | cout<<"size: "<,greater > pqmin; 36 | pqmin.push(167); 37 | pqmin.push(78); 38 | pqmin.push(87); 39 | pqmin.push(56); 40 | pqmin.push(45); 41 | pqmin.push(20); 42 | 43 | cout<<"size: "< 2 | using namespace std; 3 | 4 | int length(char s[]){ 5 | if(s[0]=='\0'){ 6 | return 0; 7 | } 8 | int smalloutput = length(s+1); 9 | return 1+smalloutput; 10 | } 11 | 12 | void removeX(char s[]){ 13 | if(s[0]=='\0'){ 14 | return; 15 | } 16 | 17 | if(s[0]!='x'){ 18 | removeX(s+1); 19 | } 20 | else{ 21 | int i=1; 22 | for(;s[i]=='\0';i++){ 23 | s[i-1]=s[i]; 24 | 25 | } 26 | s[i-1]=s[i]; 27 | removeX(s); 28 | } 29 | } 30 | 31 | void removeDuplicates(char s[]){ 32 | if(s[0]=='\0'){ 33 | return; 34 | } 35 | 36 | if(s[0]==s[1]){ 37 | int i = 0; 38 | while (s[i] != '\0') { 39 | s[i] = s[i + 1]; 40 | i++; 41 | } 42 | removeDuplicates(s); 43 | } 44 | removeDuplicates(s+1); 45 | 46 | 47 | } 48 | 49 | 50 | int main() { 51 | // Write C++ code here 52 | cout<<"---------program started---------"<>str; 55 | 56 | int l =length(str); 57 | cout<<"length of string is: "< 2 | using namespace std; 3 | 4 | void solve() 5 | { 6 | cout<<"test case running"<>testcase; 14 | while(testcase){ 15 | testcase--; 16 | solve(); 17 | } 18 | 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /dsa-code/stack_and_queue_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //inbuilt stack 3 | #include 4 | using namespace std; 5 | 6 | 7 | 8 | int main() 9 | { 10 | cout<<"Program started.."< s; 12 | s.push(12); 13 | s.push(13); 14 | s.push(14); 15 | s.push(15); 16 | s.push(16); 17 | 18 | cout< 2 | //inbuilt queue 3 | #include 4 | using namespace std; 5 | 6 | 7 | 8 | int main() 9 | { 10 | cout<<"Program started.."< q; 12 | q.push(10); 13 | q.push(20); 14 | q.push(30); 15 | 16 | 17 | cout<<"First element is: "< 2 | #include "header/tree.h" 3 | using namespace std; 4 | 5 | 6 | 7 | 8 | int main() { 9 | // Write C++ code here 10 | cout<<"---------program started---------"< 2 | using namespace std; 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | cout<<"-------------Program started-------------"< v; //to initialise a vector 10 | //if we have to initialise a char vector then vector v; 11 | 12 | //to insert an element in a vector 13 | v.push_back(10); 14 | v.push_back(20); 15 | v.push_back(30); 16 | v.push_back(40); 17 | v.push_back(50); 18 | 19 | //to print the size of vector 20 | cout<<"size of vecotr: "< 2 | using namespace std; 3 | int factorial(int n){ 4 | //base case 5 | if(n==0){ 6 | return 1; 7 | } 8 | //recursive case 9 | int small_ans=factorial(n-1); 10 | int ans=n*small_ans; 11 | return ans; 12 | /* or simply 13 | return n*factorial(n-1); 14 | */ 15 | 16 | } 17 | int main(){ 18 | int n; 19 | cin>>n; 20 | cout< 3 | using namespace std; 4 | int power_optimised(int a,int n){ 5 | int ans=1; 6 | while(n>0){ 7 | int lastbit=(n&1); 8 | if(lastbit){ 9 | ans=ans*a; 10 | } 11 | a=a*a; 12 | n=n>>1; 13 | } 14 | return ans; 15 | } 16 | int main(){ 17 | int a,n; 18 | cin>>a>>n; 19 | cout< 2 | using namespace std; 3 | //compute a^n 4 | int power(int a,int n){ 5 | if(n==0){ 6 | return 1; 7 | } 8 | return a*power(a,n-1); 9 | } 10 | //more efficient way in terms of time and space 11 | int fast_power(int a,int n){ 12 | if(n==0){ 13 | return 1; 14 | } 15 | int smaller_ans=fast_power(a,n/2); 16 | smaller_ans*=smaller_ans; 17 | if(n&1){ 18 | return smaller_ans*a; 19 | } 20 | return smaller_ans; 21 | 22 | } 23 | int main(){ 24 | int a,n; 25 | cin>>a>>n; 26 | cout< 2 | void printFibonacci(int n){ 3 | static int n1=0,n2=1,n3; 4 | if(n>0){ 5 | n3 = n1 + n2; 6 | n1 = n2; 7 | n2 = n3; 8 | printf("%d ",n3); 9 | printFibonacci(n-1); 10 | } 11 | } 12 | int main(){ 13 | int n; 14 | printf("Enter the number of elements: "); 15 | scanf("%d",&n); 16 | printf("Fibonacci Series: "); 17 | printf("%d %d ",0,1); 18 | printFibonacci(n-2);//n-2 because 2 numbers are already printed 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /freinds problem recursion.cpp: -------------------------------------------------------------------------------- 1 | /* Friends pair problem 2 | Given N friends who want to go to a party. Each guy can go as a single , or as a couple. 3 | Find the no. of ways in which N friends can go to the party. 4 | N=3 5 | ways=4 6 | solution:- 7 | base cases:- 8 | if N=0,return 1 9 | if N=1,return 1 10 | if N=2,return 2 11 | recursive case:- 12 | f(N)=f(N-1)+n-1C1f(N-2) 13 | */ 14 | 15 | #include 16 | using namespace std; 17 | int ways(int N){ 18 | //base case 19 | if(N==0){ 20 | return 1; 21 | 22 | } 23 | if(N==1){ 24 | return 1; 25 | 26 | } 27 | if(N==2){ 28 | return 2; 29 | } 30 | //recursive case 31 | return ways(N-1)+((N-1)*ways(N-2)); 32 | 33 | } 34 | int main(){ 35 | int N; 36 | cin>>N; 37 | cout< 5 | #include 6 | using namespace std; 7 | void filter_characters(int n,char a[]){ 8 | int j=0; 9 | while(n>0){ 10 | int lastbit=(n&1); 11 | if(lastbit){ //if lastbit is 1 12 | cout<>1; 16 | } 17 | cout<>a; 30 | print_subsets(a); 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /height of binary tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node*left; 8 | node*right; 9 | 10 | node(int d){ 11 | data=d; 12 | left=NULL; 13 | right=NULL; 14 | } 15 | }; 16 | node* buildTree(){ 17 | int d; 18 | cin>>d; 19 | if(d==-1){ 20 | return NULL; 21 | } 22 | node* root=new node(d); 23 | root->left=buildTree(); 24 | root->right=buildTree(); 25 | return root; 26 | } 27 | void print(node* root){ 28 | if(root==NULL){ 29 | return; 30 | } 31 | //otherwise , print the root first followed by subtrees 32 | cout<data<<" "; 33 | print(root->left); 34 | print(root->right); 35 | } 36 | int height(node* root){ 37 | if(root==NULL){ 38 | return 0; 39 | } 40 | int ls=height(root->left); 41 | int rs=height(root->right); 42 | return max(ls,rs)+1; 43 | } 44 | int main(){ 45 | node* root=buildTree(); 46 | print(root); 47 | cout< 2 | using namespace std; 3 | void insertion_sort(int arr[],int n){ 4 | for(int i=1;i<=n-1;i++){ 5 | int e=arr[i]; 6 | //place the current element at 'right' position in the sorted part. 7 | int j=i-1; 8 | while(j>=0 and arr[j]>e){ 9 | arr[j+1]=arr[j]; 10 | j=j-1; 11 | } 12 | arr[j+1]=e; 13 | } 14 | } 15 | int main(){ 16 | int n; 17 | cin>>n; 18 | int arr[100]; 19 | for(int i=0;i<=n-1;i++){ 20 | cin>>arr[i]; 21 | } 22 | insertion_sort(arr,n); 23 | for(int i=0;i<=n-1;i++){ 24 | cout< 2 | #include 3 | #include 4 | 5 | int main(){ 6 | int choice; 7 | float p, r, t, si, ci; 8 | printf("1) Simple Interest\n"); 9 | printf("2) Compound Amount\n\n"); 10 | printf("Enter your choice: "); 11 | scanf("%d", &choice); 12 | switch(choice){ 13 | 14 | case 1: 15 | printf("Enter the principal amount: "); 16 | scanf("%f", &p); 17 | printf("Enter the rate of interest: "); 18 | scanf("%f", &r); 19 | printf("Enter the time period: "); 20 | scanf("%f", &t); 21 | 22 | si = (p * r * t) / 100; 23 | printf("Simple interest = %.2f", si); 24 | break; 25 | 26 | case 2: 27 | printf("Enter the principal amount: "); 28 | scanf("%f", &p); 29 | printf("Enter the rate of interest: "); 30 | scanf("%f", &r); 31 | printf("Enter the time period: "); 32 | scanf("%f", &t); 33 | 34 | ci = p * pow((1 + r / 100), t); 35 | printf("Compound amount = %.2f", ci); 36 | break; 37 | 38 | default: 39 | printf("Invalid choice :("); 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /interpolation.c: -------------------------------------------------------------------------------- 1 | # include 2 | #define MAX_SIZE 100 3 | int interpolation(int array_nums[], int array_size, int x) 4 | { 5 | int lower_pos = 0, higher_pos = array_size - 1; 6 | while (lower_pos <= higher_pos && x >= array_nums[lower_pos] && x <= array_nums[higher_pos]) 7 | { 8 | int x_pos = lower_pos + ((x - array_nums[lower_pos]) * (higher_pos - lower_pos)) / (array_nums[higher_pos] - array_nums[lower_pos]); 9 | if (x > array_nums[x_pos]) 10 | lower_pos = x_pos + 1; 11 | else if (x < array_nums[x_pos]) 12 | higher_pos = x_pos - 1; 13 | else 14 | return x_pos; 15 | } 16 | return -1; 17 | } 18 | 19 | int main() 20 | { 21 | int i,n,x; 22 | int array_nums[MAX_SIZE]; 23 | printf("\nSIZE:"); 24 | scanf("%d",&n); 25 | printf("\nElements"); 26 | for(i=0;i0) 10 | { 11 | rem=n%10; 12 | sum+=rem; 13 | n/=10; 14 | } 15 | System.out.println("Sum of digits : "+sum); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/AddElementsinArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class AddElementsInArray { 3 | public static void main(String[] args) { 4 | Scanner sc=new Scanner(System.in); 5 | System.out.println("Enter Array Size"); 6 | int size=sc.nextInt(); 7 | System.out.println("Enter Elements of an Array"); 8 | int array[]= new int[size]; 9 | for(int i=0;i0) 28 | { 29 | System.out.print(sum[i--]); 30 | } 31 | System.out.print("\n"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /java/AllCharactersToLowerCase.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class AllCharactersToLowerCasr 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner input=new Scanner(System.in); 7 | System.out.println("Enter String"); 8 | String x=input.nextLine(); 9 | int size=x.length(); 10 | char [] x1=x.toCharArray(); 11 | int i=0; 12 | while(i!=size) 13 | { 14 | if(x1[i]!=' ') 15 | { 16 | x1[i]=(char) (x1[i]+32); 17 | } 18 | i++; 19 | } 20 | System.out.println(x); 21 | System.out.println(x1); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/Area of Circle.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Ex12AreaofCircle{ 4 | 5 | public static void main(String args[]){ 6 | 7 | Scanner userIn = new Scanner(System.in); 8 | 9 | System.out.println("Enter Radius Of Circle: "); 10 | float radius = userIn.nextFloat(); 11 | 12 | double areacircle = Math.PI * radius * radius; 13 | 14 | System.out.println("The Area Of The Circle is: " + areacircle); 15 | 16 | } 17 | } 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /java/ArrayElementBinarySearch.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | public class ArrayElementPositionUsingBinarySearch { 4 | public static int binarySearch(int array[], int key) { 5 | int start = 0; 6 | int end = array.length - 1; 7 | while (start <= end) 8 | { 9 | int mid = (start + end) / 2; 10 | if (key == ary[mid]) { 11 | return mid; 12 | } 13 | if (key al2=new ArrayList<>(); 8 | System.out.println("capcity "+al2.size()); 9 | ArrayList al1=new ArrayList<>(22); 10 | System.out.println("capcity "+al1.size()); 11 | ArrayList al=new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9)); 12 | al.add(9); 13 | al.add(1,90); 14 | al.add(3); 15 | al.remove(6); 16 | System.out.println(al.size()); 17 | System.out.println(al); 18 | System.out.println(al); 19 | Iterator itr=al.iterator(); 20 | while(itr.hasNext()) 21 | { 22 | System.out.println(itr.next()); 23 | } 24 | System.out.println(al.contains(88)); 25 | Object[] obj=al.toArray(); 26 | System.out.println(Arrays.toString(obj)); 27 | for(Object ch:obj) 28 | { 29 | System.out.println(ch); 30 | } 31 | System.out.println("getting Object "+al.get(6)); 32 | al.removeAll(al); 33 | System.out.println(al); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /java/ArrayOpsBubbleSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class ArrayOperationsUsingBubbleSort { 3 | public static void bubSort(int a[], int n) { 4 | for (int i = 0; i < n - 2; i++) { 5 | for (int j = 0; j <= n-2-i; j++) { 6 | if (a[j] > a[j + 1]) { 7 | int temp = a[j]; 8 | a[j] = a[j + 1]; 9 | a[j + 1] = temp; 10 | } 11 | } 12 | } 13 | System.out.println("After Sorting Elements "); 14 | for (int b : a) { 15 | System.out.println(b); 16 | } 17 | } 18 | public static void main(String[] args) { 19 | Scanner sc = new Scanner(System.in); 20 | System.out.println("Enter SIZE of Array"); 21 | int len = sc.nextInt(); 22 | int arry[] = new int[len]; 23 | System.out.println("Enter Array Elements "); 24 | for (int i = 0; i < len; i++) { 25 | arry[i] = sc.nextInt(); 26 | } 27 | bubSort(arry, len); 28 | System.out.println("smallest element is :"+arry[0]); 29 | System.out.println("largest element is :"+arry[len-1]); 30 | System.out.println("Enter which element you want"); 31 | int k=sc.nextInt(); 32 | for(int i=0;i=0;i--) 10 | { 11 | System.out.print(a[i]+" "); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /java/ArraysofArrays.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | public class ArraysOfArrays 3 | { 4 | public static void main(String[] args) 5 | { 6 | int [] a= {1,4,5,6,8}; 7 | int [] b= {2,3,4,5,8}; 8 | System.out.println(a); 9 | System.out.println(Arrays.toString(a)); 10 | System.out.println(b); 11 | System.out.println(Arrays.toString(b)); 12 | int [] [] c= {a,b}; 13 | System.out.println(c); 14 | System.out.println(Arrays.deepToString(c)); 15 | int [] [] [] d= { b ,c}; 16 | System.out.println(Arrays.deepToString(d)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/AverageandPercentage.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class CalculateAverageAndPercentage { 3 | public static void main(String[] args) { 4 | Scanner scan=new Scanner(System.in); 5 | System.out.println("Enter Marks of Subjects"); 6 | int sum=0; 7 | int marks[]=new int[5]; 8 | for(int i=0;i<5;i++) 9 | { 10 | marks[i]=scan.nextInt(); 11 | sum =sum+marks[i]; 12 | } 13 | int avg =sum/5; 14 | int percent=(sum/500)*100; 15 | System.out.println("Average Marks :"+avg); 16 | System.out.println("Percentage Marks :"+percent); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/factorial-trailing-zeroes: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int trailingZeroes(int n) { 3 | int count=0; 4 | while(n>0) 5 | { 6 | n/=5; 7 | count+=n; 8 | 9 | } 10 | return count; 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /java/house-robber: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rob(int[] nums) { 3 | if(nums==null || nums.length==0) 4 | return 0; 5 | if(nums.length==1) 6 | return nums[0]; 7 | if(nums.length==2) 8 | return Math.max(nums[0],nums[1]); 9 | int dp[]=new int [nums.length]; 10 | dp[0]=nums[0]; 11 | dp[1]=Math.max(nums[0],nums[1]); 12 | for(int i=2;i 2 | #include 3 | #define MAX_SIZE 100 4 | int min(int a, int b){ 5 | if(b>a) 6 | return a; 7 | else 8 | return b; 9 | } 10 | int jumpsearch(int arr[], int x, int n) 11 | { 12 | int step = sqrt(n); 13 | 14 | int prev = 0; 15 | while (arr[min(step, n)-1] < x) 16 | { 17 | prev = step; 18 | step += sqrt(n); 19 | if (prev >= n) 20 | return -1; 21 | } 22 | 23 | while (arr[prev] < x) 24 | { 25 | prev++; 26 | 27 | if (prev == min(step, n)) 28 | return -1; 29 | } 30 | 31 | if (arr[prev] == x) 32 | return prev; 33 | 34 | return -1; 35 | } 36 | int main() 37 | { 38 | int arr[MAX_SIZE]; 39 | int i,x,m; 40 | printf("\nSIZE:"); 41 | scanf("%d",&m); 42 | printf("\nElements"); 43 | for(i=0;i= 0) 51 | printf("Number is at %d index",index); 52 | else 53 | printf("Number is not exist in the array"); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /left shift.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a,b; 5 | printf("LEFT SHIFT BY 2 POSITIONS!!\n"); 6 | printf("Enter a number:"); 7 | scanf("%d",&a); 8 | b = a<<2; 9 | printf("%d",b); 10 | } 11 | -------------------------------------------------------------------------------- /length of longest subarray with sum k.cpp: -------------------------------------------------------------------------------- 1 | /*logest subarray with sum k 2 | let the cummulatice sum array be 3 | c1,c2,c3,.....c(i).....c(j)....cn 4 | the sum between i and j is c(j)-c(i)=k 5 | we can use map here 6 | find an index whose c(i)=c(j)-k 7 | where c(j) is the current index and we have to find c(i) in the remaining array accordingly for every c(j) 8 | and when the c(i)=c(j)-k then find length 9 | example - 6 is the current sum and if an index with sum 6-k is present than subarray with sum k exist else not. 10 | and if current index sum=k (c(j)=k) then length=j+1 11 | 12 | */ 13 | 14 | #include 15 | #include 16 | using namespace std; 17 | int longest_subarray_sum(int arr[],int n,int k){ 18 | unordered_map m; 19 | int pre=0; 20 | int len=0; 21 | for(int i=0;i>n>>k; 40 | int arr[n]; 41 | for(int i=0;i>arr[n]; 43 | } 44 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n,key,i; 5 | int array[1000]; 6 | cout<<"enter the no. of elements in an array:"<>n; 8 | 9 | for(i=0;i>array[i]; 11 | } 12 | cout<<"enter the value you want to search"<>key; 14 | for(i=0;i<=n-1;i++){ 15 | if(array[i]==key){ 16 | cout< 2 | using namespace std; 3 | bool check(int array[],int n,int x){ 4 | if (n==0){ 5 | return false; 6 | } 7 | else{ 8 | if (x==array[n]){ 9 | return true; 10 | } 11 | 12 | else{ 13 | return check(array,n-1,x); 14 | } 15 | } 16 | } 17 | 18 | int main(){ 19 | int n; 20 | int x; 21 | cout<<"enter value of n"; 22 | cin>>n; 23 | int array[n]; 24 | 25 | cout<<"enter array element:"; 26 | 27 | 28 | for(int i=0; i>array[i]; 30 | } 31 | cout<<"enter a number which you want to find in array"<>x; 33 | 34 | cout< 3 | using namespace std; 4 | class node{ 5 | public: 6 | int data; 7 | node *next; 8 | //node constructor 9 | node(int d){ 10 | data=d; 11 | next=NULL; 12 | } 13 | }; 14 | //implement using linked class(object oriented) 15 | /*class LinkedList{ 16 | node *head; 17 | node *tail; 18 | public: 19 | LinkedList(){ 20 | 21 | } 22 | 23 | void insert(){ 24 | 25 | } 26 | };*/ 27 | //implement using functions(procedurul programming) 28 | void build(){ 29 | 30 | } 31 | //passing the pointer by reference 32 | void insert_at_head(node *&head,int d){ 33 | if(head==NULL){ 34 | head=new node(d); 35 | return; 36 | } 37 | node *n=new node(d); 38 | n->next=head; 39 | head=n; 40 | 41 | } 42 | void print(node *head){ 43 | while(head!=NULL){ 44 | cout<data<<"--->"; 45 | head=head->next; 46 | } 47 | } 48 | int main(){ 49 | node *head=NULL; 50 | insert_at_head(head,3); 51 | insert_at_head(head,2); 52 | insert_at_head(head,1); 53 | insert_at_head(head,0); 54 | 55 | print(head); 56 | 57 | } 58 | 59 | 60 | -------------------------------------------------------------------------------- /linked list kth node from last.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node *next; 8 | 9 | node(int d){ 10 | data=d; 11 | next=NULL; 12 | 13 | } 14 | 15 | }; 16 | void insert_at_head(node *&head,int data){ 17 | if(head==NULL){ 18 | head=new node(data); 19 | return; 20 | } 21 | node *n=new node(data); 22 | n->next=head; 23 | head=n; 24 | 25 | } 26 | void print(node *head){ 27 | while(head!=NULL){ 28 | cout<data<<"--->"; 29 | head=head->next; 30 | } 31 | } 32 | int length(node *head){ 33 | int cnt=0; 34 | while(head!=NULL){ 35 | cnt++; 36 | head=head->next; 37 | } 38 | return cnt; 39 | } 40 | node* last_k_node(node *head,int k){ 41 | node *slow=head; 42 | int jump=0; 43 | while(jumpnext; 45 | jump++; 46 | } 47 | return slow; 48 | 49 | } 50 | void operator>>(istream &is,node *head){ 51 | print(head); 52 | return; 53 | } 54 | void operator<<(ostream &os,node *head){ 55 | print(head); 56 | return; 57 | } 58 | int main(){ 59 | node *head=NULL; 60 | insert_at_head(head,6); 61 | insert_at_head(head,5); 62 | insert_at_head(head,4); 63 | insert_at_head(head,3); 64 | insert_at_head(head,2); 65 | insert_at_head(head,1); 66 | 67 | 68 | print(head); 69 | int k; 70 | cin>>k; 71 | 72 | node *last=last_k_node(head,k); 73 | cout<data; 74 | 75 | } 76 | -------------------------------------------------------------------------------- /linked list mid point.cpp: -------------------------------------------------------------------------------- 1 | //insertion in linked list 2 | #include 3 | using namespace std; 4 | class node{ 5 | public: 6 | int data; 7 | node *next; 8 | node(int d){ 9 | data=d; 10 | next=NULL; 11 | } 12 | }; 13 | void insert_at_head(node *&head,int d){ 14 | if(head==NULL){ 15 | head=new node(d); 16 | return; 17 | } 18 | node *n=new node(d); 19 | n->next=head; 20 | head=n; 21 | 22 | } 23 | int length(node *head){ 24 | int cnt=0; 25 | while(head!=NULL){ 26 | cnt++; 27 | head=head->next; 28 | } 29 | return cnt; 30 | } 31 | void print(node *head){ 32 | while(head!=NULL){ 33 | cout<data<<"--->"; 34 | head=head->next; 35 | } 36 | } 37 | node* mid_point(node *head){ 38 | if(head==NULL or head->next==NULL){ 39 | return head; 40 | } 41 | node *slow=head; 42 | node *fast=head->next; 43 | while(fast->next!=NULL and fast!=NULL){ 44 | fast=fast->next->next; 45 | slow=slow->next; 46 | } 47 | return slow; 48 | 49 | 50 | } 51 | void operator<<(ostream &os,node *head){ 52 | print(head); 53 | return; 54 | } 55 | int main(){ 56 | node *head=NULL; 57 | insert_at_head(head,3); 58 | insert_at_head(head,2); 59 | insert_at_head(head,1); 60 | insert_at_head(head,0); 61 | 62 | print(head); 63 | 64 | node *mid=mid_point(head); 65 | cout<data; 68 | 69 | 70 | } 71 | 72 | -------------------------------------------------------------------------------- /linked list reverse recursively.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class node{ 4 | public: 5 | int data; 6 | node *next; 7 | 8 | node(int d){ 9 | data=d; 10 | next=NULL; 11 | } 12 | }; 13 | 14 | 15 | void insert_at_head(node *&head,int d){ 16 | if(head==NULL){ 17 | head=new node(d); 18 | return; 19 | } 20 | node *n=new node(d); 21 | n->next=head; 22 | head=n; 23 | 24 | } 25 | void print(node *head){ 26 | while(head!=NULL){ 27 | cout<data<<"--->"; 28 | head=head->next; 29 | } 30 | } 31 | node* reverse_recursively(node *head){ 32 | //for smallest linked list 33 | if(head->next==NULL or head==NULL){ 34 | return head; 35 | } 36 | //recursive case 37 | node* shead=reverse_recursively(head->next); 38 | node*temp=head->next; 39 | temp->next=head; 40 | /*node* temp=shead; 41 | while(temp->next!=NULL){ //it would take o(n2) time n for loop and n for recursion 42 | temp=temp->next; 43 | }*/ 44 | head->next=NULL; 45 | temp->next=head; 46 | return shead; 47 | 48 | } 49 | int main(){ 50 | node *head=NULL; 51 | insert_at_head(head,3); 52 | insert_at_head(head,2); 53 | insert_at_head(head,1); 54 | insert_at_head(head,0); 55 | 56 | print(head); 57 | 58 | reverse_recursively(head); 59 | 60 | cout< 2 | using namespace std; 3 | class node{ 4 | public: 5 | int data; 6 | node *next; 7 | 8 | node(int d){ 9 | data=d; 10 | next=NULL; 11 | } 12 | }; 13 | 14 | 15 | void insert_at_head(node *&head,int d){ 16 | if(head==NULL){ 17 | head=new node(d); 18 | return; 19 | } 20 | node *n=new node(d); 21 | n->next=head; 22 | head=n; 23 | 24 | } 25 | void print(node *head){ 26 | while(head!=NULL){ 27 | cout<data<<"--->"; 28 | head=head->next; 29 | } 30 | } 31 | void reverse(node *&head){ 32 | node *current=head; 33 | node *prev=NULL; 34 | node *n; 35 | while(current!=NULL){ 36 | n=current->next; //save the next node 37 | current->next=prev; //make the current node point to previous 38 | prev=current; //update previous 1 step forward 39 | current=n; //update current 1 step forward 40 | } 41 | head=prev; 42 | } 43 | int main(){ 44 | node *head=NULL; 45 | insert_at_head(head,3); 46 | insert_at_head(head,2); 47 | insert_at_head(head,1); 48 | insert_at_head(head,0); 49 | 50 | print(head); 51 | 52 | reverse(head); 53 | 54 | cout< 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node *next; 8 | node(int d){ 9 | data=d; 10 | next=NULL; 11 | 12 | } 13 | }; 14 | 15 | void insertion_at_head(node *&head , int data){ 16 | if(head==NULL){ 17 | head=new node(data); 18 | return; 19 | } 20 | node *n=new node(data); 21 | n->next=head; 22 | head=n; 23 | 24 | } 25 | void print(node *head){ 26 | while(head!=NULL){ 27 | cout<data<<"--->"; 28 | head=head->next; 29 | } 30 | } 31 | bool search(node *head,int key){ 32 | node *temp=head; 33 | while(temp!=NULL){ 34 | if(head->data==key){ 35 | return true; 36 | } 37 | head=head->next; 38 | } 39 | return false; 40 | } 41 | //search recursively 42 | bool search_recursively(node *head,int key){ 43 | //base case 44 | if(head==NULL){ 45 | return false; 46 | } 47 | //rec. case 48 | if(head->data==key){ 49 | return true; 50 | } 51 | else{ 52 | return search_recursively(head->next,key); 53 | } 54 | } 55 | 56 | int main(){ 57 | node *head=NULL; 58 | insertion_at_head(head,1); 59 | insertion_at_head(head,2); 60 | insertion_at_head(head,3); 61 | insertion_at_head(head,4); 62 | insertion_at_head(head,5); 63 | 64 | print(head); 65 | 66 | int key; 67 | cin>>key; 68 | if(search_recursively(head,key)){ 69 | cout<<"present"< 2 | using namespace std; 3 | int fibonacci(int n){ 4 | //base case 5 | if(n==0 || n==1){ 6 | return n; //if n=0,return 0 or n=1,return 1 7 | } 8 | //recursive case 9 | int f1=fibonacci(n-2); 10 | int f2=fibonacci(n-1); 11 | return f1+f2; 12 | } 13 | int main(){ 14 | int n; 15 | cin>>n; 16 | cout< 2 | using namespace std; 3 | 4 | double eps=1e-2; 5 | 6 | int main(){ 7 | long long n; 8 | cin>>n; 9 | 10 | double lo=1,hi=n,mid; 11 | while(hi-lo>eps){ 12 | mid=(hi+lo)/2; 13 | if (mid*mid 10 | using namespace std; 11 | int f(int n){ 12 | if(n==0){ 13 | return 1; 14 | } 15 | if(n==1){ 16 | return 2; 17 | } 18 | return f(n-1)+f(n-2); 19 | } 20 | int main(){ 21 | int n; 22 | cin>>n; 23 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool ispal(int a){ 7 | int x=a,rem=0,res=0; 8 | while(x>0){ 9 | rem=x%10; 10 | res=res*10+rem; 11 | x/=10; 12 | } 13 | if(res==a) 14 | return true; 15 | return false; 16 | } 17 | int PalinArray(int a[], int n) 18 | { 19 | // code here 20 | int count =1; 21 | for(int i=0;i>t; 32 | while(t--){ 33 | int n; 34 | cin>>n; 35 | int a[n]; 36 | for(int i=0;i>a[i]; 38 | Solution ob; 39 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | // your code goes here 6 | long long t; 7 | cin>>t; 8 | while(t--){ 9 | long long n,k; 10 | cin>>n>>k; 11 | long long p; 12 | p = n /k; 13 | 14 | cout<& nums,vector>&v,int i) 4 | { 5 | if(i>=nums.size()) 6 | { 7 | v.push_back(nums); 8 | return; 9 | } 10 | for(int j=i;j> permute(vector& nums) 19 | { 20 | vector>v; 21 | int i=0; 22 | solve(nums,v,i); 23 | return v; 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /print pascal triangle using function in c++ language: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void print_pascal(int ); 4 | int combi(int , int ); 5 | int factorial(int ); 6 | int main() 7 | { 8 | int a; 9 | cout<<"enter a number of lines of pascal triangle : "; 10 | cin>>a; 11 | print_pascal(a); 12 | return 0; 13 | 14 | } 15 | void print_pascal(int n) 16 | { 17 | int i,j,k,l; 18 | for(i=1;i<=n;i++) 19 | { 20 | k=1,l=0; 21 | for(j=1;j<=2*n-1;j++) 22 | { 23 | if(j>=(n+1)-i&&j<=(n-1)+i&&k) 24 | { 25 | cout<<" "< 2 | using namespace std; 3 | int partition(int a[],int s,int e){ 4 | 5 | int i=s-1; 6 | int j=s; 7 | int pivot=a[e]; 8 | for( ;j<=e-1;){ 9 | if(a[j]<=pivot){ 10 | 11 | i=i+1; 12 | swap(a[i],a[j]); 13 | 14 | } 15 | j=j+1; 16 | } 17 | 18 | swap(a[i+1],a[e]); 19 | return i+1; 20 | 21 | } 22 | void quick_sort(int a[],int s,int e){ 23 | //base case 24 | if(s>=e){ 25 | return; 26 | } 27 | //recursive case 28 | int p=partition(a,s,e); 29 | //left part 30 | quick_sort(a,s,p-1); 31 | //right part 32 | quick_sort(a,p+1,e); 33 | } 34 | int main(){ 35 | int n; 36 | cin>>n; 37 | int a[n]; 38 | for(int i=0;i>a[i]; 40 | } 41 | quick_sort(a,0,n-1); 42 | for(int i=0;i 2 | using namespace std; 3 | int partition(int array[],int si,int ei){ 4 | int pivot = array[si]; 5 | int count=0; 6 | for (int i=si+1;i<=ei;i++){ 7 | if (array[i]<=pivot){ 8 | count++; 9 | } 10 | } 11 | int pivotindex=si+count; 12 | swap(array[pivotindex],array[si]); 13 | int i=si; 14 | int j=ei; 15 | while(ipivotindex){ 16 | while(array[i]<=pivot){ 17 | i++; 18 | } 19 | while(array[j]>pivot){ 20 | j--; 21 | } 22 | if (ipivotindex){ 23 | swap(array[i++],array[j--]); 24 | } 25 | } 26 | return pivotindex; 27 | } 28 | void quicksort(int array[],int si,int ei){ 29 | if (si>=ei){ 30 | return; 31 | } 32 | 33 | int p=partition(array,si,ei); 34 | quicksort(array,si,p-1); 35 | quicksort(array,p+1,ei); 36 | } 37 | int main(){ 38 | int n; 39 | cout<<"enter value of n:"; 40 | cin>>n; 41 | int array[n]; 42 | 43 | cout<<"enter array element:"; 44 | for(int i=0; i>array[i]; 46 | } 47 | quicksort(array,0,n-1); 48 | for(int i=0; i 4 | using namespace std; 5 | bool is_sorted(int a[],int n){ 6 | //base case 7 | if(n==0 or n==1){ 8 | return true; 9 | 10 | } 11 | //recursive case 12 | if(a[0]= low: 9 | 10 | mid = (high + low) // 2 11 | 12 | # If element is present at the middle itself 13 | if arr[mid] == x: 14 | return mid 15 | 16 | # If element is smaller than mid, then it can only 17 | # be present in left subarray 18 | elif arr[mid] > x: 19 | return binary_search(arr, low, mid - 1, x) 20 | 21 | # Else the element can only be present in right subarray 22 | else: 23 | return binary_search(arr, mid + 1, high, x) 24 | 25 | else: 26 | # Element is not present in the array 27 | return -1 28 | 29 | # Test array 30 | arr = [ 2, 3, 4, 10, 40 ] 31 | x = 10 32 | 33 | # Function call 34 | result = binary_search(arr, 0, len(arr)-1, x) 35 | 36 | if result != -1: 37 | print("Element is present at index", str(result)) 38 | else: 39 | print("Element is not present in array") -------------------------------------------------------------------------------- /replace bits in N by M.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //replace bits in n by m 4 | /* you are given two 32 bits numbers , N and M and two positions i and j. write a method 5 | to set all bits between i and j in N equal to M.*/ 6 | int replace_bits(int n,int m,int i,int j){ 7 | int ones=(~0); 8 | int a=ones<>n>>m>>i>>j; 19 | cout< 2 | using namespace std; 3 | //replace pi with 3.14 4 | //xpighpimpipi----->x3.14m3.143.14 5 | void replace_pi(char a[],int i){ 6 | if(a[i]=='\0' or a[i+1]=='\0'){ 7 | return ; 8 | 9 | } 10 | //look for pi at current location 11 | if(a[i]=='p' and a[i+1]=='i'){ 12 | //shifting +replacement with 3.14 13 | int j=i+2; 14 | //jake j to the end of the array 15 | while(a[j]!='\0'){ 16 | j++; 17 | } 18 | //shifting(right to left) 19 | while(j>=i+2){ 20 | a[j+2]==a[j]; 21 | j--; 22 | 23 | } 24 | //replacement + recursive call for the remaining part 25 | a[i]='3'; 26 | a[i+1]='.'; 27 | a[i+2]='1'; 28 | a[i+3]='4'; 29 | replace_pi(a,i+4); 30 | 31 | 32 | 33 | 34 | } 35 | else{ 36 | //go to the next position 37 | replace_pi(a,i+1); 38 | 39 | } 40 | return ; 41 | 42 | } 43 | int main(){ 44 | char a[1000]; 45 | cin>>a; 46 | replace_pi(a,0); 47 | cout< 2 | using namespace std; 3 | 4 | int length(char a[], int len){ 5 | if(a[0] == '\0') 6 | return len; 7 | 8 | return length(a+1, len+1); 9 | } 10 | 11 | void remove(char a[],char c1,char c2){ 12 | if (a[0]=='\0'){ 13 | return; 14 | } 15 | 16 | int n = length(a, 0); 17 | 18 | if(a[0]!='\0'){ 19 | for (int i = 0; i < n; i++){ 20 | if(a[i] == c1){ 21 | a[i] = c2; 22 | } 23 | } 24 | } 25 | } 26 | 27 | int main(){ 28 | char c1,c2; 29 | char a[100]; 30 | cin>>a; 31 | cin>>c1>>c2; 32 | 33 | remove(a,c1,c2); 34 | cout << a << "\n"; 35 | } -------------------------------------------------------------------------------- /reversal algorithm.py: -------------------------------------------------------------------------------- 1 | # Python program for reversal algorithm of array rotation 2 | 3 | # Function to reverse arr[] from index start to end 4 | def rverseArray(arr, start, end): 5 | while (start < end): 6 | temp = arr[start] 7 | arr[start] = arr[end] 8 | arr[end] = temp 9 | start += 1 10 | end = end-1 11 | 12 | # Function to left rotate arr[] of size n by d 13 | def leftRotate(arr, d): 14 | n = len(arr) 15 | rverseArray(arr, 0, d-1) 16 | rverseArray(arr, d, n-1) 17 | rverseArray(arr, 0, n-1) 18 | 19 | # Function to print an array 20 | def printArray(arr): 21 | for i in range(0, len(arr)): 22 | print (arr[i]) 23 | 24 | # Driver function to test above functions 25 | arr = [1, 2, 3, 4, 5, 6, 7] 26 | leftRotate(arr, 2) # Rotate array by 2 27 | printArray(arr) -------------------------------------------------------------------------------- /reverseString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | char s[]="Hello World"; 7 | strrev(s); 8 | cout<<"\n"< 2 | #include 3 | #include 4 | #include 5 | 6 | int game(char player, char comp) 7 | { 8 | 9 | if (player == comp) 10 | return -1; 11 | if (player == 's' && comp == 'p') 12 | return 0; 13 | else if (player == 'p' && comp == 's') return 1; 14 | if (player == 's' && comp == 'z') 15 | return 1; 16 | else if (player == 'z' && comp == 's') 17 | return 0; 18 | if (player == 'p' && comp == 'z') 19 | return 0; 20 | else if (player == 'z' && comp == 'p') 21 | return 1; 22 | } 23 | 24 | int main() 25 | { 26 | 27 | int n; 28 | char player, comp, result; 29 | 30 | srand(time(NULL)); 31 | 32 | 33 | n = rand() % 100; 34 | 35 | if (n < 33) 36 | comp = 's'; 37 | 38 | else if (n > 33 && n < 66) 39 | comp = 'p'; 40 | 41 | 42 | else 43 | comp = 'z'; 44 | 45 | printf("\n\n\n\n\t\t\t\tEnter s for STONE, p for PAPER, z for SCISSOR\n\t\t\t\t\t\t\t"); 46 | 47 | 48 | scanf("%c", &player); 49 | 50 | // Function Call to play the game 51 | result = game(player, comp); 52 | 53 | if (result == -1) { 54 | printf("\n\n\t\t\t\tGame Draw!\n"); 55 | } 56 | else if (result == 1) { 57 | printf("\n\n\t\t\t\tWow! You Won!\n"); 58 | } 59 | else { 60 | printf("\n\n\t\t\t\tOh! You Lost!\n"); 61 | } 62 | printf("\t\t\t\tYou chose : %c and Computer chose : %c\n",player, comp); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /search-element-in-rotated-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int search(vector& arr, int n, int k) { 5 | int low = 0, high = n - 1; 6 | while (low <= high) { 7 | int mid = (low + high) / 2; 8 | 9 | //if mid points the target 10 | if (arr[mid] == k) return mid; 11 | 12 | //if left part is sorted: 13 | if (arr[low] <= arr[mid]) { 14 | if (arr[low] <= k && k <= arr[mid]) { 15 | //element exists: 16 | high = mid - 1; 17 | } 18 | else { 19 | //element does not exist: 20 | low = mid + 1; 21 | } 22 | } 23 | else { //if right part is sorted: 24 | if (arr[mid] <= k && k <= arr[high]) { 25 | //element exists: 26 | low = mid + 1; 27 | } 28 | else { 29 | //element does not exist: 30 | high = mid - 1; 31 | } 32 | } 33 | } 34 | return -1; 35 | } 36 | 37 | int main() 38 | { 39 | vector arr = {7, 8, 9, 1, 2, 3, 4, 5, 6}; 40 | int n = 9, k = 1; 41 | int ans = search(arr, n, k); 42 | if (ans == -1) 43 | cout << "Target is not present.\n"; 44 | else 45 | cout << "The index is: " << ans << "\n"; 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /searching a user entered number in an array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main () 4 | { 5 | const int y=10; 6 | int array [y] ={1,88,65,44,55,174,5,4,3,62}; 7 | int search ; 8 | cout<<" enter the number u wanted to search "; 9 | cin>>search; 10 | for (int i=0;i 2 | 3 | using namespace std; 4 | 5 | void selection_sort(int a[],int n){ 6 | for(int i=0;i>n; 20 | int a[100]; 21 | for(int i=0;i<=n-1;i++){ 22 | cin>>a[i]; 23 | } 24 | selection_sort(a,n); 25 | for(int i=0;i<=n-1;i++){ 26 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool compare(pair &a,pair &b){ 6 | if(a.second==b.second){ 7 | return a.firstb.second; 10 | } 11 | 12 | void func(int a[],int n){ 13 | unordered_map m; 14 | for(int i=0;i> vec; 18 | for(auto p:m){ 19 | vec.push_back(p); 20 | 21 | } 22 | sort(vec.begin(),vec.end(),compare); 23 | for(int i=0;i>t; 36 | for 37 | int n; 38 | cin>>n; 39 | int a[n]; 40 | for(int i=0;i>a[i]; 42 | 43 | } 44 | func(a,n); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /string remove duplicates.cpp: -------------------------------------------------------------------------------- 1 | /*Take as input S, a string. Write a function that removes all consecutive duplicates. Print the value returned. 2 | 3 | Input Format 4 | String 5 | 6 | Constraints 7 | A string of length between 1 to 1000 8 | 9 | Output Format 10 | String 11 | 12 | Sample Input 13 | aabccba 14 | Sample Output 15 | abcba 16 | Explanation 17 | For the given example, "aabccba", Consecutive Occurrence of a is 2, b is 1, and c is 2. 18 | 19 | After removing all of the consecutive occurences, the Final ans will be : - "abcba".*/ 20 | 21 | #include 22 | #include 23 | using namespace std; 24 | void remove_duplicates(char a[]){ 25 | int l=strlen(a); 26 | if(l==0 or l==1){ 27 | return; 28 | } 29 | int pre=0; 30 | for(int current=1;current 2 | #include 3 | using namespace std; 4 | 5 | int string_to_int(char *a,int n){ 6 | if(n==0){ 7 | return 0; 8 | 9 | } 10 | int digit=a[n-1]-'0'; 11 | int small_ans=string_to_int(a,n-1); 12 | return small_ans*10+digit; 13 | } 14 | int main(){ 15 | char a[]="1234"; 16 | int n=strlen(a); 17 | int x=string_to_int(a,n); 18 | cout< 2 | using namespace std; 3 | void generate_subsequences(char *in,char *out,int i,int j){ 4 | //base case 5 | if(in[i]=='\0'){ 6 | out[j]='\0'; 7 | cout< 2 | #include 3 | using namespace std; 4 | 5 | void subsets(vector> &ans, vector &temp, vector &nums, int i){ 6 | if (i>=nums.size()){ 7 | ans.push_back(temp); 8 | return; 9 | } 10 | subsets(ans,temp,nums,i+1); 11 | temp.push_back(nums[i]); 12 | 13 | subsets(ans,temp,nums,i+1); 14 | temp.pop_back(); 15 | return; 16 | } 17 | 18 | int main(){ 19 | vector nums={1,2,3}; 20 | vector> ans; 21 | vector temp; 22 | subsets(ans,temp,nums,0); 23 | 24 | for (auto x : ans) 25 | { 26 | if(x.size()>0) 27 | cout<<"["; 28 | for (auto y : x) 29 | { 30 | if(y==x[x.size()-1]) 31 | cout << y <<"]"; 32 | else 33 | cout << y <<", "; 34 | } 35 | cout << endl; 36 | } 37 | } -------------------------------------------------------------------------------- /sum of array.py: -------------------------------------------------------------------------------- 1 | # Python 3 code to find sum 2 | # of elements in given array 3 | 4 | 5 | def _sum(arr): 6 | 7 | # initialize a variable 8 | # to store the sum 9 | # while iterating through 10 | # the array later 11 | sum = 0 12 | 13 | # iterate through the array 14 | # and add each element to the sum variable 15 | # one at a time 16 | for i in arr: 17 | sum = sum + i 18 | 19 | return(sum) 20 | 21 | 22 | # driver function 23 | arr = [] 24 | # input values to list 25 | arr = [12, 3, 4, 15] 26 | 27 | # calculating length of array 28 | n = len(arr) 29 | 30 | ans = _sum(arr) 31 | 32 | # display sum 33 | print('Sum of the array is ', ans) -------------------------------------------------------------------------------- /trailingzeroes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int countTrailingZeros(int n) 6 | { 7 | int res = 0; 8 | 9 | for(int i=5; i<=n; i=i*5) 10 | { 11 | res = res + (n / i); 12 | } 13 | 14 | return res; 15 | } 16 | int main() { 17 | 18 | int number = 251; 19 | 20 | cout< 20 | using namespace std; 21 | int main(){ 22 | int n; 23 | cin>>n; 24 | int a[100000]; 25 | for(int i=0;i>a[i]; 27 | } 28 | 29 | int ans=0; 30 | for(int i=0;i