├── .gitignore ├── .vs ├── VSWorkspaceState.json ├── geeks-for-geeks-solutions │ └── v16 │ │ └── .suo └── slnx.sqlite ├── .vscode ├── c_cpp_properties.json ├── launch.json └── settings.json ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── DSA-workshop ├── QUIZ │ └── README.md ├── README.md ├── absolute-value.cpp ├── check_for_binary.cpp ├── check_if_two_arrays_are_equals_or_not.cpp ├── equilibrium-point.cpp ├── find-transiction-point.cpp ├── find-transition-point.cpp ├── first-repeating-element.cpp ├── immediate_smaller.cpp ├── max_occurrency_number.cpp ├── occurrence_of_integr_n_ll.cpp ├── pair_wise_swapping.cpp ├── print_linked_list.cpp ├── print_non_repeating_chars.cpp ├── reverse_string_using_stack.cpp ├── rotate-array.cpp ├── sort002.cpp ├── wave-array.cpp └── who-has-majority.cpp ├── LICENSE ├── README.md ├── c++ ├── Palindrom.cpp ├── Search in a row-column sorted Matrix.cpp ├── absolute-value.cpp ├── adding-two-matrices.cpp ├── addition-of-two-numbers.cpp ├── addition-under-modulo.cpp ├── alone-in-couple.cpp ├── ambstrong.cpp ├── amend-the-sentence.cpp ├── array-delete-and-shift.cpp ├── array-insert-at-end.cpp ├── array-insert-at-index.cpp ├── array-update-by-index.cpp ├── at-least-two-greater-element.cpp ├── automorphic-number.cpp ├── back-to-front.cpp ├── balanced-array.cpp ├── bfs-graph.cpp ├── bin-search.cpp ├── binary-array-sorting.cpp ├── binary-array-sorting2.cpp ├── binary-arrays-sorting.cpp ├── bitwise-operator.cpp ├── boolean-matrix-problem.cpp ├── bubble-sort.cpp ├── bubble_sort.cpp ├── c-fiend-function.cpp ├── c-generic-sort.cpp ├── c-hello-world.cpp ├── c-to-f.cpp ├── check-if-a-numbe-is-power-of-anter.cpp ├── check-if-array-is-sorted.cpp ├── check-if-b-is-an-anagram-of-a.cpp ├── check-if-k-bit-is-set.cpp ├── check-if-linked-list-is-palindrome.cpp ├── check-if-string-is-isogram.cpp ├── check-if-two-arrays-are-equals.cpp ├── check-string.cpp ├── check-whether-kth-bit-is-set.cpp ├── check_for_binary.cpp ├── circular-linked-list-head-insert.cpp ├── circular-linked-list.cpp ├── circular-list-delete-at-position.cpp ├── compare-circular-doubly-linked-list.cpp ├── compare-two-linked-lists.cpp ├── consonant-and-vowals.checkcpp ├── count-1-in-sorted-array.cpp ├── count-bigger-than-x.cpp ├── count-bst-nodes-that-lie-in-a-give-range.cpp ├── count-distinc-vowels.cpp ├── count-element-occurences.cpp ├── count-leaves-in-binary-tree.cpp ├── count-nodes-of-linked-list.cpp ├── count-non-repeating-elements.cpp ├── count-number-leaves-nodes.cpp ├── count-number-of-set-bits.cpp ├── count-numbers-divisible-by-m.cpp ├── count-occurrency-linked-list.cpp ├── count-odd-even.cpp ├── count-of-camel-case-characters.cpp ├── count-repeated-element.cpp ├── count-smaller-elements.cpp ├── count-smaller-than-x.cpp ├── count-the-zeros.cpp ├── count-words-in-string.cpp ├── count-zeros.cpp ├── data-types.cpp ├── delete-a-node-in-single-linked-list.cpp ├── delete-head-of-circular-list.cpp ├── delete-head-of-doubly-linked-list.cpp ├── delete-middle-of-linked-list.cpp ├── delete-node-without-head-pointer.cpp ├── delete-tail-of-circular-linked-list.cpp ├── delete-tail-of-doubly-linked-list.cpp ├── deque-implementation.cpp ├── detect_loop.cpp ├── detele-tail-linked-list.cpp ├── digits-in-factorial.cpp ├── display-circular-linked-list.cpp ├── display-double-circular-linked-list.cpp ├── display-double-linked-list-forward-and-backward.cpp ├── display-linked-list.cpp ├── display-longest-name.cpp ├── doubly-linked-list-head-insert.cpp ├── doubly-linked-list-tail-insert.cpp ├── equilibrium-point.cpp ├── even-odd.cpp ├── facing-the-sun.cpp ├── factorial_of_a_number.cpp ├── find-duplicates-in-array.cpp ├── find-first-set-bit-final2.cpp ├── find-first-set-bit.cpp ├── find-index.cpp ├── find-lenght-of-loop.cpp ├── find-maximum-number.cpp ├── find-minimum-and-maximum-nuber.cpp ├── find-nk-th-node-in-linked-list.cpp ├── find-number-of-numbers.cpp ├── find-odd-occurrenci.cpp ├── find-position-of-set-bit.cpp ├── find-the-frequency.cpp ├── find-transition-point.cpp ├── find-unique-element.cpp ├── find-unique-element3.cpp ├── find_immediate_smaller_than_x.cpp ├── find_index.cpp ├── find_the_fine.cpp ├── first-and-second-smallest-element.cpp ├── first-element-to-occur-k-times.cpp ├── first-repeating-element.cpp ├── first-repeating-element2.cpp ├── first_letter_of_each_word.cpp ├── for-loop-prime-check.cpp ├── frequency_of_array_elements.cpp ├── front-to-back.cpp ├── full-function-bst.cpp ├── fullprime.cpp ├── generate-permutation.cpp ├── get-length-circular-linked-list.cpp ├── get_element-at-index.cpp ├── get_element_by_index.cpp ├── get_middle_element_of_circular_double_linked-list.cpp ├── greater-on-right-side.cpp ├── greatest_of_three_nubers.cpp ├── identical-Linked-list.cpp ├── immediate-grater.cpp ├── immediate-smaller-element.cpp ├── implement-queue-using-linked-list.cpp ├── implement-stack-using-linked-list.cpp ├── index-of-an-extra-element.cpp ├── index-of-first-1-in-a-sorted-array-of-0s-and-1s.cpp ├── inorder-of-BST.cpp ├── inorder-traversal-and-bst.cpp ├── inorder-traversal.cpp ├── insert-a-node-in-a-bst.cpp ├── insert-a-node-in-doubly-linked-list.cpp ├── insert-at-the-end-of-circular-linked-list.cpp ├── insert-in-middle-of-linked-list.cpp ├── insert-in-sorted-linked-list.cpp ├── insert-in-stack.cpp ├── insert-node-in-doubly-linked-list.cpp ├── insert-tail-circular-linked-list.cpp ├── int-to-binary.cpp ├── intersaction-pooint-iny-shapped-linked-list.cpp ├── is-array-sorted.cpp ├── is-the-doubly-linked-list-circular.cpp ├── isIsogram.cpp ├── join-two-linked-list.cpp ├── k-larghest-elements.cpp ├── k-th-element-of-two-sorted-arrays.cpp ├── k_distance_from_root.cpp ├── kth-larghest-elment-in-bst.cpp ├── kth-smallest-number.cpp ├── last-index-of-1.cpp ├── leader-in-array.cpp ├── learn-to-comment.cpp ├── learning-struct.cpp ├── left-view-of-binary-tree.cpp ├── left_index.cpp ├── left_view_of_binary_tree.cpp ├── length-of-string.cpp ├── level-order-of-binary-search-tree.cpp ├── level-order-tree2.cpp ├── linked-list-delete-at-position.cpp ├── linked-list-insert.cpp ├── linked-list-lenght-even-or-odd.cpp ├── logical_operators.cpp ├── longest-substring-containing-1.cpp ├── lower-case.cpp ├── lower-to-uppercase.cpp ├── lowest-common-ancestor-bst.cpp ├── majority-element.cpp ├── map-operations.cpp ├── max-and-mimum.cpp ├── max-and-second-max.cpp ├── max-distance-between-same-elements.cpp ├── maximize-toys.cpp ├── maximum-and-minimum-in-linked-list.cpp ├── maximum-no-of-1s-row.cpp ├── maximum-product-of-tw-numbers.cpp ├── mean-and-median.cpp ├── mean.cpp ├── median-of-two-array.cpp ├── merge-sort.cpp ├── merge-two-sorted-arrays.cpp ├── microsoft-reverse-words-in-string.cpp ├── middle-element-linkedlist.cpp ├── middle-of-stack.cpp ├── min-in-BST.cpp ├── minimum-product-pair.cpp ├── mirror-tree.cpp ├── missing-characters-in-panagram.cpp ├── missing-number-in-an-array.cpp ├── modular-node.cpp ├── multimap-operations.cpp ├── multiplication-under-modulo.cpp ├── multiplication_table.cpp ├── multiply-left-and-right-array-sum.cpp ├── multiset-operator.cpp ├── naive-pattern-search.cpp ├── need-some-change.cpp ├── new_line.cpp ├── node-at-given-pos-linked-list.cpp ├── non-preating-character.cpp ├── non-repeating-character.cpp ├── number-is-sparceor-not.cpp ├── number-is-sparse-or-not.cpp ├── number-of-digits.cpp ├── number-of-occurrency.cpp ├── numbers-containing-1-2-and-3.cpp ├── occurrence-of-an-integer-in-a-linked-list.cpp ├── odd-or-even.cpp ├── operations-on-queue.cpp ├── operations-on-stack.cpp ├── pair-sum-in-vector.cpp ├── pair-sum-vector.cpp ├── pair-swap-linked-list.cpp ├── pairs-with-difference-k.cpp ├── panagram-checking.cpp ├── peack-element.cpp ├── porstOrder-of-BST.cpp ├── postorder-traversal.cpp ├── power-of-2.cpp ├── power-of-two-checker.cpp ├── preOrder-of-BST.cpp ├── preoder-traversal.cpp ├── prime-check.cpp ├── prime-number-check.cpp ├── prime-numbers-in-range.cpp ├── print-adjacency-list.cpp ├── print-alternate-elements-of-an-array.cpp ├── print-distinct-element.cpp ├── print-first-letter-of-every-word.cpp ├── print-from-1-to-n-using-recursion.cpp ├── print-linked-list.cpp ├── print_an_element.cpp ├── product-of-array-element.cpp ├── product_max_in_first_and_min_in_sec.cpp ├── queue-designer.cpp ├── queue-operations.java ├── queue-reversal.cpp ├── queue_using_array.cpp ├── remove-a-and0ac-from-given-string.cpp ├── remove-duplicate-elements.cpp ├── remove-duplicates-sorted-linked-list.cpp ├── remove-head-single-linked-list.cpp ├── remove-repeated-consecutive-digits.cpp ├── remove-spaces.cpp ├── remove_loop_from_linked_list.cpp ├── removing-consecutive-duplicates.cpp ├── repeate-char.cpp ├── repeating-character-first-appearance-leftmost.cpp ├── replace-os-with-5s.cpp ├── reverse-a-string.cpp ├── reverse-array-2.cpp ├── reverse-array-using-stack.cpp ├── reverse-columns-of-matrix.cpp ├── reverse-first-k-elements-of-queue.cpp ├── reverse-string-with-stack.cpp ├── reverse-the-array.cpp ├── reverse-word-in-given-string.cpp ├── reverse_digits.cpp ├── reverse_linked_list.cpp ├── reversing-the-vowels.cpp ├── right_and_left_rotation.cpp ├── rotate-array-by-n-elements-1587115621.cpp ├── rotate-array-by-n-elements.cpp ├── rotate-array-by-n-elements.cpp.save.1 ├── rotate-matirixe-clowise.cpp ├── rotate-matrix-of-90-degrees.cpp ├── search-element-in-bst.cpp ├── search-in-linked-list.cpp ├── search-in-matrix.cpp ├── search-in-sorted-array.cpp ├── search-node-bst.cpp ├── search0an-element-in-an-array.cpp ├── set-all-odd-bits.cpp ├── set-operations.cpp ├── shift-right.cpp ├── size-of-binary-tree.cpp ├── smallest-positive-missing-number.cpp ├── sort-a-stack.cpp ├── sort-and-reverse.cpp ├── sort-array-of-zeros-ones-and-two.cpp ├── sort-arrays-of-0-and-1.cpp ├── sort-arrays-of-0.cpp ├── sort-linked-list-of-0s-1s-2s.cpp ├── sorted-insert-for-circular-linked-list.cpp ├── split-strings.cpp ├── sqare-rooot.cpp ├── stack-designer.cpp ├── stack-implementation-with-array.cpp ├── step-by-step-condition.cpp ├── stl-vector.cpp ├── string-quicksort.cpp ├── string-validation.cpp ├── strong-number.cpp ├── strongest-neighbour.cpp ├── strstr.cpp ├── student-of-the-year.cpp ├── subarray-with-given-sum.cpp ├── sum-last-n-nodes.cpp ├── sum-of-array-elements.cpp ├── sum-of-array-elements2.cpp ├── sum-of-binary-tree.cpp ├── sum-of-digits.cpp ├── sum-of-distinct-element.cpp ├── sum-of-leaf-nodes-in-bst.cpp ├── sum-of-leaf-nodes.cpp ├── sum-of-matrix-elements.cpp ├── sum-of-nodes.cpp ├── sum_of_array.cpp ├── sum_of_array2.cpp ├── sum_of_depencies.cpp ├── sum_of_first_n_terms.cpp ├── swap-all-odd-and-evenb0-ts.cpp ├── swap-the-objects.cpp ├── taking-inputs.cpp ├── template.cpp ├── the-dice-problem.cpp ├── third-larghest-element.cpp ├── transponse.cpp ├── transpose-of-matrix.cpp ├── transpose-of-matrix.cpp.save ├── tree-level-order-traversal.cpp ├── twice-counter.cpp ├── two-dimensional-world.cpp ├── two-tree-are-identical.cpp ├── two_numers-with-sum-closest-to-zero.cpp ├── union-of-two-sorted-arrays-1587115621.cpp ├── uppercase-to-lowercase.cpp ├── value-equal-to-index-value.cpp ├── vowels-in-string.cpp ├── while-loop-print-table.cpp ├── who--has-the-majority.cpp └── winner_of_an_election.cpp ├── geeksforgeeks.png ├── images ├── geeksforgeeks.png └── spiral-traversal-matrix.png ├── java ├── Anagram.class ├── Anagram.java ├── data-types-java.java ├── even-odd.java ├── for-loop-prime-check.java ├── java-inheritance.java ├── start-coding.java └── the-new-line.java ├── must-do-coding-challenges ├── README.md ├── last-index-of-1.cpp ├── middle-element-linked-list.cpp ├── remove-duplicates3034.cpp └── sort-an-array-of-0s-1s-and-2.cpp └── python ├── add-two-numbers-represented-by-linked-lists.py ├── anagram.py ├── bitonic_point.py ├── cat-and-hat-python.py ├── check_status_python.py ├── comment-in-python.py ├── comparison.py ├── concatenating-string.py ├── count-even-odd-python.py ├── for-loop-jumper.py ├── for-loop-python.py ├── fractional-linked-list.py ├── functions-in-list.py ├── functions.py ├── hello-world.py ├── if_conditional_statment.py ├── increment-and-decrement.py ├── intro-to-list.py ├── is-and-in-operator.py ├── jumping-though-while.py ├── larghest_element_in_array.py ├── logical-operators.py ├── node-at-given-position.py ├── nth_node_from_end.py ├── operators-in-python.py ├── print-linked-list.py ├── print-the-left-element.py ├── repeat-the-string.py ├── repeatitive-printing.py ├── replace_all_zero_with_5.py ├── sep-and-end-in-print.py ├── sort-linked-list-of-0s-1s-2s.py ├── space-separate.py ├── split-the-input.py ├── sum_of_middle_element_of_sorted_arrays.py ├── sum_to_digits.py ├── taking-input.py ├── welcome-abroard-python.py └── while-loop-in-python.py /.gitignore: -------------------------------------------------------------------------------- 1 | a.out -------------------------------------------------------------------------------- /.vs/VSWorkspaceState.json: -------------------------------------------------------------------------------- 1 | { 2 | "ExpandedNodes": [ 3 | "", 4 | "\\DSA-workshop", 5 | "\\DSA-workshop\\introduction", 6 | "\\DSA-workshop\\week1" 7 | ], 8 | "SelectedNode": "\\DSA-workshop\\week1\\README.md", 9 | "PreviewInSolutionExplorer": false 10 | } -------------------------------------------------------------------------------- /.vs/geeks-for-geeks-solutions/v16/.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omonimus1/geeks-for-geeks-solutions/a414368d93acb60f0836ae9e5c566673080e6646/.vs/geeks-for-geeks-solutions/v16/.suo -------------------------------------------------------------------------------- /.vs/slnx.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omonimus1/geeks-for-geeks-solutions/a414368d93acb60f0836ae9e5c566673080e6646/.vs/slnx.sqlite -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "macos-clang-arm64", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "compilerPath": "/usr/bin/clang", 9 | "cStandard": "${default}", 10 | "cppStandard": "${default}", 11 | "intelliSenseMode": "macos-clang-arm64", 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": "lldb", 7 | "request": "launch", 8 | "args": [], 9 | "cwd": "/Users/davidepollicino/Desktop/geeks-for-geeks-solutions/c++", 10 | "program": "/Users/davidepollicino/Desktop/geeks-for-geeks-solutions/c++/build/Debug/outDebug" 11 | } 12 | ] 13 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## How to contribute? 2 | 1. Fork the repository 3 | 2. Do the desired changes (add/delete/modify) 4 | 3. Make a pull request 5 | 6 | ## When to contribute? 7 | 1. If there is no solution to a problem in the main branch. 8 | 2. If your solution is asymptotically faster than the one in the main branch. 9 | 3. If your algorithm is of the same time complexity but with reduced code size. In this case, comment out the original solution and make a pull request with your solution. 10 | 4. If you have another method of solving the problem which is asymptotically slower than the original method then, comment your solution in the main file and make a pull request. 11 | -------------------------------------------------------------------------------- /DSA-workshop/absolute-value.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/absolute-value/1/?track=dsa-workshop-1-mathematics&batchId=308 2 | /* 3 | You are given an interger I, find the absolute value of the interger I. 4 | */ 5 | int absolute(int I) { 6 | if(I >=0 ) 7 | return I; 8 | if(I < 0) 9 | return -1*I; 10 | } -------------------------------------------------------------------------------- /DSA-workshop/check_for_binary.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-for-binary/1/?track=dsa-workshop-1-strings&batchId=308 2 | bool isBinary(string str) 3 | { 4 | for(int i =0; i < str.size(); i++) 5 | { 6 | if(str[i] != '0' && str[i] != '1') 7 | return false; 8 | } 9 | return true; 10 | } -------------------------------------------------------------------------------- /DSA-workshop/check_if_two_arrays_are_equals_or_not.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-if-two-arrays-are-equal-or-not3847/1/?track=dsa-workshop-1-hashing&batchId=308 2 | bool check(vector arr, vector brr, int n) { 3 | int len1 = arr.size(); 4 | int len2 = brr.size(); 5 | 6 | if(len1 != len2) 7 | return false; 8 | unordered_mapmp, mp2; 9 | for(int i =0; i< len1; i++) 10 | mp[arr[i]]++; 11 | for(int i =0; i < len2; i++) 12 | mp2[brr[i]]++; 13 | 14 | for(auto x : mp) 15 | { 16 | if(mp2[x.first] != x.second) 17 | return false; 18 | } 19 | return true; 20 | } -------------------------------------------------------------------------------- /DSA-workshop/equilibrium-point.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://practice.geeksforgeeks.org/problems/equilibrium-point-1587115620/1/?track=dsa-workshop-1-arrays&batchId=308 3 | Given an array A of N positive numbers. The task is to find the first Equilibium Point in the array. 4 | Equilibrium Point in an array is a position such that the sum of elements before it is equal to the sum of elements after it. 5 | */ 6 | int equilibriumPoint(long long a[], int n) { 7 | if (n == 1) 8 | return 1; 9 | 10 | int total = 0; 11 | for (int i = 0; i < n; i++) 12 | total += a[i]; 13 | int left = 0; 14 | for (int i = 0; i < n; i++) 15 | { 16 | total -= a[i]; 17 | if (total == left) 18 | return 1 + i; 19 | left += a[i]; 20 | } 21 | return -1; 22 | } -------------------------------------------------------------------------------- /DSA-workshop/find-transiction-point.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/find-transition-point-1587115620/1/?track=dsa-workshop-1-arrays&batchId=308# 2 | int transitionPoint(int arr[], int n) { 3 | if (n == 1) 4 | { 5 | if (arr[0] == 1) 6 | return 0; 7 | return -1; 8 | } 9 | int left = 0; 10 | int right = n - 1; 11 | int middle; 12 | while (left < right) 13 | { 14 | middle = left + (left + right) / 2; 15 | if (arr[middle] == 0 && arr[middle + 1] == 1) 16 | return 1 + middle; 17 | if (arr[middle] == 1 && arr[middle - 1] == 0) 18 | return middle; 19 | if (arr[middle] == 0) 20 | left += 1; 21 | if (arr[middle] == 1) 22 | right--; 23 | } 24 | return -1; 25 | } -------------------------------------------------------------------------------- /DSA-workshop/find-transition-point.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/find-transition-point-1587115620/1/?track=dsa-workshop-1-arrays&batchId=308 2 | int transitionPoint(int arr[], int n) { 3 | int left = 0; 4 | int right = n-1; 5 | int middle; 6 | while(left <= right) 7 | { 8 | middle = (left+right)/2; 9 | if(arr[middle] == 0) 10 | { 11 | if(arr[middle+1] == 1) 12 | return middle+1; 13 | else 14 | left++; 15 | } 16 | else if(arr[middle] == 1) 17 | { 18 | if(arr[middle-1] ==0) 19 | return middle; 20 | else 21 | right --; 22 | } 23 | } 24 | return -1; 25 | } -------------------------------------------------------------------------------- /DSA-workshop/first-repeating-element.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/first-repeating-element4018/1/?track=dsa-workshop-1-arrays&batchId=308 2 | /* 3 | Given an array arr[] of size N, find the first repeating element. 4 | The element should occurs more than once and the index of its 5 | first occurrence should be the smallest. 6 | */ 7 | int firstRepeated(int arr[], int n) { 8 | unordered_mapmp; 9 | for (int i = 0; i < n; i++) 10 | mp[arr[i]]++; 11 | 12 | for (int i = 0; i < n; i++) 13 | { 14 | if (mp[arr[i]] > 1) 15 | return i + 1; 16 | } 17 | return -1; 18 | } -------------------------------------------------------------------------------- /DSA-workshop/immediate_smaller.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/find-immediate-smaller-than-x/1/?track=dsa-workshop-1-arrays&batchId=308 2 | int immediateSmaller(int arr[], int n, int x) 3 | { 4 | sort(arr, arr + n); 5 | int last = -1; 6 | for (int i = 0; i < n; i++) 7 | { 8 | if (arr[i] >= x) 9 | return last; 10 | if (arr[i] < x) 11 | last = arr[i]; 12 | } 13 | return last; 14 | } -------------------------------------------------------------------------------- /DSA-workshop/max_occurrency_number.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/maximum-occuring-character-1587115620/1/?track=dsa-workshop-1-strings&batchId=308 2 | char getMaxOccuringChar(char* str) 3 | { 4 | unordered_mapmp; 5 | for(int i =0; i < strlen(str); i++) 6 | mp[str[i]]++; 7 | 8 | char result; 9 | int smallest_frequency = -1; 10 | for(auto x : mp) 11 | { 12 | if(x.second > smallest_frequency) 13 | { 14 | smallest_frequency = x.second; 15 | result = x.first; 16 | } 17 | if(x.second == smallest_frequency && x.first < result) 18 | { 19 | result = x.first; 20 | } 21 | } 22 | return result; 23 | } -------------------------------------------------------------------------------- /DSA-workshop/occurrence_of_integr_n_ll.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/occurence-of-an-integer-in-a-linked-list/1/?track=dsa-workshop-1-linkedlist&batchId=308 2 | int count(struct node* head, int search_for) 3 | { 4 | int counter = 0; 5 | while(head != NULL) 6 | { 7 | if(head->data == search_for) 8 | counter++; 9 | head = head->next; 10 | } 11 | return counter; 12 | } -------------------------------------------------------------------------------- /DSA-workshop/pair_wise_swapping.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/pairwise-swap-elements-of-a-linked-list-by-swapping-data/1/?track=dsa-workshop-1-linkedlist&batchId=308 2 | Node* pairWiseSwap(struct Node* head) { 3 | if(head == NULL || head->next == NULL) 4 | return head; 5 | Node *current = head; 6 | while(current != NULL && current->next != NULL) 7 | { 8 | swap(current->data, current->next->data); 9 | current = current->next->next; 10 | } 11 | return head; 12 | } -------------------------------------------------------------------------------- /DSA-workshop/print_linked_list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/print-linked-list-elements/1/?track=dsa-workshop-1-linkedlist&batchId=308 2 | void display(Node *head) 3 | { 4 | while(head != NULL) 5 | { 6 | cout << head->data << " "; 7 | head = head->next; 8 | } 9 | } -------------------------------------------------------------------------------- /DSA-workshop/print_non_repeating_chars.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/print-distinct-elements-1587115620/1/?track=dsa-workshop-1-hashing&batchId=308 2 | vector printNonRepeated(int arr[],int n) 3 | { 4 | unordered_mapmp; 5 | for(int i =0; i< n; i++) 6 | mp[arr[i]]++; 7 | 8 | vector result; 9 | for(int i =0; i< n; i++) 10 | { 11 | if(mp[arr[i]] == 1) 12 | result.push_back(arr[i]); 13 | } 14 | return result; 15 | } -------------------------------------------------------------------------------- /DSA-workshop/reverse_string_using_stack.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/reverse-a-string-using-stack/1/?track=dsa-workshop-1-stack&batchId=308 2 | char* reverse(char *S, int len) 3 | { 4 | stackst; 5 | for(int i =0; i < len; i++) 6 | { 7 | st.push(S[i]); 8 | } 9 | for(int i =0; i< len; i++) 10 | { 11 | S[i] = st.top(); 12 | st.pop(); 13 | } 14 | return S; 15 | } -------------------------------------------------------------------------------- /DSA-workshop/rotate-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://practice.geeksforgeeks.org/problems/rotate-array-by-n-elements-1587115621/1/?track=dsa-workshop-1-arrays&batchId=308 3 | Given an unsorted array arr[] of size N, rotate it by D elements in the counter-clockwise direction. 4 | */ 5 | void rotateArr(int arr[], int d, int n) { 6 | int temp[d]; 7 | for (int i = 0; i < d; i++) 8 | temp[i] = arr[i]; 9 | 10 | // Shift array 11 | int j = 0; 12 | for (int i = d; i < n; i++) 13 | { 14 | arr[j] = arr[i]; 15 | j++; 16 | } 17 | 18 | j = 0; 19 | // store temp elements in original array, starting from index d; 20 | for (int i = n - d; i < n; i++) 21 | { 22 | arr[i] = temp[j++]; 23 | } 24 | } -------------------------------------------------------------------------------- /DSA-workshop/sort002.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s4231/1/?track=dsa-workshop-1-search-sort&batchId=308 2 | void sort012(int a[], int n) 3 | { 4 | sort(a, a+n); 5 | } -------------------------------------------------------------------------------- /DSA-workshop/wave-array.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/wave-array-1587115621/1/?track=dsa-workshop-1-arrays&batchId=308 2 | /* 3 | Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array and return it. 4 | In other words, arrange the elements into a sequence such that a1 >= a2 <= a3 >= a4 <= a5..... 5 | (considering the increasing lexicographical order). 6 | */ 7 | void convertToWave(int* arr, int n) { 8 | for (int i = 0; i < n - 1; i += 2) 9 | swap(arr[i], arr[i + 1]); 10 | } -------------------------------------------------------------------------------- /DSA-workshop/who-has-majority.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/who-has-the-majority/1/?track=dsa-workshop-1-arrays&batchId=308# 2 | /* 3 | Given an array arr[] of size N and two elements x and y, use counter variables to find which element 4 | appears most in the array, x or y. If both elements have the same frequency, then return the smaller 5 | element. 6 | Note: We need to return the element, not its count. 7 | */ 8 | int majorityWins(int arr[], int n, int x,int y) 9 | { 10 | int frequency_x = 0, frequency_y = 0; 11 | for(int i =0; i< n; i++) 12 | { 13 | if(arr[i] == x) 14 | frequency_x ++; 15 | if(arr[i] == y) 16 | frequency_y++; 17 | } 18 | if(frequency_x == frequency_y) 19 | return min(x, y); 20 | else if(frequency_x > frequency_y) 21 | return x; 22 | else 23 | return y; 24 | } -------------------------------------------------------------------------------- /c++/Palindrom.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/palindrome0746/1/?problemStatus=unsolved&difficulty[]=-2&page=1&query=problemStatusunsolveddifficulty[]-2page1 2 | class Solution 3 | { 4 | public: 5 | string is_palindrome(int n) 6 | { 7 | string number = to_string(n); 8 | int start = 0; 9 | int end = number.size()-1; 10 | for(start =0; start < number.size()/2; start++) 11 | { 12 | if(number[start] != number[end]) 13 | return "No"; 14 | end--; 15 | } 16 | return "Yes"; 17 | } 18 | }; -------------------------------------------------------------------------------- /c++/Search in a row-column sorted Matrix.cpp: -------------------------------------------------------------------------------- 1 | int search( int n,int m, int x, int mat[SIZE][SIZE]) 2 | { 3 | int row=0,col=m-1; 4 | while(row=0) 5 | { 6 | if(mat[row][col]x) 11 | { 12 | col--; 13 | } 14 | else if(mat[row][col]==x) 15 | { 16 | return 1; 17 | } 18 | } 19 | return 0; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /c++/absolute-value.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/absolute-value/1/?track=ppc-mathematics&batchId=221 2 | int absolute(int I) { 3 | if(I < 0) 4 | return -1*I; 5 | return I; 6 | } -------------------------------------------------------------------------------- /c++/adding-two-matrices.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/adding-two-matrices3512/1/?track=ppc-matrix&batchId=221 2 | void sumMatrix(int n1, int m1, int n2, int m2, int arr1[SIZE][SIZE], int arr2[SIZE][SIZE]){ 3 | 4 | if(n1 != n2 || m1 != m2) 5 | { 6 | cout << -1; 7 | return; 8 | } 9 | for(int i =0; i< n1; i++) 10 | { 11 | for(int j=0; j< m1; j++) 12 | cout << arr1[i][j] + arr2[i][j] << " "; 13 | } 14 | } -------------------------------------------------------------------------------- /c++/addition-of-two-numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int addition(int A, int B){ 4 | return A+B; 5 | } 6 | }; -------------------------------------------------------------------------------- /c++/addition-under-modulo.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/addition-under-modulo/1/?track=ppc-mathematics&batchId=221 2 | /* 3 | Modular Arithmetic: 4 | (a + b) mod M = ((a mod M) + (b mod M)) mod M. 5 | (a - b) mod M = ((a mod M) - (b mod M)) mod M. 6 | (a * b) mod M = ((a mod M) * (b mod M)) mod M. 7 | */ 8 | int sumUnderModulo(long long a,long long b) 9 | { 10 | int M=1000000007; 11 | int result = (a%M) + (b%M); 12 | return result%M; 13 | } -------------------------------------------------------------------------------- /c++/ambstrong.cpp: -------------------------------------------------------------------------------- 1 | // Author: Davide Pollicino -> github.com/omonimus1 2 | // Date: 17/04/2020 3 | // https://practice.geeksforgeeks.org/problems/armstrong-numbers/0 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | void solve_test_case() 9 | { 10 | int number = 371 , original_number; 11 | long long int sum=0 , single_digit; 12 | original_number = number; 13 | cin >> number; 14 | 15 | while(number) 16 | { 17 | single_digit = number % 10; 18 | number /= 10; 19 | sum += powl(single_digit, 3); 20 | } 21 | 22 | if(sum == original_number) 23 | cout<<"Yes"<> t; 33 | while(t--) 34 | solve_test_case(); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /c++/amend-the-sentence.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/amend-the-sentence/0/ 2 | #include 3 | using namespace std; 4 | 5 | void print_words(string word) 6 | { 7 | for(int i =0; i< word.size(); i++) 8 | { 9 | if(isupper(word[i])) 10 | { 11 | if(i > 0) cout<<" "; 12 | cout << (char)tolower(word[i]); 13 | continue; 14 | } 15 | cout << word[i]; 16 | } 17 | cout << endl; 18 | } 19 | 20 | int main() 21 | { 22 | int t; 23 | cin >> t; 24 | string word; 25 | for(int i=0; i> word; 28 | print_words(word); 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /c++/array-delete-and-shift.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/array-delete-and-shift/1/?track=DSA-Foundation-Final-Arrays&batchId=193 2 | void deleteFromArray(int arr[], int n, int index){ 3 | for(int i = index; i < n-1; i++) 4 | arr[i] = arr[i+1]; 5 | arr[n-1] = 0; 6 | } -------------------------------------------------------------------------------- /c++/array-insert-at-end.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/array-insert-at-end/1/?track=DSA-Foundation-Final-Arrays&batchId=193 2 | void insertAtEnd(int arr[],int sizeOfArray,int element) 3 | { 4 | arr[sizeOfArray-1] = element; 5 | } -------------------------------------------------------------------------------- /c++/array-insert-at-index.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/array-insert-at-index/1/?track=DSA-Foundation-Final-Arrays&batchId=193 2 | void insertAtIndex(int arr[], int sizeOfArray, int index, int element) 3 | { 4 | // Shift from index to end arrays element by one 5 | for(int i = sizeOfArray-1; i >= index; i--) 6 | { 7 | arr[i] = arr[i-1]; 8 | } 9 | arr[index] = element; 10 | 11 | } -------------------------------------------------------------------------------- /c++/array-update-by-index.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/array-update-at-index/1/?track=DSA-Foundation-Final-Arrays&batchId=193 2 | //Complete this function 3 | void updateArray(int arr[], int index, int element){ 4 | arr[index] = element; 5 | } -------------------------------------------------------------------------------- /c++/back-to-front.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/back-to-front/1 2 | /*Function to print vector in reverse order 3 | * using iterator 4 | * it1 : v.rbegin(), reverse iterator 5 | * it2 : v.rend(), reverse iterator 6 | */ 7 | void reverseIterator(vector::reverse_iterator it1, vector::reverse_iterator it2){ 8 | vector::reverse_iterator it; 9 | for (it = it1; it != it2; it++) 10 | cout << *it<<" "; 11 | cout< bfs(vector g[], int N) { 3 | queue qu; 4 | vector res; 5 | // Create vector of N and set all of the them as false 6 | vector vis(N, false); 7 | // N is the root: starting point 8 | qu.push(0); 9 | // Set first root as visited 10 | vis[0]=true; 11 | // While queue q is not empty: 12 | while(!qu.empty()) 13 | { 14 | // Deque vertex from queue 15 | int num=qu.front(); 16 | qu.pop(); 17 | // Enque all not yet visited adjancent 18 | res.push_back(num); 19 | for(auto it=g[num].begin(); it!=g[num].end();it++) 20 | { 21 | if(vis[*it]==false) 22 | { 23 | vis[*it]=true; 24 | qu.push(*it); 25 | } 26 | } 27 | } 28 | return res; 29 | } -------------------------------------------------------------------------------- /c++/bin-search.cpp: -------------------------------------------------------------------------------- 1 | 2 | int bin_search(int A[], int left, int right, int k) 3 | { 4 | if (right >= left) { 5 | int mid = left + (right - left) / 2; 6 | 7 | // If the element is present at the middle 8 | // itself 9 | if (A[mid] == k) 10 | return mid; 11 | 12 | // If element is smaller than mid, then 13 | // it can only be present in left subarray 14 | if (A[mid] > x) 15 | return bin_search(A, left, mid - 1, k); 16 | 17 | // Else the element can only be present 18 | // in right subarray 19 | return bin_search(A, mid + 1, right, k); 20 | } 21 | 22 | // We reach here when element is not 23 | // present in array 24 | return -1; 25 | } 26 | -------------------------------------------------------------------------------- /c++/binary-array-sorting.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/binary-array-sorting5355/1/?track=amazon-arrays&batchId=192 2 | vector SortBinaryArray(vector binArray) 3 | { 4 | sort(binArray.begin(), binArray.end()); 5 | return binArray; 6 | } -------------------------------------------------------------------------------- /c++/binary-array-sorting2.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/binary-array-sorting-1587115620/1/?track=DSA-Foundation-Final-Sorting&batchId=193 2 | // Cannot use sort built-in function 3 | // Max time complecity: O(N); 4 | // Function to sort the array A[] 5 | // A[]: input array 6 | // N: input array 7 | void binSort(int A[], int N) 8 | { 9 | int zeros = 0; 10 | int ones = 0; 11 | for(int i =0; i < N; i++) 12 | { 13 | if(A[i] == 1) 14 | ones++; 15 | else 16 | zeros++; 17 | } 18 | 19 | int i=0; 20 | while(zeros != 0) 21 | { 22 | A[i] = 0; 23 | i++; 24 | zeros--; 25 | } 26 | 27 | while(ones != 0) 28 | { 29 | A[i]=1; 30 | i++; 31 | ones--; 32 | } 33 | } -------------------------------------------------------------------------------- /c++/binary-arrays-sorting.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/binary-array-sorting5355/1/?track=amazon-arrays&batchId=192 2 | vector SortBinaryArray(vector binArray) 3 | { 4 | int ones=0, zeros=0; 5 | int len = binArray.size(); 6 | 7 | for(int i =0; i < len; i++) 8 | { 9 | if(binArray[i] == 1) 10 | ones++; 11 | else 12 | zeros++; 13 | } 14 | 15 | int i =0; 16 | while(zeros !=0) 17 | { 18 | binArray[i] = 0; 19 | zeros--; 20 | i++; 21 | } 22 | 23 | while(ones != 0) 24 | { 25 | binArray[i] = 1; 26 | ones--; 27 | i++; 28 | } 29 | return binArray; 30 | } 31 | -------------------------------------------------------------------------------- /c++/bitwise-operator.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/bitwise-operators/1/?track=module-1-basics-and-control-structures&batchId=113 2 | /* 3 | Example: 4 | Input: 5 | 1 6 | 4 8 2 7 | 8 | Output: 9 | 0 10 | 10 11 | 0 12 | 2 13 | -11 14 | */ 15 | void bitWiseOperation(int a, int b, int c){ 16 | int d = a ^ a; 17 | int e = c ^ b; 18 | int f = a & b; 19 | int g = c | (a ^ a); 20 | int flip_e = ~e; 21 | cout << d <arr[j+1]) 7 | swap(&arr[j],&arr[j+1]); 8 | } 9 | } -------------------------------------------------------------------------------- /c++/bubble_sort.cpp: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int arr[] = {10, 20, 30, 40, 50}, i, j, isSwapped; 4 | int n = sizeof(arr) / sizeof(*arr); 5 | isSwapped = 1; 6 | for(i = 0; i < n - 1 && isSwapped; ++i) 7 | { 8 | isSwapped = 0; 9 | for(j = 0; j < n - i - 1; ++j) 10 | if (arr[j] > arr[j + 1]) 11 | { 12 | swap(&arr[j], &arr[j + 1]); 13 | isSwapped = 1; 14 | } 15 | } 16 | for(i = 0; i < n; ++i) 17 | printf("%d ", arr[i]); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /c++/c-fiend-function.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/c-friend-function/1/ 2 | bool isAnagram(string name, string surname) 3 | { 4 | if(name.size() != surname.size()) 5 | return false; 6 | else 7 | { 8 | sort(name.begin(), name.end()); 9 | sort(surname.begin(), surname.end()); 10 | for(int i =0; i< name.size(); i++) 11 | { 12 | if(name[i] != surname[i]) 13 | return false; 14 | } 15 | return true; 16 | } 17 | } 18 | 19 | void check_name(student k) 20 | { 21 | if (isAnagram(k.first_name, k.last_name)) 22 | cout <<"ANAGRAM"< 2 | void sortArray(T a[], int n) 3 | { 4 | sort(a, a+n); 5 | } 6 | 7 | template 8 | void printArray(T a[], int n) 9 | { 10 | for(int i=0; i < n; i++) 11 | cout << a[i] << " "; 12 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | cout<<"Hello World"; 6 | return 0; 7 | } -------------------------------------------------------------------------------- /c++/c-to-f.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/convert-celsius-to-fahrenheit/1/?track=ppc-mathematics&batchId=221 2 | 3 | // Function that converts Celsius To Fahrenheit 4 | double cToF(int C) 5 | { 6 | // 0C is About 32 F 7 | return (C * 9/5) + 32; 8 | } -------------------------------------------------------------------------------- /c++/check-if-a-numbe-is-power-of-anter.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-if-a-number-is-power-of-another-number/0/?track=sp-mathematics&batchId=152 2 | #include 3 | using namespace std; 4 | 5 | bool solve_test() 6 | { 7 | long int x=7, y= 3; 8 | //cin >> x >> y; 9 | if(x ==1 && y !=x) 10 | return 0; 11 | if(x == 1 && y == 1) 12 | return 1; 13 | if(x > y) 14 | return 0; 15 | int power = x; 16 | while(power <= y) 17 | { 18 | power *= x; 19 | } 20 | if(power == y ) 21 | return 1; 22 | else 23 | return 0; 24 | 25 | } 26 | 27 | int main() 28 | { 29 | int t=1; 30 | //cin >> t; 31 | 32 | for(int i=0; i arr[i+1]) 18 | return false; 19 | } 20 | return true; 21 | } 22 | }; -------------------------------------------------------------------------------- /c++/check-if-b-is-an-anagram-of-a.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/anagram-1587115620/1/?track=DSA-Foundation-Final-String&batchId=193 2 | /* 3 | Given two strings a and b consisting of lowercase characters. 4 | The task is to check whether two given strings are an anagram of each 5 | other or not. 6 | An anagram of a string is another string that contains the same characters, 7 | only the order of characters can be different. For example, “act” and “tac” 8 | are an anagram of each other. 9 | */ 10 | bool isAnagram(string c, string d){ 11 | if(c.size() != d.size()) 12 | return false; 13 | sort(c.begin(), c.end()); 14 | sort(d.begin(), d.end()); 15 | 16 | for(int i =0; i < c.size(); i++) 17 | { 18 | if(c[i] != d[i]) 19 | return false; 20 | } 21 | return true; 22 | } -------------------------------------------------------------------------------- /c++/check-if-linked-list-is-palindrome.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-if-linked-list-is-pallindrome/1/ 2 | 3 | bool isStringPalindrome(string list_content) 4 | { 5 | if(list_content.size() <= 1) 6 | return true; 7 | int end_point = list_content.size()-1; 8 | for(int i =0; i< list_content.size()/2; i++) 9 | { 10 | if(list_content[i] != list_content[end_point]) 11 | return false; 12 | end_point--; 13 | } 14 | return true; 15 | } 16 | 17 | bool isPalindrome(Node *head) 18 | { 19 | if(head == NULL) 20 | return true; 21 | string solution = ""; 22 | Node * current = head; 23 | while(current != NULL) 24 | { 25 | solution.append(to_string(current->data)); 26 | current = current -> next; 27 | } 28 | return isStringPalindrome(solution); 29 | } -------------------------------------------------------------------------------- /c++/check-if-string-is-isogram.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-if-a-string-is-isogram-or-not/0 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | bool isIsogram(string word) 8 | { 9 | for(int i =0; i< word.size()-1; i++) 10 | { 11 | if(word[i] == word[i+1]) 12 | return false; 13 | } 14 | return true; 15 | } 16 | 17 | void solve_test() 18 | { 19 | // Declare and read string 20 | string word; 21 | cin >> word; 22 | 23 | // Sort string 24 | sort(word.begin(), word.end()); 25 | // Check if it is isogram or not 26 | if(isIsogram(word)) 27 | cout <<1<> t; 36 | 37 | while(t--) 38 | solve_test(); 39 | 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /c++/check-if-two-arrays-are-equals.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-if-two-arrays-are-equal-or-not3847/1/?track=ppc-hashing&batchId=221 2 | bool check(vector arr, vector brr, int n) { 3 | sort(arr.begin(), arr.end()); 4 | sort(brr.begin(), brr.end()); 5 | for(int i =0; i < arr.size(); i++) 6 | { 7 | if(arr[i] != brr[i]) 8 | return false; 9 | } 10 | return true; 11 | } -------------------------------------------------------------------------------- /c++/check-string.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-string1818/1/?problemStatus=unsolved&problemType=functional&difficulty[]=-2&page=2&sortBy=submissions&query=problemStatusunsolvedproblemTypefunctionaldifficulty[]-2page2sortBysubmissions 2 | //Given a string, check if all its characters are same or not. 3 | class Solution 4 | { 5 | public: 6 | bool check (string s) 7 | { 8 | int len = s.size(); 9 | for(int i =1; i< len-1; i++) 10 | { 11 | if(s[i] != s[0]) 12 | return false; 13 | } 14 | return true; 15 | } 16 | }; -------------------------------------------------------------------------------- /c++/check-whether-kth-bit-is-set.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-whether-k-th-bit-is-set-or-not-1587115620/1/?track=ppc-bit-magic&batchId=221 2 | // Function to check if Kth bit is set or not 3 | bool checkKthBit(int n, int k){ 4 | return (n>>k)&1; 5 | } -------------------------------------------------------------------------------- /c++/check_for_binary.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-for-binary/1/?track=ppc-strings&batchId=221 2 | // Return true if str is binary, else false 3 | bool isBinary(string str) 4 | { 5 | for(int i =0; i< str.size(); i++) 6 | { 7 | if(str[i] != '1' && str[i] != '0') 8 | return false; 9 | } 10 | return true; 11 | } -------------------------------------------------------------------------------- /c++/circular-linked-list.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/circular-linked-list/1/ 2 | bool isCircular(Node *head) 3 | { 4 | // Empty Lined List is circular 5 | if(head == NULL) 6 | return 1; 7 | Node *head_copy = head; 8 | while(head != NULL) 9 | { 10 | if(head->next == head_copy) 11 | return 1; 12 | head = head->next; 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /c++/circular-list-delete-at-position.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/circular-linked-list-delete-at-position/1/?track=DSA-Foundation-Final-Circular-Linked-List&batchId=193 2 | 3 | Node * deleteAtPosition(Node *head,int position) 4 | { 5 | if(head == NULL) 6 | return NULL; 7 | // Corner case: the node to remove is the head 8 | if(position == 1) 9 | { 10 | return free(head);; 11 | } 12 | else 13 | { 14 | Node *current = head; 15 | for(int i = 0; i < position-2; i++) 16 | current = current ->next; 17 | 18 | Node *temp = current->next; 19 | current->next = current->next->next; 20 | free(temp); 21 | return head; 22 | } 23 | } -------------------------------------------------------------------------------- /c++/compare-circular-doubly-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/compare-circular-doubly-linked-lists/1/?track=DSA-Foundation-Final-DLL&batchId=193 2 | /* 3 | * Compares two Circular Double Linked List, returns true if equals, false otherwise 4 | */ 5 | bool compareCLL(Node * head1, Node * head2) 6 | { 7 | if(head1 == NULL && head2 == NULL) 8 | return true; 9 | 10 | Node *current1 = head1; 11 | Node *current2 = head2; 12 | 13 | 14 | while(current1->next != head1 || current2->next != head2) 15 | { 16 | if(current1->data != current2->data) 17 | return false; 18 | current1 = current1->next; 19 | current2 = current2->next; 20 | } 21 | return true; 22 | } 23 | -------------------------------------------------------------------------------- /c++/compare-two-linked-lists.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/compare-two-linked-lists/1/?track=sp-linked-list&batchId=152 2 | // Compare two strings represented as linked lists 3 | int compare(Node *list1, Node *list2) 4 | { 5 | if(list1 == NULL && list2 == NULL) 6 | return 0; 7 | string s1 = ""; 8 | string s2 = ""; 9 | while(list1 != NULL) 10 | { 11 | s1.push_back(list1->c); 12 | list1 = list1->next; 13 | } 14 | while( list2 != NULL) 15 | { 16 | s2.push_back(list2->c); 17 | list2 = list2->next; 18 | } 19 | if(s1 == s2) 20 | return 0; 21 | if (s1 > s2) 22 | return 1; 23 | return -1; 24 | } -------------------------------------------------------------------------------- /c++/consonant-and-vowals.checkcpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/consonants-and-vowels-check/1/?track=module-2-arrays-and-strings&batchId=113 2 | void checkString(string s) 3 | { 4 | int v=0; 5 | int c=0; 6 | 7 | // convert strng to lower_case 8 | 9 | for(int i =0; i< s.size(); i++) 10 | { 11 | if(s[i] == ' ') 12 | continue; 13 | if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' ) 14 | v++; 15 | else 16 | c++; 17 | } 18 | 19 | if(v>c) 20 | cout<<"Yes"<v) 22 | cout<<"No"< x) 8 | count++; 9 | } 10 | return count; 11 | } -------------------------------------------------------------------------------- /c++/count-bst-nodes-that-lie-in-a-give-range.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-bst-nodes-that-lie-in-a-given-range/1 2 | int getCountOfNode(Node *root, int l, int h) 3 | { 4 | int count = 0; 5 | if(root == NULL) 6 | return 0; 7 | if(root->data >=l && root->data <= h) 8 | count++; 9 | return count + getCountOfNode(root->left, l, h) + getCountOfNode(root->right, l, h); 10 | } -------------------------------------------------------------------------------- /c++/count-distinc-vowels.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-distinct-vowels-in-string/1/?track=DSA-Foundation-Final-String&batchId=193 2 | /* 3 | Count the total distinct vowels in the string 4 | */ 5 | bool isVowel(char c) 6 | { 7 | if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' 8 | || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') 9 | return true; 10 | 11 | return false; 12 | } 13 | 14 | 15 | int countVowels(string str){ 16 | sets; 17 | for(int i =0; i < str.size(); i++) 18 | { 19 | if(isVowel(str[i])) 20 | s.insert(str[i]); 21 | } 22 | return s.size(); 23 | } -------------------------------------------------------------------------------- /c++/count-element-occurences.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-element-occurences/1/?track=amazon-searching&batchId=192 2 | int countOccurence(int arr[], int n, int k) { 3 | 4 | int frequency = n/k; 5 | mapmp; 6 | for(int i =0; i < n; i++) 7 | mp[arr[i]]++; 8 | 9 | int count = 0; 10 | for(auto x : mp) 11 | { 12 | if(x.second > frequency) 13 | count++; 14 | } 15 | return count; 16 | } -------------------------------------------------------------------------------- /c++/count-leaves-in-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-leaves-in-binary-tree/1/ 2 | int countLeaves(Node* root) 3 | { 4 | Node *node = root; 5 | if(node == NULL) 6 | return 0; 7 | else if(node ->left == NULL && node ->right == NULL) 8 | return 1; 9 | else 10 | return countLeaves(node->left) + countLeaves(node->right); 11 | } -------------------------------------------------------------------------------- /c++/count-nodes-of-linked-list.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/count-nodes-of-linked-list/1 2 | int getCount(struct Node* head){ 3 | Node * current_node = head; 4 | if(current_node == NULL) 5 | return 0; 6 | else 7 | { 8 | int counter=0; 9 | while(current_node != NULL) 10 | { 11 | counter++; 12 | current_node = current_node -> next; 13 | } 14 | return counter; 15 | } 16 | } -------------------------------------------------------------------------------- /c++/count-non-repeating-elements.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-distinct-elements-1587115620/1/?track=DSA-Foundation-Final-Hashing&batchId=193 2 | int countNonRepeated(int arr[], int n) 3 | { 4 | int counter = 0; 5 | unordered_mapmp; 6 | for(int i =0; i < n; i++) 7 | { 8 | mp[arr[i]]++; 9 | } 10 | for(auto x : mp) 11 | { 12 | if(x.second > 1) 13 | continue; 14 | else 15 | counter++; 16 | } 17 | return counter; 18 | } -------------------------------------------------------------------------------- /c++/count-number-of-set-bits.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-total-set-bits/0/ 2 | #include 3 | using namespace std; 4 | 5 | int count_number_of_set_bits(int number) 6 | { 7 | int result=0; 8 | int n; 9 | for(int i =1; i<= number; i++) 10 | { 11 | n = i; 12 | while( n > 0) 13 | { 14 | if(n % 2) 15 | result++; 16 | n /=2; 17 | } 18 | 19 | } 20 | return result; 21 | } 22 | 23 | 24 | int main() 25 | { 26 | int t; 27 | cin >> t; 28 | 29 | while(t--) 30 | { 31 | // Read end_range number 32 | int b; 33 | cin >> b; 34 | cout << count_number_of_set_bits(b)< 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | int number_of_test_cases, a, b, m, counter; 9 | cin >> number_of_test_cases; 10 | 11 | while(number_of_test_cases--) 12 | { 13 | counter = 0; 14 | cin >> a >> b >> m; 15 | for(int i =a; i<= b; i++) 16 | { 17 | if(i%m == 0) 18 | counter++; 19 | } 20 | cout <data == search_for) 14 | counter++; 15 | 16 | head = head->next; 17 | } 18 | return counter; 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /c++/count-odd-even.cpp: -------------------------------------------------------------------------------- 1 | void countOddEven(int arr[], int sizeof_array) 2 | { 3 | 4 | // your code here 5 | unsigned long int odds=0, evens=0; 6 | for(int i =0; i< sizeof_array; i++) 7 | { 8 | if(arr[i] %2 == 0) 9 | evens++; 10 | else 11 | odds++; 12 | } 13 | cout< 3 | using namespace std; 4 | 5 | int GetNumberCaseChaes(string sentence) 6 | { 7 | int counter = 0; 8 | for(int i =0; i< sentence.size(); i++) 9 | { 10 | if(sentence[i] >= 'A' && sentence[i] <= 'Z') 11 | counter++; 12 | } 13 | return counter; 14 | } 15 | 16 | int main() 17 | { 18 | ios_base::sync_with_stdio(false); 19 | cin.tie(NULL); 20 | long int t; 21 | cin >> t; 22 | string word; 23 | while(t--) 24 | { 25 | cin >> word; 26 | cout << GetNumberCaseChaes(word) < 3 | using namespace std; 4 | 5 | void solve_test() 6 | { 7 | long int n; 8 | cin >> n; 9 | long int arr[n]; 10 | int counter; 11 | for(int i=0; i < n; i++) cin >> arr[i]; 12 | 13 | for(int i =0; i < n-1; i++) 14 | { 15 | counter = 0; 16 | for(int j = i+1; j< n; j++) 17 | { 18 | if(arr[i] > arr[j]) 19 | counter++; 20 | } 21 | cout << counter << " "; 22 | } 23 | cout << 0 <> t; 30 | 31 | for(int i=0; i 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | long int number, list_size, test_cases, counter; 8 | cin >> test_cases; 9 | 10 | while(test_cases--) 11 | { 12 | counter = 0; 13 | cin >> list_size; 14 | for(int i=0; i< list_size; i++) 15 | { 16 | cin >> number; 17 | if(number == 0) 18 | counter++; 19 | } 20 | cout << counter << endl; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /c++/count-words-in-string.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-words-in-string/1/?track=DSA-Foundation-Final-String&batchId=193 2 | /* 3 | The goal of this exercise function is to calculate the number of words 4 | in a sentence, where every word is separated by a space. 5 | */ 6 | int countWords(string str){ 7 | int counter = 0; 8 | for(int i =0; i < str.size(); i++) 9 | { 10 | if(str[i] == ' ') 11 | counter++; 12 | } 13 | return counter+1; 14 | } 15 | -------------------------------------------------------------------------------- /c++/count-zeros.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-zeros-in-a-sorted-matrix/1 2 | // Function solutions -> Tag: Amazon - matrix 3 | int countZeroes(int A[MAX][MAX],int N) 4 | { 5 | int number_of_zeros=0; 6 | for(int i=0; inext; 8 | new_head->prev = NULL; 9 | free(head); 10 | return new_head; 11 | } -------------------------------------------------------------------------------- /c++/delete-middle-of-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/delete-middle-of-linked-list/1/?track=amazon-linkedlists&batchId=192 2 | Node* deleteMid(Node* head) { 3 | if(head ==NULL) return NULL; 4 | 5 | // Get number of nodes in the LL 6 | Node *current = head; 7 | int c = count_number_of_nodes(current); 8 | 9 | int position = (c/2); 10 | current = head; 11 | 12 | 13 | while(current != NULL && position > 1) 14 | { 15 | position--; 16 | current = current ->next; 17 | } 18 | 19 | //if(current == NULL || current->next == NULL) return NULL; 20 | 21 | Node *next = current->next->next; 22 | free(current->next); 23 | current->next = next; 24 | 25 | return head; 26 | } -------------------------------------------------------------------------------- /c++/delete-node-without-head-pointer.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/delete-without-head-pointer/1/ 2 | void deleteNode(Node *node) 3 | { 4 | // Linked List is empty 5 | if (node == NULL) 6 | return; 7 | else { 8 | if (node->next == NULL) 9 | return; 10 | // Store the next node 11 | Node* temp = node->next; 12 | 13 | // Copy data of the next node to current node 14 | node->data = node->next->data; 15 | 16 | // Set next to the node positionated after the node that will be removes 17 | node->next = node->next->next; 18 | // Remove node 19 | free(temp); 20 | } 21 | } -------------------------------------------------------------------------------- /c++/delete-tail-of-circular-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/delete-tail-of-circular-linked-list/1/?track=DSA-Foundation-Final-Circular-Linked-List&batchId=193 2 | /* 3 | * Removes tail of the Circurlar linked list 4 | */ 5 | Node * deleteTail(Node * head) 6 | { 7 | // Circular ll is empty 8 | if(head == NULL) 9 | return NULL; 10 | // There is just one node 11 | if(head->next == head) 12 | { 13 | free(head); 14 | return NULL; 15 | } 16 | 17 | Node *current = head; 18 | while(current->next->next != head) 19 | { 20 | current = current->next; 21 | } 22 | 23 | Node *to_remove = current->next; 24 | current->next = head; 25 | // Remove tail from ll 26 | free(to_remove); 27 | return head; 28 | } -------------------------------------------------------------------------------- /c++/delete-tail-of-doubly-linked-list.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/delete-tail-of-doubly-linked-list/1/?track=DSA-Foundation-Final-DLL&batchId=193 2 | Node *deleteTail(Node * head) 3 | { 4 | if(head == NULL) 5 | return NULL; 6 | Node *current = head; 7 | // Reach last element of doubly linked list 8 | while(current->next != NULL) 9 | { 10 | current = current->next; 11 | } 12 | // Set next's pointer of the future tail to NULL 13 | Node *prev= current->prev; 14 | prev->next =NULL; 15 | // Remove original tail from linked list 16 | free(current); 17 | return head; 18 | } -------------------------------------------------------------------------------- /c++/detect_loop.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/detect-loop-in-linked-list/1/ 2 | int detectloop(Node *head) { 3 | if(head == NULL && head->next == NULL) 4 | return 0; 5 | Node *slow = head; 6 | Node *fast = head; 7 | while(slow && fast && fast->next) 8 | { 9 | slow = slow->next; 10 | fast = fast->next->next; 11 | if(slow == fast) 12 | return 1; 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /c++/detele-tail-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/delete-tail-of-linked-list/1/?track=DSA-Foundation-Final-Linked-List&batchId=193 2 | Node * deleteTail(Node *head) 3 | { 4 | if(head->next == NULL) 5 | { 6 | free(head); 7 | return NULL; 8 | } 9 | 10 | Node *current = head; 11 | while(current->next->next!= NULL) 12 | current = current ->next; 13 | 14 | free(current->next); 15 | current->next= NULL; 16 | return head; 17 | } -------------------------------------------------------------------------------- /c++/digits-in-factorial.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/digits-in-factorial/1/?track=SPCF-Mathematics&batchId=154 2 | 3 | int digitsInFactorial(int n) 4 | { 5 | if (n < 0) 6 | return 0; 7 | 8 | // base case 9 | if (n <= 1) 10 | return 1; 11 | 12 | 13 | double digits = 0; 14 | for (int i=2; i<=n; i++) 15 | digits += log10(i); 16 | 17 | return floor(digits) + 1; 18 | } -------------------------------------------------------------------------------- /c++/display-circular-linked-list.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/display-circular-linked-list/1/?track=DSA-Foundation-Final-Circular-Linked-List&batchId=193 2 | void displayList(Node *head) 3 | { 4 | Node *current = head; 5 | while(current->next != head) 6 | { 7 | cout << current->data << " "; 8 | current = current->next; 9 | } 10 | // Print tail of circular linked list 11 | cout << current->data; 12 | } -------------------------------------------------------------------------------- /c++/display-double-circular-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/display-circular-doubly-linked-list/1/?track=DSA-Foundation-Final-DLL&batchId=193 2 | 3 | void displayList(Node *head) 4 | { 5 | //Head to Tail 6 | Node *current = head; 7 | while(current->next != head) 8 | { 9 | cout << current->data << " "; 10 | current = current->next; 11 | } 12 | cout<data<data << " "; 17 | current = current->prev; 18 | } 19 | cout <data; 20 | 21 | } -------------------------------------------------------------------------------- /c++/display-double-linked-list-forward-and-backward.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/display-doubly-linked-list/1/?track=DSA-Foundation-Final-DLL&batchId=193 2 | void displayList(Node *head) 3 | { 4 | //Head to Tail 5 | 6 | while(head->next !=NULL) 7 | { 8 | cout << head->data << " "; 9 | head = head->next; 10 | } 11 | cout<data<data << " "; 17 | head = head->prev; 18 | } 19 | } -------------------------------------------------------------------------------- /c++/display-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/display-linked-list/1/?track=DSA-Foundation-Final-Linked-List&batchId=193 2 | void displayList(Node *head) 3 | { 4 | while(head != NULL) 5 | { 6 | cout << head->data << " "; 7 | head = head->next; 8 | } 9 | } -------------------------------------------------------------------------------- /c++/display-longest-name.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/display-longest-name/0 2 | #include 3 | using namespace std; 4 | 5 | void solve_test() 6 | { 7 | int number_of_words; 8 | cin >> number_of_words; 9 | int max = -1; 10 | int len; 11 | string word, result=""; 12 | while(number_of_words--) 13 | { 14 | cin >> word; 15 | len = word.size(); 16 | if(len > max) 17 | { 18 | max = len; 19 | result = word; 20 | } 21 | } 22 | cout << result << endl; 23 | } 24 | 25 | int main() 26 | { 27 | ios_base::sync_with_stdio(false); 28 | cin.tie(NULL); 29 | 30 | long int t; 31 | cin >> t; 32 | while(t--) 33 | { 34 | solve_test(); 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /c++/doubly-linked-list-head-insert.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/doubly-linked-list-head-insert/1/?track=DSA-Foundation-Final-DLL&batchId=193 2 | Node *insertInHead(Node * head, int data) 3 | { 4 | Node *new_head = new Node(data); 5 | if(head == NULL) 6 | return new_head; 7 | else 8 | { 9 | new_head ->next = head; 10 | head->prev = new_head; 11 | return new_head; 12 | } 13 | } -------------------------------------------------------------------------------- /c++/doubly-linked-list-tail-insert.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/doubly-linked-list-tail-insert/1/?track=DSA-Foundation-Final-DLL&batchId=193 2 | Node *insertInTail(Node * head, int data) 3 | { 4 | Node *new_node = new Node(data); 5 | if(head == NULL) 6 | return new_node; 7 | 8 | Node *current = head; 9 | while(current->next != NULL) 10 | current = current->next; 11 | 12 | current->next = new_node; 13 | new_node->prev = current; 14 | 15 | return head; 16 | } 17 | -------------------------------------------------------------------------------- /c++/equilibrium-point.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/equilibrium-point-1587115620/1/?track=SPCF-Arrays&batchId=154 2 | // Function to find equilibrium point 3 | // a: input array 4 | // n: size of array 5 | int equilibriumPoint(long long a[], int n) { 6 | if(n==1) 7 | return 1; 8 | /* 9 | The idea is to get total sum of array first. 10 | Then Iterate through the array and keep updating the 11 | left sum which is initialized as zero. In the loop, we 12 | can get right sum by subtracting the elements one by one. 13 | Then check whether Leftsum and Rightsum are equal. 14 | */ 15 | int sum =0; 16 | for(int i =0; i duplicates(int a[], int n) { 3 | sort(a, a+n); 4 | vectorsolution; 5 | for(int i=0; i < n-1; i++) 6 | { 7 | if(a[i] == a[i+1]) 8 | { 9 | solution.push_back(a[i]); 10 | while(a[i+1] == a[i]) 11 | i++; 12 | } 13 | } 14 | if(solution.size()==0) 15 | solution.push_back(-1); 16 | return solution; 17 | } 18 | -------------------------------------------------------------------------------- /c++/find-first-set-bit-final2.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/find-first-set-bit-1587115620/1/?track=ppc-bit-magic&batchId=221 2 | /* function to find position of first set 3 | bit in the given number 4 | * n: given input for which we want to get 5 | the position of first set bit 6 | */ 7 | unsigned int getFirstSetBit(int n){ 8 | int counter =0; 9 | while(n> 0) 10 | { 11 | counter++; 12 | if(n%2 == 1) 13 | return counter; 14 | n/=2; 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /c++/find-lenght-of-loop.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/find-length-of-loop/1/?track=amazon-linkedlists&batchId=192 2 | int countNodes(struct Node *n) 3 | { 4 | int res = 1; 5 | struct Node *temp = n; 6 | while (temp->next != n) 7 | { 8 | res++; 9 | temp = temp->next; 10 | } 11 | return res; 12 | } 13 | 14 | int countNodesinLoop(struct Node *head) 15 | { 16 | if(head == NULL) 17 | return 0 ; 18 | Node *slow = head; 19 | Node *fast = head; 20 | while(slow && fast && fast->next) 21 | { 22 | fast = fast->next ->next; 23 | slow = slow->next; 24 | if(slow == fast) 25 | return countNodes(slow); 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /c++/find-maximum-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a number N, write a program to find a maximum 3 | number that can be formed using all of the digits of this number. 4 | https://practice.geeksforgeeks.org/problems/find-maximum-number2152/0/ 5 | */ 6 | class Solution { 7 | public: 8 | string findMax(string N) { 9 | sort(N.begin(), N.end()); 10 | reverse(N.begin(), N.end()); 11 | return N; 12 | } 13 | }; -------------------------------------------------------------------------------- /c++/find-minimum-and-maximum-nuber.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/find-minimum-and-maximum-element-in-an-array/0 2 | #include 3 | using namespace std; 4 | 5 | void solve_test() 6 | { 7 | long int n; 8 | cin >> n; 9 | long int arr[n]; 10 | for(int i =0; i< n; i++) cin >> arr[i]; 11 | if(n == 1) 12 | { 13 | cout << arr[0] << " "<> t; 25 | 26 | for(int i=0; inext; 9 | } 10 | 11 | return counter; 12 | } 13 | 14 | int fractional_node(struct Node *head, int k) 15 | { 16 | Node *current =head; 17 | int number_of_nodes = count_number_of_nodes(current); 18 | 19 | current = head; 20 | int position; 21 | if((number_of_nodes % k) == 0) 22 | position = number_of_nodes / k; 23 | else 24 | position = (number_of_nodes / k)+1; 25 | while(head != NULL && position >1) 26 | { 27 | position--; 28 | current = current->next; 29 | } 30 | 31 | if(current == NULL) 32 | return -1; 33 | return current->data; 34 | } -------------------------------------------------------------------------------- /c++/find-number-of-numbers.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/find-number-of-numbers/1 2 | int get_number_of_digits_k_in_number(int &number, int &k) 3 | { 4 | int counter = 0; 5 | while(number > 0) 6 | { 7 | if(number % 10 == k) 8 | counter++; 9 | number /= 10; 10 | } 11 | 12 | return counter; 13 | } 14 | 15 | int num(int a[], int n, int k) 16 | { 17 | int result = 0; 18 | for(int i =0; i < n; i++) 19 | { 20 | result += get_number_of_digits_k_in_number(a[i], k); 21 | } 22 | 23 | return result; 24 | } -------------------------------------------------------------------------------- /c++/find-odd-occurrenci.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/find-the-odd-occurence/0 2 | #include 3 | using namespace std; 4 | 5 | int solve_test() 6 | { 7 | //map mp; 8 | unordered_map umap; 9 | 10 | int n, number; 11 | cin >> n; 12 | for(int i =0; i < n; i++) 13 | { 14 | cin >> number; 15 | umap[number]++; 16 | } 17 | 18 | 19 | // Traversing an unordered map 20 | for (auto x : umap) 21 | { 22 | if( (x.second %2 ) != 0) 23 | return x.first; 24 | } 25 | // Eleemnt with odd frequency do not exists 26 | return 0; 27 | 28 | 29 | } 30 | 31 | int main() 32 | { 33 | ios_base::sync_with_stdio(false); 34 | cin.tie(NULL); 35 | 36 | long int t; 37 | cin >> t; 38 | while(t--) 39 | { 40 | cout << solve_test() < 3 | #include 4 | using namespace std; 5 | 6 | int solve_test() 7 | { 8 | long long int number; 9 | cin >> number; 10 | if(number == 1) 11 | return 1; 12 | else if(number == 2) 13 | return 2; 14 | // Number is greater than 2: 15 | if(number % 2 != 0) 16 | return -1; 17 | for(int i = 2; i < number; i++) 18 | { 19 | if( pow(2, i) > number) 20 | return -1; 21 | else if(pow(2, i) == number) 22 | return i+1; 23 | } 24 | return -1; 25 | } 26 | 27 | int main() 28 | { 29 | int t; 30 | cin >> t; 31 | 32 | while(t--) 33 | cout << solve_test()< v, int x){ 4 | return count(v.begin(), v.end(), x); 5 | /* 6 | The built-in count() method does something like: 7 | int frequency=0; 8 | for(int i =0; i 3 | using namespace std; 4 | 5 | int get_unique_number() 6 | { 7 | int list_size=7, k=3; 8 | // cin >> list_size >> k; 9 | int vett[list_size] {6,2,5,2,2,6,6}; 10 | //for(int i=0; i < list_size; i++) 11 | // cin>>vett[i]; 12 | 13 | 14 | } 15 | 16 | 17 | int main() 18 | { 19 | int t=1; 20 | //cin >> t; 21 | 22 | while(t--) 23 | cout << get_unique_number()< 3 | using namespace std; 4 | 5 | int get_unique() 6 | { 7 | int n, k; 8 | cin >> n >> k; 9 | 10 | unordered_map mp; 11 | int arr[n]; 12 | for(int i =0; i < n; i++) 13 | cin >> arr[i]; 14 | 15 | 16 | for(int i =0; i < n; i++) 17 | mp[arr[i]]++; 18 | 19 | for(int i =0; i < n; i++) 20 | { 21 | if(mp[arr[i]] == 1) 22 | return arr[i]; 23 | } 24 | return -1; 25 | } 26 | 27 | int main() 28 | { 29 | ios_base::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | 32 | long int t; 33 | cin >> t; 34 | while(t--) 35 | { 36 | cout << get_unique() <= x) 15 | return arr[i]; 16 | } 17 | return -1; 18 | } -------------------------------------------------------------------------------- /c++/find_index.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/find-index4752/1?page=1&difficulty[]=-2&status[]=unsolved&sortBy=submissions 2 | vector findIndex(int a[], int n, int key) 3 | { 4 | vector solution; 5 | int left_index = -1, right_index = -1; 6 | for (int i = 0; i < n; i++) 7 | { 8 | if (a[i] == key) 9 | { 10 | left_index = i; 11 | break; 12 | } 13 | } 14 | for (int i = n - 1; i >= left_index; i--) 15 | { 16 | if (a[i] == key) 17 | { 18 | right_index = i; 19 | break; 20 | } 21 | } 22 | solution.push_back(left_index); 23 | solution.push_back(right_index); 24 | return solution; 25 | } -------------------------------------------------------------------------------- /c++/first-element-to-occur-k-times.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/first-element-to-occur-k-times/0 2 | #include 3 | using namespace std; 4 | 5 | int solve_test() 6 | { 7 | int n, k, number; 8 | cin >> n >> k; 9 | int arr[n]; 10 | map mp; 11 | for(int i =0; i < n; i++) 12 | { 13 | cin >> arr[i]; 14 | mp[arr[i]]++; 15 | } 16 | 17 | for(auto x : arr) 18 | { 19 | if(mp[x] == k) 20 | return x; 21 | } 22 | return -1; 23 | 24 | 25 | } 26 | 27 | int main() 28 | { 29 | ios_base::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | 32 | long int t; 33 | cin >> t; 34 | while(t--) 35 | { 36 | cout << solve_test() <& arr,int N, int P) 3 | { 4 | mapmp; 5 | for(int i =0; i< N; i++) 6 | { 7 | mp[arr[i]]++; 8 | } 9 | 10 | for(int i =1; i<= N; i++){ 11 | arr[i-1] = mp[i]; 12 | } 13 | } -------------------------------------------------------------------------------- /c++/front-to-back.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/front-to-back/1 2 | //Complete this function. it1 points to vector.begin(), it2 points to vector.end() 3 | void iter(vector::iterator it1,vector::iterator it2) 4 | { 5 | //Your code here. Use iterator to print all the elements 6 | vector::iterator it; 7 | for(it = it1; it !=it2; it++) 8 | cout<<*it<<" "; 9 | cout<0) 22 | { 23 | if(!IsPrime(N%10)) 24 | { 25 | return 0; 26 | } 27 | N /=10; 28 | } 29 | return 1; 30 | } 31 | }; -------------------------------------------------------------------------------- /c++/generate-permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function to display the array 5 | void display(int a[], int n) 6 | { 7 | for (int i = 0; i < n; i++) { 8 | cout << a[i] << " "; 9 | } 10 | cout << endl; 11 | } 12 | 13 | // Function to find the permutations 14 | void findPermutations(int a[], int n) 15 | { 16 | // Sort the given array 17 | sort(a, a + n); 18 | // Find all possible permutations 19 | cout << "Possible permutations are:\n"; 20 | do { 21 | display(a, n); 22 | } while (next_permutation(a, a + n)); 23 | } 24 | 25 | // Driver code 26 | int main() 27 | { 28 | 29 | int a[] = { 10, 20, 30, 40 }; 30 | 31 | int n = sizeof(a) / sizeof(a[0]); 32 | 33 | findPermutations(a, n); 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /c++/get-length-circular-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/length-of-circular-linked-list/1/?track=DSA-Foundation-Final-Circular-Linked-List&batchId=193 2 | int getLength(Node * head) 3 | { 4 | Node *current = head; 5 | int counter = 0; 6 | while(current->next != head ) 7 | { 8 | counter++; 9 | current = current->next; 10 | } 11 | return counter+1; 12 | } -------------------------------------------------------------------------------- /c++/get_element-at-index.cpp: -------------------------------------------------------------------------------- 1 | 2 | // https://practice.geeksforgeeks.org/problems/get-element-at-index/1/?track=DSA-Foundation-Final-Arrays&batchId=193 3 | //Complete this function 4 | int getByIndex(int arr[],int n,int i) 5 | { 6 | if(i > n-1) 7 | return -1; 8 | else 9 | return arr[i]; 10 | } -------------------------------------------------------------------------------- /c++/get_element_by_index.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/get-element-at-index/1/?track=DSA-Foundation-Final-Arrays&batchId=193 2 | int getByIndex(int arr[],int n,int i) 3 | { 4 | if(i > n-1) 5 | return -1; 6 | else 7 | return arr[i]; 8 | } 9 | -------------------------------------------------------------------------------- /c++/greatest_of_three_nubers.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/greatest-of-three-numbers2520/1/?problemStatus=unsolved&problemType=functional&difficulty[]=-2&page=2&sortBy=submissions&query=problemStatusunsolvedproblemTypefunctionaldifficulty[]-2page2sortBysubmissions 2 | class Solution { 3 | public: 4 | int greatestOfThree(int A, int B, int C) { 5 | if(A >= B && A >= C) 6 | return A; 7 | if(B >=A && B >= C) 8 | return B; 9 | if(C >=A && C >=B) 10 | return C; 11 | } 12 | }; -------------------------------------------------------------------------------- /c++/identical-Linked-list.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/identical-linked-lists/1 2 | 3 | /* 4 | This function returs true if both linked lists are identical else return false, knowing that 5 | the both linkedlist ARE NOT empty. 6 | */ 7 | bool areIdentical(struct Node *head1, struct Node *head2) 8 | { 9 | Node *node1 = head1; 10 | Node *node2 = head2; 11 | while(node1 != NULL && node2 != NULL) 12 | { 13 | if(node1-> data != node2->data) 14 | return false; 15 | node1 = node1->next; 16 | node2 = node2->next; 17 | 18 | } 19 | return true; 20 | } -------------------------------------------------------------------------------- /c++/immediate-grater.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/find-immediate-greater-than-x/1/?track=DSA-Foundation-Final-Arrays&batchId=193 2 | int immediateGreater(int arr[], int n, int x){ 3 | sort(arr, arr+n); 4 | 5 | if(arr[n-1] < x) 6 | return -1; 7 | for(int i =0; i < n; i++) 8 | { 9 | if(arr[i] > x) 10 | return arr[i]; 11 | } 12 | } -------------------------------------------------------------------------------- /c++/implement-stack-using-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/implement-stack-using-linked-list/1/?track=DSA-Foundation-Final-Stack&batchId=193 2 | void MyStack ::push(int x) { 3 | StackNode *new_head = new StackNode(x); 4 | if( top == NULL) 5 | top = new_head; 6 | else 7 | { 8 | new_head->next = top; 9 | top = new_head; 10 | } 11 | } 12 | 13 | /* The method pop which return the element 14 | poped out of the stack*/ 15 | int MyStack ::pop() { 16 | if(top == NULL) 17 | return -1; 18 | else 19 | { 20 | int head = top->data; 21 | top = top->next; 22 | return head; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /c++/index-of-an-extra-element.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/index-of-an-extra-element/1/?track=amazon-searching&batchId=192 2 | int findExtra(int a[], int b[], int n) { 3 | for(int i=0; i < n; i++) 4 | { 5 | if(a[i] != b[i]) 6 | return i; 7 | } 8 | // If both arrays are equals, return index N; 9 | return n; 10 | } -------------------------------------------------------------------------------- /c++/index-of-first-1-in-a-sorted-array-of-0s-and-1s.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/index-of-first-1-in-a-sorted-array-of-0s-and-1s/0 2 | #include 3 | using namespace std; 4 | 5 | int solve_test() 6 | { 7 | int n; 8 | cin >> n; 9 | bool arr[n]; 10 | for(int i =0; i > arr[i]; 13 | } 14 | if(arr[n-1] == false) 15 | return -1; 16 | for(int i =0; i < n; i++) 17 | { 18 | if(arr[i] == true) 19 | return i; 20 | } 21 | return -1; 22 | } 23 | 24 | int main() 25 | { 26 | ios_base::sync_with_stdio(false); 27 | cin.tie(NULL); 28 | 29 | long int t; 30 | cin >> t; 31 | while(t--) 32 | { 33 | cout <left); 9 | cout << node->data << " "; 10 | inOrder(node->right); 11 | } -------------------------------------------------------------------------------- /c++/inorder-traversal.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/inorder-traversal/1/ 2 | 3 | void inorder(Node* root) 4 | { 5 | // Your code here 6 | if(root == NULL) 7 | return; 8 | else 9 | { 10 | inorder(root->left); 11 | cout<< root->data<<" "; 12 | inorder(root->right); 13 | } 14 | } -------------------------------------------------------------------------------- /c++/insert-a-node-in-a-bst.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/insert-a-node-in-a-bst/1 2 | Node* insert(Node* root, int element) 3 | { 4 | Node *node = new Node(element); 5 | 6 | if(root == NULL) 7 | return node; 8 | else 9 | { 10 | if(root->data == element) 11 | return root; 12 | if(element > root->data) 13 | root->right = insert(root->right, element); 14 | else 15 | root->left = insert(root->left, element); 16 | } 17 | return root; 18 | } -------------------------------------------------------------------------------- /c++/insert-a-node-in-doubly-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/insert-a-node-in-doubly-linked-list/1/?track=DSA-Foundation-Final-DLL&batchId=193 2 | 3 | void addNode(Node *head, int pos, int data) 4 | { 5 | if(head == NULL) 6 | return; 7 | Node *new_node = new Node(data); 8 | Node *current = head; 9 | while(pos > 0) 10 | { 11 | pos--; 12 | current = current->next; 13 | } 14 | 15 | // Get reference to the next noe 16 | Node *next = current->next; 17 | Node *prev = current; 18 | 19 | current->next = new_node; 20 | new_node->next = next; 21 | new_node->prev =prev; 22 | } -------------------------------------------------------------------------------- /c++/insert-at-the-end-of-circular-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // 2 | /* 3 | De 4 | */ -------------------------------------------------------------------------------- /c++/insert-in-sorted-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/insert-in-sorted-linked-list/1/?track=sp-linked-list&batchId=152 2 | Node * insertInSorted(Node * head, int data) 3 | { 4 | Node *new_node = new Node(data); 5 | // Case 1: LL is empty OR first element is greater the new_node->value 6 | if(head==NULL || head->data >= data) 7 | { 8 | new_node->next=head; 9 | head= new_node; 10 | return head; 11 | } 12 | // Case 2: new node is between or at the end of the LL 13 | Node *current = head; 14 | while(current->next != NULL && current->next->data <= data) 15 | { 16 | current = current->next; 17 | } 18 | new_node ->next = current->next; 19 | current->next = new_node; 20 | current = head; 21 | return current; 22 | } -------------------------------------------------------------------------------- /c++/insert-in-stack.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/insert-in-stack/1/?track=DSA-Foundation-Final-Stack&batchId=193 2 | stack insertIntoStack(int n, int arr[]){ 3 | stacks; 4 | for(int i =0; i < n; i++) 5 | s.push(arr[i]); 6 | 7 | return s; 8 | } -------------------------------------------------------------------------------- /c++/insert-node-in-doubly-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/insert-a-node-in-doubly-linked-list/1/?track=DSA-Foundation-Final-DLL&batchId=193 2 | void addNode(Node *head, int pos, int data) 3 | { 4 | if(head == NULL) 5 | return; 6 | Node *new_node = new Node(data); 7 | Node *current = head; 8 | while(pos > 0 && current != NULL) 9 | { 10 | pos--; 11 | current = current->next; 12 | } 13 | 14 | // Get reference to the next node 15 | Node *next = current->next; 16 | Node *prev = current; 17 | 18 | current->next = new_node; 19 | new_node->next = next; 20 | new_node->prev =prev; 21 | } -------------------------------------------------------------------------------- /c++/insert-tail-circular-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/circular-linked-list-tail-insert/1/?track=DSA-Foundation-Final-Circular-Linked-List&batchId=193 2 | Node *insertInTail(Node * head, int data) 3 | { 4 | Node *new_node = new Node(data); 5 | new_node->next = head; 6 | Node *current = head; 7 | while(current->next != head) 8 | { 9 | current = current->next; 10 | } 11 | current->next = new_node; 12 | 13 | current = head; 14 | return current; 15 | } -------------------------------------------------------------------------------- /c++/int-to-binary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bin (unsigned number) 5 | { 6 | if(number > 1) 7 | bin(number/2); 8 | cout< arr[i-1]) 18 | return false; 19 | } 20 | return true; 21 | } 22 | 23 | 24 | //Complete this function 25 | int isSorted(int arr[],int n) 26 | { 27 | if(n <=1) 28 | return 1; 29 | if(n == 2) 30 | { 31 | if(arr[0] == arr[1]) 32 | return false; 33 | return true; 34 | } 35 | return (increasing_order(arr, n) || decreasing_order(arr, n) ); 36 | } -------------------------------------------------------------------------------- /c++/is-the-doubly-linked-list-circular.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/is-the-doubly-linked-list-circular/1/?track=DSA-Foundation-Final-DLL&batchId=193 2 | bool isCircular(Node * head) 3 | { 4 | // Doubly linked list is empty 5 | if(head == NULL) 6 | return false; 7 | // DLL has just one element and it is not circular 8 | if(head->next ==NULL) 9 | return false; 10 | // DDL has just one node and it is circular 11 | if(head->next == head) 12 | return true; 13 | 14 | Node *current = head->next; 15 | 16 | while(current != head && current->next != NULL) 17 | current = current->next; 18 | if(current == head) 19 | return true; 20 | return false; 21 | } -------------------------------------------------------------------------------- /c++/isIsogram.cpp: -------------------------------------------------------------------------------- 1 | /* A string is Isogram when a letter does not occcurs more than once. 2 | machine: is isogram 3 | geeks : is NOT isogram (because the 'e' occurs more than once) 4 | */ 5 | bool isIsogram(string s) 6 | { 7 | for(int i =0; i < s.size(); i++) 8 | { 9 | for(int j=i+1; jnext) 14 | temp=temp->next; 15 | 16 | temp->next=head2; 17 | return head1; 18 | } 19 | -------------------------------------------------------------------------------- /c++/k-larghest-elements.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/k-largest-elements/0 2 | #include 3 | using namespace std; 4 | 5 | void solve_test() 6 | { 7 | // Create an Heap 8 | priority_queue myList; 9 | int number, list_size, k; 10 | cin >> list_size >> k; 11 | // Add element in the Heap 12 | for(int i=0; i< list_size; i++) 13 | { 14 | cin >> number; 15 | myList.push(number); 16 | } 17 | // We Know that the Heap atomatically will sort the element 18 | // in Descending Order 19 | // Print the K biggest element 20 | while(!myList.empty() && k!=0) 21 | { 22 | cout << myList.top() << " "; 23 | myList.pop(); 24 | k--; 25 | } 26 | cout<> t; 34 | 35 | while(t--) 36 | solve_test(); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /c++/k_distance_from_root.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/k-distance-from-root/1/ 2 | void printKdistance(struct Node *root, int k) 3 | { 4 | if(root == NULL) 5 | return; 6 | if(k == 0) 7 | { 8 | cout << root ->data << " "; 9 | return; 10 | } 11 | printKdistance(root->left, k-1); 12 | printKdistance(root->right, k-1); 13 | } -------------------------------------------------------------------------------- /c++/kth-larghest-elment-in-bst.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/kth-largest-element-in-bst/1/?track=ppc-bst&batchId=221 2 | vectors; 3 | void inorder(Node *root) 4 | { 5 | if(root == NULL) 6 | return; 7 | inorder(root->left); 8 | s.push_back(root->data); 9 | inorder(root->right); 10 | } 11 | // return the Kth largest element in the given BST rooted at 'root' 12 | int kthLargest(Node *root, int K) 13 | { 14 | inorder(root); 15 | return s[s.size()-K]; 16 | } -------------------------------------------------------------------------------- /c++/kth-smallest-number.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/kth-smallest-element/0 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void solve_test() 8 | { 9 | unsigned long int k, element , list_size; 10 | cin >> list_size; 11 | vector numbers; 12 | while(list_size--) 13 | { 14 | cin >> element; 15 | numbers.push_back(element); 16 | } 17 | 18 | sort(numbers.begin(), numbers.end()); 19 | 20 | cin >> k; 21 | cout<> t; 28 | 29 | while(t--) 30 | solve_test(); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /c++/last-index-of-1.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/last-index-of-1/0 2 | #include 3 | using namespace std; 4 | 5 | void solve_test() 6 | { 7 | int index = -1; 8 | string s; 9 | cin >> s; 10 | for(int i =0; i< s.size(); i++) 11 | { 12 | if(s[i] == '1') 13 | index = i; 14 | } 15 | cout << index <> t; 22 | 23 | while(t--) 24 | solve_test(); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /c++/leader-in-array.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/leaders-in-an-array-1587115620/1/?track=sp-arrays-and-searching&batchId=152 2 | 3 | // Function to find the leaders in an array of size n 4 | vector leaders(int arr[], int n){ 5 | vector res; 6 | // Ad last element of the vector beause it's always a leader 7 | res.push_back(arr[n-1]); 8 | 9 | int maxelement=arr[n-1]; 10 | 11 | for(int i=n-2;i>=0;i--) 12 | { 13 | if(arr[i]>=maxelement){ 14 | maxelement=arr[i]; 15 | res.push_back(maxelement); 16 | } 17 | } 18 | reverse(res.begin(),res.end()); 19 | return res; 20 | 21 | } -------------------------------------------------------------------------------- /c++/learn-to-comment.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/learn-to-comment/1 2 | //Comment the part that outputs b 3 | void comment(int a,int b,int c) 4 | { 5 | cout<data << " "; 12 | *max_level = level; 13 | } 14 | 15 | // Recursively iterate the tree 16 | leftViewHelper(root->left, level+1, max_level); 17 | leftViewHelper(root->right, level+1, max_level); 18 | } 19 | // A wrapper over leftViewUtil() 20 | void leftView(Node *root) 21 | { 22 | int max_level = 0; 23 | leftViewHelper(root, 1, &max_level); 24 | } -------------------------------------------------------------------------------- /c++/left_index.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/left-index-1587115620/1/?track=SPCF-Searching&batchId=154 2 | int leftIndex(int N, int arr[], int X){ 3 | if(arr[N-1] < X) 4 | return -1; 5 | 6 | for(int i =0; i < N; i++) 7 | { 8 | if(arr[i] == X) 9 | return i; 10 | } 11 | return -1; 12 | } -------------------------------------------------------------------------------- /c++/left_view_of_binary_tree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omonimus1/geeks-for-geeks-solutions/a414368d93acb60f0836ae9e5c566673080e6646/c++/left_view_of_binary_tree.cpp -------------------------------------------------------------------------------- /c++/length-of-string.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/length-of-string/1/?track=DSA-Foundation-Final-String&batchId=193 2 | int lengthString(string str) 3 | { 4 | return str.size(); 5 | } -------------------------------------------------------------------------------- /c++/level-order-tree2.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/level-order-traversal-line-by-line/1/?track=PC-W6-T&batchId=154 2 | void levelOrder(Node* node) 3 | { 4 | if(node == NULL) 5 | return; 6 | 7 | queueq; 8 | q.push(node); 9 | q.push(NULL); 10 | 11 | Node *temp; 12 | while(q.size() >1 ) 13 | { 14 | temp = q.front(); 15 | q.pop(); 16 | if(temp == NULL) 17 | { 18 | cout << "$ "; 19 | q.push(NULL); 20 | continue; 21 | } 22 | 23 | cout << temp->data<<" "; 24 | if(temp->left != NULL) q.push(temp->left); 25 | if(temp->right != NULL) q.push(temp->right); 26 | } 27 | cout << "$"; 28 | } 29 | -------------------------------------------------------------------------------- /c++/linked-list-lenght-even-or-odd.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/linked-list-length-even-or-odd/1 2 | // Function should return 0 is length is even else return 1 3 | int isLengthEvenOrOdd(struct Node* head) 4 | { 5 | int counter = 0; 6 | Node *list_element = head; 7 | // If the Linked List has 0 elements, its lenght will always be 0 an even number; 8 | if(list_element == NULL) 9 | return 0; 10 | else 11 | { 12 | while(list_element != NULL) 13 | { 14 | counter++; 15 | list_element = list_element -> next; 16 | } 17 | // If the lenght is even return 0, else return 1 18 | if(counter %2 == 0) 19 | return 0; 20 | else 21 | return 1; 22 | } 23 | } -------------------------------------------------------------------------------- /c++/logical_operators.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/logical-operators/1/?track=module-1-basics-and-control-structures&batchId=113 2 | void logicOp(int a, int b) 3 | { 4 | /*output (a&&b), (a||b), and ((!a)&&(!b))separated by spaces*/ 5 | cout<<(a&&b) <<" " <<(a||b) <<" " <<((!a)&&(!b)) < biggest) 17 | biggest = counter; 18 | } 19 | return biggest; 20 | } -------------------------------------------------------------------------------- /c++/lower-case.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/java-convert-string-to-lowercase2313/1/?problemStatus=unsolved&problemType=functional&difficulty[]=-2&page=3&sortBy=submissions&query=problemStatusunsolvedproblemTypefunctionaldifficulty[]-2page3sortBysubmissions# 2 | class Solution { 3 | public: 4 | string toLower(string S) { 5 | for(int i =0; i< S.size(); i++) 6 | { 7 | if(isupper(S[i])) 8 | S[i] = tolower(S[i]); 9 | } 10 | return S; 11 | } 12 | }; -------------------------------------------------------------------------------- /c++/lower-to-uppercase.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/lowercase-to-upercase/1/?track=DSA-Foundation-Final-String&batchId=193 2 | string caseConversion(string str){ 3 | for(int i =0; i< str.size(); i++) 4 | { 5 | if(islower(str[i])) 6 | str[i] = toupper(str[i]); 7 | } 8 | return str; 9 | } -------------------------------------------------------------------------------- /c++/lowest-common-ancestor-bst.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/lowest-common-ancestor-in-a-bst/1 2 | 3 | // Returns the LCA of the nodes with values n1 and n2 4 | // in the BST rooted at 'root' 5 | Node* LCA(Node *root, int n1, int n2) 6 | { 7 | if( n1 < root->data && n2 < root->data) 8 | return LCA(root->left, n1, n2); 9 | else if( n1 > root->data && n2 > root->data) 10 | return LCA(root->right, n1, n2); 11 | else 12 | return root; 13 | } -------------------------------------------------------------------------------- /c++/majority-element.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/majority-element-1587115620/1/?track=amazon-searching&batchId=192 2 | int majorityElement(int a[], int size) 3 | { 4 | if(size == 1) 5 | return a[0]; 6 | if (size == 2) 7 | { 8 | if(a[0] == a[1]) 9 | return a[0]; 10 | else 11 | return -1; 12 | } 13 | 14 | if(size == 3) 15 | { 16 | if(a[0] != a[1] || a[1] != a[2]) 17 | return -1; 18 | } 19 | sort(a, a+size); 20 | int counter = 0; 21 | for(int i=0; i < size; i++) 22 | { 23 | if(counter >= size/2) 24 | return a[i]; 25 | if(a[i] == a[i+1]) 26 | { 27 | counter++; 28 | continue; 29 | } 30 | counter = 0; 31 | } 32 | return -1; 33 | } -------------------------------------------------------------------------------- /c++/map-operations.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/map-operations/1/?track=module-6-sets-and-maps&batchId=113 2 | map mapInsert(int arr[],int n) 3 | { 4 | mapmp; 5 | for(int i =0; i < n; i++) 6 | mp[arr[i]] = i; 7 | 8 | return mp; 9 | } 10 | 11 | 12 | void mapDisplay(mapmp) 13 | { 14 | for(auto x : mp) 15 | cout << x.first << " " << x.second << endl; 16 | } 17 | 18 | 19 | void mapErase(map&mp,int x) 20 | { 21 | //Write the if and else condition to erase x from map 22 | if (mp.count(x) != 0) { 23 | mp.erase(x); 24 | cout<<"erased "< max ) 21 | max = j-i; 22 | } 23 | } 24 | } 25 | return max; 26 | } -------------------------------------------------------------------------------- /c++/maximum-and-minimum-in-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/maximum-and-minimum-in-linked-list/1/?track=DSA-Foundation-Final-Linked-List&batchId=193 2 | int maximum(Node *head) 3 | { 4 | int max = -1; 5 | while(head != NULL) 6 | { 7 | if(max < head->data) 8 | max =head->data; 9 | head = head->next; 10 | } 11 | return max; 12 | } 13 | 14 | int minimum(Node *head) 15 | { 16 | // Max head->data i 10^3 17 | int min = 10000; 18 | while(head != NULL) 19 | { 20 | if(head->data < min) 21 | min = head->data; 22 | head = head->next; 23 | } 24 | return min; 25 | } -------------------------------------------------------------------------------- /c++/mean-and-median.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/mean-and-median-1587115620/1/?track=DSA-Foundation-Final-Arrays&batchId=193 2 | int median(int A[],int N) 3 | { 4 | sort(A,A+N); 5 | if(N % 2 !=0 ) 6 | return A[N/2]; 7 | else 8 | { 9 | int half_index = N/2; 10 | int sum = A[half_index] + A[half_index-1]; 11 | return floor(sum/2); 12 | } 13 | } 14 | 15 | int mean(int A[],int N) 16 | { 17 | int sum = 0; 18 | for(int i =0; i < N; i++) sum += A[i]; 19 | 20 | return sum/N; 21 | } -------------------------------------------------------------------------------- /c++/mean.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/mean0021/1/?problemStatus=unsolved&difficulty[]=-2&page=1&query=problemStatusunsolveddifficulty[]-2page1 2 | class Solution { 3 | public: 4 | int mean(int N , int A[]) { 5 | int mean_result = 0; 6 | for(int i =0; i < N; i++) 7 | mean_result += A[i]; 8 | return mean_result/N; 9 | } 10 | }; -------------------------------------------------------------------------------- /c++/median-of-two-array.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/median-of-two-sorted-arrays1618/1/?track=DSASP-Searching&batchId=154 2 | int findMedian(int arr[], int n, int brr[], int m){ 3 | vectorfinal; 4 | int i; 5 | for(i=0; i< n; i++) 6 | final.push_back(arr[i]); 7 | for(i=0; i< m; i++) 8 | final.push_back(brr[i]); 9 | 10 | 11 | int len = m+n; 12 | sort(final.begin(), final.end()); 13 | if(len %2 ==0 ) 14 | return (final[len/2] + final[len/2-1])/2; 15 | else 16 | return final[len/2]; 17 | } -------------------------------------------------------------------------------- /c++/merge-two-sorted-arrays.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/merge-two-sorted-arrays-1587115620/1/?track=ppc-arrays&batchId=221 2 | void merge(int arr1[], int arr2[], int n, int m) 3 | { 4 | 5 | int i =n-1, j=0; 6 | while(i>=0 && j arr2[j]) 9 | swap(arr1[i], arr2[j]); 10 | 11 | i--; 12 | j++; 13 | } 14 | 15 | sort(arr1, arr1+n); 16 | sort(arr2, arr2+m); 17 | } -------------------------------------------------------------------------------- /c++/microsoft-reverse-words-in-string.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/reverse-words-in-a-given-string5459/1/?company[]=Microsoft&problemStatus=unsolved&page=1&query=company[]MicrosoftproblemStatusunsolvedpage1 2 | 3 | string reverseWords(string s) 4 | { 5 | vectorsub; 6 | string sub_word=""; 7 | int len = s.size(); 8 | for(int i =0; i < len; i++) 9 | { 10 | sub_word = ""; 11 | while(i < len && s[i] != '.') 12 | { 13 | sub_word += s[i]; 14 | i+=1; 15 | } 16 | sub.push_back(sub_word); 17 | } 18 | 19 | reverse(sub.begin(), sub.end()); 20 | string result = ""; 21 | for(int i =0; i < sub.size()-1; i++) 22 | { 23 | result += sub[i]; 24 | result += '.'; 25 | } 26 | result += sub[sub.size()-1]; 27 | return result; 28 | } -------------------------------------------------------------------------------- /c++/middle-element-linkedlist.cpp: -------------------------------------------------------------------------------- 1 | /* Should return data of middle node. If linked list is empty, then -1*/ 2 | int getMiddle(Node *head) 3 | { 4 | // Get number of elements in the linked list 5 | Node *e = head; 6 | if(e == NULL) 7 | return -1; 8 | else 9 | { 10 | int counter = 0; 11 | while(e!=NULL) 12 | { 13 | counter++; 14 | e = e->next; 15 | } 16 | 17 | // Estabish index of the element to return 18 | int index = counter /2; 19 | 20 | counter = 0; 21 | // Set the e at the start of the linked list 22 | e = head; 23 | while(e!=NULL) 24 | { 25 | if(counter == index) 26 | return e->data; 27 | e = e->next; 28 | counter++; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /c++/middle-of-stack.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/middle-of-stack/1/?track=DSA-Foundation-Final-Stack&batchId=193 2 | /* 3 | Sample Input: 4 | 2 5 | 5 6 | 1 2 3 4 5 7 | 4 8 | 1 2 3 4 9 | Sample Output: 10 | 3 11 | 2 12 | */ 13 | int stackMiddle(int n, stack st){ 14 | int number_of_elements = st.size(); 15 | int pos = (number_of_elements/2)+1; 16 | 17 | while(st.size() && pos > 1) 18 | { 19 | pos--; 20 | st.pop(); 21 | } 22 | return st.top(); 23 | } -------------------------------------------------------------------------------- /c++/min-in-BST.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/minimum-element-in-bst/1 2 | int minValue(Node* root) 3 | { 4 | // Code her 5 | Node *current = root; 6 | 7 | /* loop down to find the leftmost leaf */ 8 | while (current->left != NULL) 9 | { 10 | current = current->left; 11 | } 12 | return(current->data); 13 | } -------------------------------------------------------------------------------- /c++/minimum-product-pair.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/minimum-product-pair/0/ 2 | #include 3 | using namespace std; 4 | 5 | long int get_min_product() 6 | { 7 | long int n; 8 | cin >> n; 9 | long int arr[n]; 10 | 11 | for(int i =0; i < n; i++) 12 | cin >> arr[i]; 13 | 14 | sort(arr, arr+n); 15 | 16 | return (arr[0] * arr[1]); 17 | } 18 | 19 | int main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | 24 | long int t; 25 | cin >> t; 26 | while(t--) 27 | { 28 | cout << get_min_product()<left); 8 | mirror(node->right); 9 | 10 | // Swap pointer of the current node 11 | temp = node->left; 12 | node->left = node->right; 13 | node->right = temp; 14 | } -------------------------------------------------------------------------------- /c++/missing-number-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/missing-number-in-array1416/1/?track=amazon-searching&batchId=192 2 | int MissingNumber(vector& array, int n) { 3 | 4 | int len = array.size(); 5 | 6 | 7 | if(len==1) 8 | { 9 | if(array[0] >=2) 10 | return 1; 11 | else 12 | return 1+array[0]; 13 | } 14 | 15 | sort(array.begin(), array.end()); 16 | 17 | for(int i =0 ; i < len; i++) 18 | { 19 | if(array[i] != i+1) 20 | { 21 | i+=1; 22 | return i; 23 | } 24 | } 25 | return (array[len-1]+1); 26 | } -------------------------------------------------------------------------------- /c++/modular-node.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/modular-node/1 2 | int modularNode(Node* head, int k) 3 | { 4 | if(head == NULL) 5 | return -1; 6 | vectors; 7 | 8 | int i=1; 9 | while(head != NULL) 10 | { 11 | if( (i%k) == 0 ) 12 | s.push_back(head->data); 13 | head = head->next; 14 | i++; 15 | } 16 | 17 | int len = s.size(); 18 | if(len == 0) 19 | return -1; 20 | return s[len-1]; 21 | } -------------------------------------------------------------------------------- /c++/multimap-operations.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/multimap-operations/1/?track=module-6-sets-and-maps&batchId=113 2 | //User function Template for C++ 3 | multimap multimapInsert(int arr[],int n) 4 | { 5 | multimapmp; 6 | //Insert arr[i] as key and i as value in the multimap 7 | for(int i =0; i < n; i++) 8 | { 9 | mp.insert({arr[i], i}); 10 | } 11 | return mp; 12 | } 13 | 14 | 15 | void multimapDisplay(multimapmp) 16 | { 17 | //Print the keys and value pairs in new line 18 | for(auto x : mp) 19 | cout << x.first << " " << x.second << endl; 20 | } 21 | 22 | 23 | void multimapErase(multimap&mp,int x) 24 | { 25 | //If and else condition to erase x from the multimap 26 | if(mp.erase(x)) 27 | { 28 | cout<<"erased "< getTable(int N) 11 | { 12 | vectorsolution; 13 | for(int i =1; i<=10; i++) 14 | { 15 | solution.push_back(i*N); 16 | } 17 | return solution; 18 | } 19 | }; -------------------------------------------------------------------------------- /c++/multiply-left-and-right-array-sum.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/multiply-left-and-right-array-sum/0 2 | #include 3 | using namespace std; 4 | 5 | 6 | void solve_test() 7 | { 8 | long int n; 9 | cin >> n; 10 | int i; 11 | long int arr[n]; 12 | for(i =0; i < n; i++) 13 | cin >> arr[i]; 14 | 15 | int sum_first_half =0, sum_second_half=0; 16 | int end_first_half = (n/2)-1; 17 | 18 | for(i =0; i <= end_first_half; i++) 19 | sum_first_half += arr[i]; 20 | 21 | for(i = end_first_half+1; i < n; i++) 22 | sum_second_half += arr[i]; 23 | 24 | cout << sum_first_half * sum_second_half <> t; 35 | while(t--) 36 | { 37 | solve_test(); 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /c++/multiset-operator.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/multiset-operations/1/ 2 | multiset multisetInsert(int arr[],int n) 3 | { 4 | multisets; 5 | for(int i=0; is) 14 | { 15 | multiset :: iterator itr; 16 | for(itr = s.begin(); itr != s.end(); itr++) 17 | cout << *itr<< " "; 18 | cout<&s,int x) 23 | { 24 | //If and else condition to erase x from multiset 25 | if(s.erase(x)) 26 | cout<<"erased "< data; 9 | list_element = list_element -> next; 10 | counter++; 11 | } 12 | } -------------------------------------------------------------------------------- /c++/non-preating-character.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | //User function template for C++ 9 | 10 | // return the first non-repeating char in S. 11 | // if there's no non-repeating char, return "-1" 12 | void find(string S) { 13 | mapmp; 14 | 15 | for(int i =0; i< S.size(); i++) 16 | { 17 | mp[S[i]]++; 18 | } 19 | 20 | map ::reverse_iterator x; 21 | for(x=mp.rbegin(); x!=mp.rend(); ++x) 22 | { 23 | 24 | cout << x->first << " " << x->second <second == 1) 26 | //{ 27 | // cout << x->first; 28 | // return; 29 | //} 30 | 31 | } 32 | cout<< "-1"; 33 | } 34 | 35 | int main() 36 | { 37 | find("hello"); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /c++/non-repeating-character.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/non-repeating-character-1587115620/1/?track=DSA-Foundation-Final-Hashing&batchId=193 2 | char nonrepeatingCharacter(string S) 3 | { 4 | unordered_mapmp; 5 | for(int i =0; i < S.size(); i++) 6 | { 7 | mp[S[i]]++; 8 | } 9 | 10 | 11 | unordered_map::iterator itr; 12 | 13 | for(int i=0; i < S.size(); i++) 14 | { 15 | if(mp[S[i]] == 1) 16 | return S[i]; 17 | } 18 | return '$'; 19 | } -------------------------------------------------------------------------------- /c++/number-of-digits.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-total-digits-in-a-number/1/?track=DSA-Foundation-Final-Recursion&batchId=193 2 | int countDigits(int n) 3 | { 4 | int count = 0; 5 | if(n < 10 ) 6 | return 1; 7 | 8 | return 1 + countDigits(n/10); 9 | } -------------------------------------------------------------------------------- /c++/number-of-occurrency.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/number-of-occurrence/0 2 | #include 3 | using namespace std; 4 | 5 | int solve_test() 6 | { 7 | int n, target, number; 8 | int counter = 0; 9 | cin >> n >> target; 10 | for(int i =0; i < n; i++) 11 | { 12 | cin >> number; 13 | if(number == target) 14 | counter++; 15 | } 16 | if(counter == 0) 17 | return -1; 18 | return counter; 19 | } 20 | 21 | int main() 22 | { 23 | int t; 24 | cin >> t; 25 | 26 | while(t--) 27 | cout <mp; 3 | void findAll() { 4 | std::unordered_set s = {1,2,3}; 5 | 6 | bool flag; 7 | int single_digit; 8 | for(int i=1;i<=1000000;i++){ 9 | int number=i; 10 | flag=true; 11 | while(number >0){ 12 | int single_digit = number%10; 13 | number /= 10; 14 | if(!s.count(single_digit)){ 15 | flag=false; 16 | break; 17 | } 18 | } 19 | if(flag) 20 | mp.insert({i,1}); 21 | else 22 | mp.insert({i,0}); 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /c++/occurrence-of-an-integer-in-a-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/occurence-of-an-integer-in-a-linked-list/1 2 | int count(struct node* head, int search_for) 3 | { 4 | node *node = head; 5 | if(node == NULL) 6 | return 0; 7 | int counter = 0; 8 | while(node != NULL) 9 | { 10 | if(node->data == search_for) 11 | counter++; 12 | node = node->next; 13 | } 14 | return counter; 15 | } -------------------------------------------------------------------------------- /c++/odd-or-even.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/odd-or-even3618/1/?problemStatus=unsolved&difficulty[]=-2&page=1&query=problemStatusunsolveddifficulty[]-2page1 2 | class Solution{ 3 | public: 4 | string oddEven(int N){ 5 | if(N%2 ==0 ) 6 | return "even"; 7 | else 8 | return "odd"; 9 | } 10 | }; -------------------------------------------------------------------------------- /c++/operations-on-stack.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/operations-on-stack/1/?track=DSA-Foundation-Final-Stack&batchId=193 2 | // Helper class to implement functions 3 | // insert : to insert element to stack 4 | // remove : to pop element from stack 5 | // headOf_Stack : to return top of stack 6 | // find : to find the element in stack 7 | 8 | void insert(stack &s,int x) 9 | { 10 | s.push(x); 11 | } 12 | 13 | void remove(stack &s) 14 | { 15 | 16 | s.pop(); 17 | } 18 | 19 | void headOf_Stack(stack &s) 20 | { 21 | int x= s.top(); 22 | cout< s, int val) 26 | { 27 | while(s.size()) 28 | { 29 | if(s.top() == val) 30 | return true; 31 | s.pop(); 32 | } 33 | return false; 34 | } -------------------------------------------------------------------------------- /c++/pair-sum-in-vector.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/pair-sum-in-vector/1 2 | void sum(vector>v) 3 | { 4 | long int sum=0; 5 | // Calculate the sum of the second element of the pair 6 | for(int i =0; i< v.size(); i++) 7 | sum += v[i].second; 8 | cout<>v) 3 | { 4 | long long sum=0; 5 | //Your code here 6 | for(int i =0; i< v.size(); i++) 7 | { 8 | sum += v[i].second; 9 | } 10 | cout <next != NULL) 6 | { 7 | swap(current->data, current->next->data); 8 | current = current->next->next; 9 | } 10 | 11 | return head; 12 | } -------------------------------------------------------------------------------- /c++/pairs-with-difference-k.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/pairs-with-difference-k/0/ 2 | #include 3 | using namespace std; 4 | 5 | int number_of_pairs() 6 | { 7 | int list_size, k; 8 | cin >> list_size >> k; 9 | int i, j; 10 | int arr[list_size]; 11 | for(int i =0; i< list_size; i++) 12 | { 13 | cin >> arr[i]; 14 | } 15 | int number_of_pairs = 0; 16 | for(i=0; i < list_size; i++) 17 | { 18 | for(j=i-1; j>=0 ; j--) 19 | { 20 | if(arr[i] - arr[j] == k || arr[j] - arr[i] == k) 21 | number_of_pairs++; 22 | } 23 | } 24 | return number_of_pairs; 25 | } 26 | 27 | int main() 28 | { 29 | int t; 30 | cin >> t; 31 | 32 | for(int i=0; i arr[1]) 9 | return 0; 10 | return 1; 11 | } 12 | for(int i =0; i< n; i++) 13 | { 14 | if(i == 0) 15 | { 16 | if (arr[i] > arr[i+1]) 17 | return i; 18 | continue; 19 | } 20 | if(i== n-1) 21 | return n-1; 22 | if(arr[i] > arr[i-1] && arr[i] > arr[i+1]) 23 | return i; 24 | } 25 | } -------------------------------------------------------------------------------- /c++/porstOrder-of-BST.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/postorder-in-bst/1/?track=DSA-Foundation-Final-BST&batchId=193 2 | void postOrder(struct Node* node) 3 | { 4 | if(node == NULL) 5 | return; 6 | 7 | postOrder(node->left); 8 | postOrder(node->right); 9 | cout << node->data << " "; 10 | 11 | } -------------------------------------------------------------------------------- /c++/postorder-traversal.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/postorder-traversal/1/ 2 | void postOrder(Node* root) 3 | { 4 | if(root == NULL) 5 | return; 6 | else 7 | { 8 | postOrder(root->left); 9 | postOrder(root->right); 10 | cout<data<< " "; 11 | } 12 | } -------------------------------------------------------------------------------- /c++/power-of-2.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/power-of-2/0/ 2 | #include 3 | using namespace std; 4 | 5 | bool isPower() 6 | { 7 | long int number; 8 | cin >> number; 9 | for(int i=0; i <= sqrt(number); i++) 10 | { 11 | if(pow(2, i) > number) 12 | return false; 13 | if(pow(2,i) == number) 14 | return true; 15 | } 16 | return false; 17 | } 18 | 19 | 20 | int main() 21 | { 22 | int t; 23 | cin >> t; 24 | 25 | while(t--) 26 | { 27 | if(isPower()) 28 | cout << "YES"<data << " "; 8 | preOrder(node->left); 9 | preOrder(node->right); 10 | } -------------------------------------------------------------------------------- /c++/preoder-traversal.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/preorder-traversal/1/ 2 | void preorder(Node* root) 3 | { 4 | if(root == NULL) 5 | return; 6 | else 7 | { 8 | cout<data<<" "; 9 | preorder(root->left); 10 | preorder(root->right); 11 | } 12 | } -------------------------------------------------------------------------------- /c++/prime-check.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/primality-test/1/?track=ppc-mathematics&batchId=221 2 | bool isPrime(int n) 3 | { 4 | if(n < 1) 5 | return false; 6 | if(n <=2) 7 | return true; 8 | for(int i = 2; i <= sqrt(n); i++) 9 | { 10 | if(n% i == 0) 11 | return false; 12 | } 13 | return true; 14 | } -------------------------------------------------------------------------------- /c++/prime-number-check.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/prime-number/0 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool isPrime(long long int n ) 7 | { 8 | for(int i=2; i<= sqrt(n); i++) 9 | { 10 | if(n%i == 0) 11 | return false; 12 | } 13 | 14 | return true; 15 | } 16 | 17 | int main() 18 | { 19 | // To have low I/O operations time cost 20 | ios_base::sync_with_stdio(false); 21 | cin.tie(NULL); 22 | 23 | long long int t , number; 24 | cin >> t; 25 | 26 | while(t--) 27 | { cin >> number; 28 | if(isPrime(number)) 29 | cout<<"Yes"< 4 | using namespace std; 5 | 6 | bool isPrime(unsigned long int n) 7 | { 8 | if(n <=1) 9 | return false; 10 | 11 | for(int i=2; i> a >> b; 23 | for(int i =a; i<=b; i++) 24 | { 25 | if(isPrime(i)) 26 | cout<> t; 35 | 36 | while(t--) 37 | solve_test(); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /c++/print-adjacency-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/print-adjacency-list-1587115620/1/?track=DSA-Foundation-Final-Graph&batchId=193 2 | // adj: array of vectors to represent graph 3 | // V: number of vertices 4 | void printGraph(vector adj[], int V) 5 | { 6 | for(int i =0; i < V; i++) 7 | { 8 | cout << i; 9 | for(int j =0; j < adj[i].size(); j++) 10 | cout << "-> "< printNonRepeated(int arr[],int n) 6 | { 7 | unordered_mapmp; 8 | for(int i =0; i < n; i++) 9 | { 10 | mp[arr[i]]++; 11 | } 12 | 13 | vectors; 14 | 15 | // Go through the map 16 | // unordered_map ::iterator itr; 17 | for(int i =0; i < n; i++) 18 | { 19 | if(mp[arr[i]] == 1) 20 | s.push_back(arr[i]); 21 | } 22 | return s; 23 | } -------------------------------------------------------------------------------- /c++/print-first-letter-of-every-word.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/print-first-letter-of-every-word-in-the-string3632/1/?company[]=Amazon&company[]=Microsoft&company[]=Samsung&difficulty[]=-1&difficulty[]=0&page=1&query=company[]Amazoncompany[]Microsoftcompany[]Samsungdifficulty[]-1difficulty[]0page1 2 | class Solution{ 3 | public: 4 | 5 | string firstAlphabet(string S) 6 | { 7 | string solution = ""; 8 | int len = S.size(); 9 | string word; 10 | for(int i =0; i< len; i++) 11 | { 12 | word = ""; 13 | while(i < len && S[i] != ' ') 14 | { 15 | word += S[i]; 16 | i+=1; 17 | } 18 | solution += word[0]; 19 | } 20 | return solution; 21 | } 22 | }; -------------------------------------------------------------------------------- /c++/print-from-1-to-n-using-recursion.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/print-1-to-n-without-using-loops-1587115620/1/?track=DSA-Foundation-Final-Recursion&batchId=193 2 | void printNos(int N) 3 | { 4 | if(N==0) 5 | return; 6 | printNos(N-1); 7 | cout << N << " "; 8 | } -------------------------------------------------------------------------------- /c++/print-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/print-linked-list-elements/1 2 | void display(Node *head) 3 | { 4 | //your code goes here 5 | Node *list_element = head; 6 | while(list_element != NULL) 7 | { 8 | cout << list_element ->data << " "; 9 | list_element = list_element ->next; 10 | } 11 | } -------------------------------------------------------------------------------- /c++/print_an_element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://practice.geeksforgeeks.org/problems/c-array-print-an-element-set-25933/1/?category[]=Arrays&category[]=Arrays&problemStatus=unsolved&page=1&query=category[]ArraysproblemStatusunsolvedpage1category[]Arrays 3 | Given an array A[] of N integers and an index Key. Your task is to print the element present at index key in the array. 4 | */ 5 | 6 | class Solution{ 7 | public: 8 | int findElementAtIndex(int a[], int n, int key) 9 | { 10 | if(key > n-1 || key < 0) 11 | return -1; 12 | return a[key]; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /c++/product-of-array-element.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/product-of-array-element/1 -------------------------------------------------------------------------------- /c++/queue-designer.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/queue-designer/1 2 | // Functions of the solution 3 | queue_push(int arr[],int n) 4 | { 5 | // Create a queue 6 | queuemy_queue; 7 | // Push(insert) every element of the array inside the queue 8 | for(int i =0; i< n; i++) 9 | my_queue.push(arr[i]); 10 | 11 | return my_queue; 12 | 13 | } 14 | 15 | void _pop(queues) 16 | { 17 | //print front and dequeue for each element until it becomes empty 18 | while(!s.empty()) 19 | { 20 | //Print front(first)element of the queue 21 | cout < q, int k){ 5 | q.add(k); 6 | 7 | } 8 | 9 | // Function to find frequency of an element 10 | // rteturn the frequency of k 11 | static int findFrequency(Queue q, int k){ 12 | int freq = Collections.frequency(q,k); 13 | return freq; 14 | 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /c++/queue-reversal.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/queue-reversal/1/?track=pcc-queue&batchId=221 2 | queue rev(queue q) 3 | { 4 | vectors; 5 | while(q.size() !=0) 6 | { 7 | s.push_back(q.front()); 8 | q.pop(); 9 | } 10 | 11 | reverse(s.begin(), s.end()); 12 | 13 | for(int i =0; i < s.size(); i++) 14 | { 15 | q.push(s[i]); 16 | } 17 | 18 | return q; 19 | } -------------------------------------------------------------------------------- /c++/queue_using_array.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/implement-queue-using-array/1/?track=DSA-Foundation-Final-Queue&batchId=193 2 | void MyQueue :: push(int x) 3 | { 4 | // Insert at the end of the queue (last element of the array) 5 | arr[rear++] = x; 6 | } 7 | 8 | //Function to pop an element from queue and return that element. 9 | int MyQueue :: pop() 10 | { 11 | // Get value from first element of the array 12 | // check if the queue is empty 13 | if(rear == front) return -1; 14 | int number = arr[front++]; 15 | return number; 16 | } -------------------------------------------------------------------------------- /c++/remove-duplicate-elements.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/remove-duplicate-elements-from-sorted-array/1/?track=ppc-arrays&batchId=221 2 | int remove_duplicate(int A[],int N) 3 | { 4 | sets; 5 | for(int i =0; i < N; i++) 6 | s.insert(A[i]); 7 | 8 | int i=0; 9 | set ::iterator itr; 10 | for(itr= s.begin(); itr != s.end(); itr++) 11 | { 12 | A[i] = *itr; 13 | i++; 14 | } 15 | return i; 16 | } -------------------------------------------------------------------------------- /c++/remove-duplicates-sorted-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/remove-duplicate-element-from-sorted-linked-list/1/?track=DSASP-LinkedList&batchId=154 2 | Node *removeDuplicates(Node *root) 3 | { 4 | if(root == NULL || root->next == NULL) 5 | return root; 6 | Node *current = root; 7 | 8 | while(current->next != NULL && current != NULL) 9 | { 10 | //Check if the next node is equasl;s to the current 11 | if(current->data == current->next->data) 12 | { 13 | Node *next = current->next->next; 14 | delete(current->next); 15 | current->next = next; 16 | } 17 | else 18 | { 19 | // If no delation has been done 20 | current = current->next; 21 | } 22 | } 23 | return root; 24 | } -------------------------------------------------------------------------------- /c++/remove-head-single-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/delete-head-of-linked-list/1/?track=DSA-Foundation-Final-Linked-List&batchId=193 2 | Node * deleteHead(Node *head) 3 | { 4 | if(head == NULL) 5 | return NULL; 6 | else if(head->next == NULL) 7 | { 8 | free(head); 9 | return NULL; 10 | } 11 | else 12 | { 13 | Node *new_head = head->next; 14 | free(head); 15 | return new_head; 16 | } 17 | } -------------------------------------------------------------------------------- /c++/remove-repeated-consecutive-digits.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/remove-repeated-digits-in-a-given-number/0 2 | #include 3 | using namespace std; 4 | 5 | string remove_repeated_digits(string number) 6 | { 7 | stacks; 8 | s.push(number[0]); 9 | 10 | string result = ""; 11 | result.push_back(number[0]); 12 | 13 | for(int i = 1; i < number.size(); i++) 14 | { 15 | if(number[i] != s.top()) 16 | { 17 | s.push(number[i]); 18 | result.push_back(number[i]); 19 | } 20 | } 21 | return result; 22 | } 23 | 24 | 25 | int main() 26 | { 27 | ios_base::sync_with_stdio(false); 28 | cin.tie(NULL); 29 | 30 | long int t; 31 | cin >> t; 32 | string number; 33 | while(t--) 34 | { 35 | cin >> number; 36 | cout << remove_repeated_digits(number) << endl; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /c++/remove-spaces.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/remove-spaces/0/?problemStatus=unsolved&difficulty[]=-2&page=1&sortBy=submissions&query=problemStatusunsolveddifficulty[]-2page1sortBysubmissions 2 | #include 3 | using namespace std; 4 | 5 | string RemoveSpaces(string word) 6 | { 7 | string result = ""; 8 | for(int i=0; i< word.size(); i++) 9 | { 10 | if(word[i] != ' ') 11 | result += word[i]; 12 | } 13 | return result; 14 | } 15 | 16 | int main() 17 | { 18 | ios_base::sync_with_stdio(false); 19 | cin.tie(NULL); 20 | 21 | long int t; 22 | cin >> t; 23 | string word; 24 | while(t--) 25 | { 26 | getline(cin, word); 27 | cout << RemoveSpaces(word) <myStack; 7 | //Push first char inside the string 8 | myStack.push(s[0]); 9 | for(int i =1; i < s.size(); i++) 10 | { 11 | if(s[i] != myStack.top()) 12 | myStack.push(s[i]); 13 | } 14 | 15 | s = ""; 16 | while(!myStack.empty()) 17 | { 18 | s.push_back(myStack.top()); 19 | myStack.pop(); 20 | } 21 | 22 | // Reverse string 23 | reverse(s.begin(), s.end()); 24 | 25 | return s; 26 | } -------------------------------------------------------------------------------- /c++/repeate-char.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve_test() 5 | { 6 | string s; 7 | cin >> s; 8 | 9 | for(int i=0; i> test_cases; 27 | 28 | while(test_cases--) 29 | solve_test(); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /c++/repeating-character-first-appearance-leftmost.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/repeating-character-first-appearance-leftmost/1/?track=SPC-Strings&batchId=154 2 | int repeatedCharacter (string s) 3 | { 4 | unordered_mapmp; 5 | for(int i =0; i < s.size(); i++) 6 | mp[s[i]]++; 7 | 8 | for(int i =0; i < s.size(); i++) 9 | { 10 | 11 | if(mp[s[i]] > 1) 12 | return i; 13 | } 14 | return -1; 15 | } -------------------------------------------------------------------------------- /c++/replace-os-with-5s.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/replace-all-0s-with-5/1/?track=amazon-arrays&batchId=192 2 | int convertFive(int n) { 3 | string number = to_string(n); 4 | for(int i =0; i< number.size(); i++) 5 | { 6 | if(number[i] == '0') 7 | number[i] = '5'; 8 | } 9 | // Return converted number 10 | return stoi(number); 11 | } -------------------------------------------------------------------------------- /c++/reverse-a-string.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/reverse-a-string/1/?track=DSA-Foundation-Final-String&batchId=193 2 | string reverseWord(string str){ 3 | 4 | int end = str.size()-1; 5 | for(int i = 0; i < str.size()/2; i++) 6 | { 7 | swap(str[i], str[end]); 8 | end--; 9 | } 10 | return str; 11 | } -------------------------------------------------------------------------------- /c++/reverse-array-2.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/reverse-an-array/1/?track=DSASP-LinkedList&batchId=154 2 | #include 3 | using namespace std; 4 | 5 | void ReverseArray (int *arr, int len) 6 | { 7 | int start; 8 | int end = len-1; 9 | for(start = 0; start < len/2; start++) 10 | { 11 | swap(arr[start], arr[end]); 12 | end--; 13 | } 14 | } 15 | 16 | void PrintArray (int *arr, int len) 17 | { 18 | for(int i =0; i< len; i++) 19 | cout << arr[i] << " "; 20 | cout<> t; 31 | while(t--) 32 | { 33 | cin >> n; 34 | int arr[n]; 35 | for(int i =0; i< n; i++) 36 | cin >> arr[i]; 37 | ReverseArray(arr, n); 38 | PrintArray(arr, n); 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /c++/reverse-array-using-stack.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/reverse-array-using-stack/1/?track=DSA-Foundation-Final-Stack&batchId=193 2 | void reverseArray(int n, int* arr){ 3 | stacks; 4 | for(int i =0; i < n; i++) 5 | s.push(arr[i]); 6 | 7 | for(int i =0; i < n; i++) 8 | { 9 | arr[i] = s.top(); 10 | s.pop(); 11 | } 12 | } -------------------------------------------------------------------------------- /c++/reverse-columns-of-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void reverseCol(vector > &matrix) 5 | { 6 | int row = matrix.size(); 7 | int column = matrix[0].size(); 8 | int left = 0, right = column -1; 9 | while (leftstack_reverser; 9 | for(int i=0; i < len; i++) 10 | { 11 | stack_reverser.push(str[i]); 12 | } 13 | for(int i=0; i< len; i++) 14 | { 15 | str[i] = stack_reverser.top(); 16 | stack_reverser.pop(); 17 | } 18 | } -------------------------------------------------------------------------------- /c++/reverse-the-array.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/reverse-the-array/1/?track=sp-arrays-and-searching&batchId=152 2 | int reverseArray(int arr[],int n) 3 | { 4 | int start = 0; 5 | int end = n-1; 6 | //int temp; 7 | while(start <= end) 8 | { 9 | swap(arr[start], arr[end]); 10 | start++; 11 | end--; 12 | } 13 | } -------------------------------------------------------------------------------- /c++/reverse_digits.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/reverse-digit0316/1?page=1&difficulty[]=-2&status[]=unsolved&sortBy=submissions 2 | long long int reverse_digit(long long int n) 3 | { 4 | long long reverse = 0; 5 | while (n) 6 | { 7 | reverse = reverse * 10 + n % 10; 8 | n /= 10; 9 | } 10 | return reverse; 11 | } -------------------------------------------------------------------------------- /c++/reverse_linked_list.cpp: -------------------------------------------------------------------------------- 1 | // Should reverse list and return new head. 2 | Node* reverseList(Node *head) 3 | { 4 | if(head == NULL) 5 | return head; 6 | else 7 | { 8 | Node *next, *prev, *current; 9 | current = head; 10 | prev = NULL; 11 | next = NULL; 12 | while(current != NULL) 13 | { 14 | // Store the next 15 | next = current->next; 16 | // Reverse current node's pointer 17 | // Here the reversion happens. 18 | current ->next = prev; 19 | // Move to the following node 20 | prev = current; 21 | current = next; 22 | } 23 | head = prev; 24 | } 25 | } -------------------------------------------------------------------------------- /c++/right_and_left_rotation.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/rotate-deque-by-k/1/?track=DSA-Foundation-Final-Dequeue&batchId=193 2 | void left_Rotate_Deq_ByK(deque &deq, int n, int k) 3 | { 4 | // Left: extract from front and pus in tail 5 | int value; 6 | for(int i =0; i < k; i++) 7 | { 8 | value = deq.front(); 9 | deq.pop_front(); 10 | deq.push_back(value); 11 | } 12 | } 13 | 14 | void right_Rotate_Deq_ByK(deque &deq, int n, int k) 15 | { 16 | // pop from tail and push in front; 17 | int value; 18 | for(int i =0; i < k; i++) 19 | { 20 | value = deq.back(); 21 | deq.pop_back(); 22 | deq.push_front(value); 23 | } 24 | } -------------------------------------------------------------------------------- /c++/rotate-array-by-n-elements.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/rotate-array-by-n-elements/0 2 | #include 3 | using namespace std; 4 | 5 | void solve_test() 6 | { 7 | long long n, d , element; 8 | cin >> n >> d; 9 | vectornumbers; 10 | for(int i=0; i > element; 13 | numbers.push_back(element); 14 | } 15 | for(int i =d; i >test_cases; 28 | while(test_cases--) 29 | solve_test(); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /c++/rotate-array-by-n-elements.cpp.save.1: -------------------------------------------------------------------------------- 1 | 2 | //https://practice.geeksforgeeks.org/problems/rotate-array-by-n-elements0 3 | #include 4 | using namespace std; 5 | 6 | void solve_test() 7 | { 8 | long long n, d , element; 9 | cin >> n >> d; 10 | vectornumbers; 11 | for(int i=0; i > element; 14 | numbers.push_back(element); 15 | } 16 | for(int i =d; i >test_cases; 29 | while(test_cases--) 30 | solve 31 | -------------------------------------------------------------------------------- /c++/rotate-matirixe-clowise.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve_test() 5 | { 6 | int n; 7 | cin >>n; 8 | int matrix[n][n]; 9 | for(int i=0; i > matrix[i][j]; 13 | } 14 | for(int j=0; j=0; i--) 17 | cout << matrix[i][j] << " "; 18 | } 19 | cout << endl; 20 | } 21 | 22 | int main() 23 | { 24 | int t; 25 | cin >> t; 26 | 27 | for(int i=0; i 3 | using namespace std; 4 | 5 | void solve_test() 6 | { 7 | int n; 8 | cin >>n; 9 | int matrix[n][n]; 10 | for(int i=0; i< n; i++) 11 | { 12 | for(int j=0; j> matrix[i][j]; 14 | } 15 | for(int i=n-1; i >=0; i--) 16 | { 17 | for(int j=0; j> t; 27 | 28 | for(int i=0; idata == x) 9 | return true; 10 | else 11 | return search(root->left,x) || search(root->right,x); 12 | } -------------------------------------------------------------------------------- /c++/search-in-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/search-in-linked-list/1/?track=DSA-Foundation-Final-Linked-List&batchId=193 2 | bool searchLinkedList(Node *head, int x) 3 | { 4 | while(head != NULL) 5 | { 6 | if(head->data == x) 7 | { 8 | return true; 9 | } 10 | head = head->next; 11 | } 12 | return false; 13 | } -------------------------------------------------------------------------------- /c++/search-in-matrix.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/search-in-a-matrix-1587115621/1/?track=ppc-matrix&batchId=221 2 | 3 | /* Function to search x in the input matrix 4 | * n, m: rows and columns of matrix 5 | * x: element to check presence in matrix 6 | * mat[][]: input matrix 7 | */ 8 | int search( int n,int m, int x, int mat[SIZE][SIZE]) 9 | { 10 | for(int i=0; i < n; i ++) 11 | { 12 | for(int j=0; j < m; j++) 13 | { 14 | if(mat[i][j] == x) 15 | return 1; 16 | } 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /c++/search-in-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/who-will-win-1587115621/1/?track=DSA-Foundation-Final-Searching&batchId=193 2 | // Function to find element in sorted array 3 | // arr: input array 4 | // N: size of array 5 | // K: element to be searched 6 | int searchInSorted(int arr[], int N, int K) 7 | { 8 | for(int i =0; i< N; i++) 9 | { 10 | if(arr[i] == K) 11 | return 1; 12 | else if(arr[i] > K) 13 | return -1; 14 | } 15 | return -1; 16 | 17 | } -------------------------------------------------------------------------------- /c++/search-node-bst.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/search-a-node-in-bst/1 2 | 3 | bool search(Node* root, int x) 4 | { 5 | //Your code here 6 | if(root == NULL) 7 | return false; 8 | else 9 | { // Target found 10 | if(root->data == x) 11 | return true; 12 | else if(root->data > x) 13 | return search(root->left , x); 14 | else 15 | return search(root->right , x); 16 | } 17 | return false; 18 | } -------------------------------------------------------------------------------- /c++/search0an-element-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/search-an-element-in-an-array-1587115621/1/?track=DSA-Foundation-Final-Searching&batchId=193 2 | int search(int arr[], int N, int X) 3 | { 4 | for(int i =-0; i < N; i++) 5 | { 6 | if(arr[i] ==X) 7 | return i; 8 | } 9 | return -1; 10 | } 11 | -------------------------------------------------------------------------------- /c++/set-operations.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/set-operations/1 2 | set setInsert(int arr[],int n) 3 | { 4 | sets; 5 | 6 | for(int i=0; i< n; i++) 7 | s.insert(arr[i]); 8 | 9 | return s; 10 | } 11 | 12 | 13 | void setDisplay(sets) 14 | { 15 | //Your code here to display elements of s 16 | set :: iterator itr; 17 | for(itr = s.begin(); itr!= s.end(); itr++) 18 | cout << *itr << " "; 19 | 20 | cout<&s,int x) 25 | { 26 | //write if condition here 27 | if(s.erase(x)) 28 | { 29 | s.erase(x); 30 | cout<<"erased "<left)+1+getSize(node->right); 8 | } -------------------------------------------------------------------------------- /c++/smallest-positive-missing-number.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/smallest-positive-missing-number-1587115621/1/?track=DSASP-Searching&batchId=154 2 | int missingNumber(int arr[], int n) { 3 | 4 | sort(arr, arr+n); 5 | int smallest = 1; 6 | for(int i =0; i< n; i++) 7 | { 8 | if(arr[i] == smallest) 9 | smallest ++; 10 | } 11 | 12 | return smallest; 13 | 14 | } -------------------------------------------------------------------------------- /c++/sort-a-stack.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sort-a-stack/1/?track=ppc-stack&batchId=221 2 | void SortedStack :: sort() 3 | { 4 | vector vec; 5 | while(!s.empty()) 6 | { 7 | vec.push_back(s.top()); 8 | s.pop(); 9 | } 10 | 11 | std::sort(vec.begin(), vec.end()); 12 | 13 | for(int i = 0; i < vec.size(); i++) 14 | s.push(vec[i]); 15 | } -------------------------------------------------------------------------------- /c++/sort-and-reverse.cpp: -------------------------------------------------------------------------------- 1 | vector sortVector(vectorv) 2 | { 3 | //Your code here. Use library function 4 | sort(v.begin(), v.end()); 5 | return v; 6 | } 7 | vector reverseVector(vectorv) 8 | { 9 | // We could also use: reverse(v.begin() , v.end()); 10 | sort(v.begin(), v.end(), greater()); 11 | return v; 12 | } -------------------------------------------------------------------------------- /c++/sort-array-of-zeros-ones-and-two.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s-1587115621/1/?track=amazon-sorting&batchId=192 2 | void segragate012(int arr[], int n) 3 | { 4 | int zeros = 0, ones = 0, two =0; 5 | 6 | for(int i =0; i < n; i++) 7 | { 8 | if(arr[i] == 0) 9 | { 10 | zeros+=1; 11 | } 12 | else if(arr[i] == 1) 13 | { 14 | ones+=1; 15 | } 16 | else if(arr[i] == 2) 17 | { 18 | two+=1; 19 | } 20 | } 21 | 22 | int i=0; 23 | while(zeros--) 24 | { 25 | arr[i] = 0; 26 | i+=1; 27 | } 28 | 29 | while(ones--) 30 | { 31 | arr[i] = 1; 32 | i+=1; 33 | } 34 | 35 | while(two--) 36 | { 37 | arr[i] = 2; 38 | i+=1; 39 | } 40 | } -------------------------------------------------------------------------------- /c++/sort-arrays-of-0-and-1.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s4231/1 2 | void sort012(int a[], int n) 3 | { 4 | if(n == 1) 5 | { 6 | return; 7 | } 8 | sort(a, a+n); 9 | 10 | } -------------------------------------------------------------------------------- /c++/sort-arrays-of-0.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s4231/1 2 | -------------------------------------------------------------------------------- /c++/sort-linked-list-of-0s-1s-2s.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/given-a-linked-list-of-0s-1s-and-2s-sort-it/1 2 | Node* segregate(Node *head) { 3 | // Create couters of 0, 1 and 2 4 | if(head == NULL) 5 | return head; 6 | int count[] = {0, 0, 0}; 7 | Node *current = head; 8 | while(current != NULL) 9 | { 10 | count[current->data] += 1; 11 | current = current->next; 12 | } 13 | int index = 0; 14 | // GO to head of the linked list 15 | current = head; 16 | while(current != NULL) 17 | { 18 | while(count[index] > 0) 19 | { 20 | count[index] -=1; 21 | current->data = index; 22 | current = current->next; 23 | } 24 | index++; 25 | } 26 | return head; 27 | } -------------------------------------------------------------------------------- /c++/split-strings.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/split-strings5211/1/?problemStatus=unsolved&difficulty[]=-2&page=1&query=problemStatusunsolveddifficulty[]-2page1 2 | vector splitString(string S) 3 | { 4 | vectorsolution; 5 | string characters = ""; 6 | string digits = ""; 7 | string special_chars = ""; 8 | for(int i=0; i < S.size(); i++) 9 | { 10 | if(S[i] >='a' && S[i] <= 'z' || S[i] >='A' && S[i] <= 'Z') 11 | characters += S[i]; 12 | else if(S[i] >= '0' && S[i] <= '9') 13 | digits += S[i]; 14 | else 15 | special_chars += S[i]; 16 | } 17 | solution.push_back(characters); 18 | solution.push_back(digits); 19 | solution.push_back(special_chars); 20 | return solution; 21 | } -------------------------------------------------------------------------------- /c++/sqare-rooot.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/square-root/1/?track=SPCF-Searching&batchId=154 2 | long long int floorSqrt(long long int x) 3 | { 4 | return floor(sqrt(x)); 5 | } -------------------------------------------------------------------------------- /c++/stack-designer.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/stack-designer/1 2 | /* _push function to insert elements of array to 3 | stack 4 | */ 5 | stack_push(int arr[],int n) 6 | { 7 | stacks; 8 | for(int i =0; is) 18 | { 19 | //print top and pop for each element until it becomes empty 20 | while(!s.empty()) 21 | { 22 | cout << s.top() << " "; 23 | s.pop(); 24 | } 25 | } -------------------------------------------------------------------------------- /c++/stack-implementation-with-array.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/stack-from-array/1/?track=DSA-Foundation-Final-Stack&batchId=193 2 | 3 | //Function to push data at the top of stack 4 | void push(int data) 5 | { 6 | if(top == stackMax -1) 7 | { 8 | cout << "Stack Full"< 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() 10 | { 11 | unsigned int t; 12 | cin >> t; 13 | string word; 14 | for(int i=0; i>word; 17 | // This approach uses Quick sort algorithm 18 | sort(word.begin(), word.end()); 19 | cout << word< 2 | using namespace std; 3 | 4 | long long int get_factorial(int n) 5 | { 6 | long long int factorial = 1; 7 | for(int i =1; i<=n; i++) 8 | factorial = factorial *i; 9 | return factorial; 10 | } 11 | 12 | void solve_test() 13 | { 14 | int number, single_number,sum =0; 15 | 16 | cin >> number; 17 | int digits = number; 18 | while(digits > 0) 19 | { 20 | single_number = digits%10; 21 | digits /=10; 22 | sum += get_factorial(single_number); 23 | } 24 | if(sum == number) 25 | cout<<"Strong"<> t; 34 | 35 | for(int i=0; i p1, pair p2) 3 | { 4 | if(p1.second > p2.second) 5 | return true; 6 | else if(p1.second == p2.second && p1.first < p2.first) 7 | return true; 8 | return false; 9 | } 10 | 11 | vector> sortMarks(vector> v, int N){ 12 | sort(v.begin(), v.end(), comp); 13 | return v; 14 | } 15 | -------------------------------------------------------------------------------- /c++/sum-of-array-elements.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sum-of-array-elements2502/1/?track=DSA-Foundation-Final-Arrays&batchId=193 2 | int sumElement(int arr[],int n) 3 | { 4 | int sum = 0; 5 | for(int i =0; i < n; i ++) 6 | sum += arr[i]; 7 | return sum; 8 | } -------------------------------------------------------------------------------- /c++/sum-of-array-elements2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void StoreArray(int *arr, int len) 5 | { 6 | for(int i =0; i < len; i++) 7 | cin >> arr[i]; 8 | } 9 | 10 | int GetSumrrayElements(int *arr, int len) 11 | { 12 | int sum_elements =0; 13 | for(int i =0; i< len; i++) 14 | sum_elements += arr[i]; 15 | return sum_elements; 16 | } 17 | 18 | int main() 19 | { 20 | ios_base::sync_with_stdio(false); 21 | cin.tie(NULL); 22 | 23 | long int t; 24 | int n; 25 | cin >> t; 26 | 27 | while(t--) 28 | { cin >> n; 29 | int arr[n]; 30 | StoreArray(arr, n); 31 | cout << GetSumrrayElements(arr, n)<key + sumBT(root->left) + sumBT(root->right); 9 | } -------------------------------------------------------------------------------- /c++/sum-of-digits.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sum-of-digits1742/1/?problemStatus=unsolved&difficulty[]=-2&page=1&query=problemStatusunsolveddifficulty[]-2page1 2 | class Solution{ 3 | public: 4 | int sumOfDigits(int N){ 5 | int sum_digits = 0; 6 | while(N >0) 7 | { 8 | sum_digits += N%10; 9 | N /=10; 10 | } 11 | return sum_digits; 12 | } 13 | }; -------------------------------------------------------------------------------- /c++/sum-of-distinct-element.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sum-of-distinct-elements-1/0 2 | #include 3 | using namespace std; 4 | 5 | int sum_distinct_elements() 6 | { 7 | int n, number; 8 | cin >> n; 9 | unordered_sets; 10 | while(n--) 11 | { 12 | cin >> number; 13 | s.insert(number); 14 | } 15 | 16 | int sum = 0; 17 | unordered_set :: iterator itr; 18 | for(itr = s.begin(); itr!= s.end(); itr++) 19 | sum += *itr; 20 | return sum; 21 | 22 | } 23 | 24 | int main() 25 | { 26 | ios_base::sync_with_stdio(false); 27 | cin.tie(NULL); 28 | 29 | long int t; 30 | cin >> t; 31 | while(t--) 32 | { 33 | cout << sum_distinct_elements() << endl; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /c++/sum-of-leaf-nodes-in-bst.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sum-of-leaf-nodes-in-bst/1/ 2 | int sumOfLeafNodes(Node *r ){ 3 | if(r == NULL) 4 | return 0; 5 | int sum = 0; 6 | if(r->left == NULL && r->right == NULL) 7 | sum += r->data; 8 | return sum + sumOfLeafNodes(r->left) + sumOfLeafNodes(r->right); 9 | } -------------------------------------------------------------------------------- /c++/sum-of-leaf-nodes.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sum-of-leaf-nodes/1 2 | int sumLeaf(Node* root) 3 | { 4 | if(root == NULL) 5 | return 0; 6 | int sum = 0; 7 | if(root->left == NULL && root->right == NULL) 8 | sum += root->data; 9 | return sum+ sumLeaf(root->left)+sumLeaf(root->right); 10 | } -------------------------------------------------------------------------------- /c++/sum-of-matrix-elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumOfMatrix(int N, int M, vector> Grid) { 4 | int sum_of_elements=0; 5 | for(int i =0; i < N; i++) 6 | { 7 | for(int j=0; j < M; j++) 8 | sum_of_elements += Grid[i][j]; 9 | } 10 | return sum_of_elements; 11 | } 12 | 13 | }; -------------------------------------------------------------------------------- /c++/sum-of-nodes.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sum-the-nodes-of-linked-list/1/?track=DSA-Foundation-Final-Linked-List&batchId=193 2 | int sumOfElements(Node *head) 3 | { 4 | int sum = 0; 5 | while(head != NULL) 6 | { 7 | sum += head->data; 8 | head = head->next; 9 | } 10 | return sum; 11 | } -------------------------------------------------------------------------------- /c++/sum_of_array.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/c-arrays-sum-of-array-set-14805/1/?category[]=Arrays&category[]=Arrays&problemStatus=unsolved&page=1&query=category[]ArraysproblemStatusunsolvedpage1category[]Arrays 2 | /* 3 | * Given an array of N integers. Your task is to print the sum of all of the integers. 4 | */ 5 | 6 | class Solution{ 7 | public: 8 | int getSum(int a[], int n) { 9 | int solution = 0; 10 | for(int i =0; i < n; i++) 11 | solution += a[i]; 12 | 13 | return solution; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /c++/sum_of_array2.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | // function to return sum of elements 4 | // in an array of size n 5 | int sum(int arr[], int n) { 6 | int sol = 0; 7 | for(int i =0; i< n;i++) 8 | { 9 | sol += arr[i]; 10 | } 11 | return sol; 12 | } 13 | }; -------------------------------------------------------------------------------- /c++/sum_of_first_n_terms.cpp: -------------------------------------------------------------------------------- 1 | // https://www.geeksforgeeks.org/problems/sum-of-first-n-terms5843/1?page=2&difficulty=Basic&sortBy=submissions 2 | // Given an integer n, calculate the sum of series 13 + 23 + 33 + 43 + … till n-th term. 3 | class Solution { 4 | public: 5 | long long sumOfSeries(long long n) { 6 | if(n==0){ 7 | return 0; 8 | } 9 | return n*n*n + sumOfSeries(n-1); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /c++/swap-the-objects.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/swap-the-objects/1/ 2 | void swapper(Person **obj1, Person **obj2) 3 | { 4 | //Your code here 5 | Person *temp = new Person; 6 | temp = *obj1; 7 | *obj1 = *obj2; 8 | *obj2 = temp; 9 | } -------------------------------------------------------------------------------- /c++/taking-inputs.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/taking-input/1/?track=module-1-basics-and-control-structures&batchId=113 2 | void inputData() 3 | { 4 | int a; 5 | string b; 6 | cin >> a; 7 | cin >> b; 8 | cout << a<< " "< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // number of test cases 7 | int t; 8 | // Read number of test cases 9 | cin >>t; 10 | for(int i =0; i 3 | using namespace std; 4 | 5 | void solve_test() 6 | { 7 | int number; 8 | cin >> number; 9 | cout << 6-number+1 << endl; 10 | } 11 | 12 | int main() 13 | { 14 | unsigned long int test_cases; 15 | cin >> test_cases; 16 | 17 | while(test_cases) 18 | solve_test(); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /c++/third-larghest-element.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/third-largest-element/1/?track=amazon-arrays&batchId=192 2 | long long thirdLargest(long long a[], long long n) 3 | { 4 | if(n < 3) 5 | return -1; 6 | sort(a, a+n); 7 | return a[n-3]; 8 | 9 | } -------------------------------------------------------------------------------- /c++/transponse.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/transpose-of-matrix-1587115621/1/?track=ppc-matrix&batchId=221 2 | /* Function which converts A[][] to its transpose 3 | * n: size of matrix 4 | * N: globally defined for matrix 5 | * A: input matrix 6 | */ 7 | void transpose( int A[][N],int n) 8 | { 9 | int transponse_matri[n][n]; 10 | for(int i =0; i < n; i++) 11 | { 12 | for(int j =0; j < n; j++) 13 | { 14 | transponse_matri[i][j] = A[j][i]; 15 | } 16 | } 17 | 18 | for(int i =0; i < n; i++) 19 | { 20 | for(int j =0; j < n; j++) 21 | { 22 | A[i][j] = transponse_matri[i][j]; 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /c++/transpose-of-matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve_test() 5 | { 6 | int n; 7 | cin >> n; 8 | int mat[n][n]; 9 | for(int i=0; i> mat[i][j]; 14 | } 15 | } 16 | 17 | // Print its transpose 18 | for(int i=0; i> t; 34 | for(int i=0; i 2 | using namespace std; 3 | 4 | void solve_test() 5 | { 6 | int n; 7 | cin >> n; 8 | int mat[n][n]; 9 | for(int i=0; i> mat[i][j]; 14 | } 15 | } 16 | 17 | // Print its transpose 18 | for(int i=0; i> t; 34 | for(int i=0; i levelOrder(Node* node) 3 | { 4 | vectors; 5 | if(node == NULL) 6 | return s; 7 | 8 | // Apply Breadth first search algorithm 9 | // Create queue and push root inside 10 | queue myQueue; 11 | myQueue.push(node); 12 | 13 | Node *current_node; 14 | while(!myQueue.empty()) 15 | { 16 | current_node = myQueue.front(); 17 | s.push_back(current_node->data); 18 | myQueue.pop(); 19 | 20 | if(current_node->left != NULL) 21 | myQueue.push(current_node->left); 22 | 23 | if(current_node->right != NULL) 24 | myQueue.push(current_node->right); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /c++/twice-counter.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/twice-counter4236/1/ 2 | /* 3 | Given a list of N words. Count the number of words that appear exactly twice in the list. 4 | */ 5 | class Solution 6 | { 7 | public: 8 | int countWords(string list[], int n) 9 | { 10 | unordered_mapmp; 11 | for(int i =0; i < n; i++) 12 | mp[list[i]]++; 13 | int counter = 0; 14 | for(auto x : mp) 15 | { 16 | if(x.second == 2) 17 | counter++; 18 | } 19 | return counter; 20 | } 21 | }; -------------------------------------------------------------------------------- /c++/two-dimensional-world.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/two-dimensional-world/1 2 | //Function Solution 3 | void twoDimensional(int **mat,int N){ 4 | // Loop to iterate through matrix 5 | for(int i = 0;idata == r2->data && 11 | isIdentical(r1->left, r2->left) && 12 | isIdentical(r1->right, r2->right); 13 | } 14 | return false; 15 | } -------------------------------------------------------------------------------- /c++/uppercase-to-lowercase.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/uppercase-to-lowercase/1/?track=DSA-Foundation-Final-String&batchId=193 2 | string caseConversion(string str){ 3 | for(int i =0; i < str.size(); i++) 4 | { 5 | if(isupper(str[i])) 6 | str[i] = tolower(str[i]); 7 | } 8 | return str; 9 | } 10 | -------------------------------------------------------------------------------- /c++/value-equal-to-index-value.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/value-equal-to-index-value1330/1/?company[]=Amazon&problemStatus=unsolved&difficulty[]=-2&page=1&sortBy=submissions&query=company[]AmazonproblemStatusunsolveddifficulty[]-2page1sortBysubmissions 2 | class Solution{ 3 | public: 4 | 5 | vector valueEqualToIndex(int arr[], int n) { 6 | vectorsolution; 7 | for(int i=0; i< n; i++) 8 | { 9 | if(arr[i] == i+1) 10 | solution.push_back(arr[i]); 11 | } 12 | return solution; 13 | } 14 | }; -------------------------------------------------------------------------------- /c++/vowels-in-string.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/vowels-in-string/1/?track=DSA-Foundation-Final-String&batchId=193 2 | 3 | int isVowel(char c) 4 | { 5 | if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' 6 | || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') 7 | return true; 8 | 9 | return false; 10 | } 11 | 12 | int countVowels(string str){ 13 | int solution = 0; 14 | for(int i =0; i < str.size(); i++) 15 | { 16 | if(isVowel(str[i])) solution++; 17 | } 18 | return solution; 19 | 20 | } -------------------------------------------------------------------------------- /c++/while-loop-print-table.cpp: -------------------------------------------------------------------------------- 1 | void printTable(int n) 2 | { 3 | int multiplier=10; 4 | while(multiplier>=1) 5 | { 6 | cout<count_y) 15 | return x; 16 | else if(count_y>count_x) 17 | return y; 18 | else 19 | { 20 | // they have same frequency 21 | return min(x, y); 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /geeksforgeeks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omonimus1/geeks-for-geeks-solutions/a414368d93acb60f0836ae9e5c566673080e6646/geeksforgeeks.png -------------------------------------------------------------------------------- /images/geeksforgeeks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omonimus1/geeks-for-geeks-solutions/a414368d93acb60f0836ae9e5c566673080e6646/images/geeksforgeeks.png -------------------------------------------------------------------------------- /images/spiral-traversal-matrix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omonimus1/geeks-for-geeks-solutions/a414368d93acb60f0836ae9e5c566673080e6646/images/spiral-traversal-matrix.png -------------------------------------------------------------------------------- /java/Anagram.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omonimus1/geeks-for-geeks-solutions/a414368d93acb60f0836ae9e5c566673080e6646/java/Anagram.class -------------------------------------------------------------------------------- /java/data-types-java.java: -------------------------------------------------------------------------------- 1 | class Geeks{ 2 | // Function to do operations with different data types 3 | static void dataTypes(int a, float b, double c, long l, byte d){ 4 | double p = c/b; 5 | double q = b/a; 6 | double r = c/a; 7 | double m = r+l; 8 | int s = a/d; 9 | System.out.println(p + " " + q + " " + r + " " + m + " " + s); 10 | } 11 | } -------------------------------------------------------------------------------- /java/even-odd.java: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/even-odd-java/1 2 | class Geeks { 3 | static void evenOdd (int a,int b) 4 | { 5 | if(a%2 == 0) 6 | { 7 | System.out.println(a); 8 | System.out.println(b); 9 | } 10 | else 11 | { 12 | System.out.println(b); 13 | System.out.println(a); 14 | } 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /java/for-loop-prime-check.java: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/for-loop-primecheck-java/1 2 | class Geeks { 3 | static void isPrime (int n) 4 | { 5 | if(n <=1) 6 | { 7 | System.out.println("No"); 8 | return; 9 | } 10 | for(int i=2;i<=Math.sqrt(n);i++) 11 | { 12 | if( n % i == 0) 13 | { 14 | System.out.println("No"); 15 | return; 16 | } 17 | } 18 | System.out.println("Yes"); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /java/java-inheritance.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/java-inheritance/1 2 | 3 | // cl2 is a sub class of cls1 4 | class cls2 extends cls1 5 | { 6 | void mul(int p,int q) 7 | { 8 | System.out.println(p*q); 9 | } 10 | void task(int p,int q) 11 | { 12 | //Add your code here. 13 | System.out.println(p*p + q*q); 14 | } 15 | } -------------------------------------------------------------------------------- /java/start-coding.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/start-coding-java/1 2 | class Geeks{ 3 | // Function to print hello 4 | static void printHello(){ 5 | System.out.println("Hello World"); 6 | } 7 | } -------------------------------------------------------------------------------- /java/the-new-line.java: -------------------------------------------------------------------------------- 1 | class Geeks{ 2 | // Function to print each word in single line 3 | static void printIndividualLine(){ 4 | // Your code here 5 | System.out.print("Geeks\nfor\nGeeks"); 6 | } 7 | } -------------------------------------------------------------------------------- /must-do-coding-challenges/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omonimus1/geeks-for-geeks-solutions/a414368d93acb60f0836ae9e5c566673080e6646/must-do-coding-challenges/README.md -------------------------------------------------------------------------------- /must-do-coding-challenges/last-index-of-1.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/last-index-of-1/0 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | 10 | long int t; 11 | int left, right, middle; 12 | cin >> t; 13 | string number; 14 | bool found; 15 | while (t--) 16 | { 17 | found = false; 18 | cin >> number; 19 | for (int i = number.size() - 1; i >= 0; i--) 20 | { 21 | if (number[i] == '1') 22 | { 23 | cout << i << endl; 24 | found = true; 25 | break; 26 | } 27 | } 28 | if (!found) 29 | cout << "-1" << endl; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /must-do-coding-challenges/middle-element-linked-list.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/finding-middle-element-in-a-linked-list/1 2 | int countNode(Node* head) 3 | { 4 | int counter = 0; 5 | while (head != NULL) 6 | { 7 | counter++; 8 | head = head->next; 9 | } 10 | return counter; 11 | } 12 | 13 | int getMiddle(Node* head) 14 | { 15 | if (head == NULL) 16 | return -1; 17 | if (head->next == NULL) 18 | return head->data; 19 | Node* current = head; 20 | int nodes = countNode(current); 21 | current = head; 22 | nodes /= 2; 23 | while (current != NULL && nodes--) 24 | { 25 | current = current->next; 26 | } 27 | return current->data; 28 | } -------------------------------------------------------------------------------- /must-do-coding-challenges/remove-duplicates3034.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/remove-duplicates3034/1 2 | /* Given a string without spaces, the task is to remove duplicates from it.*/ 3 | //User function template for C++ 4 | class Solution { 5 | public: 6 | 7 | string removeDups(string S) 8 | { 9 | if (S == " " || S == "") 10 | return S; 11 | unordered_mapmp; 12 | int len = S.size(); 13 | for (int i = 0; i < len; i++) 14 | { 15 | mp[S[i]]++; 16 | } 17 | string result = ""; 18 | for (int i = 0; i < len; i++) 19 | { 20 | if (mp[S[i]] != -1) 21 | { 22 | result += S[i]; 23 | mp[S[i]] = -1; 24 | } 25 | } 26 | return result; 27 | } 28 | }; -------------------------------------------------------------------------------- /must-do-coding-challenges/sort-an-array-of-0s-1s-and-2.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s4231/1 2 | /* 3 | Given an array of size N containing 0s, 1s, and 2s; sort the array in ascending order. 4 | (From smallest to biggest value) 5 | */ 6 | void sort012(int a[], int n) 7 | { 8 | sort(a, a + n); 9 | } -------------------------------------------------------------------------------- /python/add-two-numbers-represented-by-linked-lists.py: -------------------------------------------------------------------------------- 1 | #https://practice.geeksforgeeks.org/problems/add-two-numbers-represented-by-linked-lists/1/?track=DSASP-LinkedList&batchId=154 2 | def extractNumberFromList(linked_list): 3 | result = "" 4 | while linked_list is not None: 5 | result += str(linked_list.data) 6 | linked_list = linked_list.next 7 | if result == "": 8 | return 0 9 | return result 10 | 11 | 12 | class Solution: 13 | def addTwoLists(self, first, second): 14 | number_one = int(extractNumberFromList(first)) 15 | second_number = int(extractNumberFromList(second)) 16 | result = str(number_one + second_number) 17 | for x in result: 18 | print(x, end=' ') 19 | -------------------------------------------------------------------------------- /python/anagram.py: -------------------------------------------------------------------------------- 1 | """ 2 | https://practice.geeksforgeeks.org/problems/anagram-1587115620/1?page=1&category[]=Strings&curated[]=1&sortBy=submissions 3 | """ 4 | class Solution: 5 | #Function is to check whether two strings are anagram of each other or not. 6 | def isAnagram(self,a,b): 7 | if len(a) != len(b): 8 | return 'NO' 9 | if a == b[::-1]: 10 | return 'YES' 11 | return 'NO' -------------------------------------------------------------------------------- /python/bitonic_point.py: -------------------------------------------------------------------------------- 1 | """ 2 | Given an array arr of n elements which is first increasing and then may be decreasing, find the maximum element in the array. 3 | Note: If the array is increasing then just print then last element will be the maximum value. 4 | https://practice.geeksforgeeks.org/problems/maximum-value-in-a-bitonic-array3001/1?page=1&company[]=Amazon&curated[]=8&sortBy=submissions 5 | """ 6 | class Solution: 7 | def findMaximum(self,arr, n): 8 | arr = sorted(arr) 9 | return arr[-1] -------------------------------------------------------------------------------- /python/cat-and-hat-python.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/cat-and-hat-python/1/?track=python-module-2&batchId=119 2 | 3 | 4 | """ 5 | Having a string, returns true if the substring 'cat' compares 6 | the same number of time of 'hat' in string 7 | """ 8 | def cat_hat(str): 9 | number_cat = str.count('cat') 10 | number_hat = str.count('hat') 11 | 12 | return number_cat == number_hat 13 | -------------------------------------------------------------------------------- /python/check_status_python.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/check-the-status/1/?track=python-module-2&batchId=119 2 | """ 3 | Given two integer variables a and b, and a boolean variable flag. The task is to check the status and return accordingly. 4 | Return "True" if either a or b (only one of them) is positive except for the case when flag is True, return True only if both of the variables a and b are negative except for the case when flag is False. 5 | """ 6 | def check_status(a, b, flag): 7 | if (a>0 and b<0) or (a<0 and b>0): 8 | return not flag 9 | elif (a < 0 and b <0): 10 | return flage -------------------------------------------------------------------------------- /python/comment-in-python.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/comment-in-python/1/?track=fork-python&batchId=119 2 | 3 | def print_func(): 4 | # print ("Geeks Classes") 5 | print ("Geeks For Geeks") 6 | """ 7 | print ("Geeks Quiz") 8 | print ("Geeks") 9 | print ("Geeks For") 10 | """ -------------------------------------------------------------------------------- /python/comparison.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/comparison-operators-python/1/?track=fork-python&batchId=119 2 | def comparison(a,b): 3 | print(a==b) ##do a==b 4 | print(a>b) ##do a>b 5 | print(a!=b) ##do a!=b 6 | print(a 1: 12 | # check for factors 13 | for i in range(2,int(math.sqrt(n))): 14 | if (n % i) == 0: 15 | return False 16 | return True -------------------------------------------------------------------------------- /python/hello-world.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/start-coding-python/1 2 | def print_fun(): 3 | print('Hello World') -------------------------------------------------------------------------------- /python/if_conditional_statment.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/if-loop-python/1/?track=python-module-2&batchId=119 2 | def friends_in_trouble(j_angry, s_angry): 3 | if j_angry and s_angry or not j_angry and not s_angry: 4 | return True 5 | return False 6 | -------------------------------------------------------------------------------- /python/increment-and-decrement.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/increment-and-decrement-python/1/?track=fork-python&batchId=119 2 | def do_operation(x, y): 3 | print (x-1) 4 | print (y+1) -------------------------------------------------------------------------------- /python/intro-to-list.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/intro-to-lists-python/1/?track=python-module-5&batchId=119 2 | def check_zero(size_array, arr): 3 | 4 | # complete the if statement to check 5 | # if first or last element in list is 0 6 | if arr[0] == 0 or arr[size_array-1] == 0 : 7 | return True 8 | 9 | return False 10 | -------------------------------------------------------------------------------- /python/is-and-in-operator.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/is-and-in-operator/1/?track=fork-python&batchId=119 2 | #Function to find if number is present in the list or not 3 | def number_present(num, query): 4 | #num is a 'list', query is a 'int' 5 | for x in num: 6 | if x is query: 7 | return True 8 | return False 9 | -------------------------------------------------------------------------------- /python/jumping-though-while.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/jumping-through-while-python/1/?track=python-module-2&batchId=119 2 | def printIncreasingPower(x): 3 | i = 1 4 | # Loop to jump in powers of 2 5 | while(pow(i,2) <= x): 6 | ##Your code here 7 | print (pow(i,2), end = " ") 8 | i += 1 9 | 10 | -------------------------------------------------------------------------------- /python/larghest_element_in_array.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/largest-element-in-array4009/1/?category[]=Arrays&category[]=Arrays&page=1&query=category[]Arrayspage1category[]Arrays 2 | # Given an array A[] of size n. The task is to find the largest element in it. 3 | def largest( arr, n): 4 | return max(arr) 5 | -------------------------------------------------------------------------------- /python/logical-operators.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/logical-operators-python/1/?track=fork-python&batchId=119 2 | 3 | def logical(a,b): 4 | print(a and b) ## do a and b 5 | print(a or b) ## do a or b 6 | print(not a) ## do not a -------------------------------------------------------------------------------- /python/node-at-given-position.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/node-at-a-given-index-in-linked-list/1?page=1&category[]=Linked%20List&curated[]=8&sortBy=submissions 2 | """ 3 | Given a singly linked list with N nodes and a number X. The task is to find 4 | the node at the given index (X)(1 based index) of linked list. 5 | 6 | Input: 7 | First line of input contains number of testcases T. For each testcase, 8 | first line of input contains space seperated two integers, 9 | length of linked list and X. 10 | """ 11 | 12 | def getNth(head, k): 13 | position = 1 14 | while head.next is not None and position <= k-1: 15 | position+=1 16 | head = head.next 17 | return head.data -------------------------------------------------------------------------------- /python/nth_node_from_end.py: -------------------------------------------------------------------------------- 1 | """ 2 | https://practice.geeksforgeeks.org/problems/nth-node-from-end-of-linked-list/1?page=1&category[]=Linked%20List&curated[]=1&sortBy=submissions 3 | Given a linked list consisting of L nodes and given a number N. The task is to find the Nth node from the end of the linked list. 4 | """ 5 | def getNthFromLast(head,n): 6 | number_of_nodes = 0 7 | my_list = [] 8 | while head: 9 | number_of_nodes +=1 10 | my_list.append(head.data) 11 | head = head.next 12 | element_location = number_of_nodes -n 13 | # add out-of-index check out manage corner cases where location of element 14 | # overcome list dimension 15 | if element_location < 0 or element_location > number_of_nodes: 16 | return -1 17 | return my_list[number_of_nodes - n] 18 | -------------------------------------------------------------------------------- /python/operators-in-python.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/operators-in-python/1/?track=fork-python&batchId=119 2 | def do_operation(x, y): 3 | print(str(x+y)) 4 | print(str(x-y)) 5 | print(str(x*y)) 6 | print(str(x/y)) 7 | print(x**y) 8 | print(x//y) 9 | 10 | -------------------------------------------------------------------------------- /python/print-linked-list.py: -------------------------------------------------------------------------------- 1 | """ 2 | https://practice.geeksforgeeks.org/problems/print-linked-list-elements/1 3 | """ 4 | class Solution: 5 | def display(self,node): 6 | while node: 7 | print(node.data, end=" ") 8 | node = node.next -------------------------------------------------------------------------------- /python/repeat-the-string.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/repeat-the-strings/1/?track=python-module-3&batchId=119 2 | def combo_string(a, b): 3 | # your code here 4 | if(len(a) > len(b)): 5 | return b+a+b 6 | return a+b+a 7 | 8 | -------------------------------------------------------------------------------- /python/repeatitive-printing.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/repeatitive-printing-python/1/ 2 | # Function to print given string 'x' times 3 | def print_fun(string, x): 4 | print(string *x) -------------------------------------------------------------------------------- /python/replace_all_zero_with_5.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/replace-all-0-with-5-in-an-input-integer/1 2 | """ 3 | Given a number N. The task is to complete the function convertFive() which replace 4 | all zeros in the number with 5 and returns the number.z 5 | """ 6 | class Solution: 7 | def convertFive(self,n): 8 | number = str(n) 9 | return number.replace('0', '5') 10 | return int(number) -------------------------------------------------------------------------------- /python/sep-and-end-in-print.py: -------------------------------------------------------------------------------- 1 | def print_func(string1, string2): 2 | print ('Geeks','For','Geeks', sep = '$', end = '$') -------------------------------------------------------------------------------- /python/space-separate.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/space-seperated-python/1/ 2 | # Function to print words with spaces 3 | def print_fun(): 4 | print('Geeks','For','Geeks', sep=" ") 5 | -------------------------------------------------------------------------------- /python/split-the-input.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/split-the-input-python/1/?track=fork-python&batchId=119 2 | def inPutS(): 3 | a=input() ## input in a single line() 4 | s,i,f = a.split() 5 | print(s+" "+ str(int(i)+float(f))) ##type cast i to int, f to float. Add i with f. Typecast the result to string 6 | -------------------------------------------------------------------------------- /python/sum_of_middle_element_of_sorted_arrays.py: -------------------------------------------------------------------------------- 1 | """ 2 | https://practice.geeksforgeeks.org/problems/sum-of-middle-elements-of-two-sorted-arrays2305/1?page=2&company[]=Amazon&category[]=Arrays&curated[]=1&sortBy=submissions 3 | Given 2 sorted arrays Ar1 and Ar2 of size N each. 4 | Merge the given arrays and find the sum 5 | of the two middle elements of the merged array. 6 | """ 7 | class Solution: 8 | def findMidSum(self, ar1, ar2, n): 9 | merged = sorted(ar1 + ar2) 10 | return merged[n-1] + merged[n] -------------------------------------------------------------------------------- /python/sum_to_digits.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/contest-problem/two-digit-sum/1/ 2 | 3 | def digitsSum(n): 4 | # Get single digits 5 | n = str(n) 6 | return (int(n[0])+int(n[1])) -------------------------------------------------------------------------------- /python/taking-input.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/taking-input-python/1/?track=fork-python&batchId=119 2 | def inPutCat(): 3 | a = input() 4 | b = input() 5 | c = input() 6 | print(a,b,c) ## comma is used as it automatically separates the variables by a space. 7 | ## + can also be used to concatenate but it would require manual spaces to print the words with spaces between them. 8 | -------------------------------------------------------------------------------- /python/welcome-abroard-python.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/welcome-aboard-python/1/?track=python-module-3&batchId=119 2 | def welcomeAboard(name): 3 | print ("Welcome", name) -------------------------------------------------------------------------------- /python/while-loop-in-python.py: -------------------------------------------------------------------------------- 1 | # https://practice.geeksforgeeks.org/problems/while-loop-in-python/1/?track=python-module-2&batchId=119 2 | def printInDecreasing(x): 3 | # Complete the code below 4 | while(x >= 0): 5 | print(x, end=" ") 6 | x -= 1 7 | 8 | --------------------------------------------------------------------------------