├── Codechef ├── Readme.md ├── DanceMoves.py ├── sod3.cpp ├── DIAGMOVE.cpp ├── ODD_GCD.cpp ├── count_subarrays.cpp ├── magicaldoors.cpp ├── fixed.cpp ├── nicepairs.cpp ├── Alternating_Subarray_prefix.cpp └── Add_and_divide.cpp ├── .gitignore ├── Hackerrank ├── Readme.md ├── Introduction │ ├── SayHelloWorldWithCPlusPlus.cpp │ ├── InputAndOutput.cpp │ ├── BasicDataTypes.cpp │ ├── Pointer.cpp │ ├── ArraysIntroduction.cpp │ ├── Functions.cpp │ ├── VariableSizedArrays.cpp │ ├── ConditionalStatements.cpp │ ├── ForLoop.cpp │ └── HourGlass.cpp ├── Strings │ ├── Strings.cpp │ └── StringStream.cpp ├── STL │ ├── VectorSort.cpp │ ├── MapsStl.cpp │ ├── VectorErase.cpp │ ├── PrintPretty.cpp │ ├── LowerBoundStl.cpp │ ├── SetsStl.cpp │ └── DequeStl.cpp ├── Class │ ├── Structs.cpp │ ├── ClassesAndObjects.cpp │ ├── InheritedCode.cpp │ └── ExceptionalServer.cpp ├── bstLowestcommoancs.app ├── dayofprogrammer.cpp ├── Inheritance │ ├── RectangleArea.cpp │ ├── InheritanceIntroduction.cpp │ └── MultiLevelInheritance.cpp ├── NumberLineJumps └── Other_Concepts │ ├── BitArray.cpp │ ├── PreprocessorSolution.cpp │ ├── OverloadOperators.cpp │ ├── ClassTemplateSpecialization.cpp │ ├── ClassTemplates.cpp │ └── heightofbinarytree.cpp ├── Leetcode ├── Readme.md ├── Cpp │ ├── 912. Sort an Array.cpp │ ├── 242. Valid Anagram.cpp │ ├── 387. First Unique Character in a String.cpp │ ├── 451. Sort Characters By Frequency.cpp │ ├── 877. Stone Game.cpp │ ├── 202.Happy Number.cpp │ ├── 704. Binary Search.cpp │ ├── Find Minimum in Rotated Sorted Array.cpp │ ├── 415. Add Strings.cpp │ ├── 204. Count Primes.cpp │ ├── 49. Group Anagram.cpp │ ├── 90. subset II.cpp │ ├── 926. Flip String to Monotone Increasing.cpp │ ├── invertbinarytree.cpp │ ├── 38. Count and Say.cpp │ ├── 73. Set Matrix Zeroes.cpp │ ├── 954. Array of Doubled Pairs.cpp │ ├── removeduplicates.cpp │ ├── 113. Path Sum II.cpp │ ├── 95. Unique Binary Search Trees II.cpp │ ├── 3. Longest Substring Without Repeating Characters.cpp │ ├── 542. 01 Matrix.cpp │ ├── 3sum.cpp │ └── 429. N-ary Tree Level Order Traversal.cpp ├── Java │ ├── arraytoBinarySearchTree.java │ ├── 303. Range Sum Query - Immutable.java │ ├── Two Sum IV - Input is a BST.java │ ├── 1. Two Sum.java │ ├── Array Nesting.java │ ├── Verify Preorder Serialization of a Binary Tree.java │ ├── Decode Ways.java │ ├── 7.Reverse Integer.java │ ├── max_depth_binary_tree.java │ ├── 137. Single Number II.java │ ├── Search A 2D Sorted Matrix.java │ ├── 3sum Closest.java │ ├── 36. Valid Sudoku.java │ ├── Count Good Nodes in Binary Tree.java │ ├── MyQueue.java │ ├── find_all_anagrams_in_string.java │ ├── Palindrome Partitioning II.java │ ├── 15. 3Sum.java │ ├── house_robber │ ├── MyStack.java │ ├── Remove Boxes.java │ └── PeakElement.java ├── findDuplicate.cpp ├── Majority-elem.cpp └── removeElement.cpp ├── DSA Java ├── helloworld.java ├── delete_head_without_pointer.java ├── permutations.java ├── kadane_algorithm.java ├── left_view.java ├── longest_increasing_subsequence.java ├── BinarySearch.java └── Bubblesort.java ├── test.js ├── DSA JavaScript └── helloworld.js ├── DSA C++ ├── Recurssion │ ├── Factorial.bin │ ├── Factorial.cpp │ ├── Fibonacci.cpp │ └── CheckSortedArray.cpp ├── Linkedlist │ ├── searchElement.bin │ └── linked List creation and traversal.cpp ├── Stack │ ├── stack_stl.cpp │ ├── balanced_parenthesis.cpp │ ├── stock_span.cpp │ ├── implicit_reverse_stack.cpp │ ├── reverse_stack.cpp │ └── reverse_stack_using_another_stack.cpp ├── trees.cpp ├── Pointers │ └── Pointers.cpp └── hashing.cpp ├── Geekforgeeks ├── G)Sorting │ ├── 5_stable_sort.cpp │ ├── 3_deque_sort_stl.cpp │ ├── 2_vector_sort_stl.cpp │ ├── 1_array_sort_stl.cpp │ ├── 26_minimum_diff_in_array.cpp │ ├── 4_own_sort.cpp │ ├── 12_intersection_of_sorted.cpp │ ├── 27_merge_overlapping_interval.cpp │ ├── 23_Chocolate_distbn.cpp │ ├── 6_bubble_sort.cpp │ ├── 7_selection_sort.cpp │ ├── 24_sort_array_two_types_elem.cpp │ ├── 28_Meeting_maximum_guests.cpp │ ├── 16_Lomuto_partition.cpp │ ├── 8_Insertion_sort.cpp │ ├── 9_Merge_sorted_array.cpp │ └── 15_Naive_partition.cpp ├── C)Recursion │ ├── 2_printing_the_binary_num.cpp │ ├── 11_sum_of_digits.cpp │ ├── 1_guessing_the_output.cpp │ ├── 4_printing_1_to_n.cpp │ ├── 7_factorial.cpp │ ├── 9_sum_of_n_numbers.cpp │ ├── 8_fibonacci.cpp │ ├── 3_printing_n_to_1.cpp │ ├── 15_josephus_problem.cpp │ ├── 5_tail_recursive_for_1ton.cpp │ ├── 6_tail_recursive_factorial.cpp │ ├── 13_subsets_of_strings.cpp │ ├── 17_printing_all_permuation.cpp │ ├── 16_subset_sum.cpp │ ├── 14_tower_of_hanoi.cpp │ ├── prac.cpp │ ├── 12_cutting_rope_problem.cpp │ └── 10_palindrome_string.cpp ├── A)Math │ ├── 1_Count_digits.cpp │ ├── 3_Factorial.cpp │ ├── 5_GCD.cpp │ ├── 6_LCM.cpp │ ├── 2_palindrome.cpp │ ├── 4_Zeros_in_factorial.cpp │ ├── 11_Iterative_power.cpp │ ├── 7_Prime.cpp │ ├── 9_All_factors.cpp │ ├── 10_Sieve_of_eratothenes.cpp │ └── 8_Prime_factors.cpp ├── D)Bitwise │ ├── 5_power_of_two.cpp │ ├── 1_Basics.cpp │ ├── 6_odd_one_occuring.cpp │ ├── 2_Check_kth_bit_set.cpp │ ├── 9_power_set_using_bits.cpp │ ├── 7_finding_the_missing_number.cpp │ ├── 3_count_set_bits.cpp │ ├── 4_look_up_table_for_set_bits.cpp │ └── 8_two_odd_occuring.cpp ├── B)Array │ ├── 1_searching.cpp │ ├── 6_sorted.cpp │ ├── 7_reverse.cpp │ ├── 16_Stonks.cpp │ ├── 19_Maximum_subarray_sum.cpp │ ├── 13_Maximum_diff_array.cpp │ ├── 4_largest.cpp │ ├── 10_Left_rotate_one.cpp │ ├── 3_deletion.cpp │ ├── 2_insertion.cpp │ ├── 9_zeros_to_end.cpp │ ├── 18_consecutive_ones.cpp │ ├── 26_N_bonacci.cpp │ ├── 20_longest_odd_even.cpp │ ├── 27_Prefix_sum_2.cpp │ ├── 23_MIN_flips.cpp │ ├── 12_Leaders_in_array.cpp │ ├── 11_Left_rotate_by_d.cpp │ ├── 14_Freq_sorted.cpp │ ├── 27_Prefix_sum_1.cpp │ ├── 8_removing_duplicates.cpp │ ├── 25_subarrray_with_given_sum.cpp │ ├── 15_freq_unsorted.cpp │ ├── 5_second_largest.cpp │ ├── 17_Trapping_rain.cpp │ ├── 21_max_circular_sum.cpp │ └── 24_window_sliding_technique.cpp ├── E)Matrices │ ├── 7_Printing_2D_array.cpp │ ├── 2_static_multi_dim_array.cpp │ ├── 1_multi_dimensional_array.cpp │ ├── 8_snake_pattern.cpp │ ├── 9_boundary_traversal.cpp │ ├── 10_transpose_of_matrix.cpp │ ├── 11_rotating_by_90.cpp │ ├── 6_vector_of_vectors.cpp │ ├── 5_array_of_vectors.cpp │ ├── 4_array_pointer_multi.cpp │ └── 12_spiral_transversal.cpp ├── H)Hashing │ ├── 15_frequencies_of_elements_of_array.cpp │ ├── 4_Collision_handling.txt │ ├── 14_Count_distinct_elements.cpp │ ├── 21_Length_of_longest_subarray_with_given_sum.cpp │ ├── 17_Union_of_two_unsorted_arrays.cpp │ ├── 16_Intersection_of_unsorted_elements.cpp │ ├── 19_Subarray_with_zero_sum.cpp │ ├── 18_Pair_with_given_sum.cpp │ ├── 2_direct_address_table.cpp │ ├── 20_Subarray_with_given_sum.cpp │ ├── 11_Open_add_vs_chaining.txt │ ├── 1_Intro_to_hashing.txt │ └── 3_Hash_functions.txt ├── Readme.md ├── F)Searching │ ├── 12_two_pointer.cpp │ ├── 14_count_pairs_with_given_sum.cpp │ ├── 11_unsorted_pair_with_sum.cpp │ ├── 6_counting_ones.cpp │ ├── 1_BS_iterative.cpp │ ├── 20_Allocating_min_pages_naive.cpp │ ├── 2_BS_recursive.cpp │ ├── 3_Index_of_1st_occ.cpp │ ├── 4_Index_of_last_occ.cpp │ ├── 7_square_root.cpp │ ├── 16_pythagorean_triplet.cpp │ ├── 15_counting_triplets_with_given_sum.cpp │ ├── 18_repeating_element.cpp │ ├── 10_peak_element.cpp │ ├── 13_two_pointer_triplet.cpp │ └── 19_repeating_elements.cpp ├── minimizeHeights.cpp └── sort012.cpp ├── PPT@Git-Github └── PPT_Link.txt ├── DSA Python ├── Recursion │ ├── Factorial.py │ ├── Check_Sorted_Array.py │ └── Fibonacci.py ├── Sorting algorithm │ ├── Insertion_sort.py │ └── Bubble_sort.py ├── ToH_DivideAndConquer.py ├── Depth_First _Traversal.py ├── Searching algorithm │ ├── Linear_search.py │ ├── Linear_search.txt │ └── Binary_search.txt └── Permutaions_using_backtracking.py ├── other_dev.html └── .github ├── feature_request.md └── bug_report.md /Codechef/Readme.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .cph 2 | -------------------------------------------------------------------------------- /Hackerrank/Readme.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Leetcode/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DSA Java/helloworld.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | console.log("testing --SJ"); 2 | -------------------------------------------------------------------------------- /DSA JavaScript/helloworld.js: -------------------------------------------------------------------------------- 1 | console.log("hello world"); 2 | -------------------------------------------------------------------------------- /DSA C++/Recurssion/Factorial.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ITESA-DYPCOE/Coffee-and-code/HEAD/DSA C++/Recurssion/Factorial.bin -------------------------------------------------------------------------------- /DSA C++/Linkedlist/searchElement.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ITESA-DYPCOE/Coffee-and-code/HEAD/DSA C++/Linkedlist/searchElement.bin -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/5_stable_sort.cpp: -------------------------------------------------------------------------------- 1 | //Stable sort ensures that the elements having same value appear in the same order as they appreared in the original array 2 | -------------------------------------------------------------------------------- /Hackerrank/Introduction/SayHelloWorldWithCPlusPlus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cout << "Hello, World!"; 6 | return 0; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /Leetcode/Cpp/912. Sort an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector sortArray(vector &nums) 5 | { 6 | sort(nums.begin(), nums.end()); 7 | return nums; 8 | } 9 | }; -------------------------------------------------------------------------------- /Leetcode/Java/arraytoBinarySearchTree.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public TreeNode sortedArrayToBST(int[] nums) 4 | { 5 | int a = 0; 6 | int b = nums.length - 1; 7 | return helper(nums, a, b); 8 | } -------------------------------------------------------------------------------- /PPT@Git-Github/PPT_Link.txt: -------------------------------------------------------------------------------- 1 | //sorry guys, i could'nt download the PPT, it requires premium pack! 2 | 3 | you can view the ppt at this link: 4 | https://slides.com/shubhamjadhavsj/deck-872b1d/fullscreen 5 | 6 | //happy contributing! -------------------------------------------------------------------------------- /DSA Python/Recursion/Factorial.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | fact=1 3 | for i in range(1,n+1): 4 | fact=fact*i 5 | return fact 6 | 7 | 8 | 9 | num=int(input("Enter number to find its Factorial : ")) 10 | print(factorial(num)) 11 | -------------------------------------------------------------------------------- /Codechef/DanceMoves.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | lst=[] 3 | for _ in range(n): 4 | lst.append(list(map(int,input().split()))) 5 | for i in lst: 6 | diff = i[1]-i[0] 7 | quo = diff // 2 8 | rem = diff % 2 9 | if rem == 0 : 10 | print (quo) 11 | else : 12 | print (quo+2) -------------------------------------------------------------------------------- /Geekforgeeks/C)Recursion/2_printing_the_binary_num.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void binary(int n) 4 | { 5 | if(n==0) 6 | return ; 7 | 8 | binary(n/2); 9 | cout< 2 | #define ll long long 3 | using namespace std; 4 | long long digits(int n) 5 | { 6 | return floor(log10(n)+1); 7 | } 8 | int main(){ 9 | int n; 10 | cin>>n; 11 | cout< 2 | using namespace std; 3 | int func(int n) 4 | { 5 | if(n<=9) 6 | { 7 | return n; 8 | } 9 | return n%10+func(n/10); 10 | } 11 | int main() 12 | { 13 | cout< 2 | using namespace std; 3 | void func(int n) 4 | { 5 | if(n==0) 6 | return ; 7 | 8 | cout< 2 | using namespace std; 3 | void func(int n) 4 | { 5 | if (n == 0) 6 | { 7 | return; 8 | } 9 | func(n - 1); 10 | cout< 2 | using namespace std; 3 | int func(int n,int k=1) 4 | { 5 | if(n==0||n==1) 6 | { 7 | return k; 8 | } 9 | return func(n-1,n*k); 10 | } 11 | int main() 12 | { 13 | cout< 2 | using namespace std; 3 | bool power_of_two(int n) 4 | { 5 | 6 | return (n!=0)&&((n&(n-1))==0); 7 | } 8 | int main(){ 9 | cout< 2 | using namespace std; 3 | int func(int n,int k=0) 4 | { 5 | if (n == 0) 6 | { 7 | return k; 8 | } 9 | return func(n - 1,k+n); 10 | } 11 | int main() 12 | { 13 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int a, b, c; 11 | cin >> a >> b >> c; 12 | cout << a + b + c << endl; 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Geekforgeeks/C)Recursion/8_fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int func(int n) 4 | { 5 | if(n==0) 6 | return 0; 7 | if(n==1) 8 | return 1; 9 | else 10 | return func(n-1)+func(n-2); 11 | } 12 | int main() 13 | { 14 | cout< 2 | using namespace std; 3 | void func(int n) 4 | { 5 | if(n==0) 6 | { 7 | cout< 2 | using namespace std; 3 | int josephus(int n,int k) 4 | { 5 | if(n==1) 6 | { 7 | return 0; 8 | } 9 | 10 | return (josephus(n-1,k)+k)%n; 11 | } 12 | int main(){ 13 | cout<<"PERSON LEFT :"< 2 | using namespace std; 3 | void func(int n,int k=1) 4 | { 5 | if (k==n) 6 | { 7 | cout< list 3 | def check_sort(a,size): 4 | # If it was the first element return true 5 | if size==0: 6 | return True 7 | elif a[size-1]>a[size]: 8 | return False 9 | return check_sort(a,size-1) 10 | 11 | a=[1,2,3,4,5,6,7,9,8,10] 12 | print(check_sort(a,len(a)-1)) 13 | -------------------------------------------------------------------------------- /Hackerrank/Introduction/BasicDataTypes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | long l; 8 | char c; 9 | float f; 10 | double d; 11 | scanf("%d%ld%*c%c%f%lf", &n, &l, &c, &f, &d); 12 | printf("%d\n%ld\n%c\n%.3f\n%.9lf\n", n , l, c, f, d); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Geekforgeeks/A)Math/3_Factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | long long factorial(int n) 5 | { 6 | if(n==0) 7 | { 8 | return 1; 9 | } 10 | return n*factorial(n-1); 11 | } 12 | int main(){ 13 | int n; 14 | cin>>n; 15 | cout< 2 | #define ll long long 3 | using namespace std; 4 | long long gcd(int a,int b) 5 | { 6 | if(b==0) 7 | { 8 | return a; 9 | } 10 | return gcd(b,a%b); 11 | } 12 | int main() 13 | { 14 | int n,m; 15 | cin >> n>>m; 16 | cout << gcd(n,m) << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Geekforgeeks/C)Recursion/6_tail_recursive_factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int func(int n,int k=1) 4 | { 5 | if (n == 0||n==1) 6 | { 7 | k=k*n; 8 | return k; 9 | } 10 | k=k*n; 11 | return func(n - 1,k); 12 | } 13 | int main() 14 | { 15 | cout< 3 | #include 4 | using namespace std; 5 | int main(){ 6 | 7 | stack s; 8 | 9 | for(int i=0;i<=5;i++){ 10 | s.push(i); 11 | } 12 | 13 | //print stack 14 | while(!s.empty()){ 15 | cout< 2 | 3 | 4 | 5 | 6 | 7 | Personal Portfolio 8 | 9 | 10 |

Heyy there, other dev here!

11 | 12 | 13 | -------------------------------------------------------------------------------- /Hackerrank/Strings/Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | string s1, s2; 7 | cin >> s1 >> s2; 8 | cout << s1.size() << ' '<< s2.size() << endl; 9 | cout << s1 + s2 << endl; 10 | char t = s1[0]; 11 | s1[0] = s2 [0]; 12 | s2[0] = t; 13 | cout << s1 << ' ' << s2; 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Geekforgeeks/C)Recursion/13_subsets_of_strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void func(string s,string sub="",size_t k=0) 4 | { 5 | if(k==s.length()) 6 | { 7 | cout< 2 | 3 | void update(int *a,int *b) { 4 | 5 | int t = *a; 6 | *a = *a + *b; 7 | *b = t > *b ? t - *b : *b - t; 8 | } 9 | 10 | int main() { 11 | int a, b; 12 | int *pa = &a, *pb = &b; 13 | 14 | scanf("%d %d", &a, &b); 15 | update(pa, pb); 16 | printf("%d\n%d", a, b); 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /DSA C++/Recurssion/Factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int n) 5 | { 6 | if (n == 0) 7 | { 8 | return 1; 9 | } 10 | int smallo = factorial(n - 1); 11 | return n * smallo; 12 | } 13 | 14 | int main() 15 | { 16 | int n; 17 | cin >> n; 18 | int output = factorial(n); 19 | cout << output << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/1_searching.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | int op(int arr[], int size,int x) 5 | { 6 | for(ll i=0;i 2 | using namespace std; 3 | unsigned int getFirstSetBit(int n) 4 | { 5 | int res = 1; 6 | while (n > 0) 7 | { 8 | if (n & 1) 9 | return res; 10 | 11 | n=n >> 1; 12 | res++; 13 | } 14 | return 0; 15 | } 16 | int main() 17 | { 18 | cout<; 2 | 3 | 4 | class Solution { 5 | public: 6 | int findDuplicate(vector& nums) { 7 | sort(nums.begin(),nums.end()); 8 | for(int i=1;i 2 | using namespace std; 3 | int odd_one(vector arr) 4 | { 5 | int x=arr[0]; 6 | for(int i=1;i v; 14 | v={4,3,3,4,4,5,5}; 15 | cout<<"ODD ONE OCCURING :"< 2 | #define ll long long 3 | using namespace std; 4 | bool op(int arr[], int size) 5 | { 6 | for(ll i=0;iarr[i+1]) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | int arr[] = {3,26,100}; 16 | int size = sizeof(arr)/sizeof(arr[0]); 17 | cout< 2 | #define ll long long 3 | using namespace std; 4 | long long gcd(int a,int b) 5 | { 6 | if(b==0) 7 | { 8 | return a; 9 | } 10 | return gcd(b,a%b); 11 | } 12 | ll lcm(int a,int b) 13 | { 14 | return (a*b)/gcd(a,b); 15 | } 16 | int main() 17 | { 18 | int n,m; 19 | cin >> n>>m; 20 | cout << lcm(n,m) << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /DSA C++/Recurssion/Fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fib(int n) 5 | { 6 | if (n == 0) 7 | { 8 | return 0; 9 | } 10 | if (n == 1) 11 | { 12 | return 1; 13 | } 14 | int so1 = fib(n - 1); 15 | int so2 = fib(n - 2); 16 | return so1 + so2; 17 | } 18 | 19 | int main() 20 | { 21 | int n; 22 | cin >> n; 23 | cout << fib(n) << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/3_deque_sort_stl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | deque v{13,56,34,12}; 5 | sort(v.begin(),v.end()); //increasing func 6 | for(auto elem:v) 7 | cout<()); //decreasing func 10 | for(auto elem:v) 11 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int n; 11 | cin >> n; 12 | int arr [n]; 13 | for (int i = 0; i < n; i ++) { 14 | cin >> arr[i]; 15 | } 16 | for (int i = n-1; i >= 0; i --) { 17 | cout << arr[i] << " "; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Geekforgeeks/C)Recursion/17_printing_all_permuation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void perm(string s,int i=0) 4 | { 5 | if(i==s.length()-1) 6 | { 7 | cout< 2 | using namespace std; 3 | bool func_left_shift(int n,int k){ 4 | return (n&(1<<(k-1))!=0); 5 | } 6 | bool func_right_shift(int n,int k){ 7 | return (n>>(k-1))%2==1; 8 | } 9 | int main(){ 10 | int N=0; 11 | int K=2; 12 | cout< 2 | using namespace std; 3 | int main(){ 4 | vector arr{10,20,5,7}; 5 | int n=4; 6 | sort(arr.begin(),arr.end()); //increasing func 7 | for(auto elem:arr) 8 | cout<()); //decreasing func 11 | for(auto elem:arr) 12 | cout<> hash('z' + 1, {0, 0}); 7 | for (char c : s) 8 | hash[c] = {hash[c].first + 1, c}; 9 | 10 | sort(hash.begin(), hash.end()); 11 | 12 | string res = ""; 13 | for (auto p : hash) 14 | res = string(p.first, p.second) + res; 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/7_reverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void op(int arr[], int size) 5 | { 6 | for(ll i=0;i<(size/2);i++) 7 | { 8 | swap(arr[i],arr[size-1-i]); 9 | } 10 | } 11 | int main() 12 | { 13 | int arr[] = {1,2,3,4,5}; 14 | int size = sizeof(arr)/sizeof(arr[0]); 15 | op(arr, size); 16 | for(ll i=0;i 2 | using namespace std; 3 | int sub_sum(int arr[],int n,int sum) 4 | { 5 | if(n==0) 6 | { 7 | return (sum==0)?1:0; 8 | } 9 | return sub_sum(arr,n-1,sum-arr[n-1])+sub_sum(arr,n-1,sum); 10 | } 11 | int main() 12 | { 13 | int arr[]={10,20,15}; 14 | int sum=25; 15 | cout<<"NUMBER OF SUBSETS WITH SUM "< 2 | #define ll long long 3 | using namespace std; 4 | long long palindrome(int n) 5 | { 6 | int rev=0,cpy=n,temp=0; 7 | while(cpy>0) 8 | { 9 | temp=cpy%10; 10 | rev=rev*10+temp; 11 | cpy/=10; 12 | } 13 | if(rev==n) 14 | return 1; 15 | else 16 | return 0; 17 | } 18 | int main(){ 19 | int n; 20 | cin>>n; 21 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int max_of_four(int a, int b, int c, int d) { 6 | int t1 = a > b ? a : b; 7 | int t2 = c > d ? c : d; 8 | int max = t1 > t2 ? t1 : t2; 9 | return max; 10 | } 11 | 12 | 13 | int main() { 14 | int a, b, c, d; 15 | scanf("%d %d %d %d", &a, &b, &c, &d); 16 | int ans = max_of_four(a, b, c, d); 17 | printf("%d", ans); 18 | 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Geekforgeeks/C)Recursion/14_tower_of_hanoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void tower_of_hanoi(int k,char source='A',char auxillary='B',char destination='C') 4 | { 5 | if(k==0) 6 | { 7 | return ; 8 | } 9 | tower_of_hanoi(k-1,source,destination,auxillary); 10 | cout<<"MOVING DISK "< 2 | using namespace std; 3 | void power_set(string s) 4 | { 5 | for(int i=0;i<=(pow(2,s.length())-1);i++) 6 | { 7 | string temp=""; 8 | for(int j=0;j 2 | using namespace std; 3 | const int R=3,C=2; 4 | void print(int arr[R][C]) 5 | { 6 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int size, a; 11 | cin >> size; 12 | vector v; 13 | for(int i = 0; i < size; i ++) { 14 | cin >> a; 15 | v.push_back(a); 16 | } 17 | sort(v.begin(), v.end()); 18 | for(int i = 0; i < size; i ++) { 19 | cout << v[i] << " "; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /DSA C++/Recurssion/CheckSortedArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool checkSort(int a[], int size) 5 | { 6 | if (size == 0 || size == 1) 7 | { 8 | return true; 9 | } 10 | 11 | if (a[0] > a[1]) 12 | { 13 | return false; 14 | } 15 | bool issmallsort = checkSort(a + 1, size - 1); 16 | return issmallsort; 17 | } 18 | 19 | int main() 20 | { 21 | int a[] = {1, 2, 6, 4, 5}; 22 | cout << checkSort(a, 5) << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Geekforgeeks/D)Bitwise/7_finding_the_missing_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int missing(vector arr) 4 | { 5 | int x=arr[0]; 6 | for(int i=1;i v; 18 | v={1,4,3}; 19 | cout<<"MISSING ELEMENT IS :"< 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | ll size=arr.size(); 7 | ll profit=0; 8 | for(ll i=0;i arr{1,5,3,1,2,8}; 20 | op(arr); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Geekforgeeks/A)Math/4_Zeros_in_factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | long long zero_factorial(int n) 5 | { 6 | ll ans = 0,cpy; 7 | for (ll i = 5; i <= n; i *= 5) 8 | { 9 | cpy=n; 10 | if(cpy/i!=0) 11 | ans=ans+cpy/i; 12 | else 13 | break; 14 | } 15 | return ans; 16 | } 17 | int main() 18 | { 19 | int n; 20 | cin >> n; 21 | cout << zero_factorial(n) << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /DSA C++/trees.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | struct node *left,*right; 7 | node(int d){ 8 | data=d; 9 | left=right=NULL; 10 | } 11 | }; 12 | 13 | int main(){ 14 | struct node *root=new node(6); 15 | root->right=new node(8); 16 | root->left=new node(4); 17 | root->left->left=new node(2); 18 | root->left->right=new node(5); 19 | root->right->left=new node(7); 20 | root->right->right=new node(9); 21 | } -------------------------------------------------------------------------------- /Geekforgeeks/A)Math/11_Iterative_power.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | double power(int x,int n) 5 | { 6 | ll temp=x; 7 | double ans=1; 8 | while(n!=0) 9 | { 10 | if(n%2!=0) 11 | { 12 | ans=ans*temp; 13 | } 14 | temp=temp*temp; 15 | n=n/2; 16 | } 17 | return ans; 18 | } 19 | int main() 20 | { 21 | int x,n; 22 | cin >> x>>n; 23 | cout< 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | ll size=arr.size(); 7 | ll max_sum=arr.at(0); 8 | ll maxi=arr.at(0); 9 | for(ll i=1;i arr{-6,-1,-8}; 19 | op(arr); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Geekforgeeks/C)Recursion/prac.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int strg(int n,int a,int b,int c) 4 | { 5 | if(n==0) 6 | return 0; 7 | if(n<0) 8 | return -1; 9 | 10 | int A=strg(n-a,a,b,c); 11 | int B=strg(n-b,a,b,c); 12 | int C=strg(n-c,a,b,c); 13 | 14 | int maxi=max(A,B); 15 | maxi=max(C,maxi); 16 | 17 | if(maxi==-1) 18 | return -1; 19 | 20 | return maxi+1; 21 | } 22 | int main(){ 23 | cout<=0 and array[j] > cur: 6 | j -= 1 7 | array.insert(j+1, cur) 8 | return array 9 | 10 | 11 | import random 12 | array=[1,100,9,10,30,1,1,1,-15,2,3,4,5,6,7,8,9,10] 13 | random.shuffle(array) # To shuffle the list 14 | 15 | print("List before sorting : ", array) 16 | 17 | print("List after sorting : ",insertion_sort(array)) -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/13_Maximum_diff_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | ll size=arr.size(); 7 | ll diff=arr.at(1)-arr.at(0),mine=arr.at(0); 8 | for(ll i=0;i arr{7,9,5,6,3,2}; 19 | op(arr); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/1_array_sort_stl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | //Sort can be used in container which allow random memory access : array,vector,deque 5 | int arr[]={10,20,5,7}; 6 | int n=4; 7 | sort(arr,arr+n); //increasing func 8 | for(auto elem:arr) 9 | cout<()); //decreasing func 12 | for(auto elem:arr) 13 | cout< &piles) { 5 | if(i > j) 6 | return 0; 7 | if(dp[i][j] != -1) 8 | return dp[i][j]; 9 | return dp[i][j] = max(piles[i] - help(i + 1, j, piles), piles[j] - help(i, j - 1, piles)); 10 | } 11 | bool stoneGame(vector& piles) { 12 | int n = piles.size(); 13 | memset(dp, -1, sizeof dp); 14 | return help(0, n - 1, piles) > 0; 15 | } 16 | }; -------------------------------------------------------------------------------- /DSA Python/ToH_DivideAndConquer.py: -------------------------------------------------------------------------------- 1 | # Recursive Python function to solve tower of hanoi 2 | 3 | def TowerOfHanoi(n , from_rod, to_rod, aux_rod): 4 | if n == 1: 5 | print("Move disk 1 from rod",from_rod,"to rod",to_rod) 6 | return 7 | TowerOfHanoi(n-1, from_rod, aux_rod, to_rod) 8 | print("Move disk",n,"from rod",from_rod,"to rod",to_rod) 9 | TowerOfHanoi(n-1, aux_rod, to_rod, from_rod) 10 | 11 | # Driver code 12 | n = 4 13 | TowerOfHanoi(n, 'A', 'C', 'B') 14 | # A, C, B are the name of rods 15 | 16 | # Contributed By Harshit Agrawal 17 | -------------------------------------------------------------------------------- /Geekforgeeks/A)Math/7_Prime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | long long prime(int n) 5 | { 6 | if(n==1)return 0; 7 | if(n==2||n==3)return 1; 8 | if(n%2==0||n%3==0)return 0; 9 | 10 | for(ll i=5;i*i<=n;i+=6) 11 | { 12 | if(n%i==0||n%(i+2)==0) 13 | { 14 | return 0; 15 | } 16 | } 17 | return 1; 18 | } 19 | int main() 20 | { 21 | int n,m; 22 | cin >> n; 23 | cout << prime(n) << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Hackerrank/Class/Structs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct Student { 9 | int age; 10 | char first_name[51]; 11 | char last_name[51]; 12 | int standard; 13 | }; 14 | 15 | int main() { 16 | Student st; 17 | 18 | cin >> st.age >> st.first_name >> st.last_name >> st.standard; 19 | cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/4_largest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | int op(ll arr[], int size) 5 | { 6 | ll max=size-1; 7 | for(ll i=size-1;i>=0;i--) 8 | { 9 | if(arr[i]>arr[max]) 10 | { 11 | max=i; 12 | } 13 | } 14 | return max; 15 | } 16 | int main() 17 | { 18 | ll arr[] = {30,20,101,69,111,200,1111111111,10000011100}; 19 | int size = sizeof(arr)/sizeof(arr[0]); 20 | int pos=op(arr, size)+1; 21 | cout< set = new HashSet<>(); 4 | return helper(root, set, k); 5 | } 6 | 7 | private boolean helper(TreeNode root, Set set, int k) { 8 | if (root == null) 9 | return false; 10 | if (set.contains(k - root.val)) 11 | return true; 12 | set.add(root.val); 13 | return helper(root.left, set, k) || helper(root.right, set, k); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/10_Left_rotate_one.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | int size=arr.size(); 7 | int temp=arr.at(0); 8 | for(ll i=0;i arr{1,2,3,4,5}; 17 | op(arr); 18 | for(ll i=0;i 2 | #define ll long long 3 | #define rep(i, t) for (ll i = 0; i < t; i++) 4 | using namespace std; 5 | void factors(int n) 6 | { 7 | ll i; 8 | for (i = 1; i * i < n; i++) 9 | { 10 | if (n % i == 0) 11 | { 12 | cout << i << endl; 13 | } 14 | } 15 | for (; i >= 1; i--) 16 | { 17 | if (n % i == 0) 18 | cout << n/i << endl; 19 | } 20 | } 21 | int main() 22 | { 23 | factors(12); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Geekforgeeks/A)Math/10_Sieve_of_eratothenes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void sieve(int n) 5 | { 6 | vector isPrime(n+1,true); 7 | for(ll i=2;i<=n;i++) 8 | { 9 | if(isPrime.at(i)) 10 | { 11 | cout<> n; 23 | sieve(n); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/26_minimum_diff_in_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int getmindiff(int arr[],int n) 4 | { 5 | sort(arr,arr+n); 6 | int min_diff=INT_MAX; 7 | for(int i=1;i 2 | using namespace std; 3 | int func(int n,int a,int b,int c) 4 | { 5 | if(n==0) 6 | { 7 | return 0; 8 | } 9 | if(n<0) 10 | { 11 | return -1; 12 | } 13 | int maxi; 14 | int A=func(n-a,a,b,c); 15 | int B=func(n-b,a,b,c); 16 | int C=func(n-c,a,b,c); 17 | maxi=max(A,B); 18 | maxi=max(maxi,C); 19 | 20 | if(maxi==-1) 21 | return -1; 22 | 23 | return maxi+1; 24 | } 25 | int main(){ 26 | cout< 2 | #define ll long long 3 | using namespace std; 4 | int op(int arr[], int size,int pos) 5 | { 6 | arr[pos-1]=0; 7 | ll i; 8 | for(i=pos-1;i map = new HashMap(); 5 | for(int i = 0; i < nums.length; i++) { 6 | if(map.containsKey(target - nums[i])) { 7 | result[1] = i; 8 | result[0] = map.get(target - nums[i]); 9 | return result; 10 | } 11 | map.put(nums[i], i); 12 | } 13 | return result; 14 | } 15 | } -------------------------------------------------------------------------------- /Geekforgeeks/E)Matrices/2_static_multi_dim_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int m = 3, n = 2; 6 | int arr[m][n]; //Variable sized array 7 | for (int i = 0; i < m; i++) 8 | { 9 | for (int j = 0; j < n; j++) 10 | { 11 | arr[i][j] = i + j; 12 | } 13 | } 14 | for (int i = 0; i < m; i++) 15 | { 16 | for (int j = 0; j < n; j++) 17 | { 18 | cout << arr[i][j] << " "; 19 | } 20 | cout << endl; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/2_insertion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | int op(int arr[], int n,int cap,int pos,int elem) 5 | { 6 | if(n==cap) 7 | return -1; 8 | 9 | for(ll i=cap-1;i>(pos-1);i--) 10 | { 11 | arr[i]=arr[i-1]; 12 | } 13 | arr[pos-1]=elem; 14 | return n+1; 15 | } 16 | int main() 17 | { 18 | int arr[6] = {1,2,3,4,5,0}; 19 | int size = 6; 20 | int size_n=op(arr,5,6,6,11); 21 | for(ll i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | long long int t; 8 | cin>>t; 9 | while(t) 10 | { 11 | t=t-1; 12 | long long int l,r; 13 | cin>>l>>r; 14 | if(l%3==0) 15 | { 16 | l=l+0; 17 | } 18 | else 19 | { 20 | l=l+(3-(l%3)); 21 | } 22 | r=r-(r%3); 23 | long long int diff=(r-l); 24 | cout<<(diff/3)+1<& arr) { 5 | 6 | int n = arr.size(); 7 | int count = 1; int num = arr[0]; 8 | for(int i = 1; i 2 | #define ll long long 3 | using namespace std; 4 | void op(int arr[], int size) 5 | { 6 | ll cntr=0; 7 | for(ll i=0;i 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | ll size=arr.size(); 7 | ll ones=0,maxo=0; 8 | for(ll i=0;i arr{0,0,1,1,1,1,0,0,0,0,1,1,1}; 25 | op(arr); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Geekforgeeks/E)Matrices/1_multi_dimensional_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int arr[3][2]={10,20,30,40,50,60}; //for 2D arraays we need not write the internal brackets 5 | for(int i=0;i<3;i++) 6 | { 7 | for(int j=0;j<2;j++) 8 | { 9 | cout<data && v2data) 15 | { 16 | return lca(root->left,v1,v2); 17 | } 18 | else 19 | if(v1>root->data && v2>root->data) 20 | { 21 | return lca(root->right,v1,v2); 22 | } 23 | else { 24 | return root; 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Codechef/DIAGMOVE.cpp: -------------------------------------------------------------------------------- 1 | //roshan raj 2 | #include 3 | using namespace std; 4 | #define int long long 5 | #define all(v) v.begin(),v.end() 6 | const int mod = 1e9 + 7; 7 | 8 | 9 | int32_t main() 10 | { 11 | ios::sync_with_stdio(false); 12 | cin.tie(NULL); 13 | int t; 14 | cin>>t; 15 | while(t) 16 | { 17 | t=t-1; 18 | int x,y; 19 | cin>>x>>y; 20 | if((abs(x)+abs(y))%2==0) 21 | { 22 | cout<<"YES"< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector parseInts(string str) { 7 | stringstream ss(str); 8 | int a; 9 | char ch; 10 | vector v; 11 | while(ss >> a) 12 | { 13 | v.push_back(a); 14 | ss >> ch; 15 | } 16 | return v; 17 | } 18 | 19 | int main() { 20 | string str; 21 | cin >> str; 22 | vector integers = parseInts(str); 23 | for(int i = 0; i < integers.size(); i++) { 24 | cout << integers[i] << "\n"; 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/26_N_bonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void op(ll N,ll n) 5 | { 6 | vector ans(n); 7 | for(ll i=0;i 2 | using namespace std; 3 | void frequenciesOfArray(int arr[],int size) 4 | { 5 | unordered_map freq; 6 | for(int i=0;i 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | ll size=arr.size(); 7 | ll maxi=0;ll len=1; 8 | for(ll i=1;i arr{10,12,8,4}; 26 | op(arr); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Geekforgeeks/H)Hashing/4_Collision_handling.txt: -------------------------------------------------------------------------------- 1 | Collision: when two keys maps to the same hash value in the hash table. 2 | 3 | Birthday paradox: if there 23 ppl in a room then there is 50% prob that two ppl have same birthday, with 70 ppl it is 99.9 percent. 4 | 5 | If we know keys in advance, then we use perfect hashing.(advance methods) 6 | 7 | If we do not know keys, then we use one of the following: 8 | 1)Chaning : we make a chain of colliding values. 9 | 2)Open addressing: if some position is occupied we try to put the value in next unoccupied position. 10 | *Linear probing 11 | *Quadractic probing 12 | *Double hashing. -------------------------------------------------------------------------------- /DSA C++/Stack/balanced_parenthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | bool isValid(char *s){ 6 | stack stk; 7 | for(int i=0;s[i]!='\0';i++){ 8 | char ch=s[i]; 9 | if(ch=='(')stk.push(ch); 10 | else if(ch==')'){ 11 | if(stk.empty() || stk.top()!='('){ 12 | return false; 13 | } 14 | stk.pop(); 15 | } 16 | } 17 | return stk.empty(); 18 | } 19 | int main(){ 20 | 21 | char s[100]="((a+b)+(c-d+e)*)(f"; 22 | if(isValid(s))cout<<"true"; 23 | else cout<<"False"; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Geekforgeeks/H)Hashing/14_Count_distinct_elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int countDistinctElement(int arr[],int size) 4 | { 5 | unordered_set s(arr,arr+size); /*cpp has contructors which allow us to directly instantiate 6 | arrays to unordered set */ 7 | return s.size(); 8 | /* 9 | Time complexity : theta(n) 10 | Space complexity : O(n) 11 | */ 12 | } 13 | int main(){ 14 | int arr[]={4,5,2,5,6,3,4,6,2,2,4}; 15 | int size=sizeof(arr)/sizeof(int); 16 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int y; 7 | cin >> y; 8 | if(y<1918){ 9 | if(y%4==0)cout<<"12.09."< 2 | #define ll long long 3 | using namespace std; 4 | void op(vector arr) 5 | { 6 | ll n=arr.size(); 7 | ll sum=0; 8 | for(ll i=0;i arr{3,6,-9,8,20,6,2}; 27 | op(arr); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Leetcode/Java/Array Nesting.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int arrayNesting(int[] nums) { 3 | boolean[] visited = new boolean[nums.length]; 4 | int res = 0; 5 | for (int i = 0; i < nums.length; i++) { 6 | if (!visited[i]) { 7 | int start = nums[i], count = 0; 8 | do { 9 | start = nums[start]; 10 | count++; 11 | visited[start] = true; 12 | } 13 | while (start != nums[i]); 14 | res = Math.max(res, count); 15 | } 16 | } 17 | return res; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/23_MIN_flips.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | ll n = arr.size(); 7 | for (ll i = 1; i < n; i++) 8 | { 9 | if (arr[i] != arr[i - 1]) 10 | { 11 | if (arr[i] != arr[0]) 12 | { 13 | cout << "From " << i + 1; 14 | } 15 | else 16 | { 17 | cout<< " to "<< i< arr{1,1,0,0,1,0,0,0,0,0,0,0,1,1,1}; 25 | op(arr); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/12_two_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int sum(int arr[],int size,int x) 4 | { 5 | int l=0,r=size-1; 6 | int cntr=0; 7 | while(lx) 11 | r--; 12 | else if(sum 0) { 6 | int d = n % 10; 7 | n = n /10; 8 | totalSum += d*d; 9 | } 10 | return totalSum; 11 | } 12 | bool isHappy(int n) { 13 | int slow = n; 14 | int fast = getNext(n); 15 | while(fast != 1 && slow != fast) { 16 | slow = getNext(slow); 17 | fast = getNext(getNext(fast)); 18 | } 19 | if(fast == 1) { 20 | return true; 21 | }else { 22 | return false; 23 | } 24 | } 25 | }; -------------------------------------------------------------------------------- /DSA Python/Searching algorithm/Linear_search.py: -------------------------------------------------------------------------------- 1 | # Time complexity of linear search is ---> O(n) 2 | 3 | # It is the simplest algorithm and also there is no any prerequisite for this algo 4 | # searching the target one by one till end 5 | 6 | def Linear_search(array, search): 7 | position = 0 8 | while position < len(array): 9 | if array[position] == search: 10 | return position 11 | position += 1 12 | return -1 13 | 14 | array=[20,99,1,2,3,4,5,6,7,8,9,10] 15 | search=3 16 | position=Linear_search(array,search) 17 | if position!=-1: 18 | print("the element is found at ",position) 19 | else: 20 | print("The element is not found") -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/4_own_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct point 4 | { 5 | int x, y; 6 | }; 7 | bool myCmp(point p1, point p2) 8 | { 9 | return p1.x > p2.x; 10 | } 11 | int main() 12 | { 13 | point arr[] = {{1, 2}, {4, 1}, {5, 2}, {3, 4}}; 14 | sort(arr, arr + 4, myCmp); //in the sort function we can pass our own function according to which it should sort 15 | for (auto elem : arr) 16 | cout << elem.x << " " << elem.y << endl; 17 | return 0; 18 | } 19 | //Internal working of sort func : time complexity is O(nlogn) and internally uses introsort(hybrid of quicksort,heapsort,insertion sort) 20 | -------------------------------------------------------------------------------- /Leetcode/Cpp/704. Binary Search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | int start = 0; 5 | int end = nums.size(); 6 | 7 | while(start <= end) { 8 | int mid = (start + end)/2; 9 | 10 | if(nums[mid] == target) { 11 | return mid; 12 | }else if(nums[mid] > target) { 13 | end = mid - 1; 14 | }else { 15 | start = mid + 1; 16 | } 17 | } 18 | if(nums[start] == target) { 19 | return start; 20 | }else { 21 | return -1; 22 | } 23 | } 24 | }; -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/12_Leaders_in_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | int size=arr.size(); 7 | int cur_ldr=INT_MIN; 8 | for(ll i=size-1;i>=0;i--) 9 | { 10 | if(arr.at(i)>cur_ldr) 11 | { 12 | cout< arr{7,10,4,3,6,5,2}; 21 | op(arr); 22 | // for(ll i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int n, q; 11 | cin >> n >> q; 12 | int* outer[n]; 13 | for(int i = 0; i < n; i ++) { 14 | int size; 15 | cin >> size; 16 | outer[i] = new int [size]; 17 | for (int j = 0; j < size; j ++) { 18 | cin >> outer [i][j]; 19 | } 20 | } 21 | 22 | for (int i = 0; i < q; i ++) { 23 | int j,k; 24 | cin >> j >> k; 25 | cout << outer[j][k] << endl; 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Leetcode/Java/Verify Preorder Serialization of a Binary Tree.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isValidSerialization(String preorder) { 3 | Stack stack = new Stack(); 4 | String[] arr = preorder.split(","); 5 | for(String token : arr) { 6 | if("#".equals(token)) { 7 | while(stack.size() >= 2 && "#".equals(stack.peek())) { 8 | stack.pop(); 9 | if(!"#".equals(stack.peek())) 10 | stack.pop(); 11 | } 12 | } 13 | stack.push(token); 14 | } 15 | return stack.size() == 1 && "#".equals(stack.pop()); 16 | } 17 | } -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/11_Left_rotate_by_d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void rev(vector &arr,int s,int n) 5 | { 6 | while(s &arr,int d) 14 | { 15 | int size=arr.size(); 16 | rev(arr,0,d-1); 17 | rev(arr,d,size-1); 18 | rev(arr,0,size-1); 19 | } 20 | int main() 21 | { 22 | vector arr{1,2,3,4,5,6,7,8,9,10}; 23 | op(arr,3); 24 | for(ll i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | int q; 12 | cin >> q; 13 | 14 | map m; 15 | 16 | for (int i = 0; i> t >> name; 21 | if (t == 1){ 22 | cin >> marks; 23 | m[name] += marks; 24 | } 25 | else if (t == 2) 26 | m.erase(name); 27 | else 28 | cout << m[name] << "\n"; 29 | } 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Leetcode/Java/Decode Ways.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numDecodings(String s) { 3 | int[] dp = new int[s.length() + 1]; 4 | 5 | dp[0] = 1; 6 | dp[1] = s.charAt(0) == '0' ? 0 : 1; 7 | for (int i = 2; i <= s.length(); i++) { 8 | int oneDigit = Integer.valueOf(s.substring(i - 1, i)); 9 | int twoDigit = Integer.valueOf(s.substring(i - 2, i)); 10 | if (oneDigit >= 1) { 11 | dp[i] += dp[i - 1]; 12 | } 13 | if (twoDigit >= 10 && twoDigit <= 26) { 14 | dp[i] += dp[i - 2]; 15 | } 16 | } 17 | return dp[s.length()]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Leetcode/Cpp/Find Minimum in Rotated Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int findMin(vector &nums) 5 | { 6 | int n = nums.size(); 7 | int high = n - 1; 8 | int low = 0; 9 | while (low < high) 10 | { 11 | if (nums[low] < nums[high]) 12 | { 13 | return nums[low]; 14 | } 15 | int mid = (low + high) / 2; 16 | if (nums[mid] >= nums[low]) 17 | { 18 | low = mid + 1; 19 | } 20 | else 21 | { 22 | high = mid; 23 | } 24 | } 25 | return nums[low]; 26 | } 27 | }; -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/14_Freq_sorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | ll size = arr.size(); 7 | ll prev = arr.at(0), freq = 1; 8 | for (ll i = 1; i < size; i++) 9 | { 10 | if (arr.at(i) == prev) 11 | freq++; 12 | else 13 | { 14 | cout << prev << " " << freq << endl; 15 | freq = 1; 16 | prev = arr.at(i); 17 | } 18 | } 19 | cout << prev << " " << freq << endl; 20 | return; 21 | } 22 | int main() 23 | { 24 | vector arr{10, 20, 20, 30, 30, 30,40}; 25 | op(arr); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/27_Prefix_sum_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void op(vector arr,int l,int r) 5 | { 6 | vector sum(arr.size()); 7 | sum.at(0)=arr.at(0); 8 | for(ll i=1;i arr{2,8,3,9,6,5,4}; 29 | op(arr,1,3); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Hackerrank/STL/VectorErase.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int size, a; 11 | cin >> size; 12 | vector v; 13 | 14 | for(int i = 0; i < size; i ++) { 15 | cin >> a; 16 | v.push_back(a); 17 | } 18 | 19 | int p; 20 | cin >> p; 21 | v.erase(v.begin() + p - 1); 22 | 23 | int m, n; 24 | cin >> m >> n; 25 | v.erase(v.begin() + m - 1, v.begin() + n -1); 26 | 27 | cout << v.size() << endl; 28 | for(int i = 0; i < v.size(); i ++) 29 | cout << v[i] << " "; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Codechef/ODD_GCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | #define all(v) v.begin(), v.end() 5 | const int mod = 1e9 + 7; 6 | 7 | int getSetBits(int n) { 8 | return log2(n & -n); 9 | } 10 | 11 | int32_t main() { 12 | ios::sync_with_stdio(false); 13 | cin.tie(NULL); 14 | int t; 15 | cin >> t; 16 | while (t--) { 17 | int n; 18 | cin >> n; 19 | int mini = INT_MAX; 20 | for (int i = 0; i < n; i++) { 21 | int x; 22 | cin >> x; 23 | int pos = getSetBits(x); 24 | mini = min(mini, pos); 25 | } 26 | cout << mini << "\n"; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/8_removing_duplicates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void op(int arr[], int size) 5 | { 6 | // sort(arr,arr+size); if we donot want any element no matter its index we include this. 7 | ll cntr=1; 8 | for(ll i=1;i 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | ll n = arr.size(); 7 | ll gsum=5; 8 | ll sum=0; 9 | ll s=0; 10 | for(ll i=0;igsum&&s arr{1,4,0,0,3,10,5}; 29 | op(arr); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Geekforgeeks/H)Hashing/21_Length_of_longest_subarray_with_given_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | #define rep(i, t) for (ll i = 0; i < t; i++) 4 | using namespace std; 5 | int LengthOfLongestSubarrayWithGivenSum(vector v,int given_sum) 6 | { 7 | unordered_map s; 8 | ll sum=0; 9 | for (auto elem : v) 10 | { 11 | sum += elem; 12 | if(s.find(sum-given_sum)) 13 | } 14 | return false; 15 | //Time complexity : O(n) 16 | //Auxillary space : O(n) 17 | } 18 | int main() 19 | { 20 | vector vect{3,2,5,6}; 21 | ll sum=10; 22 | cout << LengthOfLongestSubarrayWithGivenSum(vect,sum) << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Leetcode/Cpp/415. Add Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addStrings(string num1, string num2) { 4 | string res = ""; 5 | 6 | int i = num1.length() - 1; 7 | int j = num2.length() - 1; 8 | 9 | int carry = 0; 10 | 11 | while(i >= 0 || j >= 0 || carry != 0) { 12 | int ival = i >= 0 ? num1.at(i) - '0' : 0; 13 | int jval = j >= 0 ? num2.at(j) - '0' : 0; 14 | 15 | i--; 16 | j--; 17 | 18 | int sum = ival + jval + carry; 19 | res = to_string(sum % 10) + res; 20 | carry = sum / 10; 21 | } 22 | return res; 23 | } 24 | }; -------------------------------------------------------------------------------- /.github/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request🚀 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/14_count_pairs_with_given_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int sum(int arr[],int size,int x) 4 | { 5 | int l=0,r=size-1; 6 | int cntr=0; 7 | while(lx) 11 | r--; 12 | else if(sum 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int T; cin >> T; 7 | cout << setiosflags(ios::uppercase); 8 | cout << setw(0xf) << internal; 9 | while(T--) { 10 | double A; cin >> A; 11 | double B; cin >> B; 12 | double C; cin >> C; 13 | cout << hex << left << showbase << nouppercase; 14 | cout << (long long)A << endl; 15 | 16 | cout << dec << right << setw(15) << setfill('_') << showpos << fixed << setprecision(2); 17 | cout << B << endl; 18 | 19 | cout << uppercase << noshowpos << scientific << setprecision(9); 20 | cout << C << endl; 21 | 22 | 23 | } 24 | return 0; 25 | 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/11_unsorted_pair_with_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int sum(int arr[],int size,int x) 4 | { 5 | //We need to check whether there exists a pair with sum = x in the given array 6 | //For this we use hashing 7 | vector hash(size,0); 8 | for(int i=0;i 2 | using namespace std; 3 | int UnionOfArrays(int arr1[], int n1, int arr2[], int n2) 4 | { 5 | unordered_set s(arr1, arr1 + n1); 6 | for (int i = 0; i < n2; i++) 7 | { 8 | s.insert(arr2[i]); 9 | } 10 | for (auto elem : s) 11 | { 12 | cout << elem << endl; 13 | } 14 | return s.size(); 15 | } 16 | int main() 17 | { 18 | int arr1[] = {4, 5, 2, 5, 6}; 19 | int size1 = sizeof(arr1) / sizeof(int); 20 | int arr2[] = {4, 5, 2, 5, 6, 3, 4, 6, 2, 2, 4}; 21 | int size2 = sizeof(arr2) / sizeof(int); 22 | UnionOfArrays(arr1, size1, arr2, size2); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Hackerrank/Inheritance/RectangleArea.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Rectangle { 6 | protected: 7 | int width; 8 | int height; 9 | 10 | public: 11 | void display() { 12 | cout << width << ' ' << height << endl; 13 | } 14 | }; 15 | 16 | class RectangleArea : public Rectangle { 17 | public: 18 | void read_input() { 19 | cin >> width >> height; 20 | } 21 | void display() { 22 | cout << width * height; 23 | } 24 | }; 25 | 26 | 27 | int main() 28 | { 29 | RectangleArea r_area; 30 | 31 | r_area.read_input(); 32 | 33 | r_area.Rectangle::display(); 34 | 35 | r_area.display(); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Leetcode/Cpp/204. Count Primes.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int countPrimes(int n) 5 | { 6 | vector prime(n + 1, 1); 7 | prime[0] = 0; 8 | prime[1] = 0; 9 | int count = 0; 10 | for (int i = 2; i * i <= n; i++) 11 | { 12 | if (prime[i] == 1) 13 | { 14 | for (int j = i * i; j <= n; j += i) 15 | { 16 | prime[j] = 0; 17 | } 18 | } 19 | } 20 | for (int i = 2; i < n; i++) 21 | { 22 | if (prime[i] == 1) 23 | { 24 | count++; 25 | } 26 | } 27 | return count; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Geekforgeeks/C)Recursion/10_palindrome_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool func1(string s,string r="",int k=0) 4 | { 5 | if(k==s.length()) 6 | { 7 | if(r==s) 8 | return true; 9 | 10 | return false; 11 | } 12 | r=s.at(k)+r; 13 | return func1(s,r,k+1); 14 | } 15 | bool func2(string &s,int l,int r) 16 | { 17 | if(l>=r) 18 | { 19 | return true; 20 | } 21 | return (s.at(l)==s.at(r))&&func2(s,l+1,r-1); 22 | } 23 | int main() 24 | { 25 | cout< 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | sort(arr.begin(),arr.end()); 7 | ll size = arr.size(); 8 | ll prev = arr.at(0), freq = 1; 9 | for (ll i = 1; i < size; i++) 10 | { 11 | if (arr.at(i) == prev) 12 | freq++; 13 | else 14 | { 15 | cout << prev << " " << freq << endl; 16 | freq = 1; 17 | prev = arr.at(i); 18 | } 19 | } 20 | cout << prev << " " << freq << endl; 21 | return; 22 | } 23 | int main() 24 | { 25 | vector arr{100,40,50,10, 20, 20, 30, 30, 30,40}; 26 | op(arr); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Hackerrank/Inheritance/InheritanceIntroduction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | class Triangle{ 10 | public: 11 | void triangle(){ 12 | cout << "I am a triangle\n"; 13 | } 14 | }; 15 | 16 | class Isosceles : public Triangle{ 17 | public: 18 | void isosceles(){ 19 | cout << "I am an isosceles triangle\n"; 20 | } 21 | void description() { 22 | cout << "In an isosceles triangle two sides are equal\n"; 23 | } 24 | }; 25 | 26 | int main(){ 27 | Isosceles isc; 28 | isc.isosceles(); 29 | isc.description(); 30 | isc.triangle(); 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/6_counting_ones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int count_ones(int arr[],int n) 4 | { 5 | int l=0; 6 | int r=n-1; 7 | while(l<=r) 8 | { 9 | int mid=(l+r)/2; 10 | if(arr[mid]==0) 11 | { 12 | l=mid+1; 13 | } 14 | else 15 | { 16 | if(mid==0||arr[mid-1]==0) 17 | return n-mid; 18 | else 19 | r=mid-1; 20 | } 21 | } 22 | return 0; 23 | //time complexity: O(logn) 24 | //auxillary space: O(1) 25 | } 26 | int main(){ 27 | int arr[]={0,0,0,1,1}; 28 | int size=sizeof(arr)/sizeof(int); 29 | cout<0){ 12 | rev=(rev*10) +(x%10); 13 | x=x/10; 14 | } 15 | if(rev> Integer.MAX_VALUE){ 16 | return 0; 17 | } 18 | return neg ? (int)(-1*rev) : (int)rev; 19 | } 20 | } 21 | public static void main(String args[]) 22 | { 23 | Solution s=new Solution(); 24 | int n=123; 25 | int result=s.reverse(n); 26 | System.out.println(result); 27 | 28 | 29 | } 30 | 31 | 32 | } -------------------------------------------------------------------------------- /Leetcode/Java/max_depth_binary_tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public int maxDepth(TreeNode root) { 18 | if(root==null) 19 | return 0; 20 | int lh=maxDepth(root.left); 21 | int rh=maxDepth(root.right); 22 | 23 | int height=lh>rh?lh+1:rh+1; 24 | 25 | return height; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Leetcode/Cpp/49. Group Anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> groupAnagrams(vector& strs) { 4 | ios_base::sync_with_stdio(false); 5 | cin.tie(NULL); 6 | 7 | unordered_map> mymap; 8 | int n = strs.size(); 9 | string temp; 10 | 11 | for(int i = 0; i < n; i++) { 12 | temp = strs[i]; 13 | sort(strs[i].begin(), strs[i].end()); 14 | mymap[strs[i]].push_back(temp); 15 | } 16 | 17 | vector> result; 18 | 19 | for(auto itr = mymap.begin(); itr != mymap.end(); itr++) { 20 | result.push_back(itr -> second); 21 | } 22 | return result; 23 | } 24 | }; -------------------------------------------------------------------------------- /Leetcode/Cpp/90. subset II.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | private: 4 | void findSubsets(int ind, vector &nums, vector &ds, vector> &ans) 5 | { 6 | ans.push_back(ds); 7 | for (int i = ind; i < nums.size(); i++) 8 | { 9 | if (i != ind && nums[i] == nums[i - 1]) 10 | continue; 11 | ds.push_back(nums[i]); 12 | findSubsets(i + 1, nums, ds, ans); 13 | ds.pop_back(); 14 | } 15 | } 16 | 17 | public: 18 | vector> subsetsWithDup(vector &nums) 19 | { 20 | vector> ans; 21 | vector ds; 22 | sort(nums.begin(), nums.end()); 23 | findSubsets(0, nums, ds, ans); 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /Leetcode/Cpp/926. Flip String to Monotone Increasing.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public 4 | int minFlipsMonoIncr(String s) 5 | { 6 | int oneCount = 0, zeroToOnes = 0; 7 | int i = 0; 8 | 9 | while (i < s.length() && s.charAt(i) == '0') 10 | { 11 | i++; 12 | } 13 | for (; i < s.length(); i++) 14 | { 15 | char c = s.charAt(i); 16 | if (c == '0') 17 | { 18 | zeroToOnes++; 19 | } 20 | else 21 | { 22 | oneCount++; 23 | } 24 | if (zeroToOnes > oneCount) 25 | { 26 | zeroToOnes = oneCount; 27 | } 28 | } 29 | return zeroToOnes; 30 | } 31 | } -------------------------------------------------------------------------------- /Hackerrank/Inheritance/MultiLevelInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | class Triangle{ 9 | public: 10 | void triangle(){ 11 | cout<<"I am a triangle\n"; 12 | } 13 | }; 14 | 15 | class Isosceles : public Triangle{ 16 | public: 17 | void isosceles(){ 18 | cout<<"I am an isosceles triangle\n"; 19 | } 20 | }; 21 | 22 | class Equilateral : public Isosceles { 23 | public: 24 | void equilateral(){ 25 | cout<<"I am an equilateral triangle\n"; 26 | } 27 | }; 28 | 29 | int main(){ 30 | 31 | Equilateral eqr; 32 | eqr.equilateral(); 33 | eqr.isosceles(); 34 | eqr.triangle(); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Leetcode/Cpp/invertbinarytree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* invertTree(TreeNode* root) { 15 | if(root==NULL) 16 | { 17 | return root; 18 | } 19 | TreeNode* l=invertTree(root->left); 20 | TreeNode* r=invertTree(root->right); 21 | root->right=l; 22 | root->left=r; 23 | return root; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/5_second_largest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | int op(ll arr[], int size) 5 | { 6 | ll max = size - 1; 7 | ll smax = -1; 8 | for (int i = 0; i < size; i++) 9 | { 10 | if (arr[i] > arr[max]) 11 | { 12 | smax = max; 13 | max = i; 14 | } 15 | else if (arr[i] < arr[max]) 16 | { 17 | if (smax == -1 || arr[smax] < arr[i]) 18 | { 19 | smax = i; 20 | } 21 | } 22 | } 23 | return smax; 24 | } 25 | int main() 26 | { 27 | ll arr[] = {1, 2, 3, 4, 5, 8, 7,100,99}; 28 | int size = sizeof(arr) / sizeof(arr[0]); 29 | int pos = op(arr, size) + 1; 30 | cout << pos << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Hackerrank/STL/LowerBoundStl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int size, a; 11 | cin >> size; 12 | vector v; 13 | 14 | for(int i = 0; i < size; i ++) { 15 | cin >> a; 16 | v.push_back(a); 17 | } 18 | 19 | int q; 20 | cin >> q; 21 | 22 | for(int i = 0; i < q; i ++) { 23 | int n; 24 | cin >> n; 25 | vector :: iterator low = lower_bound(v.begin(), v.end(), n); 26 | if (v[low - v.begin()] == n) 27 | cout << "Yes" << " " << low - v.begin() + 1 << endl; 28 | else 29 | cout << "No" << " " << low - v.begin() + 1 << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Geekforgeeks/D)Bitwise/3_count_set_bits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int simple_1(int n) 4 | { 5 | int res = 0; 6 | while (n > 0) 7 | { 8 | res+=(n&1); 9 | 10 | n=n>>1; 11 | } 12 | return res; 13 | } 14 | int b_and_k_algo(int n) 15 | { 16 | int res=0; 17 | while(n>0) 18 | { 19 | n=(n&(n-1)); /* when we do n-1 it turn's on all the off bits before last on bits and turn's off the last on bits so this 20 | way we can skip counting all the zeros*/ 21 | res++; 22 | } 23 | return res++; 24 | } 25 | int main() 26 | { 27 | cout << "NUMBER OF SET BITS :" << simple_1(8) << endl; 28 | cout << "NUMBER OF SET BITS :" << b_and_k_algo(8) << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Geekforgeeks/D)Bitwise/4_look_up_table_for_set_bits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int table[256]; 4 | void initialize() 5 | { 6 | table[0]=0; 7 | for(int i=1;i<256;i++) 8 | { 9 | table[i]=(i&1)+table[i/2]; 10 | } 11 | } 12 | int count(int32_t n) 13 | { 14 | initialize(); //in this function we are initializing the number of sets bits in numbers from 0 to 255 15 | int res=0; 16 | res=table[n&0xff]; //here we are breaking the number in 8 bits and then find its set bits 17 | n=n>>8; 18 | res+=table[n&0xff]; 19 | n=n>>8; 20 | res+=table[n&0xff]; 21 | n=n>>8; 22 | res+=table[n&0xff]; 23 | return res; 24 | } 25 | int main(){ 26 | cout<<"NUMBER OF SET BITS : "<>i) & 1) ==1){ 11 | sum++; 12 | } 13 | } 14 | sum%=3; 15 | if(sum !=0){ 16 | ans|=sum< 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | ll size=arr.size(); 7 | vector lmax(size); 8 | vector rmax(size); 9 | lmax.at(0)=arr.at(0); 10 | for(ll i=1;i=0;i--) 16 | { 17 | rmax[i]=max(arr[i],lmax[i-1]); 18 | } 19 | ll rain=0; 20 | for(ll i=0;i arr{3,0,1,2,5}; 30 | op(arr); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/1_BS_iterative.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int binary_search(int arr[],int n,int x) 4 | { 5 | int l=0; 6 | int r=n-1; 7 | while(l<=r) 8 | { 9 | int mid=(l+r)/2; 10 | if(arr[mid]x) 15 | { 16 | r=mid-1; 17 | } 18 | else 19 | { 20 | return mid; 21 | } 22 | } 23 | return -1; 24 | /* 25 | Time complexity: O(logn) 26 | Space complexity: O(1) 27 | */ 28 | } 29 | int main(){ 30 | int arr[]={10,15}; 31 | int x=20; 32 | int size=sizeof(arr)/sizeof(int); 33 | cout<= 0 && i<=arr.length-1) { 12 | if (arr[i][j] == target) { 13 | return new int[] { i, j }; 14 | } else if (arr[i][j] > target) { 15 | j--; 16 | } else { 17 | i++; 18 | } 19 | } 20 | return new int[] {-1,-1}; 21 | } 22 | } -------------------------------------------------------------------------------- /DSA Python/Searching algorithm/Linear_search.txt: -------------------------------------------------------------------------------- 1 | Linear search also called sequential search is a sequential method for finding a particular value in a list. 2 | This method checks the search element with each element in sequence until the desired element is found or list is exhausted. 3 | In this searching algorithm, list need not be ordered. 4 | 5 | 6 | 7 | Pseudo Code for Linear seach :- 8 | 9 | 1. Traverse the array using for loop. 10 | 11 | 12 | 2. In every iteration, compare the target search key value with the current value of the list. 13 | 14 | •If the value matches, display the current index and value of the array 15 | 16 | •If the value do not match, move on to the next array element. 17 | 18 | 19 | 3. If no match is found display the search element not found. 20 | -------------------------------------------------------------------------------- /Geekforgeeks/H)Hashing/16_Intersection_of_unsorted_elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int intersection(int arr1[],int n1,int arr2[],int n2) 4 | { 5 | unordered_set s(arr1,arr1+n1); 6 | unordered_set freq; 7 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | int q; 12 | cin >> q; 13 | set s; 14 | for(int i = 0; i < q; i ++) { 15 | int t, x; 16 | cin >> t >> x; 17 | switch(t) { 18 | case 1: 19 | s.insert(x); 20 | break; 21 | case 2: 22 | s.erase(x); 23 | break; 24 | case 3: 25 | set :: iterator p = s.find(x); 26 | if (p == s.end()) cout << "No" << endl; 27 | else cout << "Yes" << endl; 28 | break; 29 | 30 | } 31 | } 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Codechef/count_subarrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define all(v) v.begin(), v.end() 5 | const int mod = 1e9 + 7; 6 | 7 | int main() 8 | { 9 | std::ios::sync_with_stdio(false); 10 | std::cin.tie(NULL); 11 | 12 | int t; 13 | cin >> t; 14 | while (t--) 15 | { 16 | ll n; 17 | cin >> n; 18 | vector v(n); 19 | for (ll i = 0; i < n; i++) 20 | cin >> v[i]; 21 | vector cache(n, 0); 22 | cache[0] = 0; 23 | 24 | for (ll i = 1; i < n; i++) 25 | cache[i] = v[i] >= v[i - 1] ? 1 + cache[i - 1] : 0; 26 | ll ans = 0; 27 | for (ll i = 0; i < n; i++) 28 | ans += cache[i] + 1; 29 | cout << ans << "\n"; 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/20_Allocating_min_pages_naive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int sum(int arr[],int b,int e) 4 | { 5 | int ans=0; 6 | for(int i=b;i<=e;i++) 7 | { 8 | ans+=arr[i]; 9 | } 10 | return ans; 11 | } 12 | int minPages(int arr[],int size,int k) 13 | { 14 | if(k==1) 15 | { 16 | return sum(arr,0,size-1); 17 | } 18 | if(size==1) 19 | { 20 | return arr[0]; 21 | } 22 | 23 | int res=INT_MAX; 24 | for(int i=1;i 2 | #define ll long long 3 | #define rep(i, t) for (ll i = 0; i < t; i++) 4 | using namespace std; 5 | bool subarrayWithGivenSum(vector v) 6 | { 7 | unordered_set s; 8 | ll sum = 0; 9 | for (auto elem : v) 10 | { 11 | sum += elem; 12 | if (s.find(sum) != s.end()) 13 | { 14 | return true; 15 | } 16 | if(sum==0) //this condition is used to check for the condn's like {4,-1,-3,5} 17 | { 18 | return true; 19 | } 20 | s.insert(sum); 21 | } 22 | return false; 23 | //Time complexity : O(n) 24 | } 25 | int main() 26 | { 27 | vector vect{-1,4,-3,5,2}; 28 | cout << subarrayWithGivenSum(vect) << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/21_max_circular_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr) 5 | { 6 | ll n=arr.size(); 7 | ll max_sum=arr.at(0),maxi=arr.at(0); 8 | for(ll i=1;i arr{-6,-1,-8}; 29 | op(arr); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Geekforgeeks/E)Matrices/8_snake_pattern.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int R=4,C=4; 4 | void snake_pattern(int arr[R][C]) 5 | { 6 | for(int i=0;i=0;j--) 18 | { 19 | cout< 2 | using namespace std; 3 | int binary_search(int arr[],int x,int l,int r) 4 | { 5 | if(l>r) 6 | { 7 | return -1; 8 | } 9 | int mid=(l+r)/2; 10 | if(arr[mid]x) 15 | { 16 | return binary_search(arr,x,l,mid-1); 17 | } 18 | else 19 | { 20 | return mid; 21 | } 22 | /* 23 | Time complexity: O(logn) 24 | Space complexity: O(logn) due to function call overhead of logn recursive calls. 25 | */ 26 | } 27 | int main(){ 28 | int arr[]={10,15,20,30,40,50}; 29 | int x=20; 30 | int size=sizeof(arr)/sizeof(int); 31 | cout< 5 | 6 | using namespace std; 7 | 8 | int main(void) 9 | { 10 | int x1, x2, v1, v2, t, j; 11 | 12 | cin>>x1>>v1>>x2>>v2; 13 | 14 | if ((v2-v1)!=0) 15 | { 16 | j= (x1-x2)/(v2-v1); 17 | 18 | if (((j>=0) && ((x1+(v1*j))== (x2+(v2*j))) && ((v2-v1)!=0))) 19 | { 20 | cout<<"YES\n"; 21 | } 22 | 23 | else 24 | { 25 | cout<<"NO\n"; 26 | } 27 | } 28 | 29 | else if ((v2-v1)==0) 30 | { 31 | cout<<"NO\n"; 32 | } 33 | 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Hackerrank/Other_Concepts/BitArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() 10 | { 11 | 12 | uint_fast64_t po = (uint_fast64_t)(pow(2, 31)); 13 | uint_fast64_t N, S, P, Q; 14 | cin >> N >> S >> P >> Q; 15 | 16 | bool r = false; 17 | uint_fast64_t c = 0; 18 | uint_fast64_t prv = S % po; 19 | uint_fast64_t crn = -1; 20 | uint_fast64_t i = 1; 21 | 22 | do 23 | { 24 | crn = (prv * P + Q) % po; 25 | if (crn != prv) 26 | { 27 | prv = crn; 28 | ++c; 29 | } 30 | else 31 | { 32 | r = true; 33 | } 34 | ++i; 35 | } while (i < N && !r); 36 | cout << c + 1 << endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Geekforgeeks/B)Array/24_window_sliding_technique.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void op(vector &arr, int k) 5 | { 6 | ll n = arr.size(); 7 | ll gsum=93; 8 | ll maxi = 0, sum = 0; 9 | for (ll i = 0; i < k; i++) 10 | { 11 | maxi += arr[i]; 12 | } 13 | sum = maxi; 14 | if (sum == gsum) 15 | { 16 | cout << 1 << endl; 17 | return; 18 | } 19 | for (ll i = k; i < n; i++) 20 | { 21 | sum = sum + arr[i] - arr[i - k]; 22 | if (sum == gsum) 23 | { 24 | cout << 1 << endl; 25 | return; 26 | } 27 | maxi = max(sum, maxi); 28 | } 29 | cout<<0< arr{5, -10, 6, 90, 3}; 34 | op(arr, 2); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Hackerrank/Introduction/ConditionalStatements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | int main() 8 | { 9 | int n; 10 | cin >> n; 11 | cin.ignore(numeric_limits::max(), '\n'); 12 | 13 | 14 | if ( n==1 ) { 15 | printf("one"); 16 | } else if ( n==2 ) { 17 | printf("two"); 18 | } else if ( n==3 ) { 19 | printf("three"); 20 | } else if ( n==4 ) { 21 | printf("four"); 22 | } else if ( n==5 ) { 23 | printf("five"); 24 | } else if ( n==6 ) { 25 | printf("six"); 26 | } else if ( n==7 ) { 27 | printf("seven"); 28 | } else if ( n==8 ) { 29 | printf("eight"); 30 | } else if ( n==9 ) { 31 | printf("nine"); 32 | } else { 33 | printf("Greater than 9"); 34 | } 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Leetcode/Cpp/38. Count and Say.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string countAndSay(int n) 5 | { 6 | if (n == 1) 7 | { 8 | return "1"; 9 | } 10 | if (n == 2) 11 | { 12 | return "11"; 13 | } 14 | string s = "11"; 15 | for (int i = 3; i <= n; i++) 16 | { 17 | string t = ""; 18 | s = s + '&'; 19 | int c = 1; 20 | for (int j = 1; j < s.length(); j++) 21 | { 22 | if (s[j] != s[j - 1]) 23 | { 24 | t = t + to_string(c); 25 | t = t + s[j - 1]; 26 | c = 1; 27 | } 28 | else 29 | c++; 30 | } 31 | s = t; 32 | } 33 | return s; 34 | } 35 | }; -------------------------------------------------------------------------------- /Leetcode/Cpp/73. Set Matrix Zeroes.cpp: -------------------------------------------------------------------------------- 1 | // https: //www.youtube.com/watch?v=M65xBewcqcI 2 | class Solution 3 | { 4 | public: 5 | void setZeroes(vector> &matrix) 6 | { 7 | int col0 = 1, rows = matrix.size(), cols = matrix[0].size(); 8 | 9 | for (int i = 0; i < rows; i++) 10 | { 11 | if (matrix[i][0] == 0) 12 | col0 = 0; 13 | for (int j = 1; j < cols; j++) 14 | if (matrix[i][j] == 0) 15 | matrix[i][0] = matrix[0][j] = 0; 16 | } 17 | 18 | for (int i = rows - 1; i >= 0; i--) 19 | { 20 | for (int j = cols - 1; j >= 1; j--) 21 | if (matrix[i][0] == 0 || matrix[0][j] == 0) 22 | matrix[i][j] = 0; 23 | if (col0 == 0) 24 | matrix[i][0] = 0; 25 | } 26 | } 27 | }; -------------------------------------------------------------------------------- /Geekforgeeks/E)Matrices/9_boundary_traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int R = 3, C = 2; 4 | void boundary(int arr[R][C]) 5 | { 6 | for (int i = 0; i < C; i++) 7 | { 8 | cout << arr[0][i] << " "; 9 | } 10 | for (int i = 1; i < R - 1; i++) 11 | { 12 | cout << arr[i][C - 1] << " "; 13 | } 14 | if (R > 1) 15 | { 16 | for (int i = C - 1; i >= 0; i--) 17 | { 18 | cout << arr[R - 1][i] << " "; 19 | } 20 | } 21 | if (C > 1) 22 | { 23 | for (int i = R - 2; i > 0; i--) 24 | { 25 | cout << arr[i][0] << " "; 26 | } 27 | } 28 | // Time complexity is theta(R+C) 29 | } 30 | int main() 31 | { 32 | int arr[R][C] = {{1,2},{3,4},{5,6}}; 33 | 34 | boundary(arr); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Geekforgeeks/H)Hashing/18_Pair_with_given_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | #define rep(i,t) for(ll i=0;i v,int sum) 10 | { 11 | unordered_set s; 12 | for(auto elem:v) 13 | { 14 | if(s.find(sum-elem)==s.end()) 15 | { 16 | s.insert(elem); 17 | } 18 | else 19 | { 20 | return true; 21 | } 22 | } 23 | return false; 24 | } 25 | int main(){ 26 | vector vect{3,2,8,15,-8}; 27 | int sum=17; 28 | cout<> s 3 | #define foreach(list, index) for(int index = 0; index < list.size(); index++) 4 | #define FUNCTION(name, op) void name(int &x, int y){ if(!(x op y)) x = y; } 5 | #define INF 1e9 6 | 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | #if !defined toStr || !defined io || !defined FUNCTION || !defined INF 12 | #error Missing preprocessor definitions 13 | #endif 14 | 15 | FUNCTION(minimum, <) 16 | FUNCTION(maximum, >) 17 | 18 | int main(){ 19 | int n; cin >> n; 20 | vector v(n); 21 | foreach(v, i) { 22 | io(v)[i]; 23 | } 24 | int mn = INF; 25 | int mx = -INF; 26 | foreach(v, i) { 27 | minimum(mn, v[i]); 28 | maximum(mx, v[i]); 29 | } 30 | int ans = mx - mn; 31 | cout << toStr(Result =) <<' '<< ans; 32 | return 0; 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Codechef/magicaldoors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define all(v) v.begin(),v.end() 5 | const int mod = 1e9 + 7; 6 | 7 | 8 | int main() 9 | { 10 | ios::sync_with_stdio(false); 11 | cin.tie(NULL); 12 | long long int t; 13 | cin>>t; 14 | while(t) 15 | { 16 | t=t-1; 17 | string s; 18 | cin>>s; 19 | long long int grp0=0; 20 | long long int grp1=0; 21 | long long int cnt=0; 22 | for(int i=1;i target) { 12 | b_pointer--; 13 | }else { 14 | a_pointer++; 15 | } 16 | if(Math.abs(current_sum - target) < Math.abs(result - target)) { 17 | result = current_sum; 18 | } 19 | } 20 | } 21 | return result; 22 | } 23 | }; -------------------------------------------------------------------------------- /Geekforgeeks/A)Math/8_Prime_factors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | void prime(int n) 5 | { 6 | if (n <= 1) 7 | { 8 | cout << 1 << endl; 9 | } 10 | while (n % 2 == 0) 11 | { 12 | cout << 2 << endl; 13 | n = n / 2; 14 | } 15 | while (n % 3 == 0) 16 | { 17 | cout << 3 << endl; 18 | n = n / 3; 19 | } 20 | for (ll i = 5; i * i <= n; i += 6) 21 | { 22 | while(n%i==0) 23 | { 24 | cout<3) 34 | cout<> n; 40 | prime(n); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/3_Index_of_1st_occ.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int first_occ(int arr[],int n,int x) 4 | { 5 | int l=0; 6 | int r=n-1; 7 | while(l<=r) 8 | { 9 | int mid=(l+r)/2; 10 | if(arr[mid]x) 15 | { 16 | r=mid-1; 17 | } 18 | else 19 | { 20 | if(mid==0||arr[mid-1]!=arr[mid]) 21 | return mid; 22 | else 23 | r=mid-1; 24 | } 25 | } 26 | return -1; 27 | //time complexity: O(logn) 28 | //auxillary space: O(1) 29 | } 30 | int main(){ 31 | int arr[]={10,10,10,10,15,15,20,20}; 32 | int x=20; 33 | int size=sizeof(arr)/sizeof(int); 34 | cout<next; 18 | } 19 | if(fast == NULL){ 20 | return head->next; 21 | } 22 | while(fast->next != NULL){ 23 | fast = fast->next; 24 | slow = slow->next; 25 | } 26 | 27 | slow->next = slow->next->next; 28 | return head; 29 | } 30 | }; -------------------------------------------------------------------------------- /Leetcode/Cpp/954. Array of Doubled Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public 4 | boolean canReorderDoubled(int[] A) 5 | { 6 | Map count = new HashMap(); 7 | for (int x : A) 8 | count.put(x, count.getOrDefault(x, 0) + 1); 9 | 10 | Integer[] B = new Integer[A.length]; 11 | for (int i = 0; i < A.length; ++i) 12 | B[i] = A[i]; 13 | Arrays.sort(B, Comparator.comparingInt(Math::abs)); 14 | 15 | for (int x : B) 16 | { 17 | 18 | if (count.get(x) == 0) 19 | continue; 20 | 21 | if (count.getOrDefault(2 * x, 0) <= 0) 22 | return false; 23 | 24 | 25 | count.put(x, count.get(x) - 1); 26 | count.put(2 * x, count.get(2 * x) - 1); 27 | } 28 | 29 | 30 | return true; 31 | } 32 | } -------------------------------------------------------------------------------- /Geekforgeeks/H)Hashing/2_direct_address_table.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool insert(vector &table,int elem) 4 | { 5 | table[elem]=1; 6 | return 1; 7 | } 8 | bool remove(vector &table,int elem) 9 | { 10 | table[elem]=0; 11 | return 0; 12 | } 13 | bool search(vector &table,int elem) 14 | { 15 | return table[elem]; 16 | } 17 | int main(){ 18 | vector table(1000,0); 19 | insert(table,10); 20 | insert(table,20); 21 | insert(table,119); 22 | cout< 2 | using namespace std; 3 | int last_occ(int arr[],int n,int x) 4 | { 5 | int l=0; 6 | int r=n-1; 7 | while(l<=r) 8 | { 9 | int mid=(l+r)/2; 10 | if(arr[mid]x) 15 | { 16 | r=mid-1; 17 | } 18 | else 19 | { 20 | if(mid==(n-1)||arr[mid+1]!=arr[mid]) 21 | return mid; 22 | else 23 | l=mid+1; 24 | } 25 | } 26 | return -1; 27 | //time complexity: O(logn) 28 | //auxillary space: O(1) 29 | } 30 | int main(){ 31 | int arr[]={10,10,10,10,15,15,20,20,20,100,100}; 32 | int x=100; 33 | int size=sizeof(arr)/sizeof(int); 34 | cout< 2 | using namespace std; 3 | const int R = 2, C = 2; 4 | void transpose(int arr[R][C]) //in this function we just swap the elements below the diagonal with those above it to get the transpose 5 | { 6 | for (int i = 0; i < R; i++) 7 | { 8 | for (int j = i + 1; j < C; j++) 9 | { 10 | swap(arr[i][j], arr[j][i]); 11 | } 12 | } 13 | //time complexity is O(R*C) 14 | //space complexity theta(1) 15 | } 16 | void print(int arr[R][C]) 17 | { 18 | for (int i = 0; i < R; i++) 19 | { 20 | for (int j = 0; j < C; j++) 21 | cout << arr[i][j] << " "; 22 | 23 | cout << endl; 24 | } 25 | } 26 | int main() 27 | { 28 | int arr[R][C] = {{1, 1}, {2, 2}}; 29 | 30 | transpose(arr); //in place and one traversal 31 | print(arr); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Leetcode/Java/36. Valid Sudoku.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(1) => O(9^2) => O(81) 3 | public boolean isValidSudoku(char[][] board) { 4 | Set seen = new HashSet(); 5 | for (int i=0; i<9; ++i) { 6 | for (int j=0; j<9; ++j) { 7 | char number = board[i][j]; 8 | if (number != '.') 9 | if (seen.contains(number + "_R_" + i) || 10 | seen.contains(number + "_C_ " + j) || 11 | seen.contains(number + "_B_" + i/3 + "_" + j/3)) { 12 | 13 | return false; 14 | } else { 15 | seen.add(number + "_R_" + i); 16 | seen.add(number + "_C_ " + j); 17 | seen.add(number + "_B_" + i/3 + "_" + j/3); 18 | } 19 | 20 | } 21 | } 22 | return true; 23 | } 24 | } -------------------------------------------------------------------------------- /DSA C++/Linkedlist/linked List creation and traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | struct node{ 5 | int data; 6 | struct node *next; 7 | }; 8 | struct node *head,*new_node,*temp; 9 | head=0; 10 | int choice=1; 11 | while(choice!=0){ 12 | new_node=(struct node *) malloc(sizeof(struct node)); 13 | printf("\nEnter data: "); 14 | scanf("%d",&new_node->data); 15 | if(head==0){ 16 | head=temp=new_node; 17 | } 18 | else{ 19 | temp->next=new_node; 20 | temp=new_node; 21 | temp->next=0; 22 | } 23 | printf("Do you want to continue (0,1)"); 24 | scanf("%d",&choice); 25 | } 26 | temp=head; 27 | printf("The list is:\n"); 28 | while(temp!=0){ 29 | printf("%d\t",temp->data); 30 | temp=temp->next; 31 | } 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/7_square_root.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int sqaure_root(int x) 4 | { 5 | int l=0,r=x; 6 | int ans=-1; 7 | while(l<=r) 8 | { 9 | int mid=(l+r)/2; 10 | if(mid*mid==x) 11 | return mid; 12 | else if(mid*mid>x) 13 | r=mid-1; 14 | else if(mid*mid 3 | using namespace std; 4 | #define int long long 5 | #define all(v) v.begin(),v.end() 6 | const int mod = 1e9 + 7; 7 | int32_t main() 8 | { 9 | ios::sync_with_stdio(false); 10 | cin.tie(NULL); 11 | int t; 12 | cin>>t; 13 | while(t) 14 | { 15 | t=t-1; 16 | int n,k; 17 | cin>>n>>k; 18 | if(n-k==1) 19 | { 20 | cout<<-1< 2 | using namespace std; 3 | typedef long long ll; 4 | typedef vector vi; 5 | typedef vector vl; 6 | #define pb push_back 7 | #define all(v) v.begin(), v.end() 8 | const int mod = 1e9 + 7; 9 | 10 | int main() { 11 | ios::sync_with_stdio(false); 12 | cin.tie(NULL); 13 | 14 | int t; 15 | cin >> t; 16 | while (t--) { 17 | int n; 18 | cin >> n; 19 | 20 | string str; 21 | cin >> str; 22 | 23 | unordered_map mp1, mp2; 24 | for (int i = 0; i < n; i++) { 25 | mp1[i - (str[i] - 48)]++; 26 | mp2[i + (str[i] - 48)]++; 27 | } 28 | int total = 0; 29 | for (auto x : mp1) 30 | total += (x.second * (x.second - 1) / 2); 31 | for (auto x : mp2) 32 | total += (x.second * (x.second - 1) / 2); 33 | 34 | cout << total << "\n"; 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Geekforgeeks/H)Hashing/20_Subarray_with_given_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | #define rep(i, t) for (ll i = 0; i < t; i++) 4 | using namespace std; 5 | bool subarrayWithGivenSum(vector v,int given_sum) 6 | { 7 | unordered_set s; 8 | ll sum=0; 9 | for (auto elem : v) 10 | { 11 | sum += elem; 12 | if (s.find(sum-given_sum) != s.end()) 13 | { 14 | return true; 15 | } 16 | if(sum==given_sum) //this condition is used to check for the condn's like {3,2,5,6} that is requirred sum at thr beginning. 17 | { 18 | return true; 19 | } 20 | s.insert(sum); 21 | } 22 | return false; 23 | //Time complexity : O(n) 24 | //Auxillary space : O(n) 25 | } 26 | int main() 27 | { 28 | vector vect{3,2,5,6}; 29 | ll sum=10; 30 | cout << subarrayWithGivenSum(vect,sum) << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /DSA C++/Stack/stock_span.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | void stockSpan(int prices[],int n,int span[]){ 6 | 7 | stack s;//indices of the days 8 | s.push(0); 9 | span[0]=1; 10 | 11 | //loop for rest of the days 12 | for(int i=1;i<=n;i++){ 13 | int currentPrice=prices[i]; 14 | while(!s.empty() && prices[s.top()]<=currentPrice){ 15 | s.pop(); 16 | } 17 | if(!s.empty()){ 18 | int prev_highest_index=s.top(); 19 | span[i]=i-prev_highest_index; 20 | } 21 | else{ 22 | span[i]=i+1; 23 | } 24 | s.push(i); 25 | } 26 | } 27 | int main(){ 28 | int prices[]={100,80,60,75,60,55,625}; 29 | int n=sizeof(prices)/sizeof(int); 30 | int span[100000]={0}; 31 | stockSpan(prices,n,span); 32 | for(int i=0;i increase the size of the hash table. 21 | 22 | 23 | *************FINAL CONCLUSION************* 24 | 25 | If we know the size of the chain in advance and can avoid table resizing (hence have extra space) , we can use open addressing. 26 | 27 | But if we want to use dynamic structure and have better performance we use chaining. -------------------------------------------------------------------------------- /DSA Python/Permutaions_using_backtracking.py: -------------------------------------------------------------------------------- 1 | def A_n_k(a, n, k, depth, used, curr, ans): 2 | ''' 3 | Implement permutation of k items out of n items 4 | depth: start from 0, and represent the depth of the search 5 | used: track what items are in the partial solution from the set of n 6 | curr: the current partial solution 7 | ans: collect all the valide solutions 8 | ''' 9 | if depth == k: #end condition 10 | ans.append(curr[::]) # use deepcopy because curr is tracking all partial solution, it eventually become [] 11 | return 12 | 13 | for i in range(n): 14 | if not used[i]: 15 | # generate the next solution from curr 16 | curr.append(a[i]) 17 | used[i] = True 18 | print(curr) 19 | # move to the next solution 20 | A_n_k(a, n, k, depth+1, used, curr, ans) 21 | 22 | #backtrack to previous partial state 23 | curr.pop() 24 | print('backtrack: ', curr) 25 | used[i] = False 26 | return 27 | -------------------------------------------------------------------------------- /Geekforgeeks/D)Bitwise/8_two_odd_occuring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void two_odd(vector v) 4 | { 5 | int x=0; 6 | for(auto val:v) 7 | x=x^val; //finding the xor of all will give us the xor of the odd ones occuring 8 | 9 | x=x&~(x-1); //here we are find the number with only the first different bit (b/w two numbers) set. 10 | 11 | /* the first set bit of xor represents the difference between the two odd occuring element. That bit will be set in 12 | one number and not in the other so we find them by using this concept */ 13 | 14 | int f=0,s=0; 15 | for(auto val:v) 16 | { 17 | if((x&val)!=0) 18 | f=f^val; 19 | else 20 | s=s^val; 21 | } 22 | cout<<"TWO ODD OCCURING ELEMENTS ARE :"< v; 26 | v={1,2,2,3,3,3,4,4}; 27 | two_odd(v); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/12_intersection_of_sorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void merging(int arr1[], int n1, int arr2[], int n2) 4 | { 5 | int i1 = 0, i2 = 0; 6 | while (i1 < n1 && i2 < n2) 7 | { 8 | 9 | if (i1 > 0 && arr1[i1] == arr1[i1 - 1]) 10 | { 11 | i1++; 12 | continue; 13 | } 14 | if (arr1[i1] > arr2[i2]) 15 | { 16 | i2++; 17 | } 18 | else if (arr1[i1] < arr2[i2]) 19 | { 20 | i1++; 21 | } 22 | else 23 | { 24 | cout << arr1[i1] << " "; 25 | i1++; 26 | i2++; 27 | } 28 | } 29 | } 30 | int main() 31 | { 32 | int arr1[] = {10,20,20,40,60}; 33 | int n1 = sizeof(arr1) / sizeof(int); 34 | int arr2[] = {2,20,20,20}; 35 | int n2 = sizeof(arr2) / sizeof(int); 36 | merging(arr1, n1, arr2, n2); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/27_merge_overlapping_interval.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector > merge(vector > inp) 4 | { 5 | sort(inp.begin(),inp.end()); 6 | vector> out; 7 | int mini=inp[0].first,maxi=inp[0].second; 8 | for(int i=1;i> inp{{5,10},{3,15},{18,30},{2,7}}; 26 | vector > out; 27 | out=merge(inp); 28 | for(auto elem:out) 29 | { 30 | cout< 2 | using namespace std; 3 | int chocolate(int arr[], int size, int m) 4 | { 5 | if (m < size) 6 | { 7 | sort(arr, arr + size); 8 | int min_diff = arr[m - 1] - arr[0]; 9 | for (int i = 1; i < size - (m - 1); i++) 10 | { 11 | min_diff = min(min_diff, arr[m - 1 + i] - arr[i]); 12 | } 13 | return min_diff; 14 | //Time complexity of the array is O(nlogn). 15 | } 16 | 17 | return -1; 18 | } 19 | int main() 20 | { 21 | int arr[] = {3, 4, 1, 9, 56, 7, 9, 12}; 22 | int size = sizeof(arr) / sizeof(int); 23 | int m = 5; 24 | /*In this question we are given an array which denotes the size of diff packets and we need to distribute 1 packets to 25 | each student so that difference between the size of packets with max and min is smallest. 26 | */ 27 | cout << chocolate(arr, size, m) << endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /DSA Java/kadane_algorithm.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class kadane_algorithm{ 4 | public static void main(String[] args){ 5 | int[] arr={2,4,3,-6,-4,1,7,-2,6,4,-3,2,3,-9,-6,-4,4,8}; 6 | 7 | int ci=0; 8 | int cj=0; 9 | int cs=arr[0]; 10 | int bi=0; 11 | int bj=0; 12 | int bs=arr[0]; 13 | 14 | for(int i=1;i0) 17 | { 18 | cj++; 19 | cs+=arr[i]; 20 | } 21 | else 22 | { 23 | ci=i; 24 | cj=i; 25 | cs=arr[i]; 26 | } 27 | if(cs>bs) 28 | { 29 | bi=ci; 30 | bj=cj; 31 | bs=cs; 32 | } 33 | } 34 | for(int i=bi;i<=bj;i++) 35 | { 36 | System.out.print(arr[i]+ " "); 37 | } 38 | System.out.println(); 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /DSA C++/Stack/implicit_reverse_stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | void insertAtBottom(stack &s,int x){ 6 | //base case 7 | if(s.empty()){ 8 | s.push(x); 9 | return; 10 | } 11 | int data =s.top(); 12 | s.pop(); 13 | insertAtBottom(s,x); 14 | s.push(data); 15 | } 16 | void reverseStack(stack &s){ 17 | //base case 18 | if(s.empty()){ 19 | return; 20 | } 21 | //otherwise 22 | //pop out top element and insert it at bottom of reverse smaller stack 23 | int x=s.top(); 24 | s.pop(); 25 | //recursively reverse the smaller stack 26 | reverseStack(s); 27 | insertAtBottom(s,x); 28 | } 29 | int main() { 30 | stack s; 31 | s.push(1); 32 | s.push(2); 33 | s.push(3); 34 | s.push(4); 35 | s.push(5); 36 | s.push(6); 37 | reverseStack(s); 38 | while(!s.empty()) { 39 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | void transfer(stack &s1, stack &s2,int n){ 6 | for(int i=0;i &s1){ 12 | stack s2; 13 | int n=s1.size(); 14 | int i=0; 15 | for(int i=0;i s; 30 | s.push(1); 31 | s.push(2); 32 | s.push(3); 33 | s.push(4); 34 | s.push(5); 35 | s.push(6); 36 | reverseStack(s); 37 | while(!s.empty()) { 38 | cout< 2 | using namespace std; 3 | void bubble_sort(int arr[], int n) 4 | { 5 | for (int i = 0; i < (n - 1); i++) 6 | { 7 | bool flag = 0; 8 | for (int j = 1; j < (n - i ); j++) 9 | { 10 | if (arr[j - 1] > arr[j]) 11 | { 12 | flag = 1; 13 | swap(arr[j - 1], arr[j]); 14 | } 15 | } 16 | if (flag != 1) 17 | break; 18 | } 19 | //Time complexity O(n^2) but mgiht vary if array is somewhat or completely sorted 20 | //Also it is a stable and in place since it does not requirres any temp array sorting algorithm 21 | } 22 | void print(int arr[], int n) 23 | { 24 | for (int i = 0; i < n; i++) 25 | cout << arr[i] << " "; 26 | cout << endl; 27 | } 28 | int main() 29 | { 30 | int arr[] = {4, 7, 2, 3, 1, 9, 7, 5, 4}; 31 | int n = sizeof(arr) / sizeof(int); 32 | bubble_sort(arr, n); 33 | print(arr, n); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Hackerrank/STL/DequeStl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void printKMax(int arr[], int n, int k){ 6 | deque d; 7 | for(int i=0;i=arr[d.back()]){ 10 | d.pop_back(); 11 | } 12 | d.push_back(i); 13 | } 14 | for(int i=k;i=arr[d.back()]) 22 | { 23 | d.pop_back(); 24 | } 25 | d.push_back(i); 26 | } 27 | cout<> t; 35 | while(t>0) { 36 | int n,k; 37 | cin >> n >> k; 38 | int i; 39 | int arr[n]; 40 | for(i=0;i> arr[i]; 42 | printKMax(arr, n, k); 43 | t--; 44 | } 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Leetcode/Cpp/removeduplicates.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* deleteDuplicates(ListNode* head) { 14 | ListNode* temp=head; 15 | if(temp==NULL || temp->next==NULL) 16 | return head; 17 | ListNode* newnode; 18 | while(temp!=NULL && temp->next!=NULL) 19 | { 20 | newnode=temp; 21 | if(newnode->val==temp->next->val) 22 | { 23 | ListNode* t=temp->next; 24 | newnode->next=temp->next->next; 25 | delete(t); 26 | } 27 | else 28 | { 29 | newnode=temp; 30 | temp=temp->next; 31 | } 32 | } 33 | return head; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /DSA C++/Stack/reverse_stack_using_another_stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | void transfer(stack &s1, stack &s2,int n){ 6 | for(int i=0;i &s1){ 12 | stack s2; 13 | int n=s1.size(); 14 | int i=0; 15 | for(int i=0;i s; 30 | s.push(1); 31 | s.push(2); 32 | s.push(3); 33 | s.push(4); 34 | s.push(5); 35 | s.push(6); 36 | reverseStack(s); 37 | while(!s.empty()) { 38 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | class Student { 9 | int scores[5]; 10 | public: 11 | void input(){ 12 | for(int i = 0; i < 5; i++) { 13 | cin >> scores[i]; 14 | } 15 | } 16 | int calculateTotalScore(){ 17 | int sum = 0; 18 | for (int i = 0; i < 5; i++) { 19 | sum += scores[i]; 20 | } 21 | return sum; 22 | } 23 | }; 24 | int main() { 25 | int n; 26 | cin >> n; 27 | Student *s = new Student[n]; 28 | 29 | for(int i = 0; i < n; i++){ 30 | s[i].input(); 31 | } 32 | 33 | int kristen_score = s[0].calculateTotalScore(); 34 | 35 | int count = 0; 36 | for(int i = 1; i < n; i++){ 37 | int total = s[i].calculateTotalScore(); 38 | if(total > kristen_score){ 39 | count++; 40 | } 41 | } 42 | 43 | cout << count; 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /DSA C++/Pointers/Pointers.cpp: -------------------------------------------------------------------------------- 1 | // Pointers are variables which store address of other variables 2 | // Eg: if i=10 then at cout it will find its pointer using &i and then use it to acces i 3 | 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int i = 10; 10 | // & operator is used to find the address.... also called address of operator 11 | cout << &i << endl; 12 | //output 13 | //0x61ff0c .... an hexa decimal no. 14 | 15 | //declaring pointers 16 | int *p = &i; 17 | cout << p << endl; 18 | //output 19 | //0x61ff0c 20 | 21 | // * can be used to access pointer data as a derefrence operator 22 | cout << *p << endl; 23 | //output 24 | //10 25 | 26 | if (*p == i) 27 | { 28 | cout << "true" << endl; 29 | } 30 | //proving both are same 31 | 32 | //pointers can be treated as normal numbers 33 | (*p)++; 34 | cout << i << endl; 35 | //output 36 | //10+1 = 11 37 | 38 | int a[10]; 39 | cout << a << endl; 40 | //a is a pointer 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /DSA Java/left_view.java: -------------------------------------------------------------------------------- 1 | /* A Binary Tree node 2 | class Node 3 | { 4 | int data; 5 | Node left, right; 6 | 7 | Node(int item) 8 | { 9 | data = item; 10 | left = right = null; 11 | } 12 | }*/ 13 | class Tree 14 | { 15 | //Function to return list containing elements of left view of binary tree. 16 | ArrayList leftView(Node root) 17 | { 18 | 19 | ArrayList ans=new ArrayList<>(); 20 | if(root==null) return ans; 21 | ans.add(root.data); 22 | Queue queue=new LinkedList<>(); 23 | queue.add(root); 24 | queue.add(null); 25 | while(!queue.isEmpty()){ 26 | Node curr=queue.poll(); 27 | if(curr==null){ 28 | if(queue.isEmpty()) break; 29 | ans.add(queue.peek().data); 30 | queue.add(null); 31 | } 32 | else{ 33 | if(curr.left!=null) queue.add(curr.left); 34 | if(curr.right!=null) queue.add(curr.right); 35 | } 36 | } 37 | return ans; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /DSA Java/longest_increasing_subsequence.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class longest_increasing_subsequence { 5 | 6 | public static void main(String[] args) throws Exception { 7 | Scanner scn=new Scanner(System.in); 8 | int n=scn.nextInt(); 9 | int[] arr=new int[n]; 10 | for(int i=0;i < arr.length;i++){ 11 | arr[i]=scn.nextInt(); 12 | } 13 | System.out.println("Length of Longest Increasing Subsequence is: " + longestincsubsequence(arr)); 14 | 15 | } 16 | public static int longestincsubsequence(int[] arr){ 17 | int[] dp=new int[arr.length]; 18 | dp[0]=1; 19 | int ans=1; 20 | for(int i=1;i=0;j--){ 23 | if(arr[j] < arr[i]){ 24 | max=Math.max(max,dp[j]); 25 | } 26 | } 27 | dp[i]=max + 1; 28 | ans=Math.max(dp[i],ans); 29 | } 30 | return ans; 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Leetcode/Cpp/113. Path Sum II.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution 13 | { 14 | public: 15 | vector> v; 16 | vector path; 17 | 18 | void dfs(TreeNode *root, int targetSum) 19 | { 20 | if (!root) 21 | return; 22 | path.push_back(root->val); 23 | targetSum = targetSum - root->val; 24 | 25 | if (!root->left && !root->right && targetSum == 0) 26 | v.push_back(path); 27 | dfs(root->left, targetSum); 28 | dfs(root->right, targetSum); 29 | path.pop_back(); 30 | } 31 | vector> pathSum(TreeNode *root, int targetSum) 32 | { 33 | dfs(root, targetSum); 34 | return v; 35 | } 36 | }; -------------------------------------------------------------------------------- /DSA Java/BinarySearch.java: -------------------------------------------------------------------------------- 1 | class BinarySearch{ 2 | 3 | // binary search using recursion 4 | 5 | public static int binarySearch(int arr[], int first, int last, int key){ 6 | if (last>=first){ 7 | int mid = first + (last - first)/2; 8 | if (arr[mid] == key){ 9 | return mid; 10 | } 11 | if (arr[mid] > key){ 12 | return binarySearch(arr, first, mid-1, key);//search in left subarray 13 | }else{ 14 | return binarySearch(arr, mid+1, last, key);//search in right subarray 15 | } 16 | } 17 | return -1; 18 | } 19 | public static void main(String args[]){ 20 | int arr[] = {10,20,30,40,50}; 21 | int key = 30; 22 | int last=arr.length-1; 23 | int result = binarySearch(arr,0,last,key); 24 | if (result == -1) 25 | System.out.println("Element is not found!"); 26 | else 27 | System.out.println("Element is found at index: "+result); 28 | } 29 | } -------------------------------------------------------------------------------- /Leetcode/Java/Count Good Nodes in Binary Tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | private int goodCount = 0; 18 | public int goodNodes(TreeNode root) { 19 | goodNodesHelper(root, Integer.MIN_VALUE); 20 | return goodCount; 21 | } 22 | 23 | private void goodNodesHelper(TreeNode root, Integer maxValSeen){ 24 | if(root==null){ 25 | return; 26 | } 27 | 28 | if(root.val>= maxValSeen){ 29 | goodCount++; 30 | } 31 | goodNodesHelper(root.left, Math.max(root.val, maxValSeen)); 32 | 33 | goodNodesHelper(root.right, Math.max(root.val, maxValSeen)); 34 | 35 | return; 36 | 37 | 38 | } 39 | } -------------------------------------------------------------------------------- /Leetcode/Java/MyQueue.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class MyQueue { 3 | public static class Queue{ 4 | Stack input; 5 | Stack output; 6 | 7 | public Queue() { 8 | input=new Stack(); 9 | output=new Stack(); 10 | } 11 | 12 | public void enQueue(int x) { 13 | input.push(x); 14 | } 15 | 16 | public int deQueue() { 17 | peek(); 18 | return output.pop(); 19 | } 20 | 21 | public int peek() { 22 | if(output.isEmpty()){ 23 | while(!input.isEmpty()){ 24 | output.push(input.pop()); 25 | } 26 | } 27 | return output.peek(); 28 | } 29 | 30 | public boolean empty() { 31 | return input.empty() && output.empty(); 32 | } 33 | } 34 | public static void main(String[] args){ 35 | Queue q = new Queue(); 36 | q.enQueue(1); 37 | q.enQueue(2); 38 | q.enQueue(3); 39 | 40 | System.out.println(q.deQueue()); 41 | System.out.println(q.deQueue()); 42 | System.out.println(q.deQueue()); 43 | } 44 | } -------------------------------------------------------------------------------- /Hackerrank/Other_Concepts/OverloadOperators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Complex 6 | { 7 | public: 8 | int a,b; 9 | void input(string s) 10 | { 11 | int v1=0; 12 | int i=0; 13 | while(s[i]!='+') 14 | { 15 | v1=v1*10+s[i]-'0'; 16 | i++; 17 | } 18 | while(s[i]==' ' || s[i]=='+'||s[i]=='i') 19 | { 20 | i++; 21 | } 22 | int v2=0; 23 | while(i>s1; 50 | cin>>s2; 51 | x.input(s1); 52 | y.input(s2); 53 | Complex z=x+y; 54 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class BadLengthException : public exception { 8 | int n; 9 | public: 10 | BadLengthException(int num) { 11 | n = num; 12 | } 13 | int what() { 14 | return n; 15 | } 16 | }; 17 | 18 | bool checkUsername(string username) { 19 | bool isValid = true; 20 | int n = username.length(); 21 | if(n < 5) { 22 | throw BadLengthException(n); 23 | } 24 | for(int i = 0; i < n-1; i++) { 25 | if(username[i] == 'w' && username[i+1] == 'w') { 26 | isValid = false; 27 | } 28 | } 29 | return isValid; 30 | } 31 | 32 | int main() { 33 | int T; cin >> T; 34 | while(T--) { 35 | string username; 36 | cin >> username; 37 | try { 38 | bool isValid = checkUsername(username); 39 | if(isValid) { 40 | cout << "Valid" << '\n'; 41 | } else { 42 | cout << "Invalid" << '\n'; 43 | } 44 | } catch (BadLengthException e) { 45 | cout << "Too short: " << e.what() << '\n'; 46 | } 47 | } 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Geekforgeeks/H)Hashing/1_Intro_to_hashing.txt: -------------------------------------------------------------------------------- 1 | Introduction of Hashing: 2 | 3 | 1)It is used to implement dictionaries/maps and sets. 4 | 5 | 2)It does search, insert and delete operation in O(1) time which is much better than any other data structure. 6 | 7 | 3)But for some operations it is very costly .Like for sorting or for finding keys just smaller than 8 | or just bigger takes a high cost. 9 | 10 | 4)Not useful for: 11 | * Finding closest keys. 12 | * Sorted order . 13 | (For the above two operations we use AVL tree or red black tree) 14 | * Prefix searching 15 | (For this we use trie data structure) 16 | 17 | 18 | Application of Hashing: 19 | 20 | Second most used data structures after arrays. 21 | 22 | 1)Implementing dictionaries/maps. 23 | 24 | 2)Database indexing. (or we use B or B+ indexing) 25 | 26 | 3)Cryptography 27 | 28 | 4)Caches (in websites) 29 | 30 | 5)Symbol tables in compilers/instructors : used to detect keywords 31 | 32 | 6)Routers 33 | 34 | 7)Getting data from databases : used in associative arrays for storing data. 35 | 36 | 8)Many more. -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/7_selection_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void selection_sort(int arr[], int n) 4 | { 5 | for(int i=0;iarr[j]) 11 | { 12 | mini=j; 13 | } 14 | } 15 | swap(arr[i],arr[mini]); 16 | } 17 | //Time complexity theta(n^2) and also it always takes this much time 18 | //Does less memory write compared to quick,merge,insertion etc. 19 | //But cycle sort best for memory write. 20 | //Memory write is harmful for EEPROM as it decreases its life term. 21 | //Basic idea for heapsort 22 | //Not stable but in place 23 | } 24 | void print(int arr[], int n) 25 | { 26 | for (int i = 0; i < n; i++) 27 | cout << arr[i] << " "; 28 | cout << endl; 29 | } 30 | int main() 31 | { 32 | int arr[] = {4, 7, 2, 3, 1, 9, 7, 5, 4}; 33 | int n = sizeof(arr) / sizeof(int); 34 | selection_sort(arr, n); 35 | print(arr, n); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Leetcode/Java/find_all_anagrams_in_string.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order. 3 | 4 | An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. 5 | 6 | Input: s = "cbaebabacd", p = "abc" 7 | Output: [0,6] 8 | 9 | */ 10 | 11 | class Solution { 12 | public List findAnagrams(String s, String p) { 13 | int[] cnt = new int[128]; 14 | for (char c : p.toCharArray()) cnt[c]++; 15 | 16 | List ans = new ArrayList<>(); 17 | for (int r = 0, l = 0; r < s.length(); ++r) { 18 | char c = s.charAt(r); 19 | cnt[c]--; 20 | while (cnt[c] < 0) { 21 | cnt[s.charAt(l)]++; 22 | l++; 23 | } 24 | 25 | if (cnt[c] == 0 && r - l + 1 == p.length()) { 26 | ans.add(l); 27 | cnt[s.charAt(l)]++; 28 | l++; 29 | } 30 | } 31 | return ans; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/24_sort_array_two_types_elem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void segregateIntoTwoParts(int arr[], int n) //this problem is basically partition problem of quick sort 4 | { 5 | //We can use both hoare's and lomuto's partition to solve this problem 6 | int i = -1, j = n; 7 | while (1) 8 | { 9 | do 10 | { 11 | i++; 12 | } while (arr[i] == 0); //cond for left sub-array 13 | do 14 | { 15 | j--; 16 | } while (arr[j] != 0); //cond for right sub-array 17 | if (i >= j) 18 | break; 19 | 20 | swap(arr[i], arr[j]); 21 | } 22 | //Time complexity : theta(n) 23 | //Auxillary space : O(1) 24 | //Single transversal 25 | } 26 | void print(int arr[], int n) 27 | { 28 | for (int i = 0; i < n; i++) 29 | cout << arr[i] << " "; 30 | cout << endl; 31 | } 32 | int main() 33 | { 34 | int arr[] = {0,1,0,1,1}; 35 | int size = sizeof(arr) / sizeof(int); 36 | segregateIntoTwoParts(arr, size); 37 | print(arr, size); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /DSA Python/Sorting algorithm/Bubble_sort.py: -------------------------------------------------------------------------------- 1 | def bubble_sort(list): 2 | 3 | # Creating a copy of the list -->not necessary 4 | list_copy=list 5 | 6 | # Repeat the process till n-1(length of the list - 1) times 7 | for _ in range(len(list_copy)-1): 8 | 9 | # Iterate over array (except the last element) 10 | """ 11 | we not iterate the last element 12 | because we are checking the present element and next element in the below loop. 13 | So it may cover up the last element also. 14 | """ 15 | 16 | for i in range(len(list_copy)-1): 17 | 18 | # compare the present element with next element 19 | if list_copy[i]>list_copy[i+1]: 20 | # if it is greater ,then swap the elements 21 | list_copy[i],list_copy[i+1]=list_copy[i+1],list_copy[i] 22 | 23 | return list_copy 24 | 25 | 26 | 27 | 28 | import random 29 | array=[1,100,9,10,30,1,1,1,-15,2,3,4,5,6,7,8,9,10] 30 | random.shuffle(array) # To shuffle the list 31 | 32 | print("List before sorting : ", array) 33 | 34 | print("List after sorting : ",bubble_sort(array)) 35 | -------------------------------------------------------------------------------- /Leetcode/Java/Palindrome Partitioning II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minCut(String s) { 3 | if(s == null || s.length() == 0) { 4 | return 0; 5 | } 6 | 7 | int n = s.length(); 8 | 9 | boolean[][] dp = buildMatrix(s, n); 10 | int[] totalCuts = new int[n]; 11 | 12 | for(int j = 0; j < n; j++) { 13 | int cut = j; 14 | for(int i = 0; i <= j; i++) { 15 | if(dp[i][j]) { 16 | cut = Math.min(cut, i == 0 ? 0 : totalCuts[i - 1] + 1); 17 | } 18 | } 19 | totalCuts[j] = cut; 20 | } 21 | 22 | return totalCuts[n - 1]; 23 | } 24 | 25 | boolean[][] buildMatrix(String s, int n) { 26 | boolean[][] dp = new boolean[n][n]; 27 | 28 | for(int i = n - 1; i >= 0; i--) { 29 | for(int j = i; j < n; j++) { 30 | if(s.charAt(i) == s.charAt(j) && (j - i <= 2 || dp[i + 1][j - 1])) { 31 | dp[i][j] = true; 32 | } 33 | } 34 | } 35 | return dp; 36 | } 37 | } -------------------------------------------------------------------------------- /Geekforgeeks/E)Matrices/11_rotating_by_90.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int R = 4, C = 4; 4 | void rotating(int arr[R][C]) 5 | { 6 | for (int i = 0; i < R; i++) 7 | { 8 | for (int j = i + 1; j < C; j++) 9 | { 10 | swap(arr[i][j], arr[j][i]); 11 | } 12 | } //transpose 13 | 14 | for (int i = 0; i < R / 2; i++) 15 | { 16 | for (int j = 0; j < C; j++) 17 | { 18 | swap(arr[i][j], arr[R - i-1][j]); 19 | } 20 | } //swapping of rows 21 | 22 | //time complexity O(R*C) 23 | //auxillary space O(1) 24 | } 25 | void print(int arr[R][C]) 26 | { 27 | for (int i = 0; i < R; i++) 28 | { 29 | for (int j = 0; j < C; j++) 30 | cout << arr[i][j] << " "; 31 | 32 | cout << endl; 33 | } 34 | } 35 | int main() 36 | { 37 | int arr[R][C] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; 38 | cout<<"BEFORE THE ROTATION:"< 2 | using namespace std; 3 | int max_guests(int arr[],int dep[],int n) 4 | { 5 | sort(arr,arr+n); 6 | sort(dep,dep+n); 7 | int i=0,j=0,curr=0,res=0; 8 | int max_arr=0,max_dep=0; 9 | //This function is similiar to the merge function of merge sort. 10 | while(i 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | // User function template for C++ 8 | 9 | class Solution { 10 | public: 11 | int getMinDiff(int arr[], int n, int k) { 12 | sort(arr, arr+n); 13 | int minEle, maxEle; 14 | int result = arr[n-1] - arr[0]; 15 | 16 | for(int i =1; i<=n-1; i++){ 17 | if(arr[i]>=k){ 18 | maxEle = max(arr[i-1] + k, arr[n-1]-k); 19 | minEle = min(arr[0]+k, arr[i]-k); 20 | 21 | result = min(result, maxEle-minEle); 22 | } 23 | else continue; 24 | } 25 | return result; 26 | } 27 | }; 28 | 29 | // { Driver Code Starts. 30 | int main() { 31 | int t; 32 | cin >> t; 33 | while (t--) { 34 | int n, k; 35 | cin >> k; 36 | cin >> n; 37 | int arr[n]; 38 | for (int i = 0; i < n; i++) { 39 | cin >> arr[i]; 40 | } 41 | Solution ob; 42 | auto ans = ob.getMinDiff(arr, n, k); 43 | cout << ans << "\n"; 44 | } 45 | return 0; 46 | } // } Driver Code Ends -------------------------------------------------------------------------------- /Leetcode/Cpp/95. Unique Binary Search Trees II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | vector generateBST(int begin, int end) 5 | { 6 | vectorV; 7 | 8 | if(begin>end) 9 | { 10 | V.push_back(NULL); 11 | return V; 12 | } 13 | 14 | for(int i=begin; i<=end; i++) 15 | { 16 | vectorleft = generateBST(begin, i-1); 17 | vectorright = generateBST(i+1,end); 18 | 19 | for(auto l:left) 20 | { 21 | for(auto r:right) 22 | { 23 | TreeNode* root = new TreeNode(); 24 | root->val = i; 25 | root->left = l; 26 | root->right = r; 27 | V.push_back(root); 28 | } 29 | } 30 | 31 | } 32 | return V; 33 | } 34 | 35 | vector generateTrees(int n) { 36 | 37 | vectorV; 38 | if(n==0) return V; 39 | 40 | return generateBST(1,n); 41 | } 42 | }; -------------------------------------------------------------------------------- /Geekforgeeks/E)Matrices/6_vector_of_vectors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void print(vector> &arr) 4 | { 5 | cout << "PRINTING USING PRINT FUNCTION: " << endl; 6 | for (int i = 0; i < arr.size(); i++) 7 | { 8 | for (int j = 0; j < arr[i].size(); j++) 9 | cout << arr[i][j] << " "; 10 | 11 | cout << endl; 12 | } 13 | } 14 | int main(){ 15 | int m=3,n=2; 16 | 17 | vector> arr; //here we are creating vector of vectors 18 | 19 | for(int i=0;i v; 22 | for(int j=0;j 2 | using namespace std; 3 | int sum(int arr[], int size, int x) 4 | { 5 | int cntr = 0; 6 | for (int i = 0; i < size; i++) 7 | { 8 | int xf =arr[i]*arr[i]; 9 | int l=0,r=size-1; 10 | while (l < r) 11 | { 12 | if (l == i) 13 | l++; 14 | if (r == i) 15 | r--; 16 | 17 | int sum = arr[l]*arr[l] + arr[r]*arr[r]; 18 | if (sum > xf) 19 | r--; 20 | else if (sum < xf) 21 | l++; 22 | else 23 | { 24 | printf("%d+%d+%d\n", arr[i], arr[l], arr[r]); 25 | cntr++; 26 | l++; 27 | r--; 28 | } 29 | } 30 | } 31 | return cntr; 32 | //Time complexity : O(n^2) 33 | //Space complexity : O(1) 34 | 35 | } 36 | int main() 37 | { 38 | int arr[] = {3,4,5,6,8,10}; 39 | int size = sizeof(arr) / sizeof(int); 40 | int wsum = 32; 41 | cout << sum(arr, size, wsum) << endl; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Hackerrank/Other_Concepts/ClassTemplateSpecialization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | enum class Fruit { apple, orange, pear }; 4 | enum class Color { red, green, orange }; 5 | 6 | template struct Traits; 7 | template <> 8 | struct Traits{ 9 | static string name(int index){ 10 | switch(index){ 11 | case 0:return "apple"; 12 | case 1: return "orange" ; 13 | case 2: return "pear"; 14 | } 15 | return "unknown"; 16 | } 17 | }; 18 | template <> 19 | struct Traits{ 20 | static string name(int index){ 21 | switch(index){ 22 | case 0:return "red"; 23 | case 1: return "green" ; 24 | case 2: return "orange"; 25 | } 26 | return "unknown"; 27 | } 28 | }; 29 | 30 | int main() 31 | { 32 | int t = 0; std::cin >> t; 33 | 34 | for (int i=0; i!=t; ++i) { 35 | int index1; std::cin >> index1; 36 | int index2; std::cin >> index2; 37 | cout << Traits::name(index1) << " "; 38 | cout << Traits::name(index2) << "\n"; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Leetcode/Java/15. 3Sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> threeSum(int[] nums) { 3 | 4 | List> ans= new ArrayList>(); 5 | 6 | Arrays.sort(nums); 7 | 8 | for(int i=0; i a= new ArrayList<>(); 20 | a.add(nums[i]); 21 | a.add(nums[j]); 22 | a.add(target); 23 | if(!(ans.contains(a))) ans.add(a); 24 | break; 25 | } 26 | if(nums[mid]nums.length-1){ 19 | return 0; 20 | } 21 | int count = 0; 22 | for(int i = index+2;i q1; 5 | Queue q2; 6 | public Stack() { 7 | q1=new LinkedList(); 8 | q2=new LinkedList(); 9 | } 10 | 11 | public void push(int x) { 12 | q2.add(x); 13 | while(q1.size()>0){ 14 | int ele=q1.remove(); 15 | q2.add(ele); 16 | 17 | } 18 | while(q2.size()>0){ 19 | int temp=q2.remove(); 20 | q1.add(temp); 21 | } 22 | 23 | } 24 | 25 | public int pop() { 26 | return q1.remove(); 27 | } 28 | 29 | public int top() { 30 | return q1.peek(); 31 | } 32 | 33 | public boolean empty() { 34 | return q1.isEmpty() && q2.isEmpty(); 35 | } 36 | } 37 | public static void main(String[] args){ 38 | Stack s = new Stack(); 39 | s.push(1); 40 | s.push(2); 41 | s.push(3); 42 | System.out.println(s.top()); 43 | s.pop(); 44 | System.out.println(s.top()); 45 | s.pop(); 46 | System.out.println(s.top()); 47 | 48 | } 49 | } -------------------------------------------------------------------------------- /Leetcode/Java/Remove Boxes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeBoxes(int[] boxes) { 3 | if (boxes == null || boxes.length == 0) { 4 | return 0; 5 | } 6 | 7 | int size = boxes.length; 8 | int[][][] dp = new int[size][size][size]; 9 | 10 | return get(dp, boxes, 0, size-1, 0); 11 | } 12 | 13 | private int get(int[][][] dp, int[] boxes, int left, int right, int streak) { 14 | if (left > right) { 15 | return 0; 16 | } else if (left == right) { 17 | return (streak +1)* (streak+1); 18 | } else if (dp[left][right][streak] != 0) { 19 | return dp[left][right][streak]; 20 | } else { 21 | int max = get(dp, boxes, left + 1, right, 0) + (streak+1) *(streak+1); 22 | 23 | for (int i = left + 1; i <= right; i++) { 24 | if (boxes[left] == boxes[i]) { 25 | max = Math.max(max, get(dp, boxes, left + 1, i - 1, 0) + get(dp, boxes, i, right, streak + 1)); 26 | } 27 | } 28 | 29 | dp[left][right][streak] = max; 30 | return max; 31 | } 32 | 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/15_counting_triplets_with_given_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int sum(int arr[], int size, int x) 4 | { 5 | int cntr = 0; 6 | for (int i = 0; i < size; i++) 7 | { 8 | int xf = x - arr[i]; 9 | int l = i + 1, r = size - 1; 10 | while (l < r) 11 | { 12 | if (l == i) 13 | l++; 14 | if (r == i) 15 | r--; 16 | 17 | int sum = arr[l] + arr[r]; 18 | if (sum > xf) 19 | r--; 20 | else if (sum < xf) 21 | l++; 22 | else 23 | { 24 | printf("%d+%d+%d\n", arr[i], arr[l], arr[r]); 25 | cntr++; 26 | l++; 27 | r--; 28 | } 29 | } 30 | } 31 | return cntr; 32 | //Time complexity : O(n^2) 33 | //Space complexity : O(1) 34 | 35 | } 36 | int main() 37 | { 38 | int arr[] = {2, 3, 4, 8, 9,10, 20, 40}; 39 | int size = sizeof(arr) / sizeof(int); 40 | int wsum = 32; 41 | cout << sum(arr, size, wsum) << endl; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Geekforgeeks/E)Matrices/5_array_of_vectors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void print(vector arr[], int m) 4 | { 5 | cout << "PRINTING USING PRINT FUNCTION: " << endl; 6 | for (int i = 0; i < m; i++) 7 | { 8 | for (int j = 0; j < arr[i].size(); j++) 9 | cout << arr[i][j] << " "; 10 | 11 | cout << endl; 12 | } 13 | } 14 | int main(){ 15 | int m=3,n=2; 16 | vector arr[m]; //this is an array of vectors 17 | 18 | for(int i=0;i 2 | using namespace std; 3 | /* 4 | Conditions for this problem: 5 | 1)All the elements are from 1 to max(arr) 6 | 2) 1<=max(arr)<=n-1 7 | */ 8 | int repeating_element(int arr[],int size) 9 | { 10 | //We are using the algorithm which is used to find the first element of a loop in linked list 11 | 12 | int slow=arr[0],fast=arr[0]; 13 | //in phase 1 we move one ptr one steps at a time and another two steps at a time. 14 | //when we do this we will surely meet once inside the loop 15 | do 16 | { 17 | slow=arr[slow]; 18 | fast=arr[arr[fast]]; 19 | } while (slow!=fast); 20 | slow=arr[0]; 21 | /*in phase 2 we move a element outside and one remains inside. And when we move these two at the same phase 22 | then they surely meet at the first element of the loop*/ 23 | while(slow!=fast) 24 | { 25 | slow=arr[slow]; 26 | fast=arr[fast]; 27 | } 28 | return slow; 29 | } 30 | int main(){ 31 | int arr[]={4,2,1,3,2,2}; 32 | int size=sizeof(arr)/sizeof(int); 33 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int start, end; 8 | scanf("%d%d", &start, &end); 9 | for (int i = start; i <= end; i ++) { 10 | if(i >= 1 && i <= 9) { 11 | if(i == 1) { 12 | printf("one\n"); 13 | } else if (i == 2) { 14 | printf("two\n"); 15 | } else if (i == 3) { 16 | printf("three\n"); 17 | } else if (i == 4) { 18 | printf("four\n"); 19 | } else if (i == 5) { 20 | printf("five\n"); 21 | } else if (i == 6) { 22 | printf("six\n"); 23 | } else if (i == 7) { 24 | printf("seven\n"); 25 | } else if (i == 8) { 26 | printf("eight\n"); 27 | } else if (i == 9) { 28 | printf("nine\n"); 29 | } 30 | } else if (i > 9){ 31 | if(i % 2 == 0) { 32 | printf("even\n"); 33 | } else { 34 | printf("odd\n"); 35 | } 36 | } 37 | } 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Leetcode/Java/PeakElement.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class PeakElement { 3 | public static void main(String[] args) { 4 | Scanner scan = new Scanner(System.in); 5 | int n = scan.nextInt(); 6 | int[] arr = new int[n]; 7 | for (int i = 0; i < n; i++) { 8 | arr[i] = scan.nextInt(); 9 | } 10 | System.out.println(peak(arr, 0, arr.length - 1)); 11 | } 12 | 13 | static int peak(int[] arr, int left, int right) { 14 | if (arr[left] > arr[left + 1]) { 15 | return arr[left]; 16 | } 17 | else if (arr[right] > arr[right - 1]) { 18 | return arr[right]; 19 | } 20 | else { 21 | left++; 22 | right--; 23 | while (left <= right) { 24 | int mid = (left + right) / 2; 25 | if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) { 26 | return arr[mid]; 27 | } else if (arr[mid] < arr[mid - 1]) { 28 | right = mid - 1; 29 | } else { 30 | left = mid + 1; 31 | } 32 | } 33 | } 34 | return -1; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Codechef/Alternating_Subarray_prefix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define all(v) v.begin(), v.end() 5 | const int mod = 1e9 + 7; 6 | 7 | int main() 8 | { 9 | std::ios::sync_with_stdio(false); 10 | std::cin.tie(NULL); 11 | 12 | int t; 13 | cin >> t; 14 | while (t--) 15 | { 16 | ll n; 17 | cin >> n; 18 | vector v(n); 19 | for (ll i = 0; i < n; i++) 20 | cin >> v[i]; 21 | vector memo(n, 0); 22 | memo[n - 1] = 0; 23 | 24 | for (ll i = n - 2; i >= 0; i--) 25 | { 26 | if (v[i] >= 0) 27 | { 28 | if (v[i + 1] >= 0) 29 | memo[i] = 0; 30 | else 31 | memo[i] = 1 + memo[i + 1]; 32 | } 33 | else if (v[i] < 0) 34 | { 35 | if (v[i + 1] < 0) 36 | memo[i] = 0; 37 | else 38 | memo[i] = 1 + memo[i + 1]; 39 | } 40 | } 41 | 42 | for (ll i = 0; i < n; i++) 43 | cout << memo[i] + 1 << " "; 44 | cout << "\n"; 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Geekforgeeks/E)Matrices/4_array_pointer_multi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void print(int *arr[], int m, int n) 4 | { 5 | cout << "PRINTING USING PRINT FUNCTION: " << endl; 6 | for (int i = 0; i < m; i++) 7 | { 8 | for (int j = 0; j < n; j++) 9 | cout << arr[i][j] << " "; 10 | 11 | cout << endl; 12 | } 13 | } 14 | int main() 15 | { 16 | int m = 3, n = 2; 17 | int *arr[m]; //creating array of pointer 18 | 19 | for (int i = 0; i < m; i++) 20 | arr[i] = new int[n]; 21 | 22 | for (int i = 0; i < m; i++) 23 | { 24 | for (int j = 0; j < n; j++) 25 | arr[i][j] = i + j; 26 | } 27 | 28 | for (int i = 0; i < m; i++) 29 | { 30 | for (int j = 0; j < n; j++) 31 | { 32 | cout << arr[i][j] << " "; 33 | } 34 | cout << endl; 35 | } 36 | 37 | print(arr,m,n); 38 | /* 39 | This method is quite similiar to the previous method only difference being that instead of creating double pntr we create an array 40 | of pointer (which is stored statically) and allocate spaces to each element of that array of pointer 41 | */ 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Leetcode/Cpp/3. Longest Substring Without Repeating Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | if(s == "") return 0; 5 | int i = 0, j = 0; 6 | unordered_map mp; 7 | mp[s[0]] = 1; 8 | int mx = 1; 9 | cout< j-i+1){ 34 | //not possible 35 | } 36 | } 37 | 38 | return mx; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /Geekforgeeks/E)Matrices/12_spiral_transversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int R = 3, C = 5; 4 | void spiral(int arr[R][C]) 5 | { 6 | int left = 0, right = C - 1, top = 0, bottom = R - 1; //we maintain these 4 variables to print the matrix 7 | while (left <= right && top <= bottom) 8 | { 9 | for (int i = left; i <= right; i++) 10 | cout << arr[top][i] << " "; 11 | top++; 12 | 13 | for (int i = top; i <= bottom; i++) 14 | cout << arr[i][right] << " "; 15 | right--; 16 | if (top <= bottom) //requerred when only I row left 17 | { 18 | for (int i = right; i >= left; i--) 19 | cout << arr[bottom][i] << " "; 20 | bottom--; 21 | } 22 | if (left <= right) 23 | { 24 | for (int i = bottom; i >= top; i--) 25 | cout << arr[i][left] << " "; 26 | left++; 27 | } 28 | } 29 | 30 | //we are traversing each element only once so time complexity is theta(R*C) 31 | } 32 | int main() 33 | { 34 | int arr[R][C] = {{6, 6, 2, 28, 2}, {12, 26, 3, 28, 7}, {22, 25, 3, 4, 23}}; 35 | spiral(arr); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/16_Lomuto_partition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int lomuto(int arr[],int l,int r) 4 | { 5 | srand(time(nullptr)); 6 | int p=rand()%(r-l+1)+l; //generating random pivot to prevent worst case for sorted arrays. 7 | swap(arr[r],arr[p]); 8 | int s=l-1; //denotes the ending of window containing elements smaller that pivot 9 | for(int i=l;i 2 | using namespace std; 3 | void insertion_sort(int arr[],int n) 4 | { 5 | for(int i=1;i=0&&arr[j]>key) //this loop moves all the greater element one position forward 9 | { 10 | arr[j+1]=arr[j]; 11 | j--; 12 | } 13 | arr[j+1]=key; 14 | } 15 | //O(n^2) worst case (when array sorted in reverse order) 16 | ///In place and stable 17 | //Used in practise for small arrays(TimSort and Intrasort) 18 | //TimSort and Introsort are hybrid sorting algorithm which have combination of different sorting algorithm which are used based on size of array 19 | //O(n) best case (when array sorted so we will never go inside the while loop) 20 | //O(n^2) for average case 21 | //Best arraay for sorting an almost sorted array. 22 | } 23 | void print(int arr[], int n) 24 | { 25 | for (int i = 0; i < n; i++) 26 | cout << arr[i] << " "; 27 | cout << endl; 28 | } 29 | int main(){ 30 | int arr[]={4,3,2,7,8,5,4}; 31 | int n=sizeof(arr)/sizeof(int); 32 | insertion_sort(arr,n); 33 | print(arr,n); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Codechef/Add_and_divide.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define all(v) v.begin(), v.end() 5 | const int mod = 1e9 + 7; 6 | 7 | void getFactors(vector &factors, int n) 8 | { 9 | if(n % 2 == 0) 10 | { 11 | factors.push_back(2); 12 | while(n % 2 == 0) 13 | n /= 2; 14 | } 15 | for(int i = 3; i <= sqrt(n); i += 2) 16 | { 17 | if(n % i == 0) 18 | { 19 | factors.push_back(i); 20 | while(n % i == 0) 21 | n /= i; 22 | } 23 | } 24 | if(n > 2) 25 | factors.push_back(n); 26 | return; 27 | } 28 | 29 | int main() 30 | { 31 | std::ios::sync_with_stdio(false); 32 | std::cin.tie(NULL); 33 | 34 | int t; 35 | cin >> t; 36 | while (t--) 37 | { 38 | int a, b; 39 | cin >> a >> b; 40 | 41 | vector factors; 42 | getFactors(factors, a); 43 | 44 | bool its_true = true; 45 | for(auto every_factor : factors) 46 | if(b % every_factor != 0) 47 | { 48 | its_true = false; 49 | break; 50 | } 51 | its_true ? cout << "YES\n" : cout << "NO\n"; 52 | } 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/10_peak_element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int peak_element(int arr[], int size) 4 | { 5 | // if there is an independenet array then it will always have peak element. 6 | int l = 0, r = size - 1; 7 | while (l <= r) 8 | { 9 | int mid = (l + r) / 2; 10 | if ((mid == 0 || arr[mid - 1] <= arr[mid]) && (mid == size - 1 || arr[mid + 1] <= arr[mid])) /* we need one of two in both 11 | cases to be true.*/ 12 | return arr[mid]; 13 | 14 | if (mid > 0 && arr[mid - 1] >= arr[mid]) //if left side is bigger than left contains a peak 15 | r = mid - 1; 16 | else //else right contains a peak 17 | l = mid + 1; 18 | } 19 | return -1; 20 | 21 | //if mid is not the peak and one of the two elements near it is bigger than mid then we can surely say that side contains an peak 22 | //time complexity : O(logn) 23 | //auxillary space : O(1) 24 | } 25 | int main() 26 | { 27 | int arr[] = {5, 10, 30, 40,600, 130, 150, 670}; 28 | int size = sizeof(arr) / sizeof(int); 29 | cout << peak_element(arr, size); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/9_Merge_sorted_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void merging(int arr1[], int n1, int arr2[], int n2) 4 | { 5 | int i1 = 0, i2 = 0; 6 | while (i1 < n1 && i2 < n2) //this loop will completely print one of the loop and rest will be bigger than all the printed elements 7 | { 8 | if (arr1[i1] <= arr2[i2]) //equal to for stability so that first array element appear first 9 | { 10 | cout << arr1[i1] << " "; 11 | i1++; 12 | } 13 | else 14 | { 15 | cout << arr2[i2] << " "; 16 | i2++; 17 | } 18 | } 19 | //all the rest of bigger elements will be print by the following loops 20 | while (i1 < n1) 21 | { 22 | cout << arr1[i1] << " "; 23 | i1++; 24 | } 25 | while (i2 < n2) 26 | { 27 | cout << arr2[i2] << " "; 28 | i2++; 29 | } 30 | //this is an efficeint solution of theta(n1+n2) time complexity and O(1) auxillary space 31 | } 32 | int main() 33 | { 34 | int arr1[]={3,5,8,10,15,21}; 35 | int n1=sizeof(arr1)/sizeof(int); 36 | int arr2[]={1,2,5,6,9,11}; 37 | int n2=sizeof(arr2)/sizeof(int); 38 | merging(arr1,n1,arr2,n2); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Hackerrank/Other_Concepts/ClassTemplates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | template 10 | class AddElements { 11 | T element; 12 | public: 13 | AddElements(T val) : element(val){} 14 | T add(T val) { 15 | return element + val; 16 | } 17 | T concatenate(T val) { 18 | return element + val; 19 | } 20 | }; 21 | 22 | int main () { 23 | int n,i; 24 | cin >> n; 25 | for(i=0;i> type; 28 | if(type=="float") { 29 | double element1,element2; 30 | cin >> element1 >> element2; 31 | AddElements myfloat (element1); 32 | cout << myfloat.add(element2) << endl; 33 | } 34 | else if(type == "int") { 35 | int element1, element2; 36 | cin >> element1 >> element2; 37 | AddElements myint (element1); 38 | cout << myint.add(element2) << endl; 39 | } 40 | else if(type == "string") { 41 | string element1, element2; 42 | cin >> element1 >> element2; 43 | AddElements mystring (element1); 44 | cout << mystring.concatenate(element2) << endl; 45 | } 46 | } 47 | return 0; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Leetcode/Cpp/542. 01 Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> dir{{1,0}, {-1,0}, {0,-1}, {0,1}}; 4 | vector> updateMatrix(vector>& matrix) { 5 | if(matrix.size() == 0) return matrix; 6 | queue> q; 7 | for(int i = 0; i < matrix.size(); i++) { 8 | for(int j = 0; j < matrix[0].size(); j++) { 9 | if(matrix[i][j] == 1) { 10 | matrix[i][j] = -1; 11 | } 12 | else 13 | q.push({i, j}); 14 | } 15 | } 16 | int level = 1; 17 | while(!q.empty()) { 18 | int size = q.size(); 19 | for(int k = 0; k < size; k++) { 20 | int x = q.front()[0]; 21 | int y = q.front()[1]; 22 | q.pop(); 23 | for(auto d : dir) { 24 | int i = d[0] + x; 25 | int j = d[1] + y; 26 | if(i >= 0 && j >= 0 && i < matrix.size() && j < matrix[0].size() && matrix[i][j] == -1) { 27 | matrix[i][j] = level; 28 | q.push({i, j}); 29 | } 30 | } 31 | } 32 | level++; 33 | } 34 | return matrix; 35 | } 36 | }; -------------------------------------------------------------------------------- /Hackerrank/Introduction/HourGlass.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Complete the hourglassSum function below. 8 | int hourglassSum(vector> arr) 9 | { 10 | // int n= sizeof(arr)/sizeof(arr[0]); 11 | // int m = sizeof(arr[0])/sizeof(arr[0][0]); 12 | // cout<=max) 23 | { 24 | max= s; 25 | } 26 | 27 | } 28 | 29 | 30 | } 31 | } 32 | 33 | 34 | return max; 35 | } 36 | 37 | int main() 38 | { 39 | ofstream fout(getenv("OUTPUT_PATH")); 40 | 41 | vector> arr(6); 42 | for (int i = 0; i < 6; i++) { 43 | arr[i].resize(6); 44 | 45 | for (int j = 0; j < 6; j++) { 46 | cin >> arr[i][j]; 47 | } 48 | 49 | cin.ignore(numeric_limits::max(), '\n'); 50 | } 51 | 52 | int result = hourglassSum(arr); 53 | 54 | fout << result << "\n"; 55 | 56 | fout.close(); 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /Leetcode/Cpp/3sum.cpp: -------------------------------------------------------------------------------- 1 | // Leetcode 15. 3Sum solution 2 | class Solution { 3 | public: 4 | vector> threeSum(vector& a) { 5 | vector> ans; 6 | sort(a.begin(),a.end()); 7 | int n = a.size(); 8 | for(int i=0;i0&&a[i]==a[i-1]) 11 | { 12 | continue; 13 | } 14 | int low = i+1; 15 | int high = n-1; 16 | while(lowlow && a[high-1]==a[high]) 27 | { 28 | high--; 29 | } 30 | low++; 31 | high--; 32 | } 33 | else if(sum<0) 34 | { 35 | low++; 36 | } 37 | else 38 | { 39 | high--; 40 | } 41 | } 42 | } 43 | return ans; 44 | } 45 | }; 46 | -------------------------------------------------------------------------------- /Geekforgeeks/sort012.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // } Driver Code Ends 5 | 6 | class Solution 7 | { 8 | public: 9 | void sort012(int a[], int n) 10 | { 11 | int m = 0; 12 | int h = n - 1; 13 | int l = 0; 14 | while (m <= h) 15 | { 16 | if (a[m] == 0) 17 | { 18 | swap(a[l], a[m]); 19 | l++; 20 | m++; 21 | } 22 | else if (a[m] == 1) 23 | { 24 | m++; 25 | } 26 | else if (a[m] == 2) 27 | { 28 | swap(a[m], a[h]); 29 | h--; 30 | } 31 | } 32 | } 33 | }; 34 | 35 | // { Driver Code Starts. 36 | int main() 37 | { 38 | 39 | int t; 40 | cin >> t; 41 | 42 | while (t--) 43 | { 44 | int n; 45 | cin >> n; 46 | int a[n]; 47 | for (int i = 0; i < n; i++) 48 | { 49 | cin >> a[i]; 50 | } 51 | 52 | Solution ob; 53 | ob.sort012(a, n); 54 | 55 | for (int i = 0; i < n; i++) 56 | { 57 | cout << a[i] << " "; 58 | } 59 | 60 | cout << endl; 61 | } 62 | return 0; 63 | } 64 | 65 | // } Driver Code Ends -------------------------------------------------------------------------------- /Hackerrank/Class/ExceptionalServer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | class Server { 10 | private: 11 | static int load; 12 | public: 13 | static int compute(long long A, long long B) { 14 | load += 1; 15 | if(A < 0) { 16 | throw std::invalid_argument("A is negative"); 17 | } 18 | vector v(A, 0); 19 | int real = -1, cmplx = sqrt(-1); 20 | if(B == 0) throw 0; 21 | real = (A/B)*real; 22 | int ans = v.at(B); 23 | return real + A - B*ans; 24 | } 25 | static int getLoad() { 26 | return load; 27 | } 28 | }; 29 | int Server::load = 0; 30 | 31 | int main() { 32 | int T; cin >> T; 33 | while(T--) { 34 | long long A, B; 35 | cin >> A >> B; 36 | Server s; 37 | try { 38 | int result = s.compute(A, B); 39 | cout << result << endl; 40 | } catch (std::bad_alloc& e) { 41 | cout << "Not enough memory\n"; 42 | } catch (std::exception& e) { 43 | cout << "Exception: " << e.what() << endl; 44 | } catch (exception &e){ 45 | cout << "Other Exception\n"; 46 | } catch (int i) { 47 | cout << "Other Exception\n"; 48 | } 49 | } 50 | cout << Server::getLoad() << endl; 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /DSA C++/hashing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define max 10 5 | using namespace std; 6 | void insert(); 7 | void display(); 8 | int main() 9 | { 10 | int ch; 11 | while(1) 12 | { 13 | cout<<"\n1.Insert Element\n2.Display\n3.Exit\n"; 14 | cin>>ch; 15 | switch(ch) 16 | { 17 | case 1: 18 | insert(); 19 | break; 20 | case 2: 21 | display(); 22 | break; 23 | case 3: 24 | exit(0); 25 | default: 26 | cout<<"Wrong Choice :)"; 27 | } 28 | } 29 | } 30 | struct node 31 | { 32 | int data; 33 | struct node *next; 34 | }; 35 | 36 | struct node *head[max]={NULL},*c; 37 | 38 | void insert() 39 | { 40 | int i,key,ele; 41 | cout<<"\nEnter element\n"; 42 | cin>>ele; 43 | i=ele%max; 44 | struct node *newnode=new node(); 45 | newnode->data=ele; 46 | newnode->next = NULL; 47 | if(head[i]==NULL) 48 | head[i]=newnode; 49 | else 50 | { 51 | c=head[i]; 52 | while(c->next!=NULL) 53 | { 54 | c=c->next; 55 | } 56 | c->next=newnode; 57 | } 58 | } 59 | void display() 60 | { 61 | int i; 62 | for(i=0;inext) 72 | { 73 | cout<data<<"\t"; 74 | } 75 | } 76 | } 77 | 78 | } -------------------------------------------------------------------------------- /Geekforgeeks/G)Sorting/15_Naive_partition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int partition(int arr[],int l,int r,int p) //this solution is stable 4 | { 5 | swap(arr[r],arr[p]); 6 | p=r; //we shift pivot to the end so that swapping of pivot does not take place because then it will lead to pivot being changed 7 | int temp[r-l+1],index=0; 8 | for(int i=l;i<=r;i++) 9 | { 10 | if(arr[i]<=arr[p]) 11 | { 12 | temp[index]=arr[i]; 13 | index++; 14 | } 15 | } 16 | 17 | int res=l+index-1; 18 | for(int i=l;i<=r;i++) 19 | { 20 | if(arr[i]>arr[p]) 21 | { 22 | temp[index]=arr[i]; 23 | index++; 24 | } 25 | } 26 | for(int i=l;i<=r;i++) 27 | { 28 | arr[i]=temp[i]; 29 | } 30 | return res; 31 | //Time complexity : theta(n) 32 | //Auxillary complexity : theta(n) 33 | //This algorithm is stable. 34 | } 35 | void print(int arr[], int n) 36 | { 37 | for (int i = 0; i < n; i++) 38 | cout << arr[i] << " "; 39 | cout << endl; 40 | } 41 | int main(){ 42 | int arr[]={3,8,6,12,10,7}; 43 | int p=1; 44 | int size=sizeof(arr)/sizeof(int); 45 | print(arr,size); 46 | partition(arr,0,size-1,p); 47 | print(arr,size); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/13_two_pointer_triplet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int sum(int arr[], int size, int x) 4 | { 5 | for (int i = 0; i < size; i++) 6 | { 7 | int xf = x - arr[i]; 8 | int l = i+1, r = size - 1; 9 | while (l < r) 10 | { 11 | if (l == i) 12 | l++; 13 | if (r == i) 14 | r--; 15 | 16 | int sum = arr[l] + arr[r]; 17 | if (sum > xf) 18 | r--; 19 | else if (sum < xf) 20 | l++; 21 | else 22 | { 23 | printf("%d+%d+%d\n", arr[i], arr[l], arr[r]); 24 | return 1; 25 | } 26 | } 27 | } 28 | return 0; 29 | //Time complexity : O(n^2) 30 | //Space complexity : O(1) 31 | 32 | //For sorted pair : we use two pointer approach since that take O(n) time. 33 | //For unsorted pair : we use hashing since two pointer with sorting will require O(nlogn) time 34 | //For sorted triplet finding: two pointer 35 | //For unsorted triplet finding: sorting then two pointer 36 | } 37 | int main() 38 | { 39 | int arr[] = {2, 4, 8, 9, 20, 40}; 40 | int size = sizeof(arr) / sizeof(int); 41 | int wsum = 32; 42 | cout << sum(arr, size, wsum) << endl; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Geekforgeeks/H)Hashing/3_Hash_functions.txt: -------------------------------------------------------------------------------- 1 | Hash functions group large amount of keys into smaller groups. 2 | 3 | Also hash functions should generate the same value for same input key. 4 | 5 | How hash functions work: 6 | 1)Should always map a large key to same small key. 7 | 2)Should generate values from 0 to m-1 8 | 3)Should be fast , O(1) for integers and O(len) for strings of length of len 9 | 4)Should uniformly distribute large keys into hash table slot. 10 | 11 | Some examples of hash functions: 12 | 13 | 1)Hash_func(large_keys)=large_keys%m (though this leads to lot of collision) 14 | (typically this number m is chosen as a prime number close to the size of the hash table as less common 15 | factor and better distibution of large keys) 16 | (bad decision is to use power of 2 or 10 or prime numbers close to them as they consider only some bits 17 | or some digits) 18 | (so the above is simple/bad hash function ) 19 | 20 | 2)For strings, weighting sum. 21 | str[]="abcd" 22 | sum=str[0]*x^0+str[1]*x^1... 23 | (This hash function is better than taking just the sum of ASCII values) 24 | 25 | 3)Universal hashing : means to have a set of hash functions and to randomly pick one of them to hash 26 | a value. 27 | 28 | IMP PTS: 29 | Collision is bound to happen. 30 | 31 | Size of hash table will be proportional to the numbers of elements we want to insert in it. 32 | -------------------------------------------------------------------------------- /Hackerrank/Other_Concepts/heightofbinarytree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Node { 6 | public: 7 | int data; 8 | Node *left; 9 | Node *right; 10 | Node(int d) { 11 | data = d; 12 | left = NULL; 13 | right = NULL; 14 | } 15 | }; 16 | 17 | class Solution { 18 | public: 19 | Node* insert(Node* root, int data) { 20 | if(root == NULL) { 21 | return new Node(data); 22 | } else { 23 | Node* cur; 24 | if(data <= root->data) { 25 | cur = insert(root->left, data); 26 | root->left = cur; 27 | } else { 28 | cur = insert(root->right, data); 29 | root->right = cur; 30 | } 31 | 32 | return root; 33 | } 34 | } 35 | /*The tree node has data, left child and right child 36 | class Node { 37 | int data; 38 | Node* left; 39 | Node* right; 40 | }; 41 | 42 | */ 43 | int height(Node* root) { 44 | // Write your code here. 45 | if(root==NULL) 46 | { 47 | return -1; 48 | } 49 | int lh=height(root->left); 50 | int rh=height(root->right); 51 | return 1+max(lh,rh); 52 | } 53 | 54 | }; //End of Solution 55 | -------------------------------------------------------------------------------- /Leetcode/Cpp/429. N-ary Tree Level Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector children; 7 | 8 | Node() {} 9 | 10 | Node(int _val) { 11 | val = _val; 12 | } 13 | 14 | Node(int _val, vector _children) { 15 | val = _val; 16 | children = _children; 17 | } 18 | }; 19 | */ 20 | 21 | class Solution 22 | { 23 | public: 24 | vector> levelOrder(Node *root) 25 | { 26 | queue q; 27 | vector> v; 28 | vector t; 29 | q.push(root); 30 | q.push(NULL); 31 | if (root == NULL) 32 | { 33 | return v; 34 | } 35 | while (!q.empty()) 36 | { 37 | Node *temp = q.front(); 38 | q.pop(); 39 | if (temp == NULL) 40 | { 41 | v.push_back(t); 42 | t.clear(); 43 | if (q.empty()) 44 | { 45 | return v; 46 | } 47 | else 48 | q.push(NULL); 49 | } 50 | else 51 | { 52 | t.push_back(temp->val); 53 | for (auto x : temp->children) 54 | q.push(x); 55 | } 56 | } 57 | return v; 58 | } 59 | }; -------------------------------------------------------------------------------- /Geekforgeeks/F)Searching/19_repeating_elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* 4 | Conditions for this problem: 5 | 1)Array size >=2 6 | 2)Only one element repeats 7 | 3)All the elements are from 0 to max(arr) 8 | 4)0<=max(arr)<=(n-2) 9 | */ 10 | //The only difference between the prev and this code is we increase slow and fast by 1 so that so prevent self loop 11 | int repeating_element(int arr[],int size) 12 | { 13 | //We are using the algorithm which is used to find the first element of a loop in linked list 14 | 15 | int slow=arr[0]+1,fast=arr[0]+1; 16 | //in phase 1 we move one ptr one steps at a time and another two steps at a time. 17 | //when we do this we will surely meet once inside the loop 18 | do 19 | { 20 | slow=arr[slow]+1; 21 | fast=arr[arr[fast]+1]+1; 22 | } while (slow!=fast); 23 | slow=arr[0]+1; 24 | /*in phase 2 we move a element outside and one remains inside. And when we move these two at the same phase 25 | then they surely meet at the first element of the loop*/ 26 | while(slow!=fast) 27 | { 28 | slow=arr[slow]+1; 29 | fast=arr[fast]+1; 30 | } 31 | return slow-1; 32 | } 33 | int main(){ 34 | int arr[]={0,1,2,3,4,5,5,5,5,6}; 35 | int size=sizeof(arr)/sizeof(int); 36 | cout< arr[j]){ 8 | //swap elements 9 | temp = arr[j-1]; 10 | arr[j-1] = arr[j]; 11 | arr[j] = temp; 12 | } 13 | 14 | } 15 | } 16 | 17 | } 18 | public static void main(String[] args) { 19 | int arr[] ={3,60,35,2,45,320,5}; 20 | 21 | System.out.println("Array Before Bubble Sort"); 22 | for(int i=0; i < arr.length; i++){ 23 | System.out.print(arr[i] + " "); 24 | } 25 | System.out.println(); 26 | 27 | bubbleSort(arr);//sorting array elements using bubble sort 28 | 29 | System.out.println("Array After Bubble Sort"); 30 | for(int i=0; i < arr.length; i++){ 31 | System.out.print(arr[i] + " "); 32 | } 33 | 34 | } 35 | } 36 | --------------------------------------------------------------------------------