├── .DS_Store ├── .deepsource.toml ├── .github ├── ISSUE_TEMPLATE │ ├── Contest Solutions.md │ ├── bug_report.md │ └── feature_request_New_Solution.md ├── pull_request_template.md ├── release-drafter.yml └── workflows │ ├── codeql-analysis.yml │ └── msvc-analysis.yml ├── CONTRIBUTERS.md ├── Codechef Solutions ├── Codechef Starter 14 solutions ├── c++ │ ├── AUG21C_codechef_contest │ │ └── problemDiff.cpp │ ├── October Challenge │ │ ├── ThreeBoxQ2.cpp │ │ └── WhichDivQ1.cpp │ ├── START6C-codechef_contest │ │ ├── cricRank.cpp │ │ └── thredice.cpp │ └── readme.md ├── c │ ├── olyrank │ ├── olyrank.c │ ├── probdiff │ ├── probdiff.c │ └── readme.md ├── java │ └── readme.md └── python │ └── readme.md ├── Codeforces Solutions ├── 1569A Solutions ├── c++ │ ├── 1579 │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── E1.cpp │ ├── readme.md │ └── solution for cube.cpp ├── c │ └── readme.md ├── java │ └── readme.md ├── python │ └── readme.md └── readme.md ├── Content.md ├── Geeks For Geeks Solutions ├── c++ │ ├── hit_most_balloons.cpp │ ├── min_laptops_required.cpp │ └── readme.md ├── c │ └── readme.md ├── java │ └── readme.md ├── python │ ├── add_minimum_characters.py │ ├── frogs_and_jumps.py │ ├── max_winning_score.py │ ├── readme.md │ └── transform_to_sumTree.py └── readme.md ├── Hackerearth Solutions ├── c++ │ └── readme.md ├── c │ └── readme.md ├── java │ └── readme.md ├── python │ ├── Problem-01 │ │ ├── README.md │ │ └── Solution.py │ └── readme.md └── readme.md ├── Hackerrank Solutions ├── algorithms │ ├── Apple_and_orange.py │ ├── Birthday_cake_candles.py │ └── floyd_city_of_blinding_lights.py ├── c++ │ ├── Breaking_Records.cpp │ ├── Find Merge Point of Two Lists │ │ ├── Find Merge Point of Two Lists.cpp │ │ ├── input.JPG │ │ └── output.JPG │ ├── Trees │ │ └── postOrder Traversal │ │ │ └── postOrder Traversal.cpp │ ├── compair two link-list │ │ ├── comparetwolinkedlist.cpp │ │ ├── input.jpeg │ │ └── output.jpeg │ └── readme.md ├── c │ └── readme.md ├── java │ └── readme.md └── python │ └── Find a string.py ├── Hashing ├── c++ │ ├── Longest consecutive subsequence - MED.cpp │ ├── Nuts and Bolts Problem - MED.cpp │ ├── Print Anagrams Together - MED.cpp │ ├── Sort an array according to the other - MED.cpp │ ├── Subarrays with equal 1s and 0s - MED.cpp │ └── readme.md ├── c │ └── readme.md ├── java │ └── readme.md └── python │ └── readme.md ├── LICENSE ├── LeetCode └── Hard │ ├── 149. Max Points on a Line.cpp │ ├── 354. Russian Doll Envelopes.cpp │ ├── 37_Sudoku_Solver.cpp │ ├── 4. Median of Two Sorted Arrays.cpp │ ├── 44_Wildcard_Matching.cpp │ ├── 543_Diameter_of_Binary_Tree.cpp │ ├── 76. Minimum Window Substring.cpp │ ├── 84_Largest_Rectangle_in_Histogram │ ├── 887. Super Egg Drop.cpp │ ├── 92. Reverse Linked List II.cpp │ ├── Permutation_sequence.cpp │ ├── Reverse Nodes in k-Group.cpp │ ├── best time to buy and sell stock.cpp │ ├── editDistance.cpp │ ├── max_points_in_line.cpp │ ├── median of two sorted arrays.cpp │ ├── median_of_2_sorted_arrays.cpp │ ├── merge k sorted lists.cpp │ └── reverse_nodes_in_k_groups.cpp ├── Leetcode 250 DSA sheet Solutions ├── arrays │ └── Easy │ │ ├── Best Time to Buy and Sell Stock II.cpp │ │ ├── Best Time to Buy and Sell Stock.cpp │ │ ├── Fibonacci Number.cpp │ │ ├── Find pivot Index.cpp │ │ ├── Majority Element.cpp │ │ ├── Merge Sorted Array.cpp │ │ ├── Move Zeroes.cpp │ │ ├── Remove Duplicates from Sorted Array.cpp │ │ ├── Running Sum of 1d Array.cpp │ │ ├── Squares of a Sorted Array.cpp │ │ └── Two Sum.cpp └── strings │ └── Easy │ └── add strings.cpp ├── Leetcode Solutions ├── c++ │ ├── 173. Binary Search Tree Iterator │ │ ├── 173. Binary Search Tree Iterator.cpp │ │ └── README.md │ ├── 260. Single Number III │ │ ├── 260. Single Number III.cpp │ │ └── README.md │ ├── 3Sum.cpp │ ├── ConstructBinaryTreefromPreorderandInorderTraversal.cpp │ ├── Equal stacks.cpp │ ├── Kids With the Greatest Number of Candies.cpp │ ├── Map_sum_pairs.cpp │ ├── MergeKSortedList.cpp │ ├── Next_Permutation.cpp │ ├── Remove_Nth_node_from_end_of_list.cpp │ ├── Single_Number-II.cpp │ ├── XOR Queries of a Subarray.cpp │ ├── cycle_detection.cpp │ ├── n queens.cpp │ ├── readme.md │ └── rotate array.cpp ├── c │ ├── Rotate_list.c │ └── readme.md ├── java │ ├── 410. Split Array Largest Sum.java │ ├── ContainerWithMostWater.java │ ├── ListNode.java │ ├── Reverse Linked List II.java │ └── readme.md ├── python │ ├── Remove Nth Node From End of List.py │ └── readme.md └── readme.md ├── README.md ├── algorithms ├── c++ │ ├── kadaneAlgorithm.cpp │ └── readme.md ├── c │ ├── BankersAlgo.c │ ├── Dijkstra’sAlgoritm.c │ ├── Prime Numbers │ │ └── Prime num Algo 1.c │ ├── leap year check │ │ ├── Leap Year.PNG │ │ ├── Leap_Year_Check.c │ │ └── leap_year.py │ └── matrix multiplication.c ├── java │ ├── ActivitySelection.java │ ├── Karatsuba.java │ ├── MatrixChain.java │ ├── Subset sum with backtracking.java │ ├── binarySearch.java │ └── readme.md └── python │ ├── Build-List.py │ ├── Convex_Hull.py │ ├── SortingAlgorithm_visualiser.py │ ├── buildDictionary.py │ └── readme.md ├── arrays ├── c++ │ ├── Merge-Two_Sorted_Arrays.cpp │ ├── Rearrange an array with O(1) extra space.cpp │ ├── deletion in array.cpp │ ├── insertion in array.cpp │ ├── max and min in array.cpp │ └── traversalInArray.cpp ├── c │ ├── Prime Numbers │ │ └── Prime num Algo 1.c │ ├── find max and min number.c │ ├── jackal algorithm.c │ ├── leap year check │ │ ├── Leap Year.PNG │ │ └── leap_year.py │ ├── left rotation of array.c │ └── right rotation of array.c ├── java │ └── readme.md └── python │ └── readme.md ├── binaryTree ├── c++ │ ├── Balanced or Not balance BST.cpp │ ├── Boundary traversal anticlockwise.cpp │ ├── Diameter Of BST.cpp │ ├── Inorder traversal iterative method using stack.cpp │ ├── Leastcommonancestor.cpp │ ├── Max path sum bst.cpp │ ├── Postorder traversal iterative method.cpp │ ├── Preorder traversal iterative method using stack.cpp │ ├── Same or not BST.cpp │ ├── binary_tree_inorder_preorder_postorder.cpp │ ├── depth and height.png │ ├── inorder.cpp │ ├── level order traversal.cpp │ ├── max height of bst.cpp │ ├── preorder postorder inorder traversals.cpp │ ├── readme.md │ ├── reverseLevelOrderTraversal.cpp │ └── zigzag in bst.cpp ├── c │ ├── binary-search-tree.c │ └── readme.md ├── java │ ├── BinaryTree.java │ ├── MainBinaryTree.java │ ├── Node.java │ ├── bfs.java │ └── readme.md └── python │ ├── Binary Search Tree │ ├── inorder-traversal.py │ ├── post-order-traversal.py │ └── pre-order-traversal.py │ ├── lowest-common-ancestor-binary-tree.py │ └── readme.md ├── c++ ├── 1.cpp ├── 33.Finding max & min in single scan .cpp ├── 5)Smallest subarray with all occurrences of a most frequent element.cpp ├── Accessing Inherited functions.cpp ├── Check Prime Number.cpp ├── Clear All Bits from MSB.cpp ├── Convert1Dto2DArray.cpp ├── DecimalToBinary.cpp ├── Decimal_to_binary(using string).cpp ├── DoublyLinkedList.cpp ├── DoublyLinkedList.exe ├── GCD.cpp ├── Key_Segment.cpp ├── LCM.cpp ├── LeapYear.cpp ├── Left_and_Right_Rotation_of_a_String.cpp ├── Mergesort.cpp ├── Multi level inheritance.cpp ├── N integers to binary.cpp ├── OddEven.cpp ├── Reverse_Linklist.cpp ├── STL_UsingMap.cpp ├── TravelingSalesman.cpp ├── WordCounter.cpp ├── armstrong.cpp ├── binary-addition.cpp ├── binary_search_iterative.cpp ├── conversions.cpp ├── count_ones_in_binary.cpp ├── diamond.cpp ├── exponent_calculator.cpp ├── factorial.cpp ├── fibonacci.cpp ├── hollowdiamond.cpp ├── kmp_mansi.cpp ├── mean_median_mode.cpp ├── move_last_element_to_first.cpp ├── ncr.cpp ├── num_of_islands.cpp ├── painterpartitionproblem.cpp ├── pascal-triangle.cpp ├── pattern1.cpp ├── pattern1.exe ├── prime-or-not.cpp ├── prime-till-n.cpp ├── primefunction.cpp ├── pyramid.cpp ├── pythagorean-triplets.cpp ├── reverse-a-number.cpp ├── sample.cpp ├── stringreverse.cpp ├── tempCodeRunnerFile.cpp ├── testing.cpp └── x.cpp ├── c ├── Merge_between_list.c ├── Remove_node.c ├── Reverse_list.c ├── palindrome.c └── readme.md ├── dynamic programming ├── c++ │ ├── 0-1Knapsack.cpp │ ├── Bellman_Ford_Algorithm.cpp │ ├── Combination Sum.cpp │ ├── LCS.cpp │ ├── LongestCommonSubstring.cpp │ ├── coinChange.cpp │ ├── nthFibonacciNum.cpp │ └── readme.md ├── c │ └── readme.md ├── java │ ├── 0-1knapsack.java │ └── readme.md └── python │ └── readme.md ├── graph ├── c++ │ ├── BFS.cpp │ ├── Bipartitegraph.cpp │ ├── DFS.cpp │ ├── Graph Implementation using Adjacency List.c++ │ ├── MinimumSpanningTree.cpp │ ├── Topological_sort.cpp │ ├── chk_cycle_directed.cpp │ └── readme.md ├── c │ ├── Directed Graph Implementation.c │ ├── Undirected Graph Implementation.c │ └── readme.md ├── java │ ├── Breadth First Traversal.java │ ├── Depth First Traversal.java │ ├── Graph in Java.java │ ├── Mutation Operation.java │ ├── Simple Graph.java │ └── readme.md └── python │ ├── Display Graph Edges.py │ ├── Display Graph Vertices.py │ └── readme.md ├── greedy └── python │ └── kruskal.py ├── hacktoberfest_logo.svg ├── heap └── java │ └── HeapStructure.java ├── java ├── KadaneAlgo.java ├── Linear_Search.java ├── PetersonNumber.java ├── SearchInMatrix.java ├── SortMatrixRowColWise.java ├── java loops └── spiralTraverse.java ├── linked list ├── Singly linked list │ ├── c++ │ │ ├── MenuSinglyLinkedList.cpp │ │ ├── readme.md │ │ ├── single linked list.cpp │ │ ├── single ll deletion.cpp │ │ └── single ll insertion.cpp │ ├── c │ │ ├── Prime Numbers │ │ │ └── Prime num Algo 1.c │ │ ├── Reversing elements using auxiliary array in LL.c │ │ ├── insertion deletion merging sorting displaying linked list.c │ │ ├── leap year check │ │ │ ├── Leap Year.PNG │ │ │ └── leap_year.py │ │ ├── readme.md │ │ ├── reverse linked list.c │ │ └── traversing singly linked list.c │ ├── insertion deletion merging sorting displaying ques.PNG │ ├── java │ │ ├── SinglyLinkedList.java │ │ └── readme.md │ └── python │ │ └── readme.md ├── circular linked list │ ├── c++ │ │ └── readme.md │ ├── c │ │ ├── Prime Numbers │ │ │ └── Prime num Algo 1.c │ │ ├── leap year check │ │ │ ├── Leap Year.PNG │ │ │ └── leap_year.py │ │ └── readme.md │ ├── java │ │ └── readme.md │ └── python │ │ ├── circular-doubly-linked-list.py │ │ ├── circular-singly-linked-list.py │ │ └── readme.md └── doubly linked list │ ├── c++ │ ├── double_ll.c++ │ └── readme.md │ ├── c │ ├── Prime Numbers │ │ └── Prime num Algo 1.c │ ├── doubly linked list implementation.c │ ├── insertion deletion displau doubly linked list.c │ ├── leap year check │ │ ├── Leap Year.PNG │ │ └── leap_year.py │ └── readme.md │ ├── java │ └── readme.md │ └── python │ └── readme.md ├── python ├── Games │ └── chopsticks.ipynb └── readme.md ├── queue ├── c++ │ ├── Queue.cpp │ └── readme.md ├── c │ ├── Prime Numbers │ │ └── Prime num Algo 1.c │ ├── circular queue array implementation.c │ ├── circular queue linked list implementation.c │ ├── enq deq in queue using linked list.c │ ├── leap year check │ │ ├── Leap Year.PNG │ │ └── leap_year.py │ ├── queue array implementation.c │ └── queue linked list implementation.c ├── java │ └── readme.md └── python │ ├── Queue.py │ └── readme.md ├── recursion ├── c++ │ └── readme.md ├── c │ └── readme.md ├── java │ └── readme.md └── python │ ├── readme.md │ └── tower_of_hanoi.py ├── repository_poster.gif ├── searching ├── c++ │ ├── linear-search.cpp │ └── readme.md ├── c │ ├── Prime Numbers │ │ └── Prime num Algo 1.c │ ├── binary search.c │ ├── leap year check │ │ ├── Leap Year.PNG │ │ └── leap_year.py │ ├── linearsearch in c │ ├── readme.md │ └── recursive binary search.c ├── java │ ├── LinearSearch.java │ └── readme.md └── python │ ├── binary-search.py │ ├── linear-search.py │ └── readme.md ├── sorting ├── c++ │ ├── Wave Sort.cpp │ ├── bubbleSort.cpp │ ├── merge_sort.cpp │ ├── merge_sort_optimised.cpp │ ├── quick_sort.cpp │ ├── quick_sort_optimised.cpp │ ├── readme.md │ ├── selectionsort.cc │ └── turboSortArray.cpp ├── c │ ├── Bubble Insertion Selection Sorting.c │ ├── MergeSort.c │ ├── Prime Numbers │ │ └── Prime num Algo 1.c │ ├── heap.c │ ├── leap year check │ │ ├── Leap Year.PNG │ │ └── leap_year.py │ └── selection sort without function.c ├── java │ ├── BucketSort.java │ ├── InsertionSort.class │ ├── MergeSort.java │ ├── bubbleSort.java │ ├── countSort.java │ ├── dnfSort.java │ ├── insertionSort.java │ ├── quick sort │ │ ├── QuickSort.java │ │ └── hoaresPartitioning-quick-sort.java │ └── selectionSort.java └── python │ ├── bubble-sort.py │ ├── bucket-sort.py │ ├── heap-sort.py │ ├── insertion-sort.py │ ├── merge-sort.py │ ├── quick-sort.py │ └── selection-sort.py ├── stacks ├── c++ │ ├── a.exe │ ├── readme.md │ ├── stack1.cpp │ ├── stack2.cpp │ ├── stack3.cpp │ └── stack4.cpp ├── c │ ├── Infix-to-Postfix-stack.c │ ├── Prime Numbers │ │ └── Prime num Algo 1.c │ ├── Stack_operations.c │ ├── leap year check │ │ ├── Leap Year.PNG │ │ └── leap_year.py │ ├── leapyear.c │ ├── reverse recurssion to reverse a stack.c │ ├── stack array implementation.c │ └── stack linked list implementation.c ├── java │ └── readme.md └── python │ ├── Stack.py │ └── readme.md ├── strings ├── c++ │ ├── Rabin _Karp_Algorithm.cpp │ └── readme.md ├── c │ ├── Prime Numbers │ │ └── Prime num Algo 1.c │ ├── leap year check │ │ ├── Leap Year.PNG │ │ └── leap_year.py │ ├── reverse array.c │ └── string reverse.c ├── java │ └── readme.md └── python │ ├── ReverseString.py │ └── readme.md └── web_page ├── img ├── Hi.gif ├── logo.jpg ├── poster.png ├── project-admin.jfif └── repo.png ├── index.html ├── script.js └── style.css /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/.DS_Store -------------------------------------------------------------------------------- /.deepsource.toml: -------------------------------------------------------------------------------- 1 | version = 1 2 | 3 | [[analyzers]] 4 | name = "java" 5 | enabled = true 6 | 7 | [analyzers.meta] 8 | runtime_version = "11" -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Contest Solutions.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Contest Solution 3 | about: Write an appropriate Contest Question 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | 11 | **Is your feature request related to a problem/Question? Please describe.** 12 | A clear and concise description of what the problem/question is. 13 | 14 | **Describe the solution you'd like** 15 | A clear and concise description of what you want to happen. 16 | 17 | **Additional context** 18 | Add any other context or screenshots about the feature request here if any(optional). 19 | 20 | **Describe the bug** 21 | A clear and concise description of what the bug is. 22 | 23 | **To Reproduce** 24 | Steps to reproduce the behavior: 25 | 1. Go to '...' 26 | 2. Click on '....' 27 | 3. Scroll down to '....' 28 | 4. See error 29 | 30 | **Expected behavior** 31 | A clear and concise description of what you expected to happen. 32 | 33 | **Screenshots** 34 | If applicable, add screenshots to help explain your problem. 35 | 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request_New_Solution.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request add new solution 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/Question? Please describe.** 11 | A clear and concise description of what the problem/question is. 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Additional context** 17 | Add any other context or screenshots about the feature request here if any(optional). 18 | 19 | **Describe the bug** 20 | A clear and concise description of what the bug is. 21 | 22 | **To Reproduce** 23 | Steps to reproduce the behavior: 24 | 1. Go to '...' 25 | 2. Click on '....' 26 | 3. Scroll down to '....' 27 | 4. See error 28 | 29 | **Expected behavior** 30 | A clear and concise description of what you expected to happen. 31 | 32 | **Screenshots** 33 | If applicable, add screenshots to help explain your problem. 34 | 35 | 36 | **Additional context** 37 | Add any other context about the problem here. 38 | 39 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | # Description 2 | 3 | 4 | 5 | Fixes # 6 | 7 | 8 | 9 | ## Type of change 10 | 11 | 12 | 13 | - [ ] Bug fix (non-breaking change which fixes an issue) 14 | - [ ] New feature/solution added 15 | - [ ] Contest solution added 16 | - [ ] This change requires a documentation update 17 | - [ ] Documentation/readme Update 18 | 19 | # Checklist: 20 | 21 | 22 | 23 | - [] My code follows the style guidelines(Clean Code) of this project 24 | - [] I have performed a self-review of my own code 25 | - [] I have commented my code, particularly in hard-to-understand areas 26 | - [] I have created a helpful and easy to understand `README.md` 27 | - [] I have made corresponding changes to the documentation 28 | - [] My changes generate no new warnings 29 | - [] I have updated the readme with tick in question in appropriate language. 30 | 31 | 32 | -------------------------------------------------------------------------------- /.github/release-drafter.yml: -------------------------------------------------------------------------------- 1 | template: | 2 | ## What’s Changed 3 | 4 | $CHANGES 5 | -------------------------------------------------------------------------------- /Codechef Solutions/c++/October Challenge/ThreeBoxQ2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int t; 5 | cin>>t; 6 | while(t--){ 7 | int a,b,c,d; 8 | cin>>a>>b>>c>>d; 9 | if(a<=d and b<=d and c<=d){ 10 | int sum=0; 11 | sum=(a+b+c); 12 | 13 | if(sum<=d){ 14 | cout<<1<d){ 17 | if((a+b)<=d or (b+c)<=d or (a+c)<=d ){ 18 | cout<<2< 2 | using namespace std; 3 | int main(){ 4 | int t; 5 | cin>>t; 6 | while(t--){ 7 | int a, b; 8 | cin>>a>>b; 9 | if(a >0 and b>0){ 10 | cout<<"Solution"< 2 | #define ll long long int 3 | #define debug(x) cout << #x << "=" << x << endl 4 | #define debug2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl 5 | #define PI 3.1415926535897932384626 6 | 7 | using namespace std; 8 | 9 | void solve() 10 | { 11 | string s; 12 | cin >> s; 13 | ll n = s.size(); 14 | if (n % 2) 15 | { 16 | cout << "NO"; 17 | } 18 | else 19 | { 20 | ll ans = 0; 21 | for (auto i : s) 22 | { 23 | if (i == 'A') ans++; 24 | else if (i == 'B') ans--; 25 | else ans++; 26 | } 27 | if (ans) 28 | { 29 | cout << "NO"; 30 | } 31 | else 32 | { 33 | cout << "YES"; 34 | } 35 | } 36 | cout << "\n"; 37 | } 38 | 39 | int main() 40 | { 41 | ios_base::sync_with_stdio(false); 42 | cin.tie(NULL); 43 | #ifndef ONLINE_JUDGE 44 | freopen("input.txt", "r", stdin); 45 | freopen("output.txt", "w", stdout); 46 | #endif 47 | 48 | ll t; cin >> t; while (t--) 49 | { 50 | solve(); 51 | } 52 | } -------------------------------------------------------------------------------- /Codeforces Solutions/c++/1579/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long int 3 | #define debug(x) cout << #x << "=" << x << endl 4 | #define debug2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl 5 | #define PI 3.1415926535897932384626 6 | 7 | using namespace std; 8 | 9 | void solve() 10 | { 11 | ll n; 12 | cin >> n; 13 | ll a[n]; 14 | ll b[n]; 15 | for (ll i = 0; i < n; i++) 16 | { 17 | cin >> a[i]; 18 | b[i] = a[i]; 19 | } 20 | sort(b, b + n); 21 | vector> ans; 22 | for (ll i = 0; i < n; i++) 23 | { 24 | if (a[i] != b[i]) 25 | { 26 | ll j = i + 1; 27 | while (a[j] != b[i]) 28 | { 29 | j++; 30 | } 31 | vectortemp; 32 | temp.push_back(a[j]); 33 | for (int k = i; k < j; k++) 34 | { 35 | temp.push_back(a[k]); 36 | } 37 | for (int k = i; k <= j; k++) 38 | { 39 | a[k] = temp[k - i]; 40 | } 41 | ans.push_back({i + 1, j + 1, j - i}); 42 | } 43 | } 44 | cout << ans.size() << "\n"; 45 | for (auto i : ans) 46 | { 47 | for (auto j : i) 48 | { 49 | cout << j << " "; 50 | } 51 | cout << "\n"; 52 | } 53 | } 54 | 55 | int main() 56 | { 57 | ios_base::sync_with_stdio(false); 58 | cin.tie(NULL); 59 | #ifndef ONLINE_JUDGE 60 | freopen("input.txt", "r", stdin); 61 | freopen("output.txt", "w", stdout); 62 | #endif 63 | 64 | ll t; cin >> t; while (t--) 65 | { 66 | solve(); 67 | } 68 | 69 | } -------------------------------------------------------------------------------- /Codeforces Solutions/c++/1579/E1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long int 3 | #define debug(x) cout << #x << "=" << x << endl 4 | #define debug2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl 5 | #define PI 3.1415926535897932384626 6 | 7 | using namespace std; 8 | 9 | void solve() 10 | { 11 | ll n; 12 | cin >> n; 13 | deque q; 14 | ll temp; 15 | cin >> temp; 16 | q.push_back(temp); 17 | for (ll i = 1; i < n; i++) 18 | { 19 | cin >> temp; 20 | if (q.front() >= temp) 21 | { 22 | q.push_front(temp); 23 | } 24 | else 25 | { 26 | q.push_back(temp); 27 | } 28 | } 29 | while (!q.empty()) 30 | { 31 | cout << q.front() << " "; 32 | q.pop_front(); 33 | } 34 | cout << "\n"; 35 | } 36 | 37 | int main() 38 | { 39 | ios_base::sync_with_stdio(false); 40 | cin.tie(NULL); 41 | #ifndef ONLINE_JUDGE 42 | freopen("input.txt", "r", stdin); 43 | freopen("output.txt", "w", stdout); 44 | #endif 45 | 46 | ll t; 47 | cin >> t; 48 | while (t--) 49 | { 50 | solve(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Codeforces Solutions/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /Codeforces Solutions/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /Codeforces Solutions/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /Codeforces Solutions/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /Codeforces Solutions/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes Codeforces Contests Solutions . -------------------------------------------------------------------------------- /Geeks For Geeks Solutions/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /Geeks For Geeks Solutions/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /Geeks For Geeks Solutions/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /Geeks For Geeks Solutions/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /Geeks For Geeks Solutions/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes Geeks For Geeks Contests Solutions . -------------------------------------------------------------------------------- /Hackerearth Solutions/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /Hackerearth Solutions/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /Hackerearth Solutions/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /Hackerearth Solutions/python/Problem-01/README.md: -------------------------------------------------------------------------------- 1 | ::SOLUTION OF HACKEREARTH PROBLEM CALLED "NUMBER OF STEPS" IN PYTHON:: 2 | 3 | PROBLEM STATEMENT: 4 | 5 | You are given two arrays a1,a2,a3....an and b1,b2,b3,.....bn. 6 | In each step, you can set ai=ai-bi if ai>bi. 7 | Determine the minimum number of steps that are required to make all a's equal. 8 | 9 | 10 | Input format: 11 | 12 | First line: n 13 | Second line: a1,a2,.......,an 14 | Third line: b1,b2,.......,bn 15 | 16 | 17 | Output format: 18 | 19 | Print the minimum number of steps that are required to make all a's equal. If it is not possible, then print -1. 20 | 21 | 22 | Constraints: 23 | 1<_n, ai, bi<_5000 24 | 25 | 26 | Sample input: 27 | 28 | 2 29 | 5 6 30 | 4 3 31 | 32 | 33 | Sample output: 34 | 35 | -1 36 | -------------------------------------------------------------------------------- /Hackerearth Solutions/python/Problem-01/Solution.py: -------------------------------------------------------------------------------- 1 | #Solution Of Above Problem Statement: 2 | 3 | n=int(input()) 4 | 5 | a=list(map(int,input().split())) 6 | 7 | b=list(map(int,input().split())) 8 | 9 | k=min(a) 10 | 11 | c=0 12 | 13 | flag=True 14 | 15 | for i in range(n): 16 | 17 | while(a[i]>k and b[i]!=0): 18 | 19 | a[i]=a[i]-b[i] 20 | 21 | c=c+1 22 | 23 | if(a[i]<0): 24 | 25 | flag=False 26 | 27 | else: 28 | 29 | k=a[i] 30 | 31 | for i in a: 32 | 33 | if(i!=min(a)): 34 | 35 | flag=False 36 | 37 | if(flag==False): 38 | 39 | print("-1") 40 | 41 | else: 42 | 43 | print(c) 44 | -------------------------------------------------------------------------------- /Hackerearth Solutions/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /Hackerearth Solutions/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes Hackerearth Contests Solutions . -------------------------------------------------------------------------------- /Hackerrank Solutions/algorithms/Apple_and_orange.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the countApplesAndOranges function below. 10 | def countApplesAndOranges(s, t, a, b, apples, oranges): 11 | totalapples = totaloranges = 0 12 | for i in range(len(apples)): 13 | if s <= a + apples[i] <=t: 14 | totalapples += 1 15 | for i in range(len(oranges)): 16 | if s <= b + oranges[i] <=t: 17 | totaloranges += 1 18 | print(totalapples) 19 | print(totaloranges) 20 | 21 | if __name__ == '__main__': 22 | st = input().split() 23 | 24 | s = int(st[0]) 25 | 26 | t = int(st[1]) 27 | 28 | ab = input().split() 29 | 30 | a = int(ab[0]) 31 | 32 | b = int(ab[1]) 33 | 34 | mn = input().split() 35 | 36 | m = int(mn[0]) 37 | 38 | n = int(mn[1]) 39 | 40 | apples = list(map(int, input().rstrip().split())) 41 | 42 | oranges = list(map(int, input().rstrip().split())) 43 | 44 | countApplesAndOranges(s, t, a, b, apples, oranges) 45 | 46 | -------------------------------------------------------------------------------- /Hackerrank Solutions/algorithms/Birthday_cake_candles.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # 10 | # Complete the 'birthdayCakeCandles' function below. 11 | # 12 | # The function is expected to return an INTEGER. 13 | # The function accepts INTEGER_ARRAY candles as parameter. 14 | # 15 | 16 | def birthdayCakeCandles(arr): 17 | n= len(arr) 18 | maxnum = 0 19 | count = 0 20 | for i in range(n): 21 | if arr[i] > maxnum: 22 | maxnum = arr[i] 23 | count = 1 24 | elif arr[i] == maxnum: 25 | count += 1 26 | return count 27 | 28 | if __name__ == '__main__': 29 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 30 | 31 | candles_count = int(raw_input().strip()) 32 | 33 | candles = map(int, raw_input().rstrip().split()) 34 | 35 | result = birthdayCakeCandles(candles) 36 | 37 | fptr.write(str(result) + '\n') 38 | 39 | fptr.close() 40 | 41 | -------------------------------------------------------------------------------- /Hackerrank Solutions/c++/Find Merge Point of Two Lists/Find Merge Point of Two Lists.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | // Complete the findMergeNode function below. 4 | 5 | /* 6 | * For your reference: 7 | * 8 | * SinglyLinkedListNode { 9 | * int data; 10 | * SinglyLinkedListNode* next; 11 | * }; 12 | * 13 | */ 14 | int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) { 15 | SinglyLinkedListNode* temp1=head1; 16 | SinglyLinkedListNode* temp2=head2; 17 | 18 | while(temp1!=temp2){ 19 | temp1=temp1->next; 20 | temp2=temp2->next; 21 | if(!temp1) 22 | temp1=head1; 23 | if(!temp2) 24 | temp2=head2;} 25 | /*if(temp1->next==temp2->next) 26 | { 27 | 28 | return temp1->data; 29 | 30 | 31 | } 32 | else 33 | { 34 | temp1=temp1->next; 35 | temp2=temp2->next; 36 | 37 | } 38 | 39 | }*/ 40 | return temp1->data; 41 | 42 | 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Hackerrank Solutions/c++/Find Merge Point of Two Lists/input.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/Hackerrank Solutions/c++/Find Merge Point of Two Lists/input.JPG -------------------------------------------------------------------------------- /Hackerrank Solutions/c++/Find Merge Point of Two Lists/output.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/Hackerrank Solutions/c++/Find Merge Point of Two Lists/output.JPG -------------------------------------------------------------------------------- /Hackerrank Solutions/c++/Trees/postOrder Traversal/postOrder Traversal.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Node { 3 | public: 4 | int data; 5 | Node *left; 6 | Node *right; 7 | Node(int d) { 8 | data = d; 9 | left = NULL; 10 | right = NULL; 11 | } 12 | }; 13 | 14 | */ 15 | 16 | 17 | void postOrder(Node *root) { 18 | if(root==NULL) 19 | return ; 20 | { 21 | postOrder(root->left); 22 | postOrder(root->right); 23 | cout<data <<" "; 24 | 25 | } 26 | } 27 | 28 | //~YashPassi 29 | 30 | -------------------------------------------------------------------------------- /Hackerrank Solutions/c++/compair two link-list/comparetwolinkedlist.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | // Complete the compare_lists function below. 4 | 5 | /* 6 | * For your reference: 7 | * 8 | * SinglyLinkedListNode { 9 | * int data; 10 | * SinglyLinkedListNode* next; 11 | * }; 12 | * 13 | */ 14 | bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) { 15 | 16 | while(head1->next!=NULL && head2->next!=NULL) 17 | { 18 | if(head1->data!=head2->data) 19 | { 20 | return false; 21 | } 22 | head1=head1->next; 23 | head2=head2->next; 24 | 25 | } 26 | 27 | if(head1->next!=NULL || head2->next!=NULL) 28 | { 29 | return false; 30 | } 31 | else 32 | { 33 | return true; 34 | } 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Hackerrank Solutions/c++/compair two link-list/input.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/Hackerrank Solutions/c++/compair two link-list/input.jpeg -------------------------------------------------------------------------------- /Hackerrank Solutions/c++/compair two link-list/output.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/Hackerrank Solutions/c++/compair two link-list/output.jpeg -------------------------------------------------------------------------------- /Hackerrank Solutions/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /Hackerrank Solutions/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /Hackerrank Solutions/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /Hackerrank Solutions/python/Find a string.py: -------------------------------------------------------------------------------- 1 | # Problem: https://www.hackerrank.com/challenges/find-a-string/problem 2 | # Score: 10 3 | 4 | 5 | def count_substring(string, sub_string): 6 | count = 0 7 | for i in range(len(string) - len(sub_string) + 1): 8 | if string[i: i + len(sub_string)] == sub_string: 9 | count += 1 10 | return count 11 | -------------------------------------------------------------------------------- /Hashing/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /Hashing/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /Hashing/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /Hashing/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Pranjay Poddar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LeetCode/Hard/149. Max Points on a Line.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxPoints(vector>& points) { 4 | vector > v; 5 | int n = points.size(); 6 | for(int i=0;i,int> mp[n]; 11 | int ans = 0; 12 | for(int i=0;ileft, d) ; 32 | int rh = height(root->right, d) ; 33 | d = max(d, lh + rh) ; 34 | 35 | return max(lh , rh) + 1; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /LeetCode/Hard/76. Minimum Window Substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string minWindow(string s, string t) { 4 | unordered_map m; 5 | // Statistic for count of char in t 6 | for (auto c : t) m[c]++; 7 | // counter represents the number of chars of t to be found in s. 8 | size_t start = 0, end = 0, counter = t.size(), minStart = 0, minLen = INT_MAX; 9 | size_t size = s.size(); 10 | 11 | // Move end to find a valid window. 12 | while (end < size) { 13 | // If char in s exists in t, decrease counter 14 | if (m[s[end]] > 0) 15 | counter--; 16 | // Decrease m[s[end]]. If char does not exist in t, m[s[end]] will be negative. 17 | m[s[end]]--; 18 | end++; 19 | // When we found a valid window, move start to find smaller window. 20 | while (counter == 0) { 21 | if (end - start < minLen) { 22 | minStart = start; 23 | minLen = end - start; 24 | } 25 | m[s[start]]++; 26 | // When char exists in t, increase counter. 27 | if (m[s[start]] > 0) 28 | counter++; 29 | start++; 30 | } 31 | } 32 | if (minLen != INT_MAX) 33 | return s.substr(minStart, minLen); 34 | return ""; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /LeetCode/Hard/Permutation_sequence.cpp: -------------------------------------------------------------------------------- 1 | // 60. Permutation Sequence 2 | // The set [1, 2, 3, ..., n] contains a total of n! unique permutations. 3 | 4 | // By listing and labeling all of the permutations in order, we get the following sequence for n = 3: 5 | 6 | // "123" 7 | // "132" 8 | // "213" 9 | // "231" 10 | // "312" 11 | // "321" 12 | // Given n and k, return the kth permutation sequence. 13 | 14 | // Example 1: 15 | // Input: n = 3, k = 3 16 | // Output: "213" 17 | 18 | // Example 2: 19 | // Input: n = 4, k = 9 20 | // Output: "2314" 21 | 22 | // Example 3: 23 | // Input: n = 3, k = 1 24 | // Output: "123" 25 | 26 | //Solution 27 | class Solution { 28 | public: 29 | string getPermutation(int n, int k) { 30 | if(n==1) 31 | return "1"; 32 | string ans=""; 33 | for(int i=1;i<=n;i++){ 34 | ans+=to_string(i); 35 | } 36 | while(k>1){ 37 | next_permutation(ans.begin(),ans.end()); 38 | k--; 39 | } 40 | return ans; 41 | } 42 | }; -------------------------------------------------------------------------------- /LeetCode/Hard/Reverse Nodes in k-Group.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverse(ListNode *start,ListNode*end){ 4 | ListNode *prev=NULL,*curr=start; 5 | while(prev!=end){ 6 | ListNode* next=curr->next; 7 | curr->next=prev; 8 | prev=curr; 9 | curr=next; 10 | } 11 | start=prev; 12 | 13 | } 14 | /*void reverse(ListNode* s,ListNode* e){ 15 | ListNode *p=NULL,*c=s,*n=s->next; 16 | while(p!=e){ 17 | c->next=p; 18 | p=c; 19 | c=n; 20 | if(n!=NULL) n=n->next; 21 | } 22 | }*/ 23 | ListNode* reverseKGroup(ListNode* head, int k) { 24 | if(head==NULL||head->next==NULL||k==1) return head; 25 | ListNode *start=head,*end=head; 26 | int inc=k-1; 27 | while(inc--){ 28 | end=end->next; 29 | if(end==NULL) return head; 30 | } 31 | ListNode *nextHead=reverseKGroup(end->next,k); 32 | reverse(start,end); 33 | start->next=nextHead; 34 | return end; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /LeetCode/Hard/best time to buy and sell stock.cpp: -------------------------------------------------------------------------------- 1 | /*Best Time to Buy and Sell Stock III 2 | At every instant (or index i of the array), one of the 4 events can happen. 3 | We can use the following 4 variables to denote these: 4 | 5 | firstBuy = price of 1st buy 6 | 7 | secondBuy = price of 2nd buy 8 | 9 | firstProfit = Profit after selling the first share 10 | 11 | secondProfit = profit after selling the second share 12 | 13 | Clearly we want to minimise the buying prices and maximise the profit at each and 14 | every price instant (for each element of array)*/ 15 | class Solution { 16 | public: 17 | int maxProfit(vector& prices) { 18 | int firstBuy = INT_MAX; 19 | int secondBuy = INT_MAX; 20 | 21 | int firstProfit = INT_MIN; 22 | int secondProfit = INT_MIN; 23 | 24 | for(int i=0;i 8 | 9 | double findMedianSortedArrays(vector& nums1, vector& nums2) { 10 | if(nums2.size() < nums1.size()) return findMedianSortedArrays(nums2,nums1); 11 | int n1 = nums1.size(), n2 = nums2.size(); 12 | int low = 0, high = n1; 13 | while(low <= high) { 14 | int cut1 = (low + high) / 2; 15 | int cut2 = (n1 + n2 + 1)/2 - cut1; 16 | 17 | int l1 = cut1 == 0? INT_MIN : nums1[cut1-1]; 18 | int l2 = cut2 == 0? INT_MIN : nums2[cut2-1]; 19 | 20 | int r1 = cut1 == n1 ? INT_MAX : nums1[cut1]; 21 | int r2 = cut2 == n2 ? INT_MAX : nums2[cut2]; 22 | 23 | if(l1 <= r2 && l2 <= r1) { 24 | if((n1 + n2) % 2 == 0) { 25 | return (max(l1,l2) + min(r1,r2)) / 2.0; 26 | } else 27 | return max(l1, l2); 28 | } 29 | else if(l1 > r2) { 30 | high = cut1 - 1; 31 | } else { 32 | low = cut1 + 1; 33 | } 34 | } 35 | return 0.0; 36 | } -------------------------------------------------------------------------------- /LeetCode/Hard/reverse_nodes_in_k_groups.cpp: -------------------------------------------------------------------------------- 1 | /* 25. Reverse Nodes in k-Group 2 | 3 | Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. 4 | 5 | k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes 6 | is not a multiple of k then left-out nodes, in the end, should remain as it is. 7 | 8 | You may not alter the values in the list's nodes, only nodes themselves may be changed. 9 | */ 10 | 11 | class Solution { 12 | public: 13 | ListNode* reverseKGroup(ListNode* head, int k) { 14 | ListNode* newHead = new ListNode(); 15 | newHead->next = head; 16 | ListNode * prev = newHead, *curr = newHead, *nxt = newHead; 17 | int cnt = 0; 18 | while(curr->next!=NULL) { 19 | cnt++; 20 | curr = curr->next; 21 | } 22 | int nooftimes = cnt/k; 23 | while(nooftimes-->0) { 24 | curr = prev->next; 25 | nxt = curr->next; 26 | for(int i=1;inext = nxt->next; 28 | nxt->next = prev->next; 29 | prev->next = nxt; 30 | nxt = curr->next; 31 | } 32 | prev = curr; 33 | } 34 | return newHead->next; 35 | } 36 | }; -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/arrays/Easy/Best Time to Buy and Sell Stock II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int p = 0,n = prices.size(),mn=prices[0],x = 0,sum = 0; 5 | 6 | for(int i = 0; i < n;i++){ 7 | mn = min(mn,prices[i]); 8 | x = prices[i] - mn; 9 | if(x>0){ 10 | sum+=x; 11 | mn = prices[i]; 12 | } 13 | 14 | } 15 | return sum; 16 | } 17 | }; -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/arrays/Easy/Best Time to Buy and Sell Stock.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int minm= prices[0],p=0,n = prices.size(),x=0; 5 | for(int i = 0 ; i < n;i++){ 6 | minm= min(minm,prices[i]); 7 | x = prices[i] - minm; 8 | 9 | p = max(x,p); 10 | } 11 | return p; 12 | 13 | 14 | } 15 | }; -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/arrays/Easy/Fibonacci Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | if(n == 0)return 0; 5 | if(n == 1)return 1; 6 | if(n > 1) 7 | return fib(n-1) + fib(n-2); 8 | return 0; 9 | } 10 | }; -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/arrays/Easy/Find pivot Index.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int pivotIndex(vector& nums) { 4 | long long left=0,sum=0; 5 | int n = nums.size(); 6 | for(int i = 0;i < n;i++){ 7 | sum = sum + nums[i]; 8 | } 9 | for(int i = 0;i < n;i++){ 10 | sum = sum - nums[i]; 11 | if(sum == left){ 12 | return i; 13 | } 14 | left = left +nums[i]; 15 | } 16 | return -1; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/arrays/Easy/Majority Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | unordered_map m; 5 | for(int i = 0 ; i < nums.size();i++){ 6 | m[nums[i]]++; 7 | } 8 | int mx = 0; 9 | int index=0; 10 | for(unordered_map :: iterator i = m.begin();i!=m.end();i++){ 11 | if(mx < i->second){ 12 | mx = i->second; 13 | index = i->first; 14 | } 15 | } 16 | return index; 17 | } 18 | }; -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/arrays/Easy/Merge Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | int b[m+n]; 5 | int k = 0; 6 | int i= 0,mid1 = m-1,j=0,mid2 = n-1; 7 | while(i <= mid1 && j <= mid2){ 8 | if(nums1[i] < nums2[j]){ 9 | b[k]=nums1[i]; 10 | i++; 11 | k++; 12 | } 13 | else{ 14 | b[k]=nums2[j]; 15 | j++; 16 | k++; 17 | } 18 | } 19 | if(i > mid1){ 20 | while(j <= mid2){ 21 | b[k]=nums2[j]; 22 | j++; 23 | k++; 24 | } 25 | } 26 | else{ 27 | while(i <= mid1){ 28 | b[k]=nums1[i]; 29 | i++; 30 | k++; 31 | } 32 | } 33 | for(int z = 0;z < m+n;z++){ 34 | nums1[z]=b[z]; 35 | } 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/arrays/Easy/Move Zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector& nums) { 4 | vectorv;int count = 0,n = nums.size(); 5 | for(int i = 0;i < n;i++){ 6 | if(nums[i]!=0){ 7 | v.push_back(nums[i]); 8 | } 9 | else{ 10 | count++; 11 | } 12 | } 13 | for(int i = 0;i < n-count;i++){ 14 | nums[i]=v[i]; 15 | } 16 | int x=n-count;; 17 | while(count--){ 18 | 19 | nums[x]=0; 20 | x++; 21 | } 22 | } 23 | }; -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/arrays/Easy/Remove Duplicates from Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | int c = -999999,flag = 0; 5 | vector v; 6 | for(int i= 0;i < nums.size();i++){ 7 | if(nums[i]!=c){ 8 | v.push_back(nums[i]); 9 | c=nums[i]; 10 | } 11 | } 12 | for(int i = 0;i < v.size();i++){ 13 | nums[i] = v[i]; 14 | } 15 | return v.size(); 16 | } 17 | }; -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/arrays/Easy/Running Sum of 1d Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector runningSum(vector& nums) { 4 | int sum = 0; 5 | vector v; 6 | for(int i = 0; i < nums.size();i++){ 7 | sum+= nums[i]; 8 | v.push_back(sum); 9 | } 10 | return v; 11 | } 12 | }; -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/arrays/Easy/Squares of a Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortedSquares(vector& nums) { 4 | vector v; 5 | for(int i = 0;i < nums.size();i++){ 6 | v.push_back(nums[i]*nums[i]); 7 | } 8 | sort(v.begin(),v.end()); 9 | return v; 10 | } 11 | }; -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/arrays/Easy/Two Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector &nums, int target) { 4 | // unordered_map data; 5 | // for (int i = 0; i < nums.size(); ++i) { 6 | // data.emplace(nums[i], i); 7 | // int needed_num = target - nums[i]; 8 | // if (data.find(needed_num) != data.end() && data[needed_num] != i) { 9 | // return {i, data[needed_num]}; 10 | // } 11 | // } 12 | // return {}; 13 | 14 | // sol1-----------------------------^^ 15 | 16 | unordered_map m; 17 | int n = nums.size(); 18 | for(int i = 0; i < n;i++){ 19 | int y = target - nums[i]; 20 | if(m.find(y)!=m.end()){ 21 | return {i,m[y]}; 22 | } 23 | else{ 24 | m[nums[i]]=i; 25 | } 26 | } 27 | return {}; 28 | } 29 | 30 | }; -------------------------------------------------------------------------------- /Leetcode 250 DSA sheet Solutions/strings/Easy/add strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addStrings(string num1, string num2) { 4 | string s="";int i = num1.size()-1,j = num2.size()-1; 5 | int carry = 0; 6 | 7 | while(i >=0 && j >=0){ 8 | int a = (num1[i]-'0')+(num2[j]-'0') + carry; 9 | carry = 0; 10 | if(a > 9){ 11 | carry = 1; 12 | } 13 | s = to_string(a%10) + s; 14 | i--; 15 | j--; 16 | } 17 | 18 | while(i >= 0){ 19 | int a = (num1[i]-'0')+carry; 20 | carry = 0; 21 | if(a > 9){ 22 | carry = 1; 23 | } 24 | s= to_string(a%10) + s; 25 | i--; 26 | } 27 | 28 | while(j >=0){ 29 | int a = (num2[j]-'0')+carry; 30 | carry = 0; 31 | if(a > 9){ 32 | carry = 1; 33 | } 34 | s= to_string(a%10) + s; 35 | j--; 36 | } 37 | if(i == -1 && j == -1 && carry!=0){ 38 | 39 | s = to_string(carry) + s; 40 | 41 | } 42 | return s; 43 | } 44 | }; -------------------------------------------------------------------------------- /Leetcode Solutions/c++/173. Binary Search Tree Iterator/173. Binary Search Tree Iterator.cpp: -------------------------------------------------------------------------------- 1 | class BSTIterator { 2 | public: 3 | stack s; 4 | BSTIterator(TreeNode* root) { 5 | partialInorder(root); 6 | } 7 | 8 | void partialInorder(TreeNode* root){ 9 | while(root != NULL){ 10 | s.push(root); 11 | root = root->left; 12 | } 13 | } 14 | 15 | int next() { 16 | TreeNode* top = s.top(); 17 | s.pop(); 18 | partialInorder(top->right); 19 | return top->val; 20 | } 21 | 22 | bool hasNext() { 23 | return !s.empty(); 24 | } 25 | }; -------------------------------------------------------------------------------- /Leetcode Solutions/c++/260. Single Number III/260. Single Number III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector singleNumber(vector& nums) { 4 | int xoR = 0,n = nums.size(); 5 | for(int i = 0;i 5 25 | 26 | else 27 | 2 = 010 28 | 2 = 010 29 | 3 = 011 ✅ 30 | 2^2^3 => 3 31 | 32 | ans = [3, 5] 33 | -------------------------------------------------------------------------------- /Leetcode Solutions/c++/3Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class Solution { 3 | public: 4 | vector> threeSum(vector& nums) { 5 | vector>v; 6 | int n=nums.size(); 7 | int i=0,l=0,r=0,sum=0; 8 | sort(nums.begin(),nums.end()); 9 | for(i=0;ivt; 21 | /*vt.push_back(nums[i]); 22 | vt.push_back(nums[l]); 23 | vt.push_back(nums[r]);*/ 24 | v.push_back({nums[i],nums[l],nums[r]}); 25 | while(l0){ 30 | r--; 31 | }else{ 32 | l++; 33 | } 34 | } 35 | } 36 | } 37 | 38 | return v; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /Leetcode Solutions/c++/ConstructBinaryTreefromPreorderandInorderTraversal.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Problem link: 3 | //https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal 4 | 5 | class Solution { 6 | public: 7 | int preIndex=0; 8 | TreeNode* buildTree(vector& preorder, vector& inorder) { 9 | return createTree(preorder, inorder, 0, inorder.size()-1); 10 | } 11 | 12 | TreeNode* createTree(vector &preorder, vector &inorder, int start, int end) 13 | { 14 | if(start > end) 15 | return NULL; 16 | TreeNode* node = new TreeNode(preorder[preIndex++]); 17 | int pos; 18 | for(int i=start; i<=end;i++) 19 | { 20 | if(inorder[i]==node->val) 21 | { 22 | 23 | pos=i; 24 | break; 25 | } 26 | } 27 | node->left = createTree(preorder, inorder, start, pos-1); 28 | node->right = createTree(preorder, inorder, pos+1, end); 29 | return node; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Leetcode Solutions/c++/Equal stacks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t1, t2, t3; 6 | cin >> t1 >> t2 >> t3; 7 | int heights1[t1], heights2[t2], heights3[t3], sum_1 = 0, sum_2 = 0, sum_3 = 0; 8 | 9 | for (int a = 0; a < t1; a++) { 10 | cin >> heights1[a]; 11 | sum_1 += heights1[a]; 12 | } 13 | for (int a= 0; a < t2; a++) { 14 | cin >> heights2[a]; 15 | sum_2 += heights2[a]; 16 | } 17 | for (int a = 0; a < t3; a++) { 18 | cin >> heights3[a]; 19 | sum_3 += heights3[a]; 20 | } 21 | 22 | int a = 0, b = 0, c = 0; 23 | while (! (sum_1 == sum_2 and sum_2 == sum_3)) { 24 | if (sum_1 >= sum_2 and sum_1 >= sum_3) { 25 | sum_1 -= heights1[a]; 26 | a++; 27 | continue; 28 | } 29 | if (sum_2 >= sum_3 and sum_2 >= sum_1) { 30 | sum_2 -= heights2[b]; 31 | b++; 32 | continue; 33 | } 34 | if (sum_3 >= sum_1 and sum_3 >= sum_2) { 35 | sum_3 -= heights3[c]; 36 | c++; 37 | continue; 38 | } 39 | } 40 | 41 | cout << sum_1; 42 | } 43 | 44 | /*input: 45 | 5 3 4 46 | 3 2 1 1 1 47 | 4 3 2 48 | 1 1 4 1 49 | */ -------------------------------------------------------------------------------- /Leetcode Solutions/c++/Map_sum_pairs.cpp: -------------------------------------------------------------------------------- 1 | //link(https://leetcode.com/problems/map-sum-pairs/) 2 | class MapSum { 3 | public: 4 | /** Initialize your data structure here. */ 5 | unordered_map mp; 6 | MapSum() { 7 | 8 | } 9 | 10 | void insert(string key, int val) { 11 | mp[key]=val; 12 | } 13 | 14 | int sum(string prefix) { 15 | int sum=0; 16 | for(auto i:mp) 17 | { 18 | if(i.first.substr(0,prefix.size())==prefix) 19 | sum+=i.second; 20 | } 21 | return sum; 22 | } 23 | }; -------------------------------------------------------------------------------- /Leetcode Solutions/c++/Next_Permutation.cpp: -------------------------------------------------------------------------------- 1 | // 31. Next Permutation 2 | // Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. 3 | 4 | // If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order). 5 | 6 | // The replacement must be in place and use only constant extra memory. 7 | 8 | // Example 1: 9 | // Input: nums = [1,2,3] 10 | // Output: [1,3,2] 11 | 12 | // Example 2: 13 | // Input: nums = [3,2,1] 14 | // Output: [1,2,3] 15 | 16 | // Example 3: 17 | // Input: nums = [1,1,5] 18 | // Output: [1,5,1] 19 | 20 | // Example 4: 21 | // Input: nums = [1] 22 | // Output: [1] 23 | 24 | // Solution 25 | class Solution 26 | { 27 | public: 28 | void nextPermutation(vector &nums) 29 | { 30 | int i = nums.size() - 2; 31 | while (i >= 0 && nums[i + 1] <= nums[i]) 32 | i--; 33 | if (i >= 0) 34 | { 35 | 36 | int j = nums.size() - 1; 37 | while (nums[j] <= nums[i]) 38 | j--; 39 | swap(nums[i], nums[j]); 40 | reverse(nums.begin() + i + 1, nums.end()); 41 | } 42 | if (i < 0) 43 | { 44 | reverse(nums.begin(), nums.end()); 45 | } 46 | } 47 | }; -------------------------------------------------------------------------------- /Leetcode Solutions/c++/Remove_Nth_node_from_end_of_list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | SUBMITTED BY: Deadeye001 3 | DATE: 17/10/21 4 | */ 5 | 6 | /** 7 | * Definition for singly-linked list. 8 | * struct ListNode { 9 | * int val; 10 | * ListNode *next; 11 | * ListNode() : val(0), next(nullptr) {} 12 | * ListNode(int x) : val(x), next(nullptr) {} 13 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | ListNode* removeNthFromEnd(ListNode* head, int n) { 19 | ListNode* first=head; 20 | ListNode* second=head; 21 | if(second->next==NULL) 22 | return NULL; 23 | for(int i=0;inext; 25 | if(second==NULL) 26 | { 27 | head=head->next; 28 | return head; 29 | } 30 | while(second->next) 31 | { 32 | second=second->next; 33 | first=first->next; 34 | } 35 | ListNode* temp=first->next; 36 | first->next=first->next->next; 37 | delete temp; 38 | return head; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /Leetcode Solutions/c++/Single_Number-II.cpp: -------------------------------------------------------------------------------- 1 | // 137. Single Number II 2 | // Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and return it. 3 | 4 | // You must implement a solution with a linear runtime complexity and use only constant extra space. 5 | 6 | // Example 1: 7 | // Input: nums = [2,2,3,2] 8 | // Output: 3 9 | 10 | // Example 2: 11 | // Input: nums = [0,1,0,1,0,1,99] 12 | // Output: 99 13 | 14 | //Solution 15 | class Solution { 16 | public: 17 | int singleNumber(vector& nums) { 18 | unordered_mapm; 19 | for(int i=0;inext == NULL){ 5 | return false; 6 | } 7 | 8 | ListNode *temp1= head, *temp2= head; 9 | while(temp2->next!=NULL && temp2->next->next != NULL){ 10 | cout<val<next; 12 | temp2 = temp2->next->next; 13 | if (temp1 == temp2){ 14 | return true; 15 | } 16 | } 17 | return false; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leetcode Solutions/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /Leetcode Solutions/c++/rotate array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverse(vector& nums,int start,int end){ 4 | while(start& nums, int k) { 14 | int n=nums.size(); 15 | // if(n==0||n==1) return n 16 | k=k%n; 17 | reverse(nums,0,n-1); 18 | reverse(nums,0,k-1); 19 | reverse(nums,k,n-1); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Leetcode Solutions/c/Rotate_list.c: -------------------------------------------------------------------------------- 1 | //You are given the head of a linked list, rotate the list to the right by k places. 2 | 3 | /* Example 1: 4 | Input : head=[1, 2, 3, 4, 5], k=2 5 | Output : [4, 5, 1, 2, 3] 6 | */ 7 | 8 | /* Example 2: 9 | Input : head=[0, 1, 2], k=4 10 | Output : [2, 0, 1] 11 | */ 12 | 13 | /** 14 | * Definition for singly-linked list. 15 | * struct ListNode { 16 | * int val; 17 | * struct ListNode *next; 18 | * }; 19 | */ 20 | 21 | 22 | struct ListNode* rotateRight(struct ListNode* head, int k){ 23 | if(!head) 24 | return head; 25 | struct ListNode *temp=head,*new; 26 | int len=1; 27 | while(temp->next!=NULL) 28 | { 29 | len++; 30 | temp=temp->next; 31 | } 32 | temp->next=head; 33 | if(k%=len) 34 | { 35 | for(int i=0;inext; 37 | } 38 | new=temp->next; 39 | temp->next=NULL; 40 | return new; 41 | } 42 | -------------------------------------------------------------------------------- /Leetcode Solutions/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /Leetcode Solutions/java/410. Split Array Largest Sum.java: -------------------------------------------------------------------------------- 1 | public class SplitArray { 2 | public static void main(String[] args) { 3 | 4 | } 5 | 6 | public int splitArray(int[] nums, int m) { 7 | int start = 0; 8 | int end = 0; 9 | 10 | for (int i = 0; i < nums.length; i++) { 11 | start = Math.max(start, nums[i]); // in the end of the loop this will contain the max item fro the array 12 | end += nums[i]; 13 | } 14 | 15 | // binary search 16 | while (start < end) { 17 | // try for the middle as potential ans 18 | int mid = start + (end - start) / 2; 19 | 20 | // calculate how many pieces you can divide this in with this max sum 21 | int sum = 0; 22 | int pieces = 1; 23 | for(int num : nums) { 24 | if (sum + num > mid) { 25 | // you cannot add this in this subarray, make new one 26 | // say you add this num in new subarray, then sum = num 27 | sum = num; 28 | pieces++; 29 | } else { 30 | sum += num; 31 | } 32 | } 33 | 34 | if (pieces > m) { 35 | start = mid + 1; 36 | } else { 37 | end = mid; 38 | } 39 | 40 | } 41 | return end; // here start == end 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Leetcode Solutions/java/ContainerWithMostWater.java: -------------------------------------------------------------------------------- 1 | /* 2 | Problem : Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). 3 | n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis 4 | forms a container, such that the container contains the most water. 5 | Note that you may not slant the container. 6 | 7 | Example: 8 | Input: height = [4,3,2,1,4] 9 | Output: 16 10 | */ 11 | 12 | class Solution { 13 | // function to calculate container with most water 14 | public int maxArea(int[] height) {//array with heights of barriers is provided 15 | int maxwater = 0, left = 0, right = height.length - 1; 16 | while (left < right) { 17 | maxwater = Math.max(maxwater, Math.min(height[left], height[right]) * (right - left)); 18 | if (height[left] < height[right]) 19 | maxwater = Math.max(maxwater, Math.min(height[left], height[right]) * (right - left));//updating maximum water stored 20 | if (height[left] < height[right])//updating the left and right barrier values to store maximum water 21 | left++; 22 | else 23 | right--; 24 | } 25 | return maxwater; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Leetcode Solutions/java/ListNode.java: -------------------------------------------------------------------------------- 1 | 2 | // * Definition for singly-linked list. 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode() {} 7 | ListNode(int val) { this.val = val; } 8 | ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | } 10 | 11 | class LinkedList { 12 | 13 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 14 | int sum = 0; 15 | int carry=0; 16 | ListNode t = new ListNode(0); 17 | ListNode t_s = t; 18 | 19 | while(l1 != null && l2 != null){ 20 | carry = sum/10; 21 | sum = l1.val+l2.val+carry; 22 | t.next = new ListNode(sum%10); 23 | t = t.next; 24 | l1 = l1.next; 25 | l2 = l2.next; 26 | } 27 | while (l1 != null){ 28 | carry = sum/10; 29 | sum = l1.val+carry; 30 | t.next = new ListNode(sum%10); 31 | t = t.next; 32 | l1 = l1.next; 33 | } 34 | while(l2 != null){ 35 | carry = sum/10; 36 | sum = l2.val+carry; 37 | t.next = new ListNode(sum%10); 38 | t = t.next; 39 | l2 = l2.next; 40 | } 41 | carry = sum/10; 42 | if(carry != 0){ 43 | t.next = new ListNode(carry); 44 | t = t.next; 45 | } 46 | return t_s.next; 47 | } 48 | } -------------------------------------------------------------------------------- /Leetcode Solutions/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /Leetcode Solutions/python/Remove Nth Node From End of List.py: -------------------------------------------------------------------------------- 1 | # Python3 implementation of the approach 2 | class Node: 3 | def __init__(self, new_data): 4 | self.data = new_data 5 | self.next = None 6 | class LinkedList: 7 | def __init__(self): 8 | self.head = None 9 | 10 | # createNode and and make linked list 11 | def push(self, new_data): 12 | new_node = Node(new_data) 13 | new_node.next = self.head 14 | self.head = new_node 15 | 16 | def deleteNode(self, n): 17 | first = self.head 18 | second = self.head 19 | for i in range(n): 20 | 21 | # If count of nodes in the 22 | # given list is less than 'n' 23 | if(second.next == None): 24 | 25 | # If index = n then 26 | # delete the head node 27 | if(i == n - 1): 28 | self.head = self.head.next 29 | return self.head 30 | second = second.next 31 | 32 | while(second.next != None): 33 | second = second.next 34 | first = first.next 35 | 36 | first.next = first.next.next 37 | 38 | def printList(self): 39 | tmp_head = self.head 40 | while(tmp_head != None): 41 | print(tmp_head.data, end = ' ') 42 | tmp_head = tmp_head.next 43 | 44 | # Driver Code 45 | llist = LinkedList() 46 | llist.push(7) 47 | llist.push(1) 48 | llist.push(3) 49 | llist.push(2) 50 | print("Created Linked list is:") 51 | llist.printList() 52 | llist.deleteNode(1) 53 | print("\nLinked List after Deletion is:") 54 | llist.printList() 55 | 56 | 57 | -------------------------------------------------------------------------------- /Leetcode Solutions/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /Leetcode Solutions/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes Leetcode Contest Solutions. . -------------------------------------------------------------------------------- /algorithms/c++/kadaneAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n; 8 | cin>>n; 9 | int a[n]; 10 | for(int i = 0; i < n; i++){ 11 | cin>>a[i]; 12 | } 13 | 14 | int currSum = 0; 15 | int maxSum = a[0]; 16 | for(int i = 0; i < n; i++){ 17 | currSum += a[i]; 18 | maxSum = max(maxSum,currSum); 19 | if (currSum < 0){ 20 | currSum = 0; 21 | } 22 | } 23 | 24 | cout< 2 | #include 3 | #include 4 | 5 | #define V 9 6 | 7 | 8 | int minDistance(int dist[], bool sptSet[]) 9 | { 10 | 11 | int min = INT_MAX, min_index; 12 | 13 | for (int v = 0; v < V; v++) 14 | if (sptSet[v] == false && dist[v] <= min) 15 | min = dist[v], min_index = v; 16 | 17 | return min_index; 18 | } 19 | 20 | 21 | void printSolution(int dist[]) 22 | { 23 | printf("Vertex \t\t Distance from Source\n"); 24 | for (int i = 0; i < V; i++) 25 | printf("%d \t\t %d\n", i, dist[i]); 26 | } 27 | 28 | 29 | void dijkstra(int graph[V][V], int src) 30 | { 31 | int dist[V]; 32 | 33 | bool sptSet[V]; 34 | 35 | for (int i = 0; i < V; i++) 36 | dist[i] = INT_MAX, sptSet[i] = false; 37 | 38 | 39 | dist[src] = 0; 40 | 41 | 42 | for (int count = 0; count < V - 1; count++) { 43 | 44 | int u = minDistance(dist, sptSet); 45 | 46 | 47 | sptSet[u] = true; 48 | 49 | 50 | for (int v = 0; v < V; v++) 51 | 52 | 53 | if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX 54 | && dist[u] + graph[u][v] < dist[v]) 55 | dist[v] = dist[u] + graph[u][v]; 56 | } 57 | -------------------------------------------------------------------------------- /algorithms/c/Prime Numbers/Prime num Algo 1.c: -------------------------------------------------------------------------------- 1 | // prime number algorithm 1 2 | #include 3 | int isprime(int n){ 4 | if(n <=1)return 0; 5 | if(n==2 || n==3)return 1; 6 | if(n%2==0 || n%3==0)return 0; 7 | for(int i=5;i*i<=n;i=i+6){ 8 | if(n%i==0 || n%(i+2)==0) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | int i,n; 16 | printf("Please enter a number to check if it is prime or not:"); 17 | scanf("%d",&n); 18 | if(isprime(n)){ 19 | printf("Yes\n"); 20 | } 21 | else{ 22 | printf("No\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /algorithms/c/leap year check/Leap Year.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/algorithms/c/leap year check/Leap Year.PNG -------------------------------------------------------------------------------- /algorithms/c/leap year check/Leap_Year_Check.c: -------------------------------------------------------------------------------- 1 | // Leap_Year_Check code in c programming language 2 | #include 3 | #include 4 | void main() { 5 | int year; 6 | printf("Enter a year: "); 7 | scanf("%d", &year); 8 | if(((year%4==0) && ((year%400==0) || (year%100!==0)) 9 | { 10 | printf("%d is a leap year", &year); 11 | } else { 12 | printf("%d is not a leap year", &year); 13 | } 14 | getch(); 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/c/leap year check/leap_year.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | if year % 400 == 0: 4 | leap = True 5 | elif year % 100 == 0: 6 | leap = False 7 | elif year % 4 == 0: 8 | leap = True 9 | return leap_year 10 | -------------------------------------------------------------------------------- /algorithms/c/matrix multiplication.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n ; 6 | printf("Please enter the size of matrix:"); 7 | scanf("%d",&n); 8 | int a[n][n],b[n][n],c[n][n],i,j,k; 9 | printf("Please enter the elements of matrix:"); 10 | for(i = 0;i < n;i++){ 11 | for(j = 0; j < n;j++){ 12 | scanf("%d",&a[i][j]); 13 | } 14 | } 15 | for(i = 0;i < n;i++){ 16 | for(j = 0; j < n;j++){ 17 | scanf("%d",&b[i][j]); 18 | } 19 | } 20 | for(i = 0;i < n;i++){ 21 | for(j = 0; j < n;j++){ 22 | c[i][j]=0; 23 | for(k = 0; k < n;k++){ 24 | c[i][j] = c[i][j] + a[i][k]*b[k][j]; 25 | } 26 | } 27 | printf("\n"); 28 | } 29 | 30 | for(i = 0;i < n;i++){ 31 | for(j = 0; j < n;j++){ 32 | printf("%d ",c[i][j]); 33 | } 34 | printf("\n"); 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /algorithms/java/ActivitySelection.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ActivitySelection { 4 | public static void main(String... args) { 5 | int[] s = new int[]{0, 1, 3, 0, 5, 3, 5, 6, 8, 8, 2, 12}; // Activity 0 is dummy 6 | int[] f = new int[]{0, 4, 5, 6, 7, 9, 9, 10, 11, 12, 14, 16}; // Activity 0 is dummy 7 | Set set1 = recursiveActivitySelector(s, f, 0, s.length - 1); 8 | System.out.println("Recursive: "); 9 | for (Integer i: set1) System.out.print(i + " "); 10 | Set set2 = greedyActivitySelector(s, f); 11 | System.out.println(); 12 | System.out.println("Iterative: "); 13 | for (Integer i: set2) System.out.print(i + " "); 14 | System.out.println(); 15 | } 16 | 17 | public static Set recursiveActivitySelector(int[] s, int[] f, int k, int n) { 18 | int m = k + 1; 19 | while (m <= n && s[m] < f[k]) 20 | m = m + 1; 21 | if (m <= n) { 22 | Set set = recursiveActivitySelector(s, f, m, n); 23 | set.add(m); 24 | return set; 25 | } 26 | return new java.util.HashSet(); 27 | } 28 | 29 | public static Set greedyActivitySelector(int[] s, int[] f) { 30 | int n = s.length - 1; 31 | Set set = new HashSet<>(); 32 | set.add(1); 33 | int k = 1; 34 | for (int m = 2; m <= n; m++) { 35 | if (s[m] >= f[k]) { 36 | set.add(m); 37 | k = m; 38 | } 39 | } 40 | return set; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /algorithms/java/Subset sum with backtracking.java: -------------------------------------------------------------------------------- 1 | public class SubsetSum { 2 | public static void main(String... args) { 3 | int T = 15; 4 | //Integer[] array = new Integer[] {11, 6, 5, 1, 7, 13, 12}; 5 | Integer[] array = new Integer[] {8, 6, 7, 5, 3, 10, 9}; 6 | java.util.TreeSet X = new java.util.TreeSet<>(); 7 | for (int x: array) { 8 | X.add(x); 9 | } 10 | System.out.println(subsetSum(X, T)); 11 | } 12 | 13 | @SuppressWarnings("unchecked") 14 | public static boolean subsetSum(java.util.TreeSet X, int T) { 15 | printProblem(X, T); 16 | if (T == 0) { 17 | System.out.println("Yes, there exists at least one subset!"); 18 | return true; 19 | } else if (T < 0 || X.isEmpty()) { 20 | System.out.println("Backtracking..."); 21 | return false; 22 | } else { 23 | Integer x = X.first(); 24 | X.remove(x); 25 | return ( 26 | subsetSum(((java.util.TreeSet) X.clone()), T) 27 | || 28 | subsetSum(((java.util.TreeSet) X.clone()), T - x) 29 | ); 30 | } 31 | } 32 | 33 | public static void printProblem(java.util.TreeSet X, int T) { 34 | for (Integer x: X) { 35 | System.out.print(x + " "); 36 | } 37 | System.out.println("-> " + T); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /algorithms/java/binarySearch.java: -------------------------------------------------------------------------------- 1 | class binarySearch 2 | { 3 | int binarySearch(int arr[], int l, int r, int x) 4 | { 5 | if (r>=l) 6 | { 7 | int mid = l + (r - l)/2; 8 | 9 | if (arr[mid] == x) 10 | return mid; 11 | 12 | if (arr[mid] > x) 13 | return binarySearch(arr, l, mid-1, x); 14 | 15 | return binarySearch(arr, mid+1, r, x); 16 | } 17 | 18 | return -1; 19 | } 20 | 21 | public static void main(String args[]) 22 | { 23 | BinarySearch ob = new BinarySearch(); 24 | int arr[] = {2,3,4,10,40}; 25 | int n = arr.length; 26 | int x = 10; 27 | int result = ob.binarySearch(arr,0,n-1,x); 28 | if (result == -1) 29 | System.out.println("Element not present"); 30 | else 31 | System.out.println("Element found at index " + 32 | result); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /algorithms/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /algorithms/python/Build-List.py: -------------------------------------------------------------------------------- 1 | #SCRIPT FOR CREATION OF LIST. 2 | #YOU CAN ADD ANYTHING IN LSIT WHETHER IT IS STRING, INTEGER , DECIMAL VALUE OR ANYTHINGS BUT KEEP RULE IN MIND. 3 | 4 | # Python program to demonstrate 5 | # Creation of List 6 | 7 | # Creating a List 8 | List = [] 9 | print("Blank List: ") 10 | print(List) 11 | 12 | # Creating a List of numbers 13 | List = [10, 20, 14] 14 | print("\nList of numbers: ") 15 | print(List) 16 | 17 | # Creating a List of strings and accessing 18 | # using index 19 | List = ["item1", "item2", "item3"] 20 | print("\nList Items: ") 21 | print(List[0]) 22 | print(List[2]) 23 | 24 | # Creating a Multi-Dimensional List 25 | # (By Nesting a list inside a List) 26 | List = [['Have', 'A'] , ['GOOD'],['DAY'] ] 27 | print("\nMulti-Dimensional List: ") 28 | print(List) 29 | -------------------------------------------------------------------------------- /algorithms/python/buildDictionary.py: -------------------------------------------------------------------------------- 1 | """ 2 | It creates a dictionary from a json array and includes a example. 3 | """ 4 | 5 | 6 | def addElementsToDict(collection, dictionary, key): 7 | """ 8 | Its recibes a collection of json, a dictionary and a key that is used as criterio to build the dictionary. 9 | """ 10 | for element in collection: 11 | if element[key] not in dictionary: 12 | dictionary[element[key]] = element 13 | 14 | 15 | # Example 16 | myObjects = [ 17 | {"id": 1, "code": "A001", "name": "John Doe", "value": 53}, 18 | {"id": 2, "code": "A002", "name": "Jane Doe", "value": 86} 19 | ] 20 | 21 | myDictionary = {} 22 | addElementsToDict(myObjects, myDictionary, "code") 23 | print(myDictionary) 24 | -------------------------------------------------------------------------------- /algorithms/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /arrays/c++/Merge-Two_Sorted_Arrays.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to merge two sorted arrays/ 2 | #include 3 | using namespace std; 4 | 5 | // Merge arr1[0..n1-1] and arr2[0..n2-1] into 6 | // arr3[0..n1+n2-1] 7 | void mergeArrays(int arr1[], int arr2[], int n1,int n2, int arr3[]) 8 | { 9 | int i = 0, j = 0, k = 0; 10 | 11 | // Traverse both array 12 | while (i 2 | using namespace std; 3 | int main(){ 4 | int size,pos,i,num; 5 | cout << "Please enter the size of the array:"; 6 | cin >> size; 7 | int a[size]; 8 | cout << "Please enter the position to delete a number:"; 9 | cin >> pos; 10 | cout << "Please enter the array:"; 11 | for(i = 0; i < size;i++){ 12 | cin >> a[i] ; 13 | } 14 | if (pos > size || pos < 0){ 15 | cout << "Invalid position\n"; 16 | } 17 | else{ 18 | for (i = pos - 1;i < size - 1;i++){ 19 | a[i]=a[i+1]; 20 | } 21 | size--; 22 | for(i = 0; i < size;i++){ 23 | cout << a[i] << " "; 24 | } 25 | } 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /arrays/c++/insertion in array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int size,pos,i,num; 5 | cout << "Please enter the size of the array:"; 6 | cin >> size; 7 | int a[size]; 8 | cout << "Please enter the position to insert a number:"; 9 | cin >> pos; 10 | cout << "Please enter the number to insert:"; 11 | cin >> num; 12 | cout << "Please enter the array:"; 13 | for(i = 0; i < size;i++){ 14 | cin >> a[i] ; 15 | } 16 | if (pos > size || pos < 0){ 17 | cout << "Invalid position\n"; 18 | } 19 | else{ 20 | for (i = size -1;i >=pos - 1;i--){ 21 | a[i+1]=a[i]; 22 | } 23 | a[pos-1]=num; 24 | size++; 25 | } 26 | for(i = 0; i < size;i++){ 27 | cout << a[i] << " "; 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /arrays/c++/max and min in array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct p{ 4 | int max; 5 | int min; 6 | }; 7 | 8 | struct p compare(int a[],int n){ 9 | int i; 10 | struct p num; 11 | if(n == 1){ 12 | num.max=a[0]; 13 | num.min=a[0]; 14 | return num; 15 | } 16 | if(n > 1){ 17 | if(a[0]>a[1]){ 18 | num.max=a[0]; 19 | num.min=a[1]; 20 | } 21 | else{ 22 | num.max=a[1]; 23 | num.min=a[0]; 24 | } 25 | for(i = 2;i < n;i++){ 26 | if(a[i]>num.max){ 27 | num.max=a[i]; 28 | } 29 | if(a[i]> n; 40 | int a[n]; 41 | cout << "Please Enter elements of the array:"; 42 | for(i = 0; i < n;i++){ 43 | cin >> a[i]; 44 | } 45 | struct p c; 46 | c=compare(a,n); 47 | cout << " maximum:" << c.max << endl; 48 | cout << " minimum:" << c.min << endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /arrays/c++/traversalInArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int arr[5]={10, 0, 20, 0, 30}; 6 | //traversing array 7 | for (int i = 0; i < 5; i++) 8 | { 9 | cout< 3 | int isprime(int n){ 4 | if(n <=1)return 0; 5 | if(n==2 || n==3)return 1; 6 | if(n%2==0 || n%3==0)return 0; 7 | for(int i=5;i*i<=n;i=i+6){ 8 | if(n%i==0 || n%(i+2)==0) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | int i,n; 16 | printf("Please enter a number to check if it is prime or not:"); 17 | scanf("%d",&n); 18 | if(isprime(n)){ 19 | printf("Yes\n"); 20 | } 21 | else{ 22 | printf("No\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /arrays/c/find max and min number.c: -------------------------------------------------------------------------------- 1 | #include 2 | struct p{ 3 | int max; 4 | int min; 5 | }; 6 | struct p compare(int a[],int n){ 7 | struct p comp; 8 | if(n == 1){ 9 | comp.max=a[0]; 10 | comp.min=a[0]; 11 | return comp; 12 | } 13 | if(n > 1){ 14 | if(a[1]>a[0]){ 15 | comp.max=a[1]; 16 | comp.min=a[0]; 17 | } 18 | else{ 19 | comp.max=a[0]; 20 | comp.min=a[1]; 21 | } 22 | for(int i = 2;i < n;i++){ 23 | if(a[i]>comp.max){ 24 | comp.max=a[i]; 25 | } 26 | if(a[i] 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int LeaderSum(int n, int* array) { 10 | int flag=0,sum =0; 11 | for(int i = 0;i < n;i++){ 12 | flag = 0; 13 | for(int j =i +1;j 3 | void onerotate(int a[],int n,int k); 4 | void rotate(int a[],int n,int k){ 5 | int i,j; 6 | for(i = 0;i < k;i++){ 7 | onerotate(a,n,k); 8 | } 9 | } 10 | void onerotate(int a[],int n,int k){ 11 | int i,j,temp=a[0]; 12 | 13 | for(i = 0;i < n-1;i++){ 14 | a[i]=a[i+1]; 15 | } 16 | a[n-1]=temp; 17 | 18 | } 19 | void printa(int a[],int n){ 20 | int i; 21 | for(i = 0 ;i < n;i++){ 22 | printf("%d ",a[i]); 23 | } 24 | } 25 | int main() 26 | { 27 | int i,n,k; 28 | printf("Please enter number of elements:"); 29 | scanf("%d",&n); 30 | int a[n]; 31 | printf("Please enter elements:"); 32 | for(i = 0;i < n;i++){ 33 | scanf("%d",&a[i]); 34 | } 35 | printf("Enter number of left shifts:"); 36 | scanf("%d",&k); 37 | rotate(a,n,k); 38 | printa(a,n); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /arrays/c/right rotation of array.c: -------------------------------------------------------------------------------- 1 | right rotation of array 2 | #include stdio.h 3 | void onerotate(int a[],int n,int k); 4 | void rotate(int a[],int n,int k){ 5 | int i,j; 6 | for(i = 0;i k;i++){ 7 | onerotate(a,n,k); 8 | } 9 | } 10 | void onerotate(int a[],int n,int k){ 11 | int i,j,temp=a[n-1]; 12 | 13 | for(i = 1;i n;i++){ 14 | a[n-i]=a[n-i-1]; 15 | } 16 | a[0]=temp; 17 | 18 | } 19 | void printa(int a[],int n){ 20 | int i; 21 | for(i = 0 ;i n;i++){ 22 | printf(%d ,a[i]); 23 | } 24 | } 25 | int main() 26 | { 27 | int i,n,k; 28 | printf(Please enter number of elements); 29 | scanf(%d,&n); 30 | int a[n]; 31 | printf(Please enter elements); 32 | for(i = 0;i n;i++){ 33 | scanf(%d,&a[i]); 34 | } 35 | printf(Enter number of left shifts); 36 | scanf(%d,&k); 37 | k=k%n; 38 | rotate(a,n,k); 39 | printa(a,n); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /arrays/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes solutions in java language . -------------------------------------------------------------------------------- /arrays/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes solutions in python language . -------------------------------------------------------------------------------- /binaryTree/c++/Balanced or Not balance BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct Node{ 7 | int data; 8 | struct Node * left ,*right; 9 | }; 10 | 11 | int dfsheight(Node *); 12 | 13 | bool balanced(Node * root){ 14 | if(root == NULL)return true; 15 | return dfsheight(root) != -1; 16 | } 17 | 18 | int dfsheight(Node * root){ 19 | if(root == NULL)return 0; 20 | int lh = dfsheight(root->left); 21 | if(lh == -1)return -1; 22 | int rh = dfsheight(root->right); 23 | if(rh == -1)return -1; 24 | if(abs(lh-rh) > 1) return -1; 25 | 26 | return 1 + max(lh,rh); 27 | } 28 | 29 | Node * newnode(int data){ 30 | Node *temp = new Node; 31 | temp->data = data; 32 | temp->left = temp->right = NULL; 33 | return temp; 34 | } 35 | 36 | 37 | int main() 38 | { 39 | Node * root = newnode(1); 40 | root->left = newnode(2); 41 | root->right = newnode(3); 42 | root->left->right = newnode(5); 43 | root->left->left = newnode(4); 44 | root->left->left->left = newnode(7); 45 | if(balanced(root)){ 46 | cout << "Balanced" << endl; 47 | } 48 | else{ 49 | cout << "Not balanced" << endl; 50 | } 51 | 52 | 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /binaryTree/c++/Diameter Of BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct Node{ 7 | int data; 8 | struct Node * left ,*right; 9 | }; 10 | 11 | int maximum = 0; 12 | 13 | int diameter(Node * root){ 14 | if(root == NULL)return 0; 15 | 16 | int lh = diameter(root->left); 17 | int rh = diameter(root->right); 18 | maximum = max(maximum,lh+rh); 19 | return max(lh,rh)+1; 20 | 21 | } 22 | 23 | Node * newnode(int data){ 24 | Node *temp = new Node; 25 | temp->data = data; 26 | temp->left = temp->right = NULL; 27 | return temp; 28 | } 29 | 30 | 31 | int main() 32 | { 33 | Node * root = newnode(1); 34 | root->left = newnode(2); 35 | root->right = newnode(3); 36 | root->left->right = newnode(5); 37 | root->left->left = newnode(4); 38 | root->left->left->left = newnode(7); 39 | diameter(root); 40 | cout << maximum; 41 | return 0; 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /binaryTree/c++/Inorder traversal iterative method using stack.cpp: -------------------------------------------------------------------------------- 1 | // Inorder traversal iterative method using stack 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct Node{ 8 | int data; 9 | struct Node *left,*right; 10 | }; 11 | 12 | 13 | void inorder(Node * root){ 14 | stack s; 15 | Node * node = root; 16 | while(true){ 17 | if(node!=NULL){ 18 | s.push(node); 19 | node=node->left; 20 | } 21 | else{ 22 | if(s.empty()==true)break; 23 | node = s.top(); 24 | s.pop(); 25 | cout << node->data << " "; 26 | node = node->right; 27 | 28 | } 29 | 30 | 31 | 32 | } 33 | } 34 | Node* newNode(int data){ 35 | Node*temp = new Node; 36 | temp->data = data; 37 | temp->left = temp->right = NULL; 38 | return temp; 39 | } 40 | 41 | int main() 42 | { 43 | Node *root = newNode(1); 44 | root->left = newNode(2); 45 | root->right = newNode(3); 46 | root->left->left = newNode(4); 47 | root->left->right = newNode(5); 48 | inorder(root); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /binaryTree/c++/Max path sum bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct Node{ 7 | int data; 8 | struct Node * left ,*right; 9 | }; 10 | 11 | int sum = INT_MIN; 12 | 13 | Node * newnode(int data){ 14 | Node *temp = new Node; 15 | temp->data = data; 16 | temp->left = temp->right = NULL; 17 | return temp; 18 | } 19 | 20 | 21 | int maxpath(Node *root){ 22 | if(root == NULL)return 0; 23 | int lsum = max(0,maxpath(root->left)); 24 | int rsum = max(0,maxpath(root->right)); 25 | sum = max(sum,(root->data + rsum+lsum)); 26 | return root->data + max(rsum,lsum); 27 | } 28 | 29 | int main() 30 | { 31 | Node * root = newnode(1); 32 | root->left = newnode(2); 33 | root->right = newnode(3); 34 | root->left->right = newnode(5); 35 | root->left->left = newnode(4); 36 | root->left->left->left = newnode(7); 37 | maxpath(root); 38 | cout << sum; 39 | 40 | 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /binaryTree/c++/Postorder traversal iterative method.cpp: -------------------------------------------------------------------------------- 1 | //Postorder traversal iterative method 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct Node{ 9 | int data; 10 | Node * left,*right; 11 | }; 12 | 13 | void postorder(Node * root){ 14 | stack s1; 15 | stack s2; 16 | Node * node = root; 17 | s1.push(root); 18 | while(!s1.empty()){ 19 | Node* n = s1.top(); 20 | s1.pop(); 21 | s2.push(n); 22 | if(n->left!=NULL)s1.push(n->left); 23 | if(n->right!=NULL)s1.push(n->right); 24 | } 25 | while(!s2.empty()){ 26 | cout << s2.top()->data << " "; 27 | s2.pop(); 28 | } 29 | } 30 | 31 | Node * newnode(int data){ 32 | Node *temp = new Node; 33 | temp->data = data; 34 | temp->left = temp->right = NULL; 35 | return temp; 36 | } 37 | 38 | int main(){ 39 | struct Node* root = newnode(1); 40 | root->left = newnode(2); 41 | root->right = newnode(3); 42 | root->left->left = newnode(4); 43 | root->left->right = newnode(5); 44 | 45 | postorder(root); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /binaryTree/c++/Preorder traversal iterative method using stack.cpp: -------------------------------------------------------------------------------- 1 | // Preorder traversal iterative method using stack 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct Node{ 8 | int data; 9 | struct Node *left,*right; 10 | }; 11 | 12 | 13 | void preorder(Node * root){ 14 | stack s; 15 | s.push(root); 16 | while(!s.empty()){ 17 | Node *node = s.top(); 18 | s.pop(); 19 | cout << node->data << " "; 20 | if(node->right != NULL)s.push(node->right); 21 | if(node->left != NULL)s.push(node->left); 22 | } 23 | } 24 | Node* newNode(int data){ 25 | Node*temp = new Node; 26 | temp->data = data; 27 | temp->left = temp->right = NULL; 28 | return temp; 29 | } 30 | 31 | int main() 32 | { 33 | Node *root = newNode(1); 34 | root->left = newNode(2); 35 | root->right = newNode(3); 36 | root->left->left = newNode(4); 37 | root->left->right = newNode(5); 38 | preorder(root); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /binaryTree/c++/Same or not BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct Node{ 7 | int data; 8 | struct Node * left ,*right; 9 | }; 10 | 11 | bool same(Node *p,Node *q){ 12 | if(p == NULL || q == NULL) 13 | return (p == q); 14 | return (p->data == q->data)&&same(p->left,q->left)&&same(p->right,q->right); 15 | } 16 | 17 | Node * newnode(int data){ 18 | Node *temp = new Node; 19 | temp->data = data; 20 | temp->left = temp->right = NULL; 21 | return temp; 22 | } 23 | 24 | 25 | int main() 26 | { 27 | Node * root = newnode(1); 28 | root->left = newnode(2); 29 | root->right = newnode(3); 30 | root->left->right = newnode(5); 31 | root->left->left = newnode(4); 32 | root->left->left->left = newnode(7); 33 | Node * root2 = newnode(1); 34 | root2->left = newnode(2); 35 | root2->right = newnode(3); 36 | root2->left->right = newnode(5); 37 | root2->left->left = newnode(4); 38 | root2->left->left->left = newnode(7); 39 | root2->left->left->right = newnode(7); 40 | if(same(root,root2)){ 41 | cout << "Same" << endl; 42 | } 43 | else{ 44 | cout << "Not Same" << endl; 45 | } 46 | 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /binaryTree/c++/binary_tree_inorder_preorder_postorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *left; 9 | struct Node *right; 10 | 11 | Node(int val) 12 | { 13 | data = val; 14 | left = right = NULL; 15 | } 16 | }; 17 | 18 | void printPreorder(struct Node *root) 19 | { 20 | if (root == NULL) 21 | { 22 | return; 23 | } 24 | cout << root->data << " "; 25 | printPreorder(root->left); 26 | printPreorder(root->right); 27 | } 28 | 29 | void printInorder(struct Node *root) 30 | { 31 | if (root == NULL) 32 | { 33 | return; 34 | } 35 | printInorder(root->left); 36 | cout << root->data << " "; 37 | printInorder(root->right); 38 | } 39 | 40 | void printPostorder(struct Node *root) 41 | { 42 | if (root == NULL) 43 | { 44 | return; 45 | } 46 | printPostorder(root->left); 47 | printPostorder(root->right); 48 | cout << root->data << " "; 49 | } 50 | 51 | int main() 52 | { 53 | struct Node *root = new Node(1); 54 | root->left = new Node(2); 55 | root->right = new Node(3); 56 | root->left->left = new Node(4); 57 | root->left->right = new Node(5); 58 | root->right->left = new Node(6); 59 | root->right->right = new Node(7); 60 | 61 | printPreorder(root); 62 | cout << "\n"; 63 | printInorder(root); 64 | cout << "\n"; 65 | printPostorder(root); 66 | cout << "\n"; 67 | 68 | return 0; 69 | } -------------------------------------------------------------------------------- /binaryTree/c++/depth and height.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/binaryTree/c++/depth and height.png -------------------------------------------------------------------------------- /binaryTree/c++/inorder.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | struct node{ 5 | int data; 6 | struct node *right,*left; 7 | }; 8 | struct node *new_node(int val){ 9 | struct node *n=(struct node*)malloc(sizeof(struct node)); 10 | n->data=val; 11 | n->left=n->right=NULL; 12 | return n; 13 | } 14 | struct node *insert(struct node *root,int val){ //already having a binary tree 15 | if(root==NULL){ 16 | return new_node(val); 17 | } 18 | if(valdata){ 19 | root->left=insert(root->left,val); 20 | }else{ 21 | root->right=insert(root->right,val); 22 | } 23 | return root; 24 | } 25 | void inorder(struct node *root){ 26 | if(root==NULL) 27 | return; 28 | inorder(root->left); 29 | cout<data<<" "; 30 | inorder(root->right); 31 | } 32 | 33 | int main() { 34 | 35 | struct node *root=NULL; 36 | int vals[]={15,10,20,8,12,16,25}; 37 | for(int val:vals){ 38 | root=insert(root,val); 39 | } 40 | inorder(root); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /binaryTree/c++/max height of bst.cpp: -------------------------------------------------------------------------------- 1 | // max depth / max Height 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct Node{ 9 | int data; 10 | Node * left,*right; 11 | }; 12 | 13 | int maxdepth(Node * root){ 14 | if(root == NULL)return -1; 15 | else{ 16 | int ld= maxdepth(root->left); 17 | int rd= maxdepth(root->right); 18 | 19 | if(ld > rd){ 20 | return ld + 1; 21 | } 22 | else{ 23 | return rd + 1; 24 | } 25 | } 26 | } 27 | 28 | Node * newnode(int data){ 29 | Node *temp = new Node; 30 | temp->data = data; 31 | temp->left = temp->right = NULL; 32 | return temp; 33 | } 34 | 35 | int main(){ 36 | struct Node* root = newnode(1); 37 | root->left = newnode(2); 38 | root->right = newnode(3); 39 | root->left->left = newnode(4); 40 | root->left->right = newnode(5); 41 | 42 | cout << "Height of the tree is: "<< maxdepth(root); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /binaryTree/c++/preorder postorder inorder traversals.cpp: -------------------------------------------------------------------------------- 1 | //Preorder Inorder Postorder tree traversals 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct Node{ 7 | int data; 8 | struct Node *left,*right; 9 | Node(int data){ 10 | this->data = data; 11 | left = right = NULL; 12 | } 13 | }; 14 | 15 | void preorder(struct Node *node){ 16 | if(node == NULL){ 17 | return; 18 | } 19 | cout << node->data << " "; 20 | preorder(node->left); 21 | preorder(node->right); 22 | } 23 | 24 | void inorder(struct Node *node){ 25 | if(node == NULL){ 26 | return; 27 | } 28 | inorder(node->left); 29 | cout << node->data << " "; 30 | inorder(node->right); 31 | } 32 | 33 | void postorder(struct Node *node){ 34 | if(node == NULL){ 35 | return; 36 | } 37 | postorder(node->left); 38 | postorder(node->right); 39 | cout << node->data << " "; 40 | } 41 | 42 | int main() 43 | { 44 | struct Node* root = new Node(1); 45 | root->left = new Node(2); 46 | root->right = new Node(3); 47 | root->left->left = new Node(4); 48 | root->left->right = new Node(5); 49 | preorder(root); 50 | cout << endl; 51 | inorder(root); 52 | cout << endl; 53 | postorder(root); 54 | cout << endl; 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /binaryTree/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /binaryTree/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /binaryTree/java/MainBinaryTree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MainBinaryTree 3 | * 4 | * @author Ramon Almeida 5 | * 6 | */ 7 | 8 | import java.util.Comparator; 9 | 10 | public class MainBinaryTree { 11 | public static void main(String[] args) throws Exception { 12 | 13 | BinaryTree binaryTree = new BinaryTree(new CompatorStringKey()); 14 | 15 | // Formate output toString Binary Tree 16 | // Node ("nodeActual") - F("Father") - {"left", "left"} 17 | 18 | binaryTree.add("a", 1); 19 | System.out.println(binaryTree); 20 | 21 | binaryTree.remove("a"); 22 | System.out.println(binaryTree); 23 | 24 | binaryTree.add("e", 5); 25 | binaryTree.add("c", 3); 26 | binaryTree.add("j", 10); 27 | binaryTree.add("t", 20); 28 | binaryTree.add("i", 9); 29 | binaryTree.add("g", 7); 30 | binaryTree.add("h", 8); 31 | System.out.println(binaryTree); 32 | System.out.println("\n"); 33 | binaryTree.remove("c"); 34 | System.out.println(binaryTree); 35 | } 36 | } 37 | 38 | /** 39 | * CompatorStringKey 40 | * 41 | * Class Key Comparator 42 | */ 43 | class CompatorStringKey implements Comparator { 44 | 45 | @Override 46 | public int compare(String obj1, String obj2) { 47 | return obj1.compareTo(obj2); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /binaryTree/java/Node.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Node 3 | * 4 | * @author Ramon Almeida 5 | * 6 | */ 7 | public class Node { 8 | 9 | private final K key; 10 | private final T data; 11 | private Node father; 12 | private Node left; 13 | private Node right; 14 | 15 | public Node(K key, T data) { 16 | this.key = key; 17 | this.data = data; 18 | father = left = right = null; 19 | } 20 | 21 | public Node(K key, T data, Node father) { 22 | this.key = key; 23 | this.data = data; 24 | this.father = father; 25 | left = right = null; 26 | } 27 | 28 | public K getKey() { 29 | return key; 30 | } 31 | 32 | public T getData() { 33 | return data; 34 | } 35 | 36 | public Node getFather() { 37 | return father; 38 | } 39 | 40 | public void setFather(Node father) { 41 | this.father = father; 42 | } 43 | 44 | public Node getLeft() { 45 | return left; 46 | } 47 | 48 | public void setLeft(Node left) { 49 | this.left = left; 50 | } 51 | 52 | public Node getRight() { 53 | return right; 54 | } 55 | 56 | public void setRight(Node right) { 57 | this.right = right; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /binaryTree/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /binaryTree/python/Binary Search Tree/inorder-traversal.py: -------------------------------------------------------------------------------- 1 | #Definition for a binary tree node. 2 | class TreeNode: 3 | def __init__(self, val=0, left=None, right=None): 4 | self.val = val 5 | self.left = left 6 | self.right = right 7 | 8 | class Solution: 9 | def inorderTraversal(self, root: TreeNode): 10 | result = [] 11 | if root: 12 | result.append(root.val) 13 | result += self.inorderTraversal(root.left) 14 | result += self.inorderTraversal(root.right) 15 | 16 | return result 17 | -------------------------------------------------------------------------------- /binaryTree/python/Binary Search Tree/post-order-traversal.py: -------------------------------------------------------------------------------- 1 | class TreeNode: 2 | def __init__(self, val=0, left=None, right=None): 3 | self.val = val 4 | self.left = left 5 | self.right = right 6 | 7 | class Solution: 8 | def postorderTraversal(self, root: TreeNode): 9 | result = [] 10 | if root is not None: 11 | result = self.postorderTraversal(root.left) 12 | result += self.postorderTraversal(root.right) 13 | result.append(root.val) 14 | 15 | return result 16 | -------------------------------------------------------------------------------- /binaryTree/python/Binary Search Tree/pre-order-traversal.py: -------------------------------------------------------------------------------- 1 | #Definition for a binary tree node. 2 | class TreeNode: 3 | def __init__(self, val=0, left=None, right=None): 4 | self.val = val 5 | self.left = left 6 | self.right = right 7 | 8 | class Solution: 9 | def preorderTraversal(self, root: TreeNode): 10 | result = [] 11 | if root: 12 | result.append(root.val) 13 | result += self.preorderTraversal(root.left) 14 | result += self.preorderTraversal(root.right) 15 | 16 | return result 17 | -------------------------------------------------------------------------------- /binaryTree/python/lowest-common-ancestor-binary-tree.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lowestCommonAncestor(self, root, p, q): 3 | if root is None: 4 | return None 5 | if root.val == p.val or root.val == q.val: 6 | return root 7 | 8 | left = self.lowestCommonAncestor(root.left, p, q) 9 | right = self.lowestCommonAncestor(root.right, p, q) 10 | 11 | if left is None: 12 | return right 13 | if right is None: 14 | return left 15 | return root -------------------------------------------------------------------------------- /binaryTree/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /c++/33.Finding max & min in single scan .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int A[10]={5,8,3,9,6,2,10,7,-1,4}; 6 | int n=sizeof(A)/sizeof(int); 7 | int max=A[0]; 8 | int min=A[0]; 9 | for (int i=0;imax) 14 | max=A[i]; 15 | } 16 | cout<<"Maximun is "< 2 | using namespace std; 3 | 4 | int main() { 5 | int i, n; 6 | bool isPrime = true; 7 | 8 | cout << "Enter a positive integer: "; 9 | cin >> n; 10 | 11 | // 0 and 1 are not prime numbers 12 | if (n == 0 || n == 1) { 13 | isPrime = false; 14 | } 15 | else { 16 | for (i = 2; i <= n / 2; ++i) { 17 | if (n % i == 0) { 18 | isPrime = false; 19 | break; 20 | } 21 | } 22 | } 23 | if (isPrime) 24 | cout << n << " is a prime number"; 25 | else 26 | cout << n << " is not a prime number"; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /c++/Clear All Bits from MSB.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namepsace std; 3 | 4 | int clearBitsMSBThrough(int num, int i) { 5 | int mask = ( 1 << (i + 1) ) - 1; 6 | return num & mask; 7 | } 8 | 9 | int main(){ 10 | int i,num; 11 | cout<<"Please enter a number:"<>num; 13 | cout<<"Enter the value of i:"<>i; 15 | cout< 2 | using namespace std; 3 | 4 | // function to convert decimal to binary 5 | void decToBinary(int n) 6 | { 7 | // array to store binary number 8 | int binaryNum[32]; 9 | 10 | // counter for binary array 11 | int i = 0; 12 | while (n > 0) { 13 | 14 | // storing remainder in binary array 15 | binaryNum[i] = n % 2; 16 | n = n / 2; 17 | i++; 18 | } 19 | 20 | // printing binary array in reverse order 21 | for (int j = i - 1; j >= 0; j--) 22 | cout << binaryNum[j]; 23 | } 24 | 25 | // Driver program to test above function 26 | int main() 27 | { 28 | int n = 17; 29 | decToBinary(n); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /c++/Decimal_to_binary(using string).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int dtob(long n){ 6 | string s = ""; 7 | while(n!=0){ 8 | long mod = n%2; 9 | s = to_string(mod) + s; 10 | n = n/2; 11 | } 12 | int num = 0; 13 | for(int i = 0;i < s.size();i++){ 14 | num = num*10 + s[i]-'0'; 15 | } 16 | return num; 17 | } 18 | 19 | int main() 20 | { 21 | long n; 22 | cin >> n; 23 | 24 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n1, n2, hcf; 6 | cout << "Enter two numbers: "; 7 | cin >> n1 >> n2; 8 | 9 | // swapping variables n1 and n2 if n2 is greater than n1. 10 | if ( n2 > n1) { 11 | int temp = n2; 12 | n2 = n1; 13 | n1 = temp; 14 | } 15 | 16 | for (int i = 1; i <= n2; ++i) { 17 | if (n1 % i == 0 && n2 % i ==0) { 18 | hcf = i; 19 | } 20 | } 21 | 22 | cout << "HCF = " << hcf; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /c++/Key_Segment.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n,k,x; 6 | bool check = false; 7 | cout<<"Enter size of array, key, segment size\n"; 8 | cin>>n>>x>>k; 9 | int arr[n]; 10 | cout<<"Enter elements of array\n"; 11 | for(int i = 0; i < n; i++ ) 12 | cin>>arr[i]; 13 | for(int i = 0; i < n ; i++) 14 | { 15 | if(arr[i] == x) 16 | check = true; 17 | if((i+1)%k == 0 && (i+1) != n) 18 | { 19 | if(!check) 20 | break; 21 | check = false; 22 | } 23 | } 24 | if(check) 25 | cout<<"Yes"; 26 | else 27 | cout<<"No"; 28 | } -------------------------------------------------------------------------------- /c++/LCM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n1, n2, max; 7 | 8 | cout << "Enter two numbers: "; 9 | cin >> n1 >> n2; 10 | 11 | // maximum value between n1 and n2 is stored in max 12 | max = (n1 > n2) ? n1 : n2; 13 | 14 | do 15 | { 16 | if (max % n1 == 0 && max % n2 == 0) 17 | { 18 | cout << "LCM = " << max; 19 | break; 20 | } 21 | else 22 | ++max; 23 | } while (true); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /c++/LeapYear.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int year; 6 | 7 | cout << "Enter a year: "; 8 | cin >> year; 9 | 10 | if (year % 4 == 0) { 11 | if (year % 100 == 0) { 12 | if (year % 400 == 0) 13 | cout << year << " is a leap year."; 14 | else 15 | cout << year << " is not a leap year."; 16 | } 17 | else 18 | cout << year << " is a leap year."; 19 | } 20 | else 21 | cout << year << " is not a leap year."; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /c++/Left_and_Right_Rotation_of_a_String.cpp: -------------------------------------------------------------------------------- 1 | // C program for Left Rotation and Right Rotation of a String 2 | 3 | #include 4 | using namespace std; 5 | 6 | // In-place rotates s towards left by d 7 | void leftrotate(string &s, int d) 8 | { 9 | reverse(s.begin(), s.begin()+d); 10 | reverse(s.begin()+d, s.end()); 11 | reverse(s.begin(), s.end()); 12 | } 13 | 14 | // In-place rotates s towards right by d 15 | void rightrotate(string &s, int d) 16 | { 17 | leftrotate(s, s.length()-d); 18 | } 19 | 20 | // Driver code 21 | int main() 22 | { 23 | string str1 = "GeeksforGeeks"; 24 | leftrotate(str1, 2); 25 | cout << str1 << endl; 26 | 27 | string str2 = "GeeksforGeeks"; 28 | rightrotate(str2, 2); 29 | cout << str2 << endl; 30 | return 0; 31 | } 32 | 33 | // Sample Output 34 | // Input : s = "qwertyu" 35 | // d = 2 36 | // Output : Left rotation : "ertyuqw" 37 | // Right rotation : "yuqwert" 38 | -------------------------------------------------------------------------------- /c++/Multi level inheritance.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 | -------------------------------------------------------------------------------- /c++/N integers to binary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | long long dtob(int n){ 5 | long long sum = 0,base = 1; 6 | while(n >=base){ 7 | base = base * 2; 8 | } 9 | base = base/2; 10 | while(base > 0){ 11 | int l = n/base; 12 | sum = sum *10 + l; 13 | n = n - l*base; 14 | base = base/2; 15 | } 16 | return sum ; 17 | 18 | } 19 | long long power(int b,int n){ 20 | long long s =1; 21 | for(int i = 1;i <=n;i++){ 22 | s = s * b; 23 | } 24 | return s; 25 | 26 | } 27 | 28 | long long btod(int n){ 29 | long long l, temp = 1,ans = 0; 30 | while(n > 0){ 31 | l = n%10; 32 | ans+= temp*l; 33 | temp = temp *2; 34 | n = n/10; 35 | } 36 | return ans; 37 | 38 | } 39 | 40 | int main(int argc, char** argv){ 41 | int n;cin>> n; 42 | long long a[n],b[n],c[n]; 43 | for(int i = 0;i < n;i++){ 44 | cin >> a[i]; 45 | 46 | } 47 | for(int i = 0 ;i < n;i++){ 48 | b[i]=dtob(a[i]); 49 | 50 | } 51 | for(int i = 1;i <= n;i++){ 52 | c[i-1] = (b[i-1]%power(10,i))/power(10,i-1); 53 | 54 | } 55 | long long s = 0; 56 | for(int i = n-1;i >=0;i--){ 57 | s= s*10 + c[i]; 58 | 59 | } 60 | cout << btod(s); 61 | 62 | } -------------------------------------------------------------------------------- /c++/OddEven.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to check for even or odd 2 | // using Bitwise XOR operator 3 | 4 | #include 5 | using namespace std; 6 | 7 | // Returns true if n is even, else odd 8 | bool isEven(int n) 9 | { 10 | 11 | // n^1 is n+1, then even, else odd 12 | if (n ^ 1 == n + 1) 13 | return true; 14 | else 15 | return false; 16 | } 17 | 18 | // Driver code 19 | int main() 20 | { 21 | int n = 100; 22 | isEven(n) 23 | ? cout << "Even" 24 | : cout << "Odd"; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /c++/STL_UsingMap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // empty map container 9 | map marks; 10 | // insert elements in random order 11 | marks["Shreya"] = 98; 12 | marks["Neha"] = 59; 13 | marks["Jack"] = 2; 14 | 15 | // insert in map 16 | marks.insert({"Suzy",88}); 17 | marks.insert({"Gerald",95}); 18 | 19 | // printing map marksMap 20 | map :: iterator iter; 21 | for(iter=marks.begin(); iter != marks.end(); iter++){ 22 | cout<<(*iter).first<<" "<<(*iter).second< :: iterator iter2; 32 | for (iter2 = marks.begin(); iter2 != marks.end(); ++iter2) { 33 | cout<<(*iter2).first<<" "<<(*iter2).second< 2 | using namespace std; 3 | 4 | int travelling Salesman Problem(int graph[][V], int s) { 5 | vector < int > vertex; 6 | for (int i = 0; i < V; i++) 7 | if (i != s) 8 | vertex.push_back(i); 9 | int min_path = INT_MAX; 10 | do { 11 | int current_pathweight = 0; 12 | int k = s; 13 | for (int i = 0; i < vertex.size(); i++) { 14 | current_pathweight += graph[k][vertex[i]]; 15 | k = vertex[i]; 16 | } 17 | current_pathweight += graph[k][s]; 18 | min_path = min(min_path, current_pathweight); 19 | 20 | } while ( 21 | next_permutation(vertex.begin(), vertex.end())); 22 | 23 | return min_path; 24 | } 25 | -------------------------------------------------------------------------------- /c++/WordCounter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void) 4 | { 5 | char str1[] = "Hello, this is a \nWord Counter\n Program "; 6 | char *str=str1; 7 | int state = 0, wc = 0; 8 | while (*str) 9 | { 10 | if (*str == ' ' || *str == '\n' || *str == '\t') 11 | state = 0; 12 | else if (state == 0) 13 | { 14 | state = 1; 15 | ++wc; 16 | } 17 | ++str; 18 | } 19 | cout<<"No of words : "< 2 | using namespace std; 3 | 4 | int main(){ 5 | int i,j,n,t,sum = 0; 6 | cout << "Please enter a number:"; 7 | cin >> n; 8 | t = n; 9 | while(n > 0){ 10 | i = n % 10; 11 | j = i*i*i; 12 | sum = sum + j; 13 | n = n/10; 14 | } 15 | if(t == sum) 16 | cout << sum << " is an Armstrong Number" << endl; 17 | else 18 | cout << "Not An Armstrong Number" << endl; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /c++/binary_search_iterative.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | int binarySearch(int arr[], int left, int right, int value){ 4 | 5 | while(left<=right){ 6 | int mid = left + (right - left)/2; 7 | if(arr[mid]==value){return mid;} 8 | else if(arr[mid] 2 | using namespace std; 3 | int NumberOfOne(int n) 4 | { 5 | int count=0; 6 | while (n) 7 | { 8 | n=n & (n-1); 9 | count++; 10 | } 11 | return count; 12 | } 13 | 14 | int main() 15 | { 16 | cout< 2 | using namespace std; 3 | int main(){ 4 | 5 | int i,j,r; 6 | cout << "Please enter number of rows:"; 7 | cin >> r; 8 | for(i=1;i<=2*r;i++){ 9 | if(i <=r){ 10 | for(j=1;j <= r-i;j++){ 11 | cout << " "; 12 | } 13 | for(;j<=r;j++){ 14 | cout << "*"; 15 | } 16 | for(;j 2 | using namespace std; 3 | 4 | long long exponent_calc(long long a, long long b){ 5 | if(b==0){ 6 | return (long long)(1); 7 | } 8 | if(b==1){ 9 | return a; 10 | } 11 | long long val = exponent_calc(a, b/2); 12 | if(b%2){ 13 | return a*val*val; 14 | } 15 | return val*val; 16 | } 17 | 18 | int main(){ 19 | long long a, b; //To calculate a^b; b>=0 20 | cout<<"Enter a: "; 21 | cin>>a; 22 | cout<<"Enter b: "; 23 | cin>>b; 24 | cout< 2 | using namespace std; 3 | 4 | int fact(int n){ 5 | int i=n; 6 | while(n>1){ 7 | i = i*(n-1); 8 | --n; 9 | } 10 | return i; 11 | } 12 | 13 | int main(){ 14 | int n; 15 | cout << "Please enter a number:"; 16 | cin >> n; 17 | cout << fact(n) << endl; 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /c++/fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void fib(int n){ 4 | int t1=0,t2=1,next; 5 | for(int i = 1;i <= n;i++){ 6 | cout << t1 << " "; 7 | next = t1 + t2; 8 | t1= t2; 9 | t2= next; 10 | } 11 | return; 12 | } 13 | 14 | int main(){ 15 | int n; 16 | cout << "Please enter a number:"; 17 | cin >> n; 18 | fib(n); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /c++/hollowdiamond.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int i,j,n; 5 | cout << "Please enter number of rows:"; 6 | cin >> n; 7 | int space= (2*n-1)/2; 8 | for(i = 1;i <= n;i++){ 9 | for(j = 1;j <= space;j++){ 10 | cout << " "; 11 | } 12 | for(j = 1;j <=2*i-1;j++){ 13 | if(j==1 || j == 2*i -1) 14 | cout << "*"; 15 | else 16 | cout << " "; 17 | } 18 | 19 | space--; 20 | cout << endl; 21 | } 22 | space = 0; 23 | for(i = 1;i <= n;i++){ 24 | for(j = 1; j <= space;j++){ 25 | cout <<" "; 26 | } 27 | for(j = i; j <= 2*n-i;j++){ 28 | if(j==i || j == 2*n-i) 29 | cout << "*"; 30 | else 31 | cout << " "; 32 | } 33 | 34 | space++; 35 | cout << endl; 36 | } 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /c++/ncr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int n){ 5 | int fact = n; 6 | while(n > 1){ 7 | fact = fact * (n-1); 8 | --n; 9 | } 10 | return fact; 11 | } 12 | 13 | int main(){ 14 | int n,r,ans; 15 | cout << "Please enter n, r:"; 16 | cin >> n >> r; 17 | ans = factorial(n)/(factorial(r)*factorial(n-r)); 18 | cout << ans << endl; 19 | 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /c++/num_of_islands.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int X[] = {-1 , 0 , 0 , 1}; 8 | int Y[] = {0 , -1 , 1 , 0}; 9 | 10 | 11 | 12 | void dfs(int graph[][50] , int n , bool visited[][50] , int i , int j){ 13 | 14 | visited[i][j] = true; 15 | 16 | for(int k=0 ; k<4 ; k++){ 17 | int x = i + X[k]; 18 | int y = j + Y[k]; 19 | if(x>=0 && y>=0 && x>n; 36 | int graph[50][50]{0}; 37 | 38 | for(int i=0 ; i>graph[i][j]; 41 | } 42 | } 43 | 44 | bool visited[50][50]{0}; 45 | int num_of_cc = 0; 46 | for(int i=0 ; i 2 | using namespace std; 3 | 4 | int fact(int n){ 5 | int f = 1; 6 | for(int i = 2; i <= n;i++){ 7 | f *= i; 8 | } 9 | return f; 10 | } 11 | 12 | int main(){ 13 | int i,j,n; 14 | cout << "Please enter number of rows:"; 15 | cin >> n; 16 | for(i = 0;i < n;i++){ 17 | for(j = 0;j <= i;j++){ 18 | int ans = fact(i)/(fact(j) * fact(i - j)); 19 | cout << ans << " "; 20 | } 21 | cout << endl; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /c++/pattern1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int i,j,n; 5 | cout << "Please enter rows:"; 6 | cin >> n ; 7 | int count; 8 | int num; 9 | for(i=1;i <= n;i++){ 10 | count = 6; 11 | num = 1; 12 | for(j = 1;j < 2*n;j++){ 13 | count --; 14 | 15 | if(j >=1 && j <= n-i) { 16 | cout <<" "; 17 | 18 | } 19 | else if(j <= n){ 20 | cout << count ; 21 | } 22 | 23 | else if(j>5 && j 1){ 24 | num++; 25 | cout << num ; 26 | } 27 | 28 | 29 | 30 | } 31 | 32 | 33 | cout << endl; 34 | 35 | } 36 | return 0; 37 | 38 | } -------------------------------------------------------------------------------- /c++/pattern1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/c++/pattern1.exe -------------------------------------------------------------------------------- /c++/prime-or-not.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int i,j,n; 6 | cout << "Please enter a number:"; 7 | cin >> n; 8 | int flag = 0; 9 | for(i = 2;i <= sqrt(n);i++){ 10 | if(n%i==0){ 11 | cout << " Non-Prime" < 2 | using namespace std; 3 | int main(){ 4 | int n,num = 2; 5 | cout << "Please enter the number:"; 6 | cin >> n; 7 | while( num < n){ 8 | int div = 2; 9 | while( div < num){ 10 | if(num%div==0){ 11 | num = num + 1; 12 | } 13 | else{ 14 | div = div + 1; 15 | } 16 | } 17 | cout << num << " "; 18 | num= num+1; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /c++/primefunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isprime(int n){ 6 | int i; 7 | for(i = 2;i <= sqrt(n);i++){ 8 | if(n%i == 0){ 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | 15 | int main(){ 16 | int a,b,n,i; 17 | cout << "Please enter a numbers a and b:"; 18 | cin >> a >> b; 19 | for(i=a;i<=b;i++){ 20 | if(isprime(i)){ 21 | cout << i << " "; 22 | } 23 | } 24 | cin >> n; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /c++/pyramid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int i,j,n; 5 | cout << "Please enter number of rows:"; 6 | cin >> n; 7 | int space=(2*n-1)/2; 8 | int count; 9 | for(i = 1;i <= n;i++){ 10 | count =1 ; 11 | for(j = 1;j<=space;j++){ 12 | cout << " "; 13 | } 14 | for(j=1;j <=i;j++){ 15 | cout << count << " " ; 16 | count++; 17 | } 18 | space--; 19 | cout << endl; 20 | } 21 | } -------------------------------------------------------------------------------- /c++/pythagorean-triplets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool check(int x,int y ,int z){ 5 | int a = max(x,max(y,z)); 6 | int b,c; 7 | if(a == x){ 8 | b = y; 9 | c = z; 10 | } 11 | else if(a == y){ 12 | b = x; 13 | c = z; 14 | } 15 | else{ 16 | b = x; 17 | c = y; 18 | } 19 | if(a*a == b*b + c*c){ 20 | return true; 21 | } 22 | else { 23 | return false; 24 | } 25 | } 26 | 27 | int main(){ 28 | int x,y,z; 29 | cin >> x >> y >> z; 30 | if(check(x,y,z)){ 31 | cout << "Pythagorean Triplet"; 32 | } 33 | else{ 34 | cout << "Not a Pythagorean Triplet"; 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /c++/reverse-a-number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int i,sum = 0,n; 6 | cout << "Please enter a number:"; 7 | cin >> n; 8 | while(n!=0){ 9 | i = n%10; 10 | sum = sum*10 +i; 11 | n = n/10; 12 | } 13 | cout << sum; 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /c++/sample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int i,j,n; 6 | cout << "Please enter a number:"; 7 | cin >> n; 8 | 9 | return 0; 10 | } -------------------------------------------------------------------------------- /c++/stringreverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reverseStr(string& str) 5 | { 6 | int n = str.length(); 7 | 8 | for (int i = 0; i < n / 2; i++) 9 | swap(str[i], str[n - i - 1]); 10 | } 11 | 12 | int main() 13 | { 14 | string str = "reverse this string"; 15 | reverseStr(str); 16 | cout << str; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /c++/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | for(i = 1;i < n;i++){ 2 | // for(j = 1; j < n;j++){ 3 | // cout <<""; 4 | // } 5 | // } -------------------------------------------------------------------------------- /c++/testing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | string n ="CFGHJI"; 6 | cout << n < 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | class Node{ 9 | public: 10 | int data; 11 | Node * next; 12 | }*head,*newnode,*temp; 13 | 14 | void insert(int n){ 15 | 16 | newnode = new Node(); 17 | newnode->next = 0; 18 | newnode->data = n; 19 | if(head ==0){ 20 | temp = head =newnode; 21 | } 22 | else{ 23 | temp->next = newnode; 24 | temp=newnode; 25 | } 26 | } 27 | 28 | void printl(){ 29 | temp = head ; 30 | while(temp!=0){ 31 | cout << temp->data << " "; 32 | temp = temp->next; 33 | } 34 | } 35 | int main() { 36 | int n; 37 | head = 0; 38 | cin >> n; 39 | while(n--){ 40 | int num; 41 | cin >> num; 42 | insert(num); 43 | } 44 | printl(); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /c/Merge_between_list.c: -------------------------------------------------------------------------------- 1 | /* You are given two linked lists list1, list2 of size n amd m respectively. 2 | Remove ath to bth node of list1 and insert list2 in their place. 3 | */ 4 | 5 | /* Example 1: 6 | Input : list1 = [0, 1, 2, 3, 4, 5], a=3, b=4, list2 = [10, 20, 30] 7 | Output : [0, 1, 2, 10, 20, 30, 5] 8 | */ 9 | 10 | /* Example 2: 11 | Input : list1 = [1, 2, 3, 4, 5, 6], a=2, b=4, list2 = [100, 200, 300] 12 | Output : [1, 2, 100, 200, 300, 6] 13 | */ 14 | 15 | /* 16 | Definition for singly-linked list. 17 | struct ListNode { 18 | int val; 19 | struct ListNode *next; 20 | }; 21 | */ 22 | 23 | 24 | struct ListNode* mergeInBetween(struct ListNode* list1, int a, int b, struct ListNode* list2){ 25 | struct ListNode *temp=list1,*temp1,*temp2; 26 | int i; 27 | for(i=0;inext; 29 | temp1=temp; 30 | for(;i<=b;i++) 31 | temp=temp->next; 32 | temp2=temp; 33 | temp1->next=list2; 34 | while(list2->next!=NULL) 35 | list2=list2->next; 36 | list2->next=temp2; 37 | return list1; 38 | } 39 | -------------------------------------------------------------------------------- /c/Remove_node.c: -------------------------------------------------------------------------------- 1 | // You are given the head of a linked list and Nth node from the end of the list. 2 | 3 | // Delete the Nth node and return the updated linked list. 4 | 5 | /* Example 1 6 | Input : head=[1, 2, 3, 4, 5] 7 | n=2 8 | Output : [1, 2, 3, 5] 9 | */ 10 | 11 | /* Example 2 12 | Input : head=[2, 4, 6, 1, 5, 2 ,4] 13 | n=4 14 | Output : [2, 4, 6, 5, 2, 4] 15 | */ 16 | 17 | 18 | /** 19 | * Definition for singly-linked list. 20 | * struct ListNode { 21 | * int val; 22 | * struct ListNode *next; 23 | * }; 24 | */ 25 | 26 | 27 | struct ListNode* removeNthFromEnd(struct ListNode* head, int n){ 28 | int i=0; 29 | struct ListNode *temp=head; 30 | if(head->next==NULL) 31 | return NULL; 32 | while(temp!=NULL) 33 | { 34 | i++; 35 | temp=temp->next; 36 | } 37 | if(i==n) 38 | return head->next; 39 | i=i-n-1; 40 | temp=head; 41 | while(i>0) 42 | { 43 | i--; 44 | temp=temp->next; 45 | } 46 | temp->next=temp->next->next; 47 | return head; 48 | } 49 | -------------------------------------------------------------------------------- /c/Reverse_list.c: -------------------------------------------------------------------------------- 1 | /*You are given a linked list and two integers left and right (left<=right) 2 | you have to reverse the list from position left to right, and 3 | return the reversed linked list*/ 4 | 5 | /* Example 1: 6 | 7 | Input: head=[1, 2, 3, 4, 5], left=2, right=4 8 | Output: [1, 4, 3, 2, 5] 9 | */ 10 | 11 | /* Example 2: 12 | 13 | Input: head=[1, 3, 4, 7, 8, 2, 9], left=3, right=5 14 | Output: [1, 3, 8, 7, 4, 2, 9] 15 | */ 16 | 17 | /* 18 | Definition for singly-linked list. 19 | struct ListNode { 20 | int val; 21 | struct ListNode *next; 22 | }; 23 | */ 24 | 25 | 26 | struct ListNode* reverseBetween(struct ListNode* head, int m, int n){ 27 | if(head==NULL) 28 | return NULL; 29 | struct ListNode *cur=head,*prev=NULL; 30 | while(m>1) 31 | { 32 | prev=cur; 33 | cur=cur->next; 34 | m--; 35 | n--; 36 | } 37 | struct ListNode *con=prev,*tail=cur,*next=NULL; 38 | while(n>0) 39 | { 40 | next=cur->next; 41 | cur->next=prev; 42 | prev=cur; 43 | cur=next; 44 | n--; 45 | } 46 | if(con!=NULL) 47 | con->next=prev; 48 | else 49 | head=prev; 50 | tail->next=cur; 51 | return head; 52 | } 53 | -------------------------------------------------------------------------------- /c/palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int reverse(int num); 4 | int p_d(int num); 5 | int main() 6 | { 7 | int num; 8 | printf("Number:"); 9 | scanf("%d", &num); 10 | if(p_d(num) == 1) 11 | { 12 | printf("Palindrome"); 13 | } 14 | else 15 | { 16 | printf("Not Palindrome"); 17 | } 18 | return 0; 19 | } 20 | int p_d(int num) 21 | { 22 | if(num == reverse(num)) 23 | { 24 | return 1; 25 | } 26 | return 0; 27 | } 28 | int reverse(int num) 29 | { 30 | int rem; 31 | static int sum=0; 32 | if(num!=0){ 33 | rem=num%10; 34 | sum=sum*10+rem; 35 | reverse(num/10); 36 | } 37 | else 38 | return sum; 39 | return sum; 40 | } 41 | -------------------------------------------------------------------------------- /c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes DSA problems and algorithms in C language. -------------------------------------------------------------------------------- /dynamic programming/c++/0-1Knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int knapSack(int W, int wt[], int val[], int n) 4 | { 5 | // making and initializing dp array 6 | int dp[W + 1]; 7 | memset(dp, 0, sizeof(dp)); 8 | 9 | for (int i = 1; i < n + 1; i++) { 10 | for (int w = W; w >= 0; w--) { 11 | 12 | if (wt[i - 1] <= w) 13 | // finding the maximum value 14 | dp[w] = max(dp[w], 15 | dp[w - wt[i - 1]] + val[i - 1]); 16 | } 17 | } 18 | return dp[W]; // returning the maximum value of knapsack 19 | } 20 | int main() 21 | { 22 | int n ; 23 | cout<<"Enter number of items" ; 24 | cin>>n ; 25 | int va[n] ,wt[n]; 26 | cout<<"Enter Value of items\n" ; 27 | for(int i = 0 ; i>val[i] ; 30 | } 31 | cout<<"Enter Weight of items\n" ; 32 | for(int i = 0 ; i>wt[i] ; 35 | } 36 | cout<<"Enter capacity of Knapsack\n" ; 37 | int W ; 38 | cin>>W ; 39 | cout << knapSack(W, wt, val, n); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /dynamic programming/c++/coinChange.cpp: -------------------------------------------------------------------------------- 1 | //Problem link: 2 | //https://leetcode.com/problems/coin-change/ 3 | 4 | class Solution { 5 | public: 6 | int coinChange(vector& coins, int amount) { 7 | 8 | vector dp(amount+1); 9 | fill(dp.begin(), dp.end(), amount+1); 10 | 11 | dp[0] = 0; 12 | 13 | for(int coin : coins) 14 | { 15 | for(int i=coin; i<=amount; i++) 16 | { 17 | dp[i] = min(dp[i], dp[i-coin] + 1); 18 | } 19 | } 20 | 21 | return dp[amount] <= amount ? dp[amount] : -1; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /dynamic programming/c++/nthFibonacciNum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | int main(){ 5 | int n; 6 | cout<<"Enter the value of n : "; 7 | cin>>n; 8 | 9 | vectordp(n+1,0); // Initializing dp vector which will store the ith fibnoacci number at ith index 10 | dp[2] = 1; // Defining base case 11 | 12 | for(int i = 3 ; i <= n ; i++){ 13 | dp[i] = dp[i-1] + dp[i-2]; // Using previous results to obtain the next 14 | } 15 | cout<<"Finonacci Number : "< 2 | using namespace std; 3 | 4 | void BFS(vector adj[], int V, int s) 5 | { 6 | bool visited[V]; 7 | for(int i = 0; i < V; i++) 8 | visited[i] = false; 9 | 10 | queue q; 11 | 12 | visited[s] = true; 13 | q.push(s); 14 | 15 | while(q.empty()==false) 16 | { 17 | int u = q.front(); 18 | q.pop(); 19 | cout << u << " "; 20 | 21 | for(int v:adj[u]){ 22 | if(visited[v]==false){ 23 | visited[v]=true; 24 | q.push(v); 25 | } 26 | } 27 | } 28 | } 29 | 30 | void addEdge(vector adj[], int u, int v){ 31 | adj[u].push_back(v); 32 | adj[v].push_back(u); 33 | } 34 | 35 | int main() 36 | { 37 | int V=5; 38 | vector adj[V]; 39 | addEdge(adj,0,1); 40 | addEdge(adj,0,2); 41 | addEdge(adj,1,2); 42 | addEdge(adj,2,3); 43 | addEdge(adj,1,3); 44 | addEdge(adj,3,4); 45 | addEdge(adj,2,4); 46 | 47 | cout << "Breadth First Traversal: "<< endl; 48 | BFS(adj,V,0); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /graph/c++/DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void DFSRec(vector adj[], int s, bool visited[]) 5 | { 6 | visited[s]=true; 7 | cout<< s <<" "; 8 | 9 | for(int u:adj[s]){ 10 | if(visited[u]==false) 11 | DFSRec(adj,u,visited); 12 | } 13 | } 14 | 15 | void DFS(vector adj[], int V, int s){ 16 | bool visited[V]; 17 | for(int i = 0;i adj[], int u, int v){ 24 | adj[u].push_back(v); 25 | adj[v].push_back(u); 26 | } 27 | 28 | int main() 29 | { 30 | int V=5; 31 | vector adj[V]; 32 | addEdge(adj,0,1); 33 | addEdge(adj,0,2); 34 | addEdge(adj,2,3); 35 | addEdge(adj,1,3); 36 | addEdge(adj,1,4); 37 | addEdge(adj,3,4); 38 | 39 | cout << "Following is Depth First Traversal: "<< endl; 40 | DFS(adj,V,0); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /graph/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /graph/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /graph/java/Breadth First Traversal.java: -------------------------------------------------------------------------------- 1 | // define a method to perform the breadth-first traversal: 2 | 3 | Set breadthFirstTraversal(Graph graph, String root) { 4 | Set visited = new LinkedHashSet(); 5 | Queue queue = new LinkedList(); 6 | queue.add(root); 7 | visited.add(root); 8 | while (!queue.isEmpty()) { 9 | String vertex = queue.poll(); 10 | for (Vertex v : graph.getAdjVertices(vertex)) { 11 | if (!visited.contains(v.label)) { 12 | visited.add(v.label); 13 | queue.add(v.label); 14 | } 15 | } 16 | } 17 | return visited; 18 | } -------------------------------------------------------------------------------- /graph/java/Depth First Traversal.java: -------------------------------------------------------------------------------- 1 | // defined a method to perform the depth-first traversal 2 | 3 | Set depthFirstTraversal(Graph graph, String root) { 4 | Set visited = new LinkedHashSet(); 5 | Stack stack = new Stack(); 6 | stack.push(root); 7 | while (!stack.isEmpty()) { 8 | String vertex = stack.pop(); 9 | if (!visited.contains(vertex)) { 10 | visited.add(vertex); 11 | for (Vertex v : graph.getAdjVertices(vertex)) { 12 | stack.push(v.label); 13 | } 14 | } 15 | } 16 | return visited; 17 | } -------------------------------------------------------------------------------- /graph/java/Graph in Java.java: -------------------------------------------------------------------------------- 1 | class Vertex { 2 | String label; 3 | Vertex(String label) { 4 | this.label = label; 5 | } 6 | 7 | // equals and hashCode 8 | } -------------------------------------------------------------------------------- /graph/java/Mutation Operation.java: -------------------------------------------------------------------------------- 1 | // adding and removing elements from vertices Set 2 | 3 | void addVertex(String label) { 4 | adjVertices.putIfAbsent(new Vertex(label), new ArrayList<>()); 5 | } 6 | 7 | void removeVertex(String label) { 8 | Vertex v = new Vertex(label); 9 | adjVertices.values().stream().forEach(e -> e.remove(v)); 10 | adjVertices.remove(new Vertex(label)); 11 | } 12 | 13 | // defined a method to add a edge 14 | 15 | void addEdge(String label1, String label2) { 16 | Vertex v1 = new Vertex(label1); 17 | Vertex v2 = new Vertex(label2); 18 | adjVertices.get(v1).add(v2); 19 | adjVertices.get(v2).add(v1); 20 | } 21 | 22 | // defined the removeEdge() method: 23 | 24 | void removeEdge(String label1, String label2) { 25 | Vertex v1 = new Vertex(label1); 26 | Vertex v2 = new Vertex(label2); 27 | List eV1 = adjVertices.get(v1); 28 | List eV2 = adjVertices.get(v2); 29 | if (eV1 != null) 30 | eV1.remove(v2); 31 | if (eV2 != null) 32 | eV2.remove(v1); 33 | } -------------------------------------------------------------------------------- /graph/java/Simple Graph.java: -------------------------------------------------------------------------------- 1 | // created a Simple graph using the methods defined 2 | 3 | Graph createGraph() { 4 | Graph graph = new Graph(); 5 | graph.addVertex("Bobby"); 6 | graph.addVertex("Alic"); 7 | graph.addVertex("Marry"); 8 | graph.addVertex("Robin"); 9 | graph.addVertex("Maria"); 10 | graph.addEdge("Bobby", "Alic"); 11 | graph.addEdge("Bobby", "Robin"); 12 | graph.addEdge("Alic", "Marry"); 13 | graph.addEdge("Robin", "Marry"); 14 | graph.addEdge("Alic", "Maria"); 15 | graph.addEdge("Robin", "Maria"); 16 | return graph; 17 | } -------------------------------------------------------------------------------- /graph/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in java language . -------------------------------------------------------------------------------- /graph/python/Display Graph Edges.py: -------------------------------------------------------------------------------- 1 | class graph: 2 | def __init__(self,gdict=None): 3 | if gdict is None: 4 | gdict = {} 5 | self.gdict = gdict 6 | 7 | def edges(self): 8 | return self.findedges() 9 | # Find the distinct list of edges 10 | def findedges(self): 11 | edgename = [] 12 | for vrtx in self.gdict: 13 | for nxtvrtx in self.gdict[vrtx]: 14 | if {nxtvrtx, vrtx} not in edgename: 15 | edgename.append({vrtx, nxtvrtx}) 16 | return edgename 17 | # Create the dictionary with graph elements 18 | graph_elements = { 19 | "a" : ["b","c"], 20 | "b" : ["a", "d"], 21 | "c" : ["a", "d"], 22 | "d" : ["e"], 23 | "e" : ["d"] 24 | } 25 | g = graph(graph_elements) 26 | print(g.edges()) -------------------------------------------------------------------------------- /graph/python/Display Graph Vertices.py: -------------------------------------------------------------------------------- 1 | class graph: 2 | def __init__(self,gdict=None): 3 | if gdict is None: 4 | gdict = [] 5 | self.gdict = gdict 6 | # Get the keys of the dictionary 7 | def getVertices(self): 8 | return list(self.gdict.keys()) 9 | # Create the dictionary with graph elements 10 | graph_elements = { 11 | "a" : ["b","c"], 12 | "b" : ["a", "d"], 13 | "c" : ["a", "d"], 14 | "d" : ["e"], 15 | "e" : ["d"] 16 | } 17 | g = graph(graph_elements) 18 | print(g.getVertices()) -------------------------------------------------------------------------------- /graph/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /java/KadaneAlgo.java: -------------------------------------------------------------------------------- 1 | class KadaneAlgo{ 2 | // arr: input array 3 | // n: size of array 4 | //Function to find the sum of contiguous subarray with maximum sum. 5 | long maxSubarraySum(int arr[], int n){ 6 | long currSum=0; //track current sum 7 | long sumSoFar=Long.MIN_VALUE; 8 | 9 | for(int i=0;i 0) 28 | { 29 | //determines the last digit of the given number 30 | int digit = n % 10; 31 | //determines the factorial of the digit and add it to the variable sum 32 | sum += factorial[digit]; 33 | //removes the last digit of the given number 34 | n = n / 10; 35 | } 36 | //compares sum with num if they are equal returns the number itself 37 | return (sum == num); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /java/SearchInMatrix.java: -------------------------------------------------------------------------------- 1 | package Matrix; 2 | 3 | import java.util.Scanner; 4 | 5 | public class SearchInMatrix { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner s = new Scanner(System.in); 10 | int n = s.nextInt(); 11 | int m = s.nextInt(); 12 | int[][] arr = new int[n][m]; 13 | for (int i = 0; i < n; i++) { 14 | for (int j = 0; j < m; j++) { 15 | arr[i][j] = s.nextInt(); 16 | } 17 | } 18 | int target = s.nextInt(); 19 | System.out.println(searchMatrix(arr, target)); 20 | s.close(); 21 | } 22 | 23 | private static boolean searchMatrix(int[][] arr, int target) { 24 | int n = arr.length; 25 | int m = arr[0].length; 26 | for (int i = 0; i < n; i++) { 27 | for (int j = i; j < m; j++) { 28 | if (arr[i][j] == target) { 29 | return true; 30 | } 31 | } 32 | } 33 | return false; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /java/SortMatrixRowColWise.java: -------------------------------------------------------------------------------- 1 | package Matrix; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class SortMatrixRowColWise { 7 | 8 | public static void main(String[] args) { 9 | // TODO Auto-generated method stub 10 | Scanner s = new Scanner(System.in); 11 | int n = s.nextInt(); 12 | int m = s.nextInt(); 13 | int[][] arr = new int[n][m]; 14 | for(int i=0; i 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node * next; 8 | }*head,*newnode,*temp,*temp1; 9 | 10 | void printlist(){ 11 | temp = head ; 12 | while( temp!=0){ 13 | cout << temp->data << "->"; 14 | temp = temp->next; 15 | } 16 | } 17 | 18 | int main() 19 | { 20 | int num; 21 | cout << "Please enter the number of elements: "; 22 | cin >> num; 23 | head = 0; 24 | while(num--){ 25 | newnode = new Node(); 26 | cout << "Please enter the number: "; 27 | cin >> newnode->data; 28 | newnode->next=0; 29 | if (head == 0){ 30 | temp = head = newnode; 31 | } 32 | else{ 33 | temp->next = newnode; 34 | temp = newnode; 35 | } 36 | } 37 | printlist(); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /linked list/Singly linked list/c/Prime Numbers/Prime num Algo 1.c: -------------------------------------------------------------------------------- 1 | // prime number algorithm 1 2 | #include 3 | int isprime(int n){ 4 | if(n <=1)return 0; 5 | if(n==2 || n==3)return 1; 6 | if(n%2==0 || n%3==0)return 0; 7 | for(int i=5;i*i<=n;i=i+6){ 8 | if(n%i==0 || n%(i+2)==0) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | int i,n; 16 | printf("Please enter a number to check if it is prime or not:"); 17 | scanf("%d",&n); 18 | if(isprime(n)){ 19 | printf("Yes\n"); 20 | } 21 | else{ 22 | printf("No\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /linked list/Singly linked list/c/leap year check/Leap Year.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/linked list/Singly linked list/c/leap year check/Leap Year.PNG -------------------------------------------------------------------------------- /linked list/Singly linked list/c/leap year check/leap_year.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | if year % 400 == 0: 4 | leap = True 5 | elif year % 100 == 0: 6 | leap = False 7 | elif year % 4 == 0: 8 | leap = True 9 | return leap_year 10 | -------------------------------------------------------------------------------- /linked list/Singly linked list/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /linked list/Singly linked list/c/traversing singly linked list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | int main() 9 | { 10 | 11 | int choice=1; 12 | struct node *head=NULL,*temp,*newdata; 13 | while(choice) 14 | { 15 | 16 | newdata = (struct node*)(malloc(sizeof(struct node))); 17 | printf("Please enter a value:\n"); 18 | scanf("%d",&newdata->data); 19 | newdata->next = NULL; 20 | 21 | if (head == NULL) 22 | { 23 | head = temp = newdata; 24 | } 25 | else 26 | { 27 | temp -> next =newdata; 28 | temp = newdata; 29 | } 30 | printf("please enter ur choice(0,1):"); 31 | scanf("%d",&choice); 32 | } 33 | 34 | temp = head; 35 | while (temp!= NULL) 36 | { 37 | printf("%d",temp->data); 38 | temp = temp->next; 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /linked list/Singly linked list/insertion deletion merging sorting displaying ques.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/linked list/Singly linked list/insertion deletion merging sorting displaying ques.PNG -------------------------------------------------------------------------------- /linked list/Singly linked list/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /linked list/Singly linked list/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /linked list/circular linked list/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /linked list/circular linked list/c/Prime Numbers/Prime num Algo 1.c: -------------------------------------------------------------------------------- 1 | // prime number algorithm 1 2 | #include 3 | int isprime(int n){ 4 | if(n <=1)return 0; 5 | if(n==2 || n==3)return 1; 6 | if(n%2==0 || n%3==0)return 0; 7 | for(int i=5;i*i<=n;i=i+6){ 8 | if(n%i==0 || n%(i+2)==0) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | int i,n; 16 | printf("Please enter a number to check if it is prime or not:"); 17 | scanf("%d",&n); 18 | if(isprime(n)){ 19 | printf("Yes\n"); 20 | } 21 | else{ 22 | printf("No\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /linked list/circular linked list/c/leap year check/Leap Year.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/linked list/circular linked list/c/leap year check/Leap Year.PNG -------------------------------------------------------------------------------- /linked list/circular linked list/c/leap year check/leap_year.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | if year % 400 == 0: 4 | leap = True 5 | elif year % 100 == 0: 6 | leap = False 7 | elif year % 4 == 0: 8 | leap = True 9 | return leap_year 10 | -------------------------------------------------------------------------------- /linked list/circular linked list/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /linked list/circular linked list/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /linked list/circular linked list/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /linked list/doubly linked list/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . 2 | # Doubley Linked List 3 | ## a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains three fields: two link fields and one data field. 4 | -------------------------------------------------------------------------------- /linked list/doubly linked list/c/Prime Numbers/Prime num Algo 1.c: -------------------------------------------------------------------------------- 1 | // prime number algorithm 1 2 | #include 3 | int isprime(int n){ 4 | if(n <=1)return 0; 5 | if(n==2 || n==3)return 1; 6 | if(n%2==0 || n%3==0)return 0; 7 | for(int i=5;i*i<=n;i=i+6){ 8 | if(n%i==0 || n%(i+2)==0) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | int i,n; 16 | printf("Please enter a number to check if it is prime or not:"); 17 | scanf("%d",&n); 18 | if(isprime(n)){ 19 | printf("Yes\n"); 20 | } 21 | else{ 22 | printf("No\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /linked list/doubly linked list/c/leap year check/Leap Year.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/linked list/doubly linked list/c/leap year check/Leap Year.PNG -------------------------------------------------------------------------------- /linked list/doubly linked list/c/leap year check/leap_year.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | if year % 400 == 0: 4 | leap = True 5 | elif year % 100 == 0: 6 | leap = False 7 | elif year % 4 == 0: 8 | leap = True 9 | return leap_year 10 | -------------------------------------------------------------------------------- /linked list/doubly linked list/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /linked list/doubly linked list/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /linked list/doubly linked list/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /queue/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /queue/c/Prime Numbers/Prime num Algo 1.c: -------------------------------------------------------------------------------- 1 | // prime number algorithm 1 2 | #include 3 | int isprime(int n){ 4 | if(n <=1)return 0; 5 | if(n==2 || n==3)return 1; 6 | if(n%2==0 || n%3==0)return 0; 7 | for(int i=5;i*i<=n;i=i+6){ 8 | if(n%i==0 || n%(i+2)==0) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | int i,n; 16 | printf("Please enter a number to check if it is prime or not:"); 17 | scanf("%d",&n); 18 | if(isprime(n)){ 19 | printf("Yes\n"); 20 | } 21 | else{ 22 | printf("No\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /queue/c/enq deq in queue using linked list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define N 5 4 | int num; 5 | //function prototypes 6 | void enq(); 7 | void deq(); 8 | 9 | struct node 10 | { 11 | int data; 12 | struct node* next; 13 | }*front=NULL,*rear=NULL,*newnode; 14 | 15 | //enqueue function 16 | void enq() 17 | { 18 | newnode=(struct node *)(malloc)(sizeof(struct node)); 19 | 20 | int x; 21 | scanf("%d",&x); 22 | newnode->data=x; 23 | newnode->next=NULL; 24 | if(front == 0 && rear == 0) 25 | { 26 | front=rear=newnode; 27 | } 28 | else 29 | { 30 | rear->next=newnode; 31 | rear=newnode; 32 | } 33 | } 34 | 35 | //dequeue function 36 | void deq() 37 | { 38 | if(front == rear) 39 | { 40 | printf("Underflow"); 41 | num=0; 42 | 43 | } 44 | else 45 | { 46 | struct node* temp =front; 47 | front=front->next; 48 | free(temp); 49 | } 50 | } 51 | 52 | //display function 53 | void display() 54 | { 55 | struct node*temp=front; 56 | while(temp!=NULL) 57 | { 58 | printf("%d->",temp->data); 59 | temp=temp->next; 60 | } 61 | printf("\n"); 62 | } 63 | 64 | //main function 65 | int main() { 66 | do{ 67 | scanf("%d",&num); 68 | switch(num) 69 | { 70 | case 1: 71 | 72 | enq(); 73 | break; 74 | case 2: 75 | deq(); 76 | break; 77 | case 3: 78 | display(); 79 | break; 80 | } 81 | }while(num); 82 | return 0; 83 | } -------------------------------------------------------------------------------- /queue/c/leap year check/Leap Year.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/queue/c/leap year check/Leap Year.PNG -------------------------------------------------------------------------------- /queue/c/leap year check/leap_year.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | if year % 400 == 0: 4 | leap = True 5 | elif year % 100 == 0: 6 | leap = False 7 | elif year % 4 == 0: 8 | leap = True 9 | return leap_year 10 | -------------------------------------------------------------------------------- /queue/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /queue/python/Queue.py: -------------------------------------------------------------------------------- 1 | class Queue: 2 | 3 | def __init__(self): 4 | 5 | self.queue = [] 6 | 7 | def insert(self, elem): 8 | 9 | self.queue.append(elem) 10 | 11 | def remove(self): 12 | 13 | if len(self.queue) == 0: 14 | 15 | print("Queue is Empty") 16 | 17 | else: 18 | 19 | self.queue.pop(0) 20 | 21 | def show(self): 22 | 23 | if len(self.queue) == 0: 24 | 25 | print("Queue is Empty") 26 | 27 | else: 28 | 29 | print("Queue Contains:",self.queue) 30 | 31 | queue=Queue() 32 | 33 | while(True): 34 | 35 | print("""1. Insert 36 | 37 | 2. Remove 38 | 39 | 3. Show 40 | 41 | 4. Exit""") 42 | 43 | choice=input() 44 | 45 | if(choice=='1'): 46 | 47 | queue.insert(int(input("Enter element to be insert:"))) 48 | 49 | elif(choice=='2'): 50 | 51 | queue.remove() 52 | 53 | elif(choice=='3'): 54 | 55 | queue.show() 56 | 57 | elif(choice=='4'): 58 | 59 | break 60 | 61 | else: 62 | 63 | print("Invalid Input") 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /queue/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /recursion/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /recursion/c/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C language . -------------------------------------------------------------------------------- /recursion/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /recursion/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /recursion/python/tower_of_hanoi.py: -------------------------------------------------------------------------------- 1 | def TowerOfHanoi(number_of_plates , from_rod, to_rod, aux_rod): 2 | if number_of_plates == 1: 3 | print("Move disk 1 from rod",from_rod,"to rod",to_rod) 4 | return 5 | TowerOfHanoi(number_of_plates-1, from_rod, aux_rod, to_rod) 6 | print("Move disk",number_of_plates,"from rod",from_rod,"to rod",to_rod) 7 | TowerOfHanoi(number_of_plates-1, aux_rod, to_rod, from_rod) 8 | 9 | # Driver code 10 | n = 4 11 | TowerOfHanoi(n, 'A', 'C', 'B') 12 | # A, C, B are the name of rods 13 | -------------------------------------------------------------------------------- /repository_poster.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/repository_poster.gif -------------------------------------------------------------------------------- /searching/c++/linear-search.cpp: -------------------------------------------------------------------------------- 1 | //[PROG] -> LINEAR SEARCH USING C++ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | // Creatig a dynamic array 9 | int n,*arr,key,i; 10 | 11 | //Entering the size of array 12 | cout<<"Enter the size of array: "; 13 | cin>>n; 14 | arr = new int [n]; 15 | 16 | //Inserting the values in the array 17 | cout<<"Enter the values in the array: "; 18 | for(int i=0;i>*(arr+i); 20 | } 21 | 22 | //Entering the search value 23 | cout<<"Enter the value to seacrh: "; 24 | cin>>key; 25 | 26 | //Traversing the array to search the value which was entered by the user 27 | for(i=0;i 3 | int isprime(int n){ 4 | if(n <=1)return 0; 5 | if(n==2 || n==3)return 1; 6 | if(n%2==0 || n%3==0)return 0; 7 | for(int i=5;i*i<=n;i=i+6){ 8 | if(n%i==0 || n%(i+2)==0) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | int i,n; 16 | printf("Please enter a number to check if it is prime or not:"); 17 | scanf("%d",&n); 18 | if(isprime(n)){ 19 | printf("Yes\n"); 20 | } 21 | else{ 22 | printf("No\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /searching/c/binary search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void binary_search(int a[],int size,int item) 4 | { 5 | int l=0 ,u= size-1,m; 6 | while (l <=u) 7 | { 8 | m= (l + u)/2; 9 | if (a[m] == item) 10 | { 11 | printf("search successful at index %d",m); 12 | return; 13 | } 14 | else if(a[m] > item ) 15 | { 16 | u = m - 1; 17 | 18 | } 19 | else 20 | { 21 | l = m + 1; 22 | 23 | } 24 | } 25 | } 26 | 27 | int main() 28 | { 29 | // sorted array 30 | int A[10]={2,9,12,15,26,35,44,50,66,78}; 31 | binary_search(A,10,44); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /searching/c/leap year check/Leap Year.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/searching/c/leap year check/Leap Year.PNG -------------------------------------------------------------------------------- /searching/c/leap year check/leap_year.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | if year % 400 == 0: 4 | leap = True 5 | elif year % 100 == 0: 6 | leap = False 7 | elif year % 4 == 0: 8 | leap = True 9 | return leap_year 10 | -------------------------------------------------------------------------------- /searching/c/linearsearch in c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | 5 | { 6 | 7 | int n,key,i; 8 | 9 | int A[n]; 10 | 11 | printf("Enter the number of elements of array\n"); 12 | 13 | scanf("%d",&n); 14 | 15 | printf("Enter the array elements\n"); 16 | 17 | for (i=0;i 3 | int binary_search(int a[],int item,int u,int l) 4 | { 5 | int m =(u+l)/2; 6 | if (a[m] == item) 7 | { 8 | return m; 9 | } 10 | else if (a[m] > item) 11 | { 12 | binary_search(a,item,m-1,0); 13 | } 14 | else if (a[m] < item) 15 | { 16 | binary_search(a,item,u,m+1); 17 | } 18 | } 19 | int main() 20 | { 21 | //sorted std::array 22 | int arr[10]={1,2,3,4,5,6,7,8,9,10}; 23 | int a = binary_search(arr,4,9,0); 24 | printf("%d",a); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /searching/java/LinearSearch.java: -------------------------------------------------------------------------------- 1 | class LinearSearch 2 | { 3 | public static int search(int arr[], int x) 4 | { 5 | int n = arr.length; 6 | for (int i = 0; i < n; i++) 7 | { 8 | if (arr[i] == x) 9 | return i; 10 | } 11 | return -1; 12 | } 13 | 14 | 15 | public static void main(String args[]) 16 | { 17 | int arr[] = { 2, 3, 4, 10, 40 }; 18 | int x = 10; 19 | 20 | 21 | int result = search(arr, x); 22 | if (result == -1) 23 | System.out.print( 24 | "Element is not present in array"); 25 | else 26 | System.out.print("Element is present at index " 27 | + result); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /searching/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /searching/python/binary-search.py: -------------------------------------------------------------------------------- 1 | def search(arr,number): 2 | 3 | arr.sort() 4 | 5 | low,high=0,len(arr)-1 6 | 7 | while(low<=high): 8 | 9 | mid=low+high//2 10 | 11 | if(arr[mid]==number): 12 | 13 | return mid 14 | 15 | elif(arr[mid]>number): 16 | 17 | high=mid-1 18 | 19 | else: 20 | 21 | low=mid+1 22 | 23 | else: 24 | 25 | return -1 26 | 27 | 28 | 29 | arr = list(map(int,input("Enter Numbers: ").split())) 30 | 31 | elem = int(input("Enter Number to be searched: ")) 32 | 33 | index=search(arr,elem) 34 | 35 | if(index == -1): 36 | 37 | print("Element not found") 38 | 39 | else: 40 | 41 | print("Element found") 42 | 43 | 44 | -------------------------------------------------------------------------------- /searching/python/linear-search.py: -------------------------------------------------------------------------------- 1 | def search(arr,elem): 2 | for i in arr: 3 | if i==elem: 4 | print("Element found at index:",arr.index(elem)) 5 | else: 6 | print("Element not found") 7 | 8 | 9 | arr = list(map(int,input("Enter Numbers: ").split())) 10 | elem = int(input("Enter Number to be searched: ")) 11 | search(arr,elem) 12 | 13 | -------------------------------------------------------------------------------- /searching/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /sorting/c++/Wave Sort.cpp: -------------------------------------------------------------------------------- 1 | // It sorts like a wave 2 | // if 1st element is higher, than next element is lower, then higher, then lower 3 | // and so on .... vice-versa as well 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | int n; 13 | cout << "Enter the number of elements: "; 14 | cin >> n; 15 | int a[n];// {1,3,4,2,7,8}; 16 | // sort the array in wave form 17 | 18 | for(int i = 0; i < n; i++) 19 | { 20 | cin >> a[i]; 21 | } 22 | 23 | for(int i = 0; i < n; i+=2) 24 | { 25 | 26 | if(i!=0 && a[i] < a[i-1]) 27 | { 28 | 29 | swap(a[i], a[i-1]); 30 | } 31 | 32 | 33 | if(i!= n-1 && a[i] < a[i+1]) 34 | { 35 | 36 | swap(a[i], a[i+1]); 37 | } 38 | 39 | 40 | } 41 | 42 | 43 | 44 | for (int i = 0; i < n; i++) 45 | { 46 | 47 | cout << a[i] << " "; 48 | } 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /sorting/c++/bubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cout<<"Enter number of element: "; 7 | cin>>n; 8 | int arr[n],i,j; 9 | cout<<"Enter array values:\n"; 10 | //taking the array value 11 | //from user 12 | for(i=0;i>arr[i]; 15 | } 16 | //Now we will sort the array 17 | for(i=0;iarr[j+1]) 24 | { 25 | //temp will temporarily store 26 | //the value of arr[j] 27 | //then we will swap the values 28 | int temp=arr[j]; 29 | arr[j]=arr[j+1]; 30 | arr[j+1]=temp; 31 | } 32 | } 33 | } 34 | cout<<"After Bubble sort the array is:\n"; 35 | for(i=0;i 2 | #include 3 | 4 | using namespace std; 5 | 6 | void merge(int a[],int lb,int mid,int ub){ 7 | int i = lb,j = mid+1,k = lb; 8 | int b[ub - lb + 1]; 9 | while(i <= mid && j <=ub){ 10 | if(a[i] <= a[j]){ 11 | b[k] = a[i]; 12 | i++; 13 | k++; 14 | } 15 | else{ 16 | b[k] = a[j]; 17 | j++; 18 | k++; 19 | } 20 | } 21 | if(i > mid){ 22 | while(j <= ub){ 23 | b[k] = a[j]; 24 | j++; 25 | k++; 26 | } 27 | } 28 | else{ 29 | while(i <= mid){ 30 | b[k] = a[i]; 31 | i++; 32 | k++; 33 | } 34 | } 35 | for(int i = lb; i <= ub;i++){ 36 | a[i]=b[i]; 37 | } 38 | } 39 | 40 | void mergesort(int a[],int lb,int ub){ 41 | if(lb < ub){ 42 | int mid = (lb + ub)/2; 43 | mergesort(a,lb,mid); 44 | mergesort(a,mid+1,ub); 45 | merge(a,lb,mid,ub); 46 | } 47 | } 48 | 49 | int main() 50 | { 51 | int n; 52 | cin >> n; 53 | int a[n]; 54 | for(int i = 0; i < n;i++){ 55 | cin >> a[i]; 56 | } 57 | mergesort(a,0,9); 58 | for(int i = 0;i < n;i++){ 59 | cout << a[i] << " "; 60 | } 61 | return 0; 62 | } 63 | 64 | -------------------------------------------------------------------------------- /sorting/c++/quick_sort.cpp: -------------------------------------------------------------------------------- 1 | //quick sort as first element as pivot 2 | #include 3 | using namespace std; 4 | int partitionLowPivot(int a[],int low,int high){ 5 | 6 | int pivot=a[low]; 7 | int left=low; 8 | int right=high+1; 9 | 10 | while(leftpivot); 20 | 21 | 22 | if(left>n; 53 | int a[n]; 54 | cout<<"Enter the elements of Array"<>a[i]; 57 | 58 | quickSort(a,0,n-1); 59 | cout<<"Array after Sorting"< 2 | #include 3 | 4 | using namespace std; 5 | 6 | void swap(int *a,int *b){ 7 | int temp = *a; 8 | *a = *b; 9 | *b = temp; 10 | } 11 | 12 | int partition(int a[],int lb,int ub){ 13 | int pivot = a[lb]; 14 | int start = lb; 15 | int end = ub; 16 | 17 | while(start < end){ 18 | while(a[start] <= pivot){ 19 | start++; 20 | } 21 | while(a[end] > pivot){ 22 | end--; 23 | } 24 | if(start < end){ 25 | swap(a[start],a[end]); 26 | } 27 | } 28 | swap(a[lb],a[end]); 29 | return end; 30 | } 31 | 32 | //recursive function 33 | void quicksort(int a[],int lb,int ub){ 34 | if(lb < ub){ 35 | int loc = partition(a,lb,ub); 36 | quicksort(a,lb,loc-1); 37 | quicksort(a,loc+1,ub); 38 | } 39 | } 40 | 41 | int main(){ 42 | int n; 43 | cin >> n; 44 | int a[n]; 45 | for(int i = 0;i < n;i++){ 46 | cin >> a[i]; 47 | } 48 | quicksort(a,0,n-1); 49 | for(int i = 0;i < n ;i++){ 50 | cout << a[i] << " "; 51 | } 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /sorting/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /sorting/c++/selectionsort.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void 7 | selectionsort(int a[], int n) 8 | { 9 | for (int i = 0; i < n - 1; ++i) { 10 | int min = i; 11 | for (int j = i + 1; j < n; ++j) { 12 | if (a[j] < a[min]) 13 | min = j; 14 | } 15 | 16 | swap(a[min], a[i]); 17 | } 18 | } 19 | 20 | int 21 | min_i(int a[], int i, int j) 22 | { 23 | if (i == j) 24 | return i; 25 | 26 | int k = min_i(a, i + 1, j); 27 | return (a[i] < a[k]) ? i : k; 28 | } 29 | 30 | void 31 | selectionsort_rec(int* a, int b, int e = 0) 32 | { 33 | /* Recursive version */ 34 | if (b == e) 35 | return; 36 | 37 | int key = min_i(a, e, b - 1); 38 | if (key != e) 39 | swap(a[key], a[e]); 40 | 41 | selectionsort_rec(a, b, e + 1); 42 | } 43 | 44 | int 45 | main() 46 | { 47 | int arr[] = { 3, 450, 12, 4, -1, 0, 24, 95, 123, 0 }; 48 | for (int i = 0; i < 10; ++i) { 49 | cout << *(arr + i) << ' '; 50 | } 51 | cout << endl; 52 | selectionsort_rec(arr, 10); 53 | for (int i = 0; i < 10; ++i) { 54 | cout << *(arr + i) << ' '; 55 | } 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /sorting/c++/turboSortArray.cpp: -------------------------------------------------------------------------------- 1 | // Turbo Sort Array Program In C++ 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n, item; 7 | cin>>n; 8 | // declaring a vector of integer type 9 | vector vect; 10 | 11 | // using for loop elements elements to the vector using its push_back method 12 | for(int i=0; i>item; 14 | vect.push_back(item); 15 | } 16 | 17 | // calling the sort() method that in-place sorts the vector elements and doesn't return anything 18 | sort(vect.begin(), vect.end()); 19 | 20 | // displaying the sorted vector elements 21 | for(auto i:vect){ 22 | cout< 3 | int isprime(int n){ 4 | if(n <=1)return 0; 5 | if(n==2 || n==3)return 1; 6 | if(n%2==0 || n%3==0)return 0; 7 | for(int i=5;i*i<=n;i=i+6){ 8 | if(n%i==0 || n%(i+2)==0) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | int i,n; 16 | printf("Please enter a number to check if it is prime or not:"); 17 | scanf("%d",&n); 18 | if(isprime(n)){ 19 | printf("Yes\n"); 20 | } 21 | else{ 22 | printf("No\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /sorting/c/leap year check/Leap Year.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/sorting/c/leap year check/Leap Year.PNG -------------------------------------------------------------------------------- /sorting/c/leap year check/leap_year.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | if year % 400 == 0: 4 | leap = True 5 | elif year % 100 == 0: 6 | leap = False 7 | elif year % 4 == 0: 8 | leap = True 9 | return leap_year 10 | -------------------------------------------------------------------------------- /sorting/c/selection sort without function.c: -------------------------------------------------------------------------------- 1 | selection sort 2 | 3 | #include 4 | int main() 5 | { 6 | int min,i,j,loc,temp,n = 10,a[10]={9,8,7,6,5,4,3,2,1,13}; 7 | for (i = 0;i < n-1;i++) 8 | { 9 | loc = i; 10 | min = a[i]; 11 | for (j = i + 1;j < n;j++) 12 | { 13 | if (a[j] < min) 14 | { 15 | loc = j; 16 | min = a[j]; 17 | } 18 | } 19 | 20 | if (loc != i) 21 | { 22 | temp = a[i]; 23 | a[i] = a[loc]; 24 | a[loc] = temp; 25 | } 26 | } 27 | for (i = 0; i [] bucket = new ArrayList[n]; 8 | 9 | for (int i = 0; i < n; i++) // Create empty buckets 10 | bucket[i] = new ArrayList(); 11 | 12 | for (int i = 0; i < n; i++) { // Add elements into the buckets 13 | int bucketIndex = (int) arr[i] * n; 14 | bucket[bucketIndex].add(arr[i]); 15 | } 16 | 17 | for (int i = 0; i < n; i++) { 18 | Collections.sort((bucket[i])); // Sort the elements of each bucket 19 | } 20 | 21 | int index = 0; 22 | for (int i = 0; i < n; i++) { 23 | for (int j = 0, size = bucket[i].size(); j < size; j++) { 24 | arr[index++] = bucket[i].get(j); // Getting the sorted array 25 | } 26 | } 27 | } 28 | 29 | public static void main(String[] args) { 30 | BucketSort object = new BucketSort(); 31 | float[] arr = { (float) 0.82, (float) 0.62, (float) 0.15, (float) 0.22, (float) 0.37, (float) 0.92, 32 | (float) 0.53 }; 33 | object.bucketSort(arr, 7); 34 | 35 | for (float i : arr) 36 | System.out.print(i + " "); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /sorting/java/InsertionSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/sorting/java/InsertionSort.class -------------------------------------------------------------------------------- /sorting/java/bubbleSort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class BubbleSort 3 | { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | int n = scanner.nextInt(); 7 | int[] arr = new int[n]; 8 | //input from user 9 | for(int i = 0; i < n; i++){ 10 | arr[i] = scanner.nextInt(); 11 | } 12 | //bubble algorithm 13 | for(int i = 0; i < n; i++){ 14 | for(int j = 0; j < n-i-1; j++){ 15 | if(arr[j] > arr[j+1]){ 16 | int temp = arr[j]; 17 | arr[j] = arr[j+1]; 18 | arr[j+1] = temp; 19 | } 20 | } 21 | } 22 | //output 23 | for(int i = 0; i < n; i++){ 24 | System.out.print(arr[i]+" "); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /sorting/java/countSort.java: -------------------------------------------------------------------------------- 1 | 2 | class CountingSort { 3 | void sort(char arr[]) 4 | { 5 | int n = arr.length; 6 | 7 | char output[] = new char[n]; 8 | 9 | 10 | int count[] = new int[256]; 11 | for (int i = 0; i < 256; ++i) 12 | count[i] = 0; 13 | 14 | 15 | for (int i = 0; i < n; ++i) 16 | ++count[arr[i]]; 17 | 18 | 19 | for (int i = 1; i <= 255; ++i) 20 | count[i] += count[i - 1]; 21 | 22 | 23 | for (int i = n - 1; i >= 0; i--) { 24 | output[count[arr[i]] - 1] = arr[i]; 25 | --count[arr[i]]; 26 | } 27 | 28 | for (int i = 0; i < n; ++i) 29 | arr[i] = output[i]; 30 | } 31 | 32 | 33 | public static void main(String args[]) 34 | { 35 | CountingSort ob = new CountingSort(); 36 | char arr[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o', 37 | 'r', 'g', 'e', 'e', 'k', 's' }; 38 | 39 | ob.sort(arr); 40 | 41 | System.out.print("Sorted character array is "); 42 | for (int i = 0; i < arr.length; ++i) 43 | System.out.print(arr[i]); 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /sorting/java/insertionSort.java: -------------------------------------------------------------------------------- 1 | import java.lang.*; 2 | 3 | public class InsertionSort { 4 | 5 | public static void main(String[] args) { 6 | 7 | int[] arr={10,-6,-9,110,68,-101,10,1}; 8 | 9 | for(int firstUnsortedIndex=1;firstUnsortedIndex0&&arr[i-1]>newElement;i--){ 15 | 16 | arr[i]=arr[i-1]; 17 | } 18 | 19 | arr[i]=newElement; 20 | 21 | } 22 | 23 | for (int j : arr) 24 | System.out.println(j); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /sorting/java/quick sort/QuickSort.java: -------------------------------------------------------------------------------- 1 | // TODO: Sort an Array using Quick Sort Technique 2 | package array.sorting; 3 | 4 | import java.util.Arrays; 5 | 6 | public class Quick_Sort { 7 | 8 | public static void main(String[] args) { 9 | int arr[] = { 4, 21, 1, 9, -12, 5, 32, 2, 6 }; 10 | System.out.println("Array before Sorting: " + Arrays.toString(arr)); 11 | 12 | int n = arr.length; 13 | quickSort(arr, 0, n - 1); 14 | System.out.println("Array after Sorting: " + Arrays.toString(arr)); 15 | } 16 | 17 | public static void quickSort(int arr[], int l, int h) { 18 | if (l < h) { 19 | int pivotIndex = partition(arr, l, h); 20 | quickSort(arr, l, pivotIndex - 1); 21 | quickSort(arr, pivotIndex + 1, h); 22 | } 23 | } 24 | 25 | private static int partition(int arr[], int l, int h) { 26 | int pivot = arr[l]; 27 | int i = l; 28 | int j = h; 29 | while (i < j) { 30 | while (i <= h && arr[i] <= pivot) 31 | i++; 32 | while (j >= l && arr[j] > pivot) 33 | j--; 34 | if (i < j) 35 | swap(arr, i, j); 36 | } 37 | swap(arr, j, l); 38 | return j; 39 | } 40 | 41 | static void swap(int arr[], int i, int j) { 42 | int temp = arr[i]; 43 | arr[i] = arr[j]; 44 | arr[j] = temp; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /sorting/java/quick sort/hoaresPartitioning-quick-sort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class QuickSort 3 | { 4 | public static int partition(int[] arr, int low, int high){ 5 | int pivot = arr[low]; 6 | int i = low-1, j = high+1; 7 | while(true){ 8 | do{ 9 | i++; 10 | }while(arr[i] < pivot); 11 | do{ 12 | j--; 13 | }while(arr[j] > pivot); 14 | if(i >= j){ 15 | return j; 16 | } 17 | else{ 18 | int temp = arr[i]; 19 | arr[i] = arr[j]; 20 | arr[j] = temp; 21 | } 22 | } 23 | } 24 | public static void quickSort(int[] arr, int low, int high){ 25 | if(low < high){ 26 | int q = partition(arr, low, high); 27 | quickSort(arr, low, q); 28 | quickSort(arr, q+1, high); 29 | } 30 | } 31 | public static void main(String[] args) { 32 | Scanner scanner = new Scanner(System.in); 33 | int n = scanner.nextInt(); 34 | int[] arr = new int[n]; 35 | //input from user 36 | for(int i = 0; i < n; i++){ 37 | arr[i] = scanner.nextInt(); 38 | } 39 | //quick sort 40 | quickSort(arr, 0, arr.length-1); 41 | //output 42 | for(int i = 0; i < n; i++){ 43 | System.out.print(arr[i]+" "); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /sorting/java/selectionSort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class SelectionSort 3 | { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | int n = scanner.nextInt(); 7 | int[] arr = new int[n]; 8 | //input from user 9 | for(int i = 0; i < n; i++){ 10 | arr[i] = scanner.nextInt(); 11 | } 12 | //selection algorithm 13 | for(int i = 1; i < n; i++){ 14 | int j = i-1; 15 | int temp = arr[i]; 16 | while(j >= 0 && arr[j] > temp){ 17 | arr[j+1] = arr[j]; 18 | j--; 19 | } 20 | arr[j+1] = temp; 21 | } 22 | //output 23 | for(int i = 0; i < n; i++){ 24 | System.out.print(arr[i]+" "); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /sorting/python/bubble-sort.py: -------------------------------------------------------------------------------- 1 | def sort(arr): 2 | 3 | for i in range(len(arr)-1): 4 | 5 | for j in range(len(arr)-1-i): 6 | 7 | if( arr[j] > arr[j+1] ): 8 | 9 | arr[j],arr[j+1] = arr[j+1],arr[j] 10 | 11 | return arr 12 | 13 | arr = list(map(int,input("Enter Numbers: ").split())) 14 | 15 | print(sort(arr)) 16 | -------------------------------------------------------------------------------- /sorting/python/bucket-sort.py: -------------------------------------------------------------------------------- 1 | # bucket sort 2 | # When you can guarantee that your input is approximately uniformly distributed. 3 | # dividing the elements into several groups called buckets. 4 | def bucketSort(array): 5 | bucket = [] 6 | 7 | # Create empty buckets 8 | for i in range(len(array)): 9 | bucket.append([]) 10 | 11 | # Insert elements into their respective buckets 12 | for j in array: 13 | index_b = int(10 * j) 14 | bucket[index_b].append(j) 15 | 16 | # Sort the elements of each bucket 17 | for i in range(len(array)): 18 | bucket[i] = sorted(bucket[i]) 19 | 20 | # Get the sorted elements 21 | k = 0 22 | for i in range(len(array)): 23 | for j in range(len(bucket[i])): 24 | array[k] = bucket[i][j] 25 | k += 1 26 | return array 27 | 28 | 29 | array = [.42, .32, .33, .52, .37, .47, .51] 30 | print("Sorted Array in descending order is") 31 | print(bucketSort(array)) -------------------------------------------------------------------------------- /sorting/python/insertion-sort.py: -------------------------------------------------------------------------------- 1 | def sort(arr): 2 | for i in range(1, len(arr)): 3 | key = arr[i] 4 | j = i-1 5 | while j >=0 and key < arr[j] : 6 | arr[j+1] = arr[j] 7 | j -= 1 8 | arr[j+1] = key 9 | return arr 10 | arr = list(map(int,input("Enter Numbers: ").split())) 11 | print(sort(arr)) 12 | -------------------------------------------------------------------------------- /sorting/python/merge-sort.py: -------------------------------------------------------------------------------- 1 | def merge(left, right): 2 | if len(left) == 0: 3 | return right 4 | 5 | if len(right) == 0: 6 | return left 7 | 8 | result = [] 9 | index_left = index_right = 0 10 | 11 | while len(result) < len(left) + len(right): 12 | if left[index_left] <= right[index_right]: 13 | result.append(left[index_left]) 14 | index_left += 1 15 | else: 16 | result.append(right[index_right]) 17 | index_right += 1 18 | 19 | if index_right == len(right): 20 | result += left[index_left:] 21 | break 22 | 23 | if index_left == len(left): 24 | result += right[index_right:] 25 | break 26 | 27 | return result 28 | 29 | def merge_sort(array): 30 | if len(array) < 2: 31 | return array 32 | 33 | midpoint = len(array) // 2 34 | 35 | return merge( 36 | left=merge_sort(array[:midpoint]), 37 | right=merge_sort(array[midpoint:])) 38 | 39 | arr=list(map(int,input().split())) 40 | print(merge_sort(arr)) 41 | -------------------------------------------------------------------------------- /sorting/python/quick-sort.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | 3 | def quicksort(array): 4 | if len(array) < 2: 5 | return array 6 | 7 | low, same, high = [], [], [] 8 | pivot = array[randint(0, len(array) - 1)] 9 | 10 | for item in array: 11 | if item < pivot: 12 | low.append(item) 13 | elif item == pivot: 14 | same.append(item) 15 | elif item > pivot: 16 | high.append(item) 17 | return quicksort(low) + same + quicksort(high) 18 | 19 | arr = list(map(int,input().split())) 20 | print(quicksort(arr)) 21 | -------------------------------------------------------------------------------- /sorting/python/selection-sort.py: -------------------------------------------------------------------------------- 1 | def sort(arr): 2 | for i in range(len(arr)): 3 | min_idx = i 4 | for j in range(i+1, len(arr)): 5 | if arr[min_idx] > arr[j]: 6 | min_idx = j 7 | arr[i], arr[min_idx] = arr[min_idx], arr[i] 8 | return arr 9 | arr = list(map(int,input("Enter Numbers: ").split())) 10 | print(sort(arr)) 11 | -------------------------------------------------------------------------------- /stacks/c++/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/stacks/c++/a.exe -------------------------------------------------------------------------------- /stacks/c++/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in C++ language . -------------------------------------------------------------------------------- /stacks/c/Prime Numbers/Prime num Algo 1.c: -------------------------------------------------------------------------------- 1 | // prime number algorithm 1 2 | #include 3 | int isprime(int n){ 4 | if(n <=1)return 0; 5 | if(n==2 || n==3)return 1; 6 | if(n%2==0 || n%3==0)return 0; 7 | for(int i=5;i*i<=n;i=i+6){ 8 | if(n%i==0 || n%(i+2)==0) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | int i,n; 16 | printf("Please enter a number to check if it is prime or not:"); 17 | scanf("%d",&n); 18 | if(isprime(n)){ 19 | printf("Yes\n"); 20 | } 21 | else{ 22 | printf("No\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /stacks/c/leap year check/Leap Year.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/stacks/c/leap year check/Leap Year.PNG -------------------------------------------------------------------------------- /stacks/c/leap year check/leap_year.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | if year % 400 == 0: 4 | leap = True 5 | elif year % 100 == 0: 6 | leap = False 7 | elif year % 4 == 0: 8 | leap = True 9 | return leap_year 10 | -------------------------------------------------------------------------------- /stacks/c/leapyear.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a; 5 | printf("enter a year\n"); 6 | scanf("%d",&a); 7 | 8 | if (a%4==0 || a%100==0 || a%400==0) 9 | 10 | printf("%d is a leap year",a); 11 | else 12 | printf("%d is not leap year",a); 13 | return 0; 14 | 15 | 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /stacks/java/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in Java language . -------------------------------------------------------------------------------- /stacks/python/Stack.py: -------------------------------------------------------------------------------- 1 | def create_stack(): 2 | 3 | stack=[] 4 | 5 | return stack 6 | 7 | def isEmpty(stack): 8 | 9 | return len(stack) == 0 10 | 11 | 12 | 13 | def push_op(stack,elem): 14 | 15 | stack.append(elem) 16 | 17 | 18 | 19 | def pop_op(stack): 20 | 21 | if(isEmpty(stack)): 22 | 23 | print("Stack is Empty") 24 | 25 | else: 26 | 27 | stack.pop() 28 | 29 | def show(stack): 30 | 31 | if(isEmpty(stack)): 32 | 33 | print("Stack is Empty") 34 | 35 | else: 36 | 37 | print("Stack Contains:",stack) 38 | 39 | stack=create_stack() 40 | 41 | while(True): 42 | 43 | print("""1. Push 44 | 45 | 2. Pop 46 | 47 | 3. Show 48 | 49 | 4. Exit""") 50 | 51 | choice=input() 52 | 53 | if(choice=='1'): 54 | 55 | push_op(stack,int(input("Enter element to be push:"))) 56 | 57 | elif(choice=='2'): 58 | 59 | pop_op(stack) 60 | 61 | elif(choice=='3'): 62 | 63 | show(stack) 64 | 65 | elif(choice=='4'): 66 | 67 | break 68 | 69 | else: 70 | 71 | print("Invalid Input") 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /stacks/python/readme.md: -------------------------------------------------------------------------------- 1 | #### This Folder includes algorithms in python language . -------------------------------------------------------------------------------- /strings/c++/Rabin _Karp_Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define d 10 5 | void rabinkarp(char pattern[],char text[],int q) 6 | { 7 | int m=strlen(pattern); 8 | int n=strlen(text); 9 | int i,j; 10 | int p=0; 11 | int t=0; 12 | int h=1; 13 | for(i=0;i 3 | int isprime(int n){ 4 | if(n <=1)return 0; 5 | if(n==2 || n==3)return 1; 6 | if(n%2==0 || n%3==0)return 0; 7 | for(int i=5;i*i<=n;i=i+6){ 8 | if(n%i==0 || n%(i+2)==0) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | int i,n; 16 | printf("Please enter a number to check if it is prime or not:"); 17 | scanf("%d",&n); 18 | if(isprime(n)){ 19 | printf("Yes\n"); 20 | } 21 | else{ 22 | printf("No\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /strings/c/leap year check/Leap Year.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjay-poddar/Data-Structures-And-Algorithms/50d53ca92706c2acef22eea80ef7ade20a1d62fa/strings/c/leap year check/Leap Year.PNG -------------------------------------------------------------------------------- /strings/c/leap year check/leap_year.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | if year % 400 == 0: 4 | leap = True 5 | elif year % 100 == 0: 6 | leap = False 7 | elif year % 4 == 0: 8 | leap = True 9 | return leap_year 10 | -------------------------------------------------------------------------------- /strings/c/reverse array.c: -------------------------------------------------------------------------------- 1 | #include 2 | void reverse(int n,int a[]){ 3 | int i; 4 | for(i = 0;i < n/2;i++){ 5 | int temp = a[i]; 6 | a[i]=a[n-i-1]; 7 | a[n-i-1]=temp; 8 | } 9 | } 10 | int main() 11 | { 12 | int i,n; 13 | scanf("%d",&n); 14 | int a[n]; 15 | for(i = 0;i < n;i++){ 16 | scanf("%d",&a[i]); 17 | } 18 | reverse(n,a); 19 | for(i = 0;i < n;i++){ 20 | printf("%d ",a[i]); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /strings/c/string reverse.c: -------------------------------------------------------------------------------- 1 | string reverseWord(string str){ 2 | 3 | //Your code here 4 | for(int i=0;i