├── 21 days ├── Day - 01 │ ├── (GFG)Count nodes of Linked List.cpp │ ├── (GFG)Linked List Length Even or Odd.cpp │ └── (GFG)Reverse a Linked List.cpp ├── Day - 02 │ ├── (GFG)Check if circular linked list.cpp │ ├── (GFG)Delete Alternate node.cpp │ └── (GFG)Print Linked List Elements.cpp ├── Day - 10 │ ├── (GFG)Equilibrium index of an array.cpp │ └── (GFG)Rotate by 90 Degree.cpp ├── Day - 11 │ └── Swapping Pair make sum equal.cpp ├── Day - 14 │ ├── (GFG)Count the zeros.cpp │ ├── (GFG)Factorials of large numbers.cpp │ └── (GFG)Star Elements.cpp ├── Day - 16 │ ├── (GFG)CamelCase Pattern Matching.cpp │ └── (GFG)Drive the car.cpp ├── Day - 18 │ ├── (GFG)Fill array with 1's.cpp │ ├── (GFG)Reorganize the array.cpp │ └── (GFG)You and your books.cpp ├── Day - 19 │ ├── (GFG)Check if Linked List is Palindrome.cpp │ ├── (GFG)Find the highest number.cpp │ └── (GFG)Good or Bad String.cpp ├── Day - 20 │ ├── (GFG)Chocolate Station.cpp │ ├── (GFG)Find the length of loop.cpp │ ├── (GFG)Find the maximum number of handshakes.cpp │ └── (GFG)Two Mirror Trees.cpp ├── Day - 21 │ ├── (GFG)Determine if Two Trees are Identical.cpp │ ├── (GFG)Extract Maximum.cpp │ ├── (GFG)Meta Strings.cpp │ └── (GFG)Roman number to integer.cpp ├── Day - 22 │ ├── (GFG)Element with left side smaller and right side greater.cpp │ └── (GFG)K - Palindrome.cpp ├── Day - 3 │ └── Linked List All Operations.cpp ├── Day - 4 │ ├── (GFG)print the elements of array.cpp │ ├── Euclidean Algorithm │ │ ├── Euclid's alogithm.cpp │ │ └── Explanation │ ├── Extended Euclidean Algorithm │ └── Extended Euclidean Algorithm Explanation ├── Day - 5 │ ├── Implementation of Sieve of Erathosthenes.cpp │ └── Sieve of Eratosthenes Explanation ├── Day - 6 │ ├── (GFG)Multiply array elements.cpp │ └── (GFG)Sum of array.cpp ├── Day - 7 │ ├── (GFG)Balanced Array.cpp │ └── (GFG)Count the zeros.cpp ├── Day - 8 │ ├── (GFG)Minimum number to form the sum even.cpp │ └── (GFG)Number of occurrence.cpp ├── Day - 9 │ ├── (GFG)Check Set Bits.cpp │ ├── (GFG)How many X's.cpp │ └── (GFG)Sorted matrix.cpp ├── Day -12 │ └── Maximum and Minimum Of Array Elements.cpp ├── Day -13 │ ├── (GFG)Anagram of String.cpp │ ├── (GFG)Bubble Sort.cpp │ ├── (GFG)Children Sum Parent.cpp │ └── (GFG)Sum of all substrings of a number.cpp ├── Day -15 │ ├── (GFG)Binary array sorting.cpp │ ├── (GFG)Count Number of Hops.cpp │ ├── (GFG)Decode the pattern.cpp │ ├── (GFG)Evaluate postfix expression.cpp │ ├── (GFG)Finding Position.cpp │ ├── (GFG)Reach a given score.cpp │ ├── (GFG)Remove character.cpp │ ├── (GFG)Repetitive addition of digits.cpp │ ├── (GFG)Student Record.cpp │ └── (GFG)URLify a given string.cpp └── Day -17 │ └── (GFG)Knight Walk.cpp └── README.md /21 days/Day - 01/(GFG)Count nodes of Linked List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node* next; 8 | 9 | Node(int x){ 10 | data = x; 11 | next = NULL; 12 | } 13 | }; 14 | 15 | int getCount(struct Node* head); 16 | 17 | int main() 18 | { 19 | int t; 20 | cin>>t; 21 | while(t--) 22 | { 23 | int n; 24 | cin>>n; 25 | 26 | int data; 27 | cin>>data; 28 | struct Node *head = new Node(data); 29 | struct Node *tail = head; 30 | for (int i = 0; i < n-1; ++i) 31 | { 32 | cin>>data; 33 | tail->next = new Node(data); 34 | tail = tail->next; 35 | } 36 | cout << getCount(head) << endl; 37 | } 38 | return 0; 39 | } 40 | 41 | int getCount(struct Node* head){ 42 | int cnt=0; 43 | Node*curr=head; 44 | while(curr!= NULL) 45 | {cnt++; 46 | curr = curr->next; 47 | } 48 | return cnt; 49 | } 50 | -------------------------------------------------------------------------------- /21 days/Day - 01/(GFG)Linked List Length Even or Odd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node* next; 8 | 9 | Node(int x){ 10 | data = x; 11 | next = NULL; 12 | } 13 | }; 14 | 15 | int getCount(struct Node* head); 16 | 17 | int main() 18 | { 19 | int t; 20 | cin>>t; 21 | while(t--) 22 | { 23 | int n; 24 | cin>>n; 25 | 26 | int data; 27 | cin>>data; 28 | struct Node *head = new Node(data); 29 | struct Node *tail = head; 30 | for (int i = 0; i < n-1; ++i) 31 | { 32 | cin>>data; 33 | tail->next = new Node(data); 34 | tail = tail->next; 35 | } 36 | cout << getCount(head) << endl; 37 | } 38 | return 0; 39 | } 40 | 41 | 42 | int getCount(struct Node* head){ 43 | int cnt=0; 44 | Node*curr=head; 45 | while(curr!= NULL) 46 | {cnt++; 47 | curr = curr->next; 48 | } 49 | return cnt; 50 | } 51 | -------------------------------------------------------------------------------- /21 days/Day - 01/(GFG)Reverse a Linked List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | struct Node *next; 7 | Node(int x) 8 | { 9 | data = x; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | struct Node *reverseList(struct Node *head); 15 | 16 | void printList(struct Node *head) 17 | { 18 | struct Node *temp = head; 19 | while (temp != NULL) 20 | { 21 | printf("%d ", temp->data); 22 | temp = temp->next; 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | int T,n,l,firstdata; 29 | cin>>T; 30 | 31 | while(T--) 32 | { 33 | struct Node *head = NULL, *tail = NULL; 34 | 35 | cin>>n; 36 | 37 | cin>>firstdata; 38 | head = new Node(firstdata); 39 | tail = head; 40 | 41 | for (int i=1; i>l; 44 | tail->next = new Node(l); 45 | tail = tail->next; 46 | } 47 | 48 | head = reverseList(head); 49 | 50 | printList(head); 51 | cout << endl; 52 | } 53 | return 0; 54 | } 55 | 56 | 57 | struct Node* reverseList(struct Node *head) 58 | { 59 | 60 | Node* current = head; 61 | Node *prev = NULL, *next = NULL; 62 | 63 | while (current != NULL) { 64 | 65 | next = current->next; 66 | current->next = prev; 67 | prev = current; 68 | current = next; 69 | } 70 | head = prev; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /21 days/Day - 02/(GFG)Check if circular linked list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | /* Link list Node */ 7 | struct Node 8 | { 9 | int data; 10 | struct Node* next; 11 | 12 | Node(int x){ 13 | data = x; 14 | next = NULL; 15 | } 16 | 17 | }; 18 | 19 | int main() 20 | { 21 | int T,i,n,l,k; 22 | 23 | cin>>T; 24 | 25 | while(T--){ 26 | 27 | cin>>n>>k; 28 | Node *head=NULL, *tail = NULL; 29 | int x; 30 | cin >> x; 31 | head = new Node(x); 32 | tail = head; 33 | for(int i=0;i>x; 36 | tail -> next = new Node(x); 37 | tail = tail -> next; 38 | } 39 | if (k==1 && n >= 1) 40 | tail->next = head; 41 | 42 | 43 | printf("%d\n", isCircular(head)); 44 | } 45 | return 0; 46 | } 47 | 48 | 49 | bool isCircular(Node *head) 50 | { 51 | if (head == NULL) 52 | return true; 53 | struct Node *node = head->next; 54 | 55 | while (node != NULL && node != head) 56 | node = node->next; 57 | return (node == head); 58 | } 59 | -------------------------------------------------------------------------------- /21 days/Day - 02/(GFG)Delete Alternate node.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | 9 | Node(int x){ 10 | int data = x; 11 | next = NULL; 12 | } 13 | }; 14 | 15 | void deleteAlt(struct Node *head); 16 | 17 | void printList(struct Node *node) 18 | { 19 | while (node != NULL) 20 | { 21 | cout<data<<' '; 22 | node = node->next; 23 | } 24 | cout<>t; 31 | while(t--){ 32 | int n, tmp; 33 | struct Node* head = NULL; 34 | cin>>n; 35 | while(n--){ 36 | cin>>tmp; 37 | append(&head, tmp); 38 | } 39 | deleteAlt(head); 40 | printList(head); 41 | } 42 | return 0; 43 | } 44 | 45 | void deleteAlt(Node *head) 46 | { 47 | if (head == NULL) 48 | return; 49 | 50 | Node *node = head->next; 51 | 52 | if (node == NULL) 53 | return; 54 | 55 | head->next = node->next; 56 | free(node); 57 | deleteAlt(head->next); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /21 days/Day - 02/(GFG)Print Linked List Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct Node 8 | { 9 | int data; 10 | struct Node *next; 11 | }*start; 12 | 13 | void insert(); 14 | void display(Node *head); 15 | 16 | int main() 17 | { 18 | int t; 19 | cin>>t; 20 | while(t--) 21 | { 22 | start=NULL; 23 | insert(); 24 | display(start); 25 | cout<>n; 36 | struct Node *temp; 37 | for(int i=0;i>value; 40 | if(i==0) 41 | { 42 | start=(struct Node *) malloc( sizeof(struct Node) ); 43 | start->data=value; 44 | start->next=NULL; 45 | temp=start; 46 | continue; 47 | } 48 | else 49 | { 50 | temp->next= (struct Node *) malloc( sizeof(struct Node) ); 51 | temp=temp->next; 52 | temp->data=value; 53 | temp->next=NULL; 54 | } 55 | } 56 | } 57 | 58 | 59 | void display(struct Node *head) 60 | { 61 | if(head!=NULL) 62 | { 63 | cout<< head->data<<" "; 64 | display(head->next); 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /21 days/Day - 10/(GFG)Equilibrium index of an array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | 5 | int findEquilibrium(int [], int ); 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--) 11 | { 12 | int n; 13 | cin>>n; 14 | int a[n]; 15 | for(int i=0;i>a[i]; 17 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | int n; 7 | 8 | cin >> t; 9 | for (int x = 0; x < t; x++) { 10 | cin >> n; 11 | int m[n][n]; 12 | for (int i = 0; i < n; i++) { 13 | for (int j = 0; j < n; j++) { 14 | cin >> m[i][j]; 15 | } 16 | } 17 | 18 | for (int j = n - 1; j >= 0; j--) { 19 | for(int i = 0; i < n; i++) { 20 | cout << m[i][j] << ' '; 21 | } 22 | } 23 | cout << '\n'; 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /21 days/Day - 11/Swapping Pair make sum equal.cpp: -------------------------------------------------------------------------------- 1 | include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; cin>>t; while(t--){ 6 | int n,m; cin>>n>>m; 7 | int a[n],b[m],sa=0,sb=0; 8 | for(int i=0; i>a[i]; sa+=a[i]; } 9 | for(int i=0; i>b[i]; sb+=b[i]; } 10 | int diff= abs(sa-sb) , c=0; 11 | set ma; 12 | for(int i=0; i 2 | using namespace std; 3 | 4 | class Solution{ 5 | public: 6 | int countZeroes(int arr[], int n) { 7 | int cnt=0; 8 | for(int i=0;i> t; 22 | while (t--) { 23 | int n; 24 | cin >> n; 25 | int arr[n]; 26 | for (int i = 0; i < n; i++) { 27 | cin >> arr[i]; 28 | } 29 | Solution ob; 30 | auto ans = ob.countZeroes(arr, n); 31 | cout << ans << "\n"; 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /21 days/Day - 14/(GFG)Factorials of large numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAX 1000 5 | 6 | int multiply(int x, int res[], int res_size); 7 | 8 | void factorial(int n) 9 | { 10 | int res[MAX]; 11 | 12 | res[0] = 1; 13 | int res_size = 1; 14 | 15 | 16 | for (int x=2; x<=n; x++) 17 | res_size = multiply(x, res, res_size); 18 | 19 | for (int i=res_size-1; i>=0; i--) 20 | { cout << res[i];} 21 | cout<>t; 52 | while(t--){ 53 | cin>>n; 54 | factorial(n); 55 | } 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /21 days/Day - 14/(GFG)Star Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int test; 9 | cin >> test; 10 | 11 | while(test--){ 12 | int n; 13 | cin >> n; 14 | 15 | vector temp(n); 16 | 17 | for(int i = 0; i < n; i++){ 18 | cin >> temp[i]; 19 | } 20 | 21 | vector rmax(n, 0) ; 22 | rmax[n-1] = 1; 23 | 24 | int maxi = temp[n-1], last = n-1; 25 | bool super = true; 26 | 27 | for(int i = n-2; i >= 0; i--){ 28 | if(temp[i] > maxi){ 29 | rmax[i] = 1; 30 | maxi = temp[i]; 31 | super = true; 32 | } 33 | else if(temp[i] == maxi){ 34 | super = false; 35 | } 36 | } 37 | 38 | for(int i = 0; i < n; i++){ 39 | if(rmax[i]){ 40 | last = min(last, i); 41 | cout << temp[i] << " "; 42 | } 43 | } 44 | 45 | cout << endl; 46 | 47 | if(!super){ 48 | cout << "-1"; 49 | } 50 | else{ 51 | cout << temp[last]; 52 | } 53 | 54 | cout << endl; 55 | } 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /21 days/Day - 16/(GFG)CamelCase Pattern Matching.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string findCamel(string s) { 5 | string res=""; 6 | for(int i=0;i='A' && s[i]<='Z') 8 | res+=s[i]; 9 | 10 | return res; 11 | } 12 | 13 | int lcsq(string X, string Y) { 14 | for(int i=0;i>t; 25 | while(t--) { 26 | 27 | int n; 28 | cin>>n; 29 | map> mp; 30 | 31 | for(int i=0;i>temp; 35 | 36 | string temp2=findCamel(temp); 37 | mp[temp2].push_back(temp); 38 | } 39 | 40 | string pattern; 41 | cin>>pattern; 42 | int flag=0; 43 | 44 | for(auto i=mp.begin();i!=mp.end();i++) { 45 | vector v=i->second; 46 | if(lcsq(pattern,i->first)) 47 | for(int j=0;j 2 | using namespace std; 3 | 4 | int getMinPetrolRequired(int arr[], int n, int k) { 5 | 6 | int refill = 0; 7 | 8 | for (int i = 0; i < n; ++i) { 9 | 10 | if (arr[i] > k) { 11 | refill += arr[i] - k; 12 | k = arr[i]; 13 | } 14 | 15 | } 16 | 17 | if (refill == 0) 18 | return -1; 19 | else 20 | return refill; 21 | } 22 | 23 | int main() { 24 | 25 | int t; 26 | cin >> t; 27 | while (t--) { 28 | 29 | int n, k; 30 | cin >> n >> k; 31 | 32 | int arr[n]; 33 | for (int i = 0; i < n; ++i) 34 | cin >> arr[i]; 35 | 36 | int res = getMinPetrolRequired(arr, n, k); 37 | cout << res << '\n'; 38 | 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /21 days/Day - 18/(GFG)Fill array with 1's.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int test; 9 | cin >> test; 10 | 11 | while(test--){ 12 | int n; 13 | cin >> n; 14 | 15 | vector temp(n); 16 | 17 | for(int i = 0; i < n; i++){ 18 | cin >> temp[i]; 19 | } 20 | 21 | int last = -1, i = 0, ans = -1; 22 | 23 | while(i < n){ 24 | if(temp[i] == 1){ 25 | if(last == -1){ 26 | ans = i; 27 | } 28 | else{ 29 | ans = max(ans, (i-last)/2); 30 | } 31 | last = i; 32 | } 33 | 34 | if(i == n-1 && temp[i] == 0){ 35 | ans = max(ans, i-last); 36 | } 37 | 38 | i++; 39 | } 40 | 41 | if(last == -1){ 42 | ans = -1; 43 | } 44 | 45 | cout << ans << endl; 46 | 47 | } 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /21 days/Day - 18/(GFG)Reorganize the array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void printVector(vector& temp){ 7 | for(int i = 0; i < temp.size(); i++){ 8 | cout << temp[i] << " "; 9 | } 10 | } 11 | 12 | void swap(int& a, int& b){ 13 | int temp = a; 14 | a = b; 15 | b = temp; 16 | } 17 | 18 | void rearrangeVector(vector& temp){ 19 | int n = temp.size(); 20 | 21 | for(int i = 0; i < n; i++){ 22 | while(temp[i] != -1 && temp[i] < n && temp[i] != i){ 23 | if(temp[i] == temp[temp[i]]){ 24 | break; 25 | } 26 | swap(temp[i], temp[temp[i]]); 27 | } 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | //code 34 | int test; 35 | cin >> test; 36 | 37 | while(test--){ 38 | int n; 39 | cin >> n; 40 | 41 | vector temp(n); 42 | 43 | for(int i = 0; i < n; i++){ 44 | cin >> temp[i]; 45 | } 46 | 47 | rearrangeVector(temp); 48 | printVector(temp); 49 | cout << endl; 50 | } 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /21 days/Day - 18/(GFG)You and your books.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int max_Books(int[], int, int); 8 | int main() 9 | { 10 | int t; 11 | cin>>t; 12 | while(t--) 13 | { 14 | int n,k; 15 | cin>>n>>k; 16 | int ar[n]; 17 | for(int i=0;i>ar[i]; 20 | } 21 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | /* Link list Node */ 7 | struct Node { 8 | int data; 9 | struct Node *next; 10 | Node(int x) { 11 | data = x; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | 17 | 18 | bool isPalindrome(Node *head); 19 | /* Driver program to test above function*/ 20 | int main() 21 | { 22 | int T,i,n,l,firstdata; 23 | cin>>T; 24 | while(T--) 25 | { 26 | 27 | struct Node *head = NULL, *tail = NULL; 28 | cin>>n; 29 | // taking first data of LL 30 | cin>>firstdata; 31 | head = new Node(firstdata); 32 | tail = head; 33 | // taking remaining data of LL 34 | for(i=1;i>l; 37 | tail->next = new Node(l); 38 | tail = tail->next; 39 | } 40 | cout<next)){ 62 | return true; 63 | } 64 | 65 | Node* slow = head; 66 | Node* fast = head; 67 | 68 | while(fast != NULL){ 69 | slow = slow->next; 70 | if(fast->next == NULL){ 71 | break; 72 | } 73 | fast = (fast->next)->next; 74 | } 75 | 76 | Node* prev = NULL; 77 | 78 | while(slow){ 79 | Node* temp = slow->next; 80 | slow->next = prev; 81 | prev = slow; 82 | slow = temp; 83 | } 84 | 85 | Node* curr = head; 86 | 87 | while(curr && prev){ 88 | if(curr->data != prev->data){ 89 | return false; 90 | } 91 | curr = curr->next; 92 | prev = prev->next; 93 | } 94 | 95 | return true; 96 | } 97 | -------------------------------------------------------------------------------- /21 days/Day - 19/(GFG)Find the highest number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class Solution { 6 | public: 7 | int findPeakElement(vector& temp){ 8 | int ans = temp[0], n = temp.size(); 9 | 10 | for(int i = 1; i < n; i++){ 11 | if(temp[i] < ans){ 12 | break; 13 | } 14 | ans = temp[i]; 15 | } 16 | 17 | return ans; 18 | } 19 | }; 20 | 21 | int main(){ 22 | int T; 23 | cin >> T; 24 | while(T--) 25 | { 26 | int n; 27 | cin >> n; 28 | vectora(n); 29 | for(int i = 0; i < n; i++) 30 | cin>>a[i]; 31 | Solution ob; 32 | int ans = ob.findPeakElement(a); 33 | cout << ans << "\n"; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /21 days/Day - 19/(GFG)Good or Bad String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool isVowel(char c){ 6 | if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){ 7 | return true; 8 | } 9 | return false; 10 | } 11 | 12 | int main() 13 | { 14 | int test; 15 | cin >> test; 16 | 17 | while(test--){ 18 | string s; 19 | cin >> s; 20 | 21 | int n = s.size(), i = 0, vCount = 0, cCount = 0, ans = 1; 22 | 23 | while(i < n){ 24 | if(s[i] == '?'){ 25 | vCount++; 26 | cCount++; 27 | } 28 | else if(isVowel(s[i])){ 29 | vCount++; 30 | cCount = 0; 31 | } 32 | else{ 33 | cCount++; 34 | vCount = 0; 35 | } 36 | if(vCount == 6 || cCount == 4){ 37 | ans = 0; 38 | break; 39 | } 40 | i++; 41 | } 42 | 43 | cout << ans << endl; 44 | } 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /21 days/Day - 20/(GFG)Chocolate Station.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int test; 9 | cin >> test; 10 | 11 | while(test--){ 12 | int n, p; 13 | cin >> n; 14 | 15 | vector temp(n); 16 | 17 | for(int i = 0; i < n; i++){ 18 | cin >> temp[i]; 19 | } 20 | 21 | cin >> p; 22 | 23 | int curr = 0, last = 0, ans = 0; 24 | 25 | for(int i = 0; i < n; i++){ 26 | curr = curr + last - temp[i]; 27 | last = temp[i]; 28 | if(curr < 0){ 29 | ans -= curr; 30 | curr = 0; 31 | } 32 | } 33 | 34 | cout << ans*p << endl; 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /21 days/Day - 20/(GFG)Find the length of loop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; 7 | Node* next; 8 | 9 | Node(int val) 10 | { 11 | data = val; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | void loopHere(Node* head, Node* tail, int position) 17 | { 18 | if(position==0) return; 19 | 20 | Node* walk = head; 21 | for(int i=1; inext; 23 | tail->next = walk; 24 | } 25 | 26 | int countNodesinLoop(Node* head); 27 | 28 | int main() 29 | { 30 | int t; 31 | cin>>t; 32 | while(t--) 33 | { 34 | int n, num; 35 | cin>>n; 36 | 37 | Node *head, *tail; 38 | cin>> num; 39 | head = tail = new Node(num); 40 | 41 | for(int i=0 ; i> num; 44 | tail->next = new Node(num); 45 | tail = tail->next; 46 | } 47 | 48 | int pos; 49 | cin>> pos; 50 | loopHere(head,tail,pos); 51 | 52 | cout<< countNodesinLoop(head) << endl; 53 | } 54 | return 0; 55 | } 56 | 57 | 58 | 59 | int countNodesinLoop(struct Node *head) 60 | { 61 | if(!head){ 62 | return 0; 63 | } 64 | 65 | Node* slow = head, *fast = head->next; 66 | 67 | while(fast != slow){ 68 | if(!fast || !fast->next){ 69 | return 0; 70 | } 71 | slow = slow->next; 72 | fast = (fast->next)->next; 73 | } 74 | 75 | Node* temp = slow; 76 | 77 | int len = 0; 78 | 79 | do{ 80 | len++; 81 | temp = temp->next; 82 | }while(slow != temp); 83 | 84 | return len; 85 | } 86 | -------------------------------------------------------------------------------- /21 days/Day - 20/(GFG)Find the maximum number of handshakes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int test; 8 | cin >> test; 9 | 10 | while(test--){ 11 | long long int n; 12 | cin >> n; 13 | 14 | cout << (n*(n-1))/2 << endl; 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /21 days/Day - 20/(GFG)Two Mirror Trees.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Tree Node 6 | struct Node { 7 | int data; 8 | Node *left; 9 | Node *right; 10 | 11 | Node(int val) { 12 | data = val; 13 | left = right = NULL; 14 | } 15 | }; 16 | 17 | // Function to Build Tree 18 | Node *buildTree(string str) { 19 | // Corner Case 20 | if (str.length() == 0 || str[0] == 'N') return NULL; 21 | 22 | // Creating vector of strings from input 23 | // string after spliting by space 24 | vector ip; 25 | 26 | istringstream iss(str); 27 | for (string str; iss >> str;) ip.push_back(str); 28 | 29 | // Create the root of the tree 30 | Node *root = new Node(stoi(ip[0])); 31 | 32 | // Push the root to the queue 33 | queue queue; 34 | queue.push(root); 35 | 36 | // Starting from the second element 37 | int i = 1; 38 | while (!queue.empty() && i < ip.size()) { 39 | 40 | // Get and remove the front of the queue 41 | Node *currNode = queue.front(); 42 | queue.pop(); 43 | 44 | // Get the current Node's value from the string 45 | string currVal = ip[i]; 46 | 47 | // If the left child is not null 48 | if (currVal != "N") { 49 | 50 | // Create the left child for the current Node 51 | currNode->left = new Node(stoi(currVal)); 52 | 53 | // Push it to the queue 54 | queue.push(currNode->left); 55 | } 56 | 57 | // For the right child 58 | i++; 59 | if (i >= ip.size()) break; 60 | currVal = ip[i]; 61 | 62 | // If the right child is not null 63 | if (currVal != "N") { 64 | 65 | // Create the right child for the current Node 66 | currNode->right = new Node(stoi(currVal)); 67 | 68 | // Push it to the queue 69 | queue.push(currNode->right); 70 | } 71 | i++; 72 | } 73 | 74 | return root; 75 | } 76 | 77 | int areMirror(Node *, Node *); 78 | 79 | int main() { 80 | int tc; 81 | scanf("%d ", &tc); 82 | while (tc--) { 83 | string treeString1, treeString2; 84 | getline(cin, treeString1); 85 | Node *root1 = buildTree(treeString1); 86 | 87 | getline(cin, treeString2); 88 | Node *root2 = buildTree(treeString2); 89 | 90 | cout << areMirror(root1, root2) << "\n"; 91 | } 92 | return 0; 93 | }// } Driver Code Ends 94 | 95 | 96 | /* Node structure 97 | struct Node 98 | { 99 | int data; 100 | Node* left, *right; 101 | }; */ 102 | 103 | /* Given two trees, should return true if they are 104 | mirror of each other. */ 105 | int areMirror(Node* a, Node* b) 106 | { 107 | if(!a && !b){ 108 | return 1; 109 | } 110 | else if(!a || !b){ 111 | return 0; 112 | } 113 | else if(a->data != b->data){ 114 | return 0; 115 | } 116 | return areMirror(a->left, b->right) & areMirror(a->right, b->left); 117 | } 118 | -------------------------------------------------------------------------------- /21 days/Day - 21/(GFG)Determine if Two Trees are Identical.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node *left; 8 | struct Node *right; 9 | 10 | Node(int x){ 11 | data = x; 12 | left = NULL; 13 | right = NULL; 14 | } 15 | }; 16 | 17 | bool isIdentical(Node* a, Node* b); 18 | 19 | Node *buildTree(string str) { 20 | 21 | if (str.length() == 0 || str[0] == 'N') 22 | return NULL; 23 | 24 | vector ip; 25 | 26 | istringstream iss(str); 27 | for (string str; iss >> str;) 28 | ip.push_back(str); 29 | Node *root = new Node(stoi(ip[0])); 30 | 31 | queue queue; 32 | queue.push(root); 33 | 34 | int i = 1; 35 | while (!queue.empty() && i < ip.size()) { 36 | 37 | 38 | Node *currNode = queue.front(); 39 | queue.pop(); 40 | 41 | 42 | string currVal = ip[i]; 43 | 44 | 45 | if (currVal != "N") { 46 | currNode->left = new Node(stoi(currVal)); 47 | 48 | queue.push(currNode->left); 49 | } 50 | 51 | i++; 52 | if (i >= ip.size()) 53 | break; 54 | currVal = ip[i]; 55 | 56 | 57 | if (currVal != "N") { 58 | 59 | 60 | currNode->right = new Node(stoi(currVal)); 61 | 62 | 63 | queue.push(currNode->right); 64 | } 65 | i++; 66 | } 67 | 68 | return root; 69 | } 70 | 71 | int main() { 72 | int tc; 73 | scanf("%d ", &tc); 74 | while (tc--) { 75 | string str, str1; 76 | getline(cin, str); 77 | Node *rootA = buildTree(str); 78 | getline(cin, str1); 79 | Node *rootB = buildTree(str1); 80 | if (isIdentical(rootA, rootB)) { 81 | cout << "Yes\n"; 82 | } else { 83 | cout << "No\n"; 84 | } 85 | } 86 | return 0; 87 | } 88 | 89 | 90 | 91 | bool isIdentical(Node *r1, Node *r2) 92 | { 93 | if(!r1 && !r2){ 94 | return true; 95 | } 96 | else if(!r1 || !r2){ 97 | return false; 98 | } 99 | else if(r1->data != r2->data){ 100 | return false; 101 | } 102 | return isIdentical(r1->left, r2->left) && isIdentical(r1->right, r2->right); 103 | } 104 | -------------------------------------------------------------------------------- /21 days/Day - 21/(GFG)Extract Maximum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | string removeZero(string s){ 7 | int i = 0, n = s.size(); 8 | 9 | while(s[i] == '0' && i < n){ 10 | i++; 11 | } 12 | 13 | string ans = ""; 14 | 15 | while(i < n){ 16 | ans = ans + s[i]; 17 | i++; 18 | } 19 | 20 | return ans; 21 | } 22 | 23 | string chooseMax(string s1, string s2){ 24 | s1 = removeZero(s1); 25 | s2 = removeZero(s2); 26 | 27 | int n1 = s1.size(), n2 = s2.size(); 28 | 29 | if(n1 > n2){ 30 | return s1; 31 | } 32 | else if(n1 < n2){ 33 | return s2; 34 | } 35 | 36 | for(int i = 0; i < n1; i++){ 37 | if(s1[i] < s2[i]){ 38 | return s2; 39 | } 40 | else if(s1[i] > s2[i]){ 41 | return s1; 42 | } 43 | } 44 | 45 | return s1; 46 | } 47 | 48 | int main() { 49 | //code 50 | int test; 51 | cin >> test; 52 | 53 | while(test--){ 54 | string s, temp = "", ans = ""; 55 | cin >> s; 56 | 57 | int i = 0, n = s.size(); 58 | 59 | while(i < n){ 60 | if(isdigit(s[i])){ 61 | temp = temp + s[i]; 62 | } 63 | else{ 64 | temp = ""; 65 | } 66 | ans = chooseMax(temp, ans); 67 | i++; 68 | } 69 | 70 | if(ans.size() == 0){ 71 | ans = "0"; 72 | } 73 | 74 | cout << ans << endl; 75 | } 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /21 days/Day - 21/(GFG)Meta Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int test; 8 | cin >> test; 9 | 10 | while(test){ 11 | string s1, s2; 12 | cin >> s1 >> s2; 13 | 14 | int first = -1, second = -1, n = s1.size(), m = s2.size(), i = 0, count = 0, ans = 1; 15 | 16 | if(n != m){ 17 | ans = 0; 18 | goto ANS; 19 | } 20 | 21 | while(i < n){ 22 | if(s1[i] != s2[i]){ 23 | if(first == -1){ 24 | first = i; 25 | } 26 | else if(second == -1){ 27 | second = i; 28 | if(s1[first] != s2[second]){ 29 | ans = 0; 30 | goto ANS; 31 | } 32 | if(s2[first] != s1[second]){ 33 | ans = 0; 34 | goto ANS; 35 | } 36 | } 37 | 38 | count++; 39 | 40 | if(count > 2){ 41 | ans = 0; 42 | goto ANS; 43 | } 44 | } 45 | 46 | 47 | i++; 48 | } 49 | 50 | if(!count){ 51 | ans = 0; 52 | } 53 | 54 | ANS:cout << ans << endl; 55 | 56 | test--; 57 | } 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /21 days/Day - 21/(GFG)Roman number to integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int getVal(char c){ 7 | if(c == 'I'){ 8 | return 1; 9 | } 10 | else if(c == 'V'){ 11 | return 5; 12 | } 13 | else if(c == 'X'){ 14 | return 10; 15 | } 16 | else if(c == 'L'){ 17 | return 50; 18 | } 19 | else if(c == 'C'){ 20 | return 100; 21 | } 22 | else if(c == 'D'){ 23 | return 500; 24 | } 25 | return 1000; 26 | } 27 | 28 | bool isNextSymbolGreater(char c, char d){ 29 | int val1 = getVal(c); 30 | int val2 = getVal(d); 31 | 32 | if(val1 < val2){ 33 | return true; 34 | } 35 | return false; 36 | } 37 | 38 | int main() { 39 | int test; 40 | cin >> test; 41 | 42 | while(test){ 43 | string s; 44 | cin >> s; 45 | 46 | int ans = 0, n = s.size(), i = 0; 47 | 48 | while(i < n){ 49 | if(i+1 < n && isNextSymbolGreater(s[i], s[i+1])){ 50 | ans = ans - getVal(s[i]); 51 | } 52 | else{ 53 | ans = ans + getVal(s[i]); 54 | } 55 | i++; 56 | } 57 | 58 | cout << ans << endl; 59 | 60 | test--; 61 | } 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /21 days/Day - 22/(GFG)Element with left side smaller and right side greater.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int test; 9 | cin >> test; 10 | 11 | while(test--){ 12 | int n, ans = -1; 13 | cin >> n; 14 | 15 | vector temp(n); 16 | 17 | for(int i = 0; i < n; i++){ 18 | cin >> temp[i]; 19 | } 20 | 21 | vector small(n, -1); 22 | vector great(n, -1); 23 | 24 | int mini = temp[n-1], maxi = temp[0]; 25 | 26 | for(int i = n-2; i >= 0; i--){ 27 | if(temp[i] <= mini){ 28 | small[i] = 1; 29 | mini = temp[i]; 30 | } 31 | } 32 | 33 | for(int i = 1; i < n; i++){ 34 | if(temp[i] >= maxi){ 35 | great[i] = 1; 36 | maxi = temp[i]; 37 | } 38 | } 39 | 40 | for(int i = 0; i < n; i++){ 41 | if(small[i] == 1 && great[i] == 1){ 42 | ans = temp[i]; 43 | break; 44 | } 45 | } 46 | 47 | cout << ans << endl; 48 | } 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /21 days/Day - 22/(GFG)K - Palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | bool is_k_palin(string s,int k); 6 | 7 | int main() { 8 | 9 | int t; 10 | cin>>t; 11 | while(t--) 12 | { 13 | string s ; 14 | cin>>s; 15 | int k; 16 | cin>>k; 17 | 18 | cout<= 0){ 31 | ans += s[n]; 32 | n--; 33 | } 34 | 35 | return ans; 36 | } 37 | 38 | 39 | bool is_k_palin(string s,int k) 40 | { 41 | 42 | int n = s.size(), maxi = 0; 43 | 44 | string revs = rev(s); 45 | 46 | vector > temp(n+1, vector(n+1, 0)); 47 | 48 | for(int i = 1; i <= n; i++){ 49 | for(int j = 1; j <= n; j++){ 50 | if(s[i-1] == revs[j-1]){ 51 | temp[i][j] = 1 + temp[i-1][j-1]; 52 | } 53 | else{ 54 | temp[i][j] = max(temp[i-1][j], temp[i][j-1]); 55 | } 56 | maxi = max(temp[i][j], maxi); 57 | 58 | } 59 | 60 | } 61 | 62 | if(n-maxi <= k){ 63 | return true; 64 | } 65 | 66 | return false; 67 | } 68 | -------------------------------------------------------------------------------- /21 days/Day - 3/Linked List All Operations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef struct node 6 | { 7 | int data; 8 | struct node *next; 9 | }node; 10 | 11 | node *create(); 12 | void display(node*); 13 | node *c_n(); 14 | node *insert_f(node*,int); 15 | node *insert_l(node*,int); 16 | node *insert_b(node*,int,int); 17 | int count_node(node*); 18 | node *delete_f(node*); 19 | node *reverse(node*); 20 | node *sort(node*); 21 | node *remove_dup(node*); 22 | node *merge(node*,node*); 23 | node *merge_sort(node*,node*); 24 | 25 | int main() 26 | { 27 | node *HEAD=NULL,*HEAD1=NULL,*HEAD2=NULL,*HEAD3=NULL; 28 | 29 | int ch,x,pos; 30 | while(1) 31 | { 32 | cout<<"\n******************************************\n"; 33 | cout<<"\n Linked List Different Operations \n"; 34 | cout<<"\n******************************************\n"; 35 | cout<<"\n1 : Create SLL"; 36 | cout<<"\n2 : Display SLL"; 37 | cout<<"\n3 : Insert First Node in SLL"; 38 | cout<<"\n4 : Insert Last Node in SLL"; 39 | cout<<"\n5 : Insert Between Node in SLL"; 40 | cout<<"\n6 : Delete First Node From SLL"; 41 | cout<<"\n7 : Delete Last Node From SLL"; 42 | cout<<"\n8 : Delete Between Node From SLL"; 43 | cout<<"\n9 : Count Nodes in SLL"; 44 | cout<<"\n10 : Delete Duplicate SLL"; 45 | cout<<"\n11 : Exit"; 46 | cout<<"\n**************************************\n"; 47 | 48 | cout<<"Enter Your Choice : "; 49 | cin>>ch; 50 | 51 | switch(ch) 52 | { 53 | case 1: 54 | HEAD = create(); 55 | cout<<"\nCreate Success SLL"; 56 | break; 57 | case 2: 58 | display(HEAD); 59 | break; 60 | case 3: 61 | cout<<"Enter Data : "; 62 | cin>>x; 63 | HEAD = insert_f(HEAD,x); 64 | cout<<"\nInsert Success"; 65 | break; 66 | case 4: 67 | cout<<"Enter Data : "; 68 | cin>>x; 69 | HEAD = insert_l(HEAD,x); 70 | cout<<"\nInsert Success"; 71 | break; 72 | case 5: 73 | cout<<"Enter Data & Position: "; 74 | cin>>x>>pos; 75 | HEAD = insert_b(HEAD,x,pos); 76 | cout<<"\nInsert Success"; 77 | break; 78 | case 6: 79 | HEAD = delete_f(HEAD); 80 | break; 81 | 82 | case 9: 83 | x=count_node(HEAD); 84 | cout<<"\nNumber Of Nodes are "<>n;//3 108 | head =c_n(); 109 | cout<<"Enter Data : "; 110 | cin>>x;//30 111 | head->data=x; 112 | head->next=NULL; 113 | 114 | p=head; 115 | 116 | while(i>x; 121 | q->data=x; 122 | q->next=NULL; 123 | 124 | p->next=q; 125 | p = p->next;//p = 3002 126 | 127 | i++;//2++ 128 | } 129 | 130 | return head;//1002 131 | 132 | } 133 | void display(node *head)//1002 134 | { 135 | node *p; 136 | p=head; 137 | if(p==NULL) 138 | cout<<"\nEmpty SLL"; 139 | else 140 | { 141 | 142 | p=head; 143 | while(p!=NULL) 144 | { 145 | cout<data<<"\t"; 146 | p=p->next; 147 | } 148 | 149 | } 150 | } 151 | //SLL 152 | //p head 153 | //5|1002 10|2002 20|3002 30|NULL 154 | //4002 1002 2002 3002 155 | //============================== 156 | node *insert_f(node *head,int x)//1002 157 | { 158 | node *p; 159 | 160 | if(head==NULL) 161 | { 162 | head=c_n(); 163 | head->data=x; 164 | head->next=NULL; 165 | return head; 166 | } 167 | else 168 | { 169 | p=c_n(); 170 | p->data=x; 171 | p->next=head; 172 | head=p; 173 | } 174 | return head;//4002 175 | } 176 | node *insert_l(node *head,int x) 177 | { 178 | node *p,*q; 179 | if(head==NULL) 180 | { 181 | head=c_n(); 182 | head->data=x; 183 | head->next=NULL; 184 | return head; 185 | } 186 | p=head; 187 | while(p->next!=NULL)//NULL!=NULL 188 | p=p->next;//p=3002 189 | q=c_n(); 190 | q->data=x; 191 | q->next=NULL; 192 | p->next=q; 193 | return head; 194 | } 195 | node *insert_b(node *head,int x,int pos) 196 | { 197 | node *p,*q; 198 | int i=1 , n ; 199 | char ch; 200 | n=count_node(head);//n=0 201 | if(n==pos-1)//0==4 202 | { 203 | head = insert_l(head,x); 204 | return head; 205 | } 206 | if(nnext; 228 | i++;//2++ 229 | } 230 | 231 | q=c_n(); 232 | q->data=x; 233 | q->next=p->next; 234 | p->next=q; 235 | 236 | return head; 237 | } 238 | //head p 239 | //10|2002 20|3002 30|5002 35|4002 40|NULL 240 | //1002 2002 3002 5002 4002 241 | 242 | int count_node(node *p) 243 | { 244 | int count=0; 245 | while(p!=NULL)//NULL!=NULL 246 | { 247 | count++;//5 248 | p=p->next;//p=NULL 249 | } 250 | return count;//5 251 | } 252 | node *delete_f(node *head) 253 | { 254 | node *p; 255 | if(head==NULL) 256 | { 257 | cout<<"\nDelete Failed!!!"; 258 | return head; 259 | } 260 | p=head; 261 | head=head->next; 262 | free(p); 263 | cout<<"\nSuccess Delete"; 264 | return head; 265 | } 266 | //head p 267 | //10|2002 20|3002 30|NULL 268 | //1002 2002 3002 269 | node *delete_l(node *head) 270 | { 271 | node *p,*q; 272 | if(head == NULL || head->next== NULL ) 273 | { 274 | free(head); 275 | return NULL; 276 | } 277 | 278 | p=head; 279 | while(p->next->next!=NULL) 280 | p=p->next; 281 | 282 | q=p->next; 283 | free(q); 284 | p->next=NULL; 285 | } 286 | 287 | node *delete_b(node *head,int pos) 288 | { 289 | node *p,*q; 290 | int i=1 , n ; 291 | char ch; 292 | n=count_node(head);//n=0 293 | if(n==0) 294 | { 295 | cout<<"\nDelete Failed!!!"; 296 | return head; 297 | } 298 | if(n==pos)//3==4 299 | { 300 | head = delete_l(head); 301 | cout<<"\nDelete Last- Success"; 302 | return head; 303 | } 304 | if(nnext; 326 | i++;//1++ 327 | } 328 | q=p->next; 329 | p->next=q->next; 330 | free(q); 331 | //head p 332 | //10|2002 20|4002 40|NULL 333 | //1002 2002 4002 334 | return head; 335 | } 336 | node *c_n() 337 | { 338 | return (node*)malloc(sizeof(node)); 339 | 340 | } 341 | 342 | node *remove_dup(node *head) 343 | { 344 | node *p,*q,*r; 345 | 346 | if(head==NULL || head->next==NULL) 347 | return head; 348 | 349 | p=head; 350 | while(p->next!=NULL) 351 | { 352 | q=p->next; 353 | while(q!=NULL) 354 | { 355 | if(p->data==q->data) 356 | { 357 | r=p; 358 | while(r->next!=q) 359 | r=r->next; 360 | 361 | r->next=q->next; 362 | free(q); 363 | q=r; 364 | } 365 | q=q->next; 366 | } 367 | p=p->next; 368 | } 369 | return head; 370 | } 371 | -------------------------------------------------------------------------------- /21 days/Day - 4/(GFG)print the elements of array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution{ 5 | public: 6 | void printArray(int arr[], int n) 7 | { 8 | for(int i=0;i> t; 20 | while (t--) { 21 | int n, i; 22 | cin >> n; 23 | int arr[n]; 24 | for (i = 0; i < n; i++) { 25 | cin >> arr[i]; 26 | } 27 | Solution ob; 28 | ob.printArray(arr, n); 29 | cout << "\n"; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /21 days/Day - 4/Euclidean Algorithm/Euclid's alogithm.cpp: -------------------------------------------------------------------------------- 1 | // Basic Euclidean Algorithm 2 | #include 3 | using namespace std; 4 | 5 | // Function to return 6 | // gcd of a and b 7 | int gcd(int a, int b) 8 | { 9 | if (a == 0) 10 | return b; 11 | return gcd(b % a, a); 12 | } 13 | 14 | int main() 15 | { 16 | int a = 10, b = 15; 17 | cout << "GCD(" << a << ", " 18 | << b << ") = " << gcd(a, b) 19 | << endl; 20 | a = 35, b = 10; 21 | cout << "GCD(" << a << ", " 22 | << b << ") = " << gcd(a, b) 23 | << endl; 24 | a = 31, b = 2; 25 | cout << "GCD(" << a << ", " 26 | << b << ") = " << gcd(a, b) 27 | << endl; 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /21 days/Day - 4/Euclidean Algorithm/Explanation: -------------------------------------------------------------------------------- 1 | The algorithm is based on below facts. 2 | 3 | - If we subtract smaller number from larger (we reduce larger number), GCD doesn’t change. 4 | So if we keep subtracting repeatedly the larger of two, we end up with GCD. 5 | 6 | - Now instead of subtraction, if we divide smaller number, the algorithm stops when we find remainder 0. 7 | -------------------------------------------------------------------------------- /21 days/Day - 4/Extended Euclidean Algorithm: -------------------------------------------------------------------------------- 1 | // C++ program to demonstrate working of extended Euclidean Algorithm 2 | #include 3 | using namespace std; 4 | 5 | 6 | int gcdExtended(int a, int b, int *x, int *y) 7 | { 8 | // Base Case 9 | if (a == 0) 10 | { 11 | *x = 0; 12 | *y = 1; 13 | return b; 14 | } 15 | 16 | int x1, y1; 17 | int gcd = gcdExtended(b%a, a, &x1, &y1); 18 | 19 | // recursive call 20 | *x = y1 - (b/a) * x1; 21 | *y = x1; 22 | 23 | return gcd; 24 | } 25 | 26 | int main() 27 | { 28 | int x, y, a = 35, b = 15; 29 | int g = gcdExtended(a, b, &x, &y); 30 | cout << "GCD(" << a << ", " << b 31 | << ") = " << g << endl; 32 | return 0; 33 | } 34 | 35 | //Output example: 36 | //gcd(35, 15) = 5 37 | -------------------------------------------------------------------------------- /21 days/Day - 4/Extended Euclidean Algorithm Explanation: -------------------------------------------------------------------------------- 1 | Extended Euclidean algorithm also finds integer coefficients x and y such that - 2 | ax + by = gcd(a, b) 3 | 4 | Let's say for an example 5 | 6 | Input: a = 30, b = 20 7 | Output: gcd = 10 8 | x = 1, y = -1 9 | (30*1 + 20*(-1) = 10) 10 | 11 | Input: a = 35, b = 15 12 | Output: gcd = 5 13 | x = 1, y = -2 14 | (35*1 + 15*(-2) = 5) 15 | -------------------------------------------------------------------------------- /21 days/Day - 5/Implementation of Sieve of Erathosthenes.cpp: -------------------------------------------------------------------------------- 1 | //Implementation: 2 | //Following is the implementation of the above algorithm. 3 | //In the following implementation, a boolean array arr[] of size n is used to mark multiples of prime numbers. 4 | 5 | #include 6 | using namespace std; 7 | 8 | void SieveOfEratosthenes(int n) 9 | { 10 | 11 | bool prime[n+1]; 12 | memset(prime, true, sizeof(prime)); 13 | 14 | for (int p=2; p*p<=n; p++) 15 | { 16 | if (prime[p] == true) 17 | { 18 | 19 | for (int i=p*p; i<=n; i += p) 20 | prime[i] = false; 21 | } 22 | } 23 | 24 | for (int p=2; p<=n; p++) 25 | if (prime[p]) 26 | cout << p << " "; 27 | } 28 | 29 | 30 | int main() 31 | { 32 | int n = 30; 33 | cout << "Following are the prime numbers smaller " 34 | << " than or equal to " << n << endl; 35 | SieveOfEratosthenes(n); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /21 days/Day - 5/Sieve of Eratosthenes Explanation: -------------------------------------------------------------------------------- 1 | - Given a number n, print all primes smaller than or equal to n. 2 | - It is also given that n is a small number. 3 | - The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million 4 | 5 | Working of Algorithm 6 | - Create a list of consecutive integers from 2 to n: (2, 3, 4, …, n). 7 | - Initially, let p equal 2, the first prime number. 8 | - Starting from p2, count up in increments of p and mark each of these numbers greater than or equal to p2 itself in the list. 9 | These numbers will be p(p+1), p(p+2), p(p+3), etc.. 10 | - Find the first number greater than p in the list that is not marked. 11 | If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3. 12 | 13 | For more info,watch this 14 | https://youtu.be/ATyAnOCI1Ms 15 | -------------------------------------------------------------------------------- /21 days/Day - 6/(GFG)Multiply array elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int multiply(int array[], int n) 5 | { 6 | int pro = 1; 7 | for (int i = 0; i < n; i++) 8 | pro = pro * array[i]; 9 | return pro; 10 | } 11 | 12 | int main() 13 | { 14 | int n; 15 | int arr[n]; 16 | for (int i = 0; i < n; i++) 17 | { 18 | cin>>arr[i]; 19 | } 20 | cout << multiply(array, n); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /21 days/Day - 6/(GFG)Sum of array.cpp: -------------------------------------------------------------------------------- 1 | //sum of array 2 | #include 3 | using namespace std; 4 | int main () 5 | { 6 | int n, i, sum = 0; 7 | cin >> n; 8 | int arr[n]; 9 | for (i = 0; i < n; i++) 10 | cin >> arr[i]; 11 | for (i = 0; i < n; i++) 12 | { 13 | sum += arr[i]; 14 | } 15 | cout << sum; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /21 days/Day - 7/(GFG)Balanced Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int minValueToBalance(int a[], int n) 10 | { 11 | int sum1 = 0; 12 | for (int i = 0; i < n/2; i++) 13 | sum1 += a[i]; 14 | 15 | int sum2 = 0; 16 | for (int i = n/2; i < n; i++) 17 | sum2 += a[i]; 18 | 19 | return abs(sum1 - sum2); 20 | } 21 | }; 22 | 23 | 24 | int main() 25 | { 26 | int t; 27 | cin>>t; 28 | while(t--) 29 | { 30 | int n; 31 | cin>>n; 32 | int a[n]; 33 | for(int i=0;i>a[i]; 35 | Solution ob; 36 | cout< 2 | using namespace std; 3 | 4 | class Solution{ 5 | public: 6 | int countZeroes(int arr[], int n) 7 | { 8 | int cnt=0; 9 | for(int i=0;i> t; 22 | while (t--) { 23 | int n; 24 | cin >> n; 25 | int arr[n]; 26 | for (int i = 0; i < n; i++) { 27 | cin >> arr[i]; 28 | } 29 | Solution ob; 30 | auto ans = ob.countZeroes(arr, n); 31 | cout << ans << "\n"; 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /21 days/Day - 8/(GFG)Minimum number to form the sum even.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution{ 5 | 6 | 7 | public: 8 | int minNum(long long int arr[],int n) 9 | { 10 | 11 | int odd = 0; 12 | for (int i = 0; i < n; i++) 13 | if (arr[i] % 2) 14 | odd += 1; 15 | 16 | return (odd % 2)? 1 : 2; 17 | } 18 | }; 19 | 20 | 21 | 22 | int main() 23 | { 24 | 25 | int t; 26 | cin >> t; 27 | while (t--) 28 | { 29 | int n; 30 | cin>>n; 31 | 32 | long long a[n]; 33 | for(int i=0; i>a[i]; 35 | 36 | Solution ob; 37 | cout << ob.minNum(a, n); 38 | 39 | cout << "\n"; 40 | 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /21 days/Day - 8/(GFG)Number of occurrence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution{ 5 | public: 6 | 7 | int count(int arr[], int n, int x) 8 | { 9 | int res = 0; 10 | for (int i=0; i> t; 21 | while (t--) { 22 | int n, x; 23 | cin >> n >> x; 24 | int arr[n]; 25 | for (int i = 0; i < n; i++) { 26 | cin >> arr[i]; 27 | } 28 | Solution ob; 29 | auto ans = ob.count(arr, n, x); 30 | cout << ans << "\n"; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /21 days/Day - 9/(GFG)Check Set Bits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int t, n; 8 | int exp; 9 | 10 | cin >> t; 11 | for (int i = 0; i < t; i++) { 12 | cin >> n; 13 | if (n == 0) 14 | cout << "NO\n"; 15 | else if (n == 1) 16 | cout << "YES\n"; 17 | else { 18 | exp = ceil(log(n) / log(2)); 19 | if (n == pow(2, exp) - 1) 20 | cout << "YES\n"; 21 | else 22 | cout << "NO\n"; 23 | } 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /21 days/Day - 9/(GFG)How many X's.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int t; 8 | int x, l, u, size, count; 9 | string str; 10 | cin >> t; 11 | for (int i = 0; i < t; i++) { 12 | cin >> x; 13 | cin >> l; 14 | cin >> u; 15 | count = 0; 16 | for (int j = l+1; j < u; j++) { 17 | str = to_string(j); 18 | size = str.size(); 19 | for (int k = 0; k < size; k++) { 20 | if (str[k] - '0' == x) { 21 | count++; 22 | } 23 | } 24 | } 25 | cout << count << '\n'; 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /21 days/Day - 9/(GFG)Sorted matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | 10 | int t, n, e; 11 | vector v; 12 | 13 | cin >> t; 14 | for (int i = 0; i < t; i++) { 15 | cin >> n; 16 | int size = pow(n, 2); 17 | for (int j = 0; j < size; j++) { 18 | cin >> e; 19 | v.push_back(e); 20 | } 21 | 22 | sort(v.begin(), v.end()); 23 | 24 | for (int j = 0; j < size-1; j++) { 25 | cout << v[j] << ' '; 26 | } 27 | cout << v[size-1] << '\n'; 28 | v.clear(); 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /21 days/Day -12/Maximum and Minimum Of Array Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | int A[n]; 12 | for(int i=0;i>A[i]; 15 | } 16 | int maxt=A[0]; 17 | int mint=A[0]; 18 | 19 | for(int i=0;imaxt) 22 | { 23 | maxt=A[i]; 24 | } 25 | } 26 | for(int i=0;i 2 | using namespace std; 3 | const int CHARS = 26; 4 | 5 | int remAnagram(string str1, string str2); 6 | 7 | 8 | int main() 9 | { 10 | int t; 11 | cin>>t; 12 | while(t--) 13 | { 14 | string str1,str2; 15 | cin>>str1>>str2; 16 | cout << remAnagram(str1, str2); 17 | cout< > m; 27 | 28 | for(int i = 0; i < str1.size(); i++){ 29 | if(m.find(str1[i]) == m.end()){ 30 | pair p(1, 0); 31 | m[str1[i]] = p; 32 | } 33 | else{ 34 | m[str1[i]].first++; 35 | } 36 | } 37 | 38 | for(int i = 0; i < str2.size(); i++){ 39 | if(m.find(str2[i]) == m.end()){ 40 | pair p(0, 1); 41 | m[str2[i]] = p; 42 | } 43 | else{ 44 | m[str2[i]].second++; 45 | } 46 | } 47 | 48 | map > :: iterator it = m.begin(); 49 | 50 | int ans = 0; 51 | 52 | while(it != m.end()){ 53 | ans += abs((it->second).first - (it->second).second); 54 | it++; 55 | } 56 | 57 | return ans; 58 | } 59 | -------------------------------------------------------------------------------- /21 days/Day -13/(GFG)Bubble Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swap(int *xp, int *yp) 4 | { 5 | int temp = *xp; 6 | *xp = *yp; 7 | *yp = temp; 8 | } 9 | 10 | void bubble(int arr[], int i, int n); 11 | 12 | 13 | void bubbleSort(int arr[], int n) 14 | { 15 | int i, j; 16 | for (i = 0; i < n-1; i++) 17 | bubble(arr, i, n); 18 | } 19 | 20 | 21 | void printArray(int arr[], int size) 22 | { 23 | int i; 24 | for (i=0; i < size; i++) 25 | printf("%d ", arr[i]); 26 | printf("\n"); 27 | } 28 | 29 | 30 | int main() 31 | { 32 | int arr[1000],n,T,i; 33 | 34 | scanf("%d",&T); 35 | 36 | while(T--){ 37 | 38 | scanf("%d",&n); 39 | 40 | for(i=0;i i; j--){ 59 | if(arr[j] < arr[i]){ 60 | swap(arr[j], arr[i]); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /21 days/Day -13/(GFG)Children Sum Parent.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Tree Node 6 | struct Node { 7 | int data; 8 | Node *left; 9 | Node *right; 10 | 11 | Node(int val) { 12 | data = val; 13 | left = right = NULL; 14 | } 15 | }; 16 | 17 | Node *buildTree(string str) { 18 | // Corner Case 19 | if (str.length() == 0 || str[0] == 'N') 20 | return NULL; 21 | 22 | vector ip; 23 | 24 | istringstream iss(str); 25 | for (string str; iss >> str;) 26 | ip.push_back(str); 27 | 28 | 29 | Node *root = new Node(stoi(ip[0])); 30 | 31 | 32 | queue queue; 33 | queue.push(root); 34 | 35 | 36 | int i = 1; 37 | while (!queue.empty() && i < ip.size()) { 38 | 39 | 40 | Node *currNode = queue.front(); 41 | queue.pop(); 42 | 43 | string currVal = ip[i]; 44 | 45 | 46 | if (currVal != "N") { 47 | 48 | 49 | currNode->left = new Node(stoi(currVal)); 50 | 51 | 52 | queue.push(currNode->left); 53 | } 54 | 55 | 56 | i++; 57 | if (i >= ip.size()) 58 | break; 59 | currVal = ip[i]; 60 | 61 | if (currVal != "N") { 62 | 63 | 64 | currNode->right = new Node(stoi(currVal)); 65 | 66 | 67 | queue.push(currNode->right); 68 | } 69 | i++; 70 | } 71 | 72 | return root; 73 | } 74 | 75 | int isSumProperty(struct Node *node); 76 | 77 | int main() { 78 | int tc; 79 | scanf("%d ", &tc); 80 | while (tc--) { 81 | string str; 82 | getline(cin, str); 83 | Node *root = buildTree(str); 84 | cout << isSumProperty(root) << "\n"; 85 | 86 | 87 | } 88 | 89 | 90 | return 0; 91 | } 92 | 93 | struct Node 94 | { 95 | int data; 96 | struct Node* left; 97 | struct Node* right; 98 | 99 | Node(int x){ 100 | data = x; 101 | left = right = NULL; 102 | } 103 | }; 104 | 105 | int isSumProperty(Node *node) 106 | { 107 | 108 | if(!node){ 109 | return 1; 110 | } 111 | else if(!node->left && !node->right){ 112 | return 1; 113 | } 114 | 115 | int value = 0; 116 | 117 | if(node->left){ 118 | value += (node->left)->data; 119 | } 120 | if(node->right){ 121 | value += (node->right)->data; 122 | } 123 | 124 | if(node->data != value){ 125 | return 0; 126 | } 127 | 128 | return isSumProperty(node->left) && isSumProperty(node->right); 129 | } 130 | -------------------------------------------------------------------------------- /21 days/Day -13/(GFG)Sum of all substrings of a number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | string getString(string s, int i, int j){ 7 | string ans = ""; 8 | 9 | for(int y = i; y <= j; y++){ 10 | ans = ans + s[y]; 11 | } 12 | 13 | return ans; 14 | } 15 | 16 | int main() 17 | { 18 | int test; 19 | cin >> test; 20 | 21 | while(test){ 22 | string s; 23 | cin >> s; 24 | 25 | int n = s.size(); 26 | 27 | vector temp(n, 0); 28 | 29 | long long int ans = 0; 30 | 31 | for(int i = 0; i < n; i++){ 32 | for(int j = 0; j <= i; j++){ 33 | string t = getString(s, j, i); 34 | temp[i] = temp[i] + stoll(t); 35 | } 36 | ans = ans + temp[i]; 37 | } 38 | 39 | cout << ans << endl; 40 | 41 | test--; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /21 days/Day -15/(GFG)Binary array sorting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int t, n, e; 8 | vector v; 9 | 10 | cin >> t; 11 | for (int i = 0; i < t; i++) { 12 | cin >> n; 13 | for (int j = 0; j < n; j++) { 14 | cin >> e; 15 | v.push_back(e); 16 | } 17 | 18 | int l = 0, r = n - 1; 19 | while (l < r) { 20 | if (v[l] == 0) { 21 | l++; 22 | if (v[r] == 1) r--; 23 | } 24 | else { 25 | if (v[r] == 1) r--; 26 | else swap(v[l], v[r]); 27 | } 28 | } 29 | 30 | for (int j = 0; j < n-1; j++) 31 | cout << v[j] << ' '; 32 | cout << v[n-1] << '\n'; 33 | 34 | v.clear(); 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /21 days/Day -15/(GFG)Count Number of Hops.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int t, n; 8 | int ways[51]; 9 | 10 | memset(ways, 0, sizeof(ways)); 11 | ways[0] = 1; 12 | ways[1] = 1; 13 | ways[2] = 2; //1+1, 2 14 | 15 | for (int i = 3; i <= 50; i++) { 16 | ways[i] = ways[i-1] + ways[i-2] + ways[i-3]; 17 | } 18 | 19 | cin >> t; 20 | while (t--) { 21 | cin >> n; 22 | cout << ways[n] << '\n'; 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /21 days/Day -15/(GFG)Decode the pattern.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t, n; 6 | 7 | cin >> t; 8 | while (t--) { 9 | cin >> n; 10 | 11 | string base1 = "1"; 12 | string base2 = "11"; 13 | 14 | if (n == 1) { 15 | cout << base1 << '\n'; 16 | } 17 | else if (n == 2) { 18 | cout << base2 << '\n'; 19 | } 20 | else { 21 | string res; 22 | string base = base2; 23 | for (int i = 3; i <= n; i++) { 24 | res = ""; 25 | int len = base.length(); 26 | int count = 1; 27 | for (int j = 1; j < len; j++) { 28 | if (base[j] == base[j-1]) { 29 | count += 1; 30 | } 31 | else { 32 | res += count + '0'; 33 | res += base[j-1]; 34 | count = 1; 35 | } 36 | } 37 | res += count + '0'; 38 | res += base[len-1]; 39 | base = res; 40 | } 41 | cout << res << '\n'; 42 | } 43 | } 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /21 days/Day -15/(GFG)Evaluate postfix expression.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int t, a, b, res; 8 | stack s; 9 | 10 | cin >> t; 11 | while (t--) { 12 | string exp; 13 | cin >> exp; 14 | for (char c : exp) { 15 | if (isdigit(c)) { 16 | s.push(c - '0'); 17 | } 18 | else { 19 | b = s.top(); 20 | s.pop(); 21 | a = s.top(); 22 | s.pop(); 23 | switch(c) { 24 | case '+': res = a + b; 25 | break; 26 | case '-': res = a - b; 27 | break; 28 | case '*': res = a * b; 29 | break; 30 | case '/': res = a / b; 31 | break; 32 | } 33 | s.push(res); 34 | } 35 | } 36 | cout << s.top() << '\n'; 37 | s.pop(); 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /21 days/Day -15/(GFG)Finding Position.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | int t, n; 8 | 9 | cin >> t; 10 | while (t--) { 11 | cin >> n; 12 | 13 | cout << pow(2, floor(log2(n))) << '\n'; 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /21 days/Day -15/(GFG)Reach a given score.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int t, n; 8 | 9 | cin >> t; 10 | while (t--) { 11 | cin >> n; 12 | 13 | int ways[n+1]; 14 | memset(ways, 0, sizeof(ways)); 15 | 16 | ways[0] = 1; 17 | 18 | for (int i = 3; i <= n; i++) { 19 | ways[i] += ways[i-3]; 20 | } 21 | 22 | for (int i = 5; i <= n; i++) { 23 | ways[i] += ways[i-5]; 24 | } 25 | 26 | for (int i = 10; i <= n; i++) { 27 | ways[i] += ways[i-10]; 28 | } 29 | 30 | cout << ways[n] << '\n'; 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /21 days/Day -15/(GFG)Remove character.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int t; 9 | string s1, s2; 10 | 11 | cin >> t; 12 | for (int i = 0; i < t; i++) { 13 | cin >> s1; 14 | cin >> s2; 15 | 16 | for (char c : s2) { 17 | s1.erase(remove(s1.begin(), s1.end(), c), s1.end()); 18 | } 19 | 20 | cout << s1 << '\n'; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /21 days/Day -15/(GFG)Repetitive addition of digits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int t, n; 7 | int sum; 8 | 9 | cin >> t; 10 | for (int i = 0; i < t; i++) { 11 | cin >> n; 12 | sum = 0; 13 | while (true) { 14 | while (n > 0) { 15 | sum += n % 10; 16 | n /= 10; 17 | } 18 | if (sum < 10) 19 | break; 20 | n = sum; 21 | sum = 0; 22 | } 23 | cout << sum << '\n'; 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /21 days/Day -15/(GFG)Student Record.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct Record { 7 | string name; 8 | int marks1; 9 | int marks2; 10 | int marks3; 11 | int avgMarks; 12 | }; 13 | 14 | int main() { 15 | int t, n, m1, m2, m3, avg; 16 | 17 | vector v; 18 | 19 | cin >> t; 20 | while (t--) { 21 | cin >> n; 22 | for (int i = 0; i < n; i++) { 23 | string str; 24 | cin >> str; 25 | cin >> m1 >> m2 >> m3; 26 | struct Record rec = {str, m1, m2, m3, (m1+m2+m3)/3}; 27 | v.push_back(rec); 28 | } 29 | 30 | int maxAvg = v[0].avgMarks; 31 | for (int i = 1; i < n; i++) { 32 | if (v[i].avgMarks > maxAvg) { 33 | maxAvg = v[i].avgMarks; 34 | } 35 | } 36 | 37 | for (int i = 0; i < n; i++) { 38 | if (v[i].avgMarks == maxAvg) { 39 | cout << v[i].name << ' '; 40 | } 41 | } 42 | cout << maxAvg << '\n'; 43 | 44 | v.clear(); 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /21 days/Day -15/(GFG)URLify a given string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t, k; 7 | 8 | cin >> t; 9 | while (t--) { 10 | string str; 11 | cin.ignore(); 12 | getline(cin, str, '\n'); 13 | cin >> k; 14 | 15 | str.erase(0, str.find_first_not_of(' ')); 16 | 17 | str.erase(str.find_last_not_of(' ') + 1); 18 | 19 | for (int i = 0; i < str.length(); i++) { 20 | if (str[i] == ' ') { 21 | str.replace(i, 1, "%20"); 22 | i += 2; 23 | } 24 | } 25 | cout << str << '\n'; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /21 days/Day -17/(GFG)Knight Walk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct temp{ 8 | int x; 9 | int y; 10 | int steps; 11 | }; 12 | 13 | temp makeStruct(int xx, int yy, int ssteps){ 14 | temp t; 15 | t.x = xx; 16 | t.y = yy; 17 | t.steps = ssteps; 18 | return t; 19 | } 20 | 21 | int main() { 22 | //code 23 | int test; 24 | cin >> test; 25 | 26 | while(test--){ 27 | int n, m; 28 | cin >> n >> m; 29 | 30 | int sx, sy, dx, dy, ans = 1000; 31 | cin >> sx >> sy >> dx >> dy; 32 | 33 | vector > visited(n, vector(m, false)); 34 | 35 | queue q; 36 | q.push(makeStruct(sx-1, sy-1, 0)); 37 | 38 | dx--; 39 | dy--; 40 | 41 | while(!q.empty()){ 42 | temp t = q.front(); 43 | q.pop(); 44 | if(!(t.x < 0 || t.x >=n || t.y < 0 || t.y >= m)){ 45 | if(!visited[t.x][t.y]){ 46 | visited[t.x][t.y] = true; 47 | q.push(makeStruct((t.x)-1, (t.y)-2, (t.steps)+1)); 48 | q.push(makeStruct((t.x)-1, (t.y)+2, (t.steps)+1)); 49 | q.push(makeStruct((t.x)+1, (t.y)-2, (t.steps)+1)); 50 | q.push(makeStruct((t.x)+1, (t.y)+2, (t.steps)+1)); 51 | q.push(makeStruct((t.x)-2, (t.y)-1, (t.steps)+1)); 52 | q.push(makeStruct((t.x)-2, (t.y)+1, (t.steps)+1)); 53 | q.push(makeStruct((t.x)+2, (t.y)-1, (t.steps)+1)); 54 | q.push(makeStruct((t.x)+2, (t.y)+1, (t.steps)+1)); 55 | } 56 | } 57 | if((t.x == dx) && (t.y == dy)){ 58 | ans = min(ans, t.steps); 59 | visited[dx][dy] = false; 60 | } 61 | } 62 | 63 | // int ans = find(visited, sx-1, sy-1, dx-1, dy-1); 64 | 65 | if(ans >= 1000){ 66 | ans = -1; 67 | } 68 | 69 | cout << ans << endl; 70 | } 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 📍📍21-Days Competitive Programming📍📍 2 | ## _🔥🔥Competitive Programming and learning DS and Algorithm🔥🔥_ 3 | 4 | # Day 1📌 5 | 6 | ## Linked List🤔 7 | ``` 8 | - A linked list is a linear data structure 9 | - In linked list, elements are not stored in continous manner 10 | - The elements in a linked list are linked using pointers 11 | ``` 12 | 13 | ## Types of Linked⬇ 14 | - Singly Linked List 15 | - Doubly Linked List 16 | - Circular Linked List 17 | 18 | ## Why we prefer linked list over arrays⬇ 19 | - Dynamic nature in respect of size 20 | - Doing operations like updation/insertion/deletion are easy in linked list 21 | 22 | ## Drawbacks of linked list✅ 23 | - Random access is not allowed. 24 | - Extra memory space for a pointer is required. 25 | 26 | ## Initialization of linked list✍ 27 | ```diff 28 | - struct node 29 | - { 30 | - int data; 31 | - struct node *next; 32 | - }; 33 | ``` 34 | 35 | ## Some brief about linked list📝 36 | - Linked list is made of nodes connected to each other 37 | - And Nodes consists of two blocks namely 38 | - Data 39 | - Pointer(also called 'next' while programming) 40 | - Data refers to the value at the data block of node 41 | - Pointer points to the next node in order to link every node with each other 42 | 43 | ## Did three basic questions based on linked list on GFG👨‍💻 44 | - Count Node Of Linked List 45 | - Linked List Length Even or Odd 46 | - Reverse a Linked List 47 | 48 | # Day 2📌 49 | 50 | ## Learned the basics of linked list 51 | - learned the opertions performed on linked list insertion/deletion/updation 52 | 53 | ## Did three questions on GFG✅ 54 | - Print linked list elements 55 | - Delete alternate node 56 | - Check if circular linked list 57 | 58 | # Day 3📌 59 | 60 | ## Linked list start from creating the linked list to operations performed on linked list(Singly Linked List) 61 | - Creating Linked List 62 | - Displaying Linked List 63 | 64 | ## Learned the operations performed in linked list 65 | 66 | ### Insertion⚡ 67 | - Insertion at first 68 | - Insertion at last 69 | - Insertion in between 70 | 71 | ### Deletion⚡ 72 | - Deletion at first 73 | - Deletion at last 74 | - Deletion in between 75 | 76 | ### Others⚡ 77 | - Count nodes 78 | - Remove duplicates 79 | 80 | # Day 4📌 81 | 82 | ## Started learning some algorithms 83 | ## Euclid Algo 84 | ```diff 85 | + A basic funda to find the GCD 86 | ``` 87 | 88 | ## Extended Euclid Algo 89 | ```diff 90 | + Extended Euclidean algorithm also finds integer coefficients x and y 91 | ``` 92 | 93 | ## Practiced some questions based on the algorithm 94 | - Will practice some more questions in order to grasp the concept totally 95 | 96 | 97 | # Day 5📌 98 | 99 | ## Started learning some algorithm✅ 100 | ## Sieve of Eratosthenes 101 | ```diff 102 | + The sieve of Eratosthenes is one of the most efficient ways to
find all primes smaller than n when n is smaller than 10 million 103 | ``` 104 | 105 | # Day 6📌 106 | 107 | ## Solved some problems on GFG✅ 108 | - Multiply array elements 109 | - Sum of array 110 | 111 | # Day 7📌 112 | 113 | ## Solved some problems on GFG✅ 114 | - Balanced array 115 | - Count the zeros 116 | 117 | ## "Change📗 is the end result of all true learning" 🚶‍♂️🏃‍♂️ ― Leo Buscaglia📍 118 | ## 7 days streak💚💚💚💚 119 | 120 | # Day 8📌 121 | 122 | ## Solved some problems on GFG✅ 123 | - Minimum number to form the sum even 124 | - Number of occuerences 125 | 126 | # Day 9📌 127 | 128 | ## Solved some problems on GFG✅ 129 | - Check set Bits 130 | - How many X's 131 | - Sorted matrix 132 | 133 | # Day 10📌 134 | 135 | ## Solved some problems on GFG✅ 136 | - Equilibrium index of an array 137 | - Rotate by 90 Degree 138 | 139 | # Day 11📌 140 | 141 | ## Solved problem on GFG✅ 142 | - Swapping Pair make sum equal 143 | 144 | # Day 12📌 145 | 146 | ## Solved problem on GFG✅ 147 | - Maximum and Minimum Of Array Elements 148 | 149 | # Day 13📌 150 | 151 | ## Solved problems on GFG✅ 152 | - Anagram of String 153 | - Bubble Sort 154 | - Children Sum Parent 155 | - Sum of all substrings of a number 156 | 157 | # Day 14📌 158 | 159 | ## Solved problems on GFG✅ 160 | - Count the zeros 161 | - Factorials of large numbers 162 | - Star Elements 163 | 164 | ## “Education📗📙📘📕 is not the filling of a pail, but the lighting⚡ of a fire🔥🔥.” – W.B. Yeats📍 165 | ## 14 days streak💙💙💙💙 166 | 167 | # Day 15📌 168 | 169 | ## Solved problems on GFG✅ 170 | - Remove Character 171 | - Repetitive Addition of digits 172 | - Student Record 173 | - URLify a given string 174 | - Reach a given score 175 | - Finding position 176 | - Evaluate postfix expression 177 | - Decode the pattern 178 | - Count number of Hops 179 | - Binary Array Sorting 180 | 181 | # Day 16📌 182 | 183 | ## Solved problems on GFG✅ 184 | - CamelCase Pattern Matching 185 | - Drive the car 186 | 187 | # Day 17📌 188 | 189 | ## Solved problem on GFG✅ 190 | - Knight Walk 191 | 192 | # Day 18📌 193 | 194 | ## Solved problem on GFG✅ 195 | - Fill array with 1's 196 | - Reorganize the array 197 | - You and your books 198 | 199 | # Day 19📌 200 | 201 | ## Solved problem on GFG✅ 202 | - Check if linked list is palindrome 203 | - Find the highest number 204 | - Good or Bad String 205 | 206 | # Day 20📌 207 | 208 | ## Solved problems on GFG✅ 209 | - Chocolate Station 210 | - Find the length of loop 211 | - Find the maximum number of handshakes 212 | - Two mirror trees 213 | 214 | # Day 21📌 215 | 216 | ## Solved problems of GFG✅ 217 | - Determine if two trees are identical 218 | - Extract Maximum 219 | - Roman Number To Integer 220 | - Meta String 221 | ## “The key to success🚩🏃‍♂️ is consistency👨‍💻” - Zak Frazer📍 222 | ## 21 days streak💛💛💛💛 and it continues.......🏃‍♂️ 223 | 224 | # Day 22📌 225 | 226 | ## Solved some problems of GFG✅ 227 | - K - Palindrome 228 | - Element with left side smaller and right side greater 229 | --------------------------------------------------------------------------------