├── _config.yml
├── 01. DataStructures
├── 03. Queue
│ └── Basic Operations
│ │ ├── 08. Linked Queue
│ │ └── Images
│ │ │ ├── README
│ │ │ ├── Screenshot_1.png
│ │ │ └── Screenshot_2.png
│ │ ├── 01. Queue using Array
│ │ └── pseudocode.png
│ │ ├── 04. Circular Queue using Array
│ │ └── README.md
│ │ ├── 05. Circular Queue using Linked List
│ │ └── README.md
│ │ ├── 02. Queue using Linked List
│ │ └── README.md
│ │ ├── 06. Dequeue using circular array
│ │ └── README.md
│ │ ├── 03. Queue using Stacks
│ │ └── README.md
│ │ ├── 07. Priority Queue
│ │ ├── KLargestElement.java
│ │ └── README.md
│ │ └── Queue Reversal
│ │ ├── Recursion implementation.cpp
│ │ └── Stack Implementation.cpp
├── 04. Tree
│ ├── 01. Introduction to Binary Tree
│ │ └── Tree Operations
│ │ │ ├── Readme.md
│ │ │ └── Height of Tree
│ │ │ └── Readme.md
│ ├── Problems
│ │ ├── Path Sum #112
│ │ │ ├── PathSum.md
│ │ │ └── Soultion.java
│ │ ├── Recover Binary Search Tree #99
│ │ │ └── RecoverBinarySearchTree.md
│ │ ├── MaximumSumBST
│ │ │ └── maximumSumBST.md
│ │ ├── All Nodes At Distance K #863
│ │ │ └── AllNodesAtDistanceK.md
│ │ ├── Lowest Common Ancestor #236
│ │ │ └── LowestCommonAncestor.md
│ │ └── Subtree of Another Tree
│ │ │ └── README.md
│ ├── 05. Heap
│ │ └── Leetcode Questions
│ │ │ ├── 03. Top K Frequent Elements #347
│ │ │ ├── Top K Frequent Elements.md
│ │ │ └── Solution.java
│ │ │ ├── 04. Maximum Frequency Stack #895
│ │ │ └── Maximum Frequency Stack.md
│ │ │ ├── 02. K Closest Points to Origin #973
│ │ │ ├── K Closest Points to Origin.md
│ │ │ └── Solution.java
│ │ │ └── 01. Kth Largest Element in an Array #215
│ │ │ └── Kth Largest Element in an Array.md
│ ├── Leetcode Questions
│ │ ├── Kth Smallest Element in a BST
│ │ │ └── README.md
│ │ └── Maximum depth of a binary tree.java
│ └── 02. Binary Search Tree
│ │ └── Binary_search_tree.py
├── 01. Linked List
│ ├── 1. Singly-Linked-List
│ │ ├── Problems
│ │ │ ├── Add Two Numbers
│ │ │ │ └── README.md
│ │ │ ├── Merge Two Sorted Lists
│ │ │ │ └── README.md
│ │ │ ├── Remove Nth Node From End of List
│ │ │ │ └── README.md
│ │ │ ├── Mid-of-single-linked-list
│ │ │ │ └── output.png
│ │ │ └── Reverse Singly LinkedList
│ │ │ │ └── README.md
│ │ ├── InterviewBit Questions
│ │ │ ├── 11. Rotate LL by K place
│ │ │ │ ├── README.md
│ │ │ │ └── solution_11.py
│ │ │ ├── 03. Kth Node From Middle
│ │ │ │ ├── README.md
│ │ │ │ └── solution 3.java
│ │ │ ├── 12. Reverse LL-ii
│ │ │ │ ├── README.md
│ │ │ │ └── solution_12.py
│ │ │ ├── 10. Reverse LL in K group GQ
│ │ │ │ ├── Iterative Solution Self.md
│ │ │ │ ├── Recursive Solution Apna College.md
│ │ │ │ └── solution 10-AC.java
│ │ │ ├── 01. ReverseLL
│ │ │ │ ├── README.md
│ │ │ │ ├── solution.js
│ │ │ │ ├── solution_in_cpp.cpp
│ │ │ │ └── solution 1.java
│ │ │ ├── 04. Palindrome List GQ
│ │ │ │ ├── README.md
│ │ │ │ └── solution.js
│ │ │ ├── 09. Add two no. as List
│ │ │ │ ├── README.md
│ │ │ │ └── solution_9.py
│ │ │ ├── 05. Merge Two Sorted List GQ
│ │ │ │ ├── Striver Solution.md
│ │ │ │ ├── Apna College Solution (Good Explaination).md
│ │ │ │ └── solution.js
│ │ │ ├── 02. IntersectionOfLL
│ │ │ │ └── README.md
│ │ │ ├── 08. Remove nth Node from List end GQ
│ │ │ │ └── README.md
│ │ │ ├── 06. Remove Duplicates From Sorted List
│ │ │ │ ├── README.md
│ │ │ │ └── solution 6.java
│ │ │ ├── 07. Remove Duplicates From Sorted List- II GQ
│ │ │ │ ├── README.md
│ │ │ │ └── solution.js
│ │ │ ├── 14. Palindrome List
│ │ │ │ └── README.md
│ │ │ └── 13. Sort Binary Linked List
│ │ │ │ └── README.md
│ │ └── Basic Operations
│ │ │ └── README.md
│ ├── LeetCode Questions
│ │ ├── 02. Sort List #148
│ │ │ └── SortList.md
│ │ └── 01. Reverse Linked List #206
│ │ │ ├── ReverseLinkedList.md
│ │ │ └── Solution.java
│ ├── 3. Circular-Linked-List
│ │ └── README.md
│ └── 2. Doubly-Linked-List
│ │ ├── Basic Operations
│ │ └── README.md
│ │ └── Questions
│ │ └── 1. Two Pair Sum
│ │ └── 2 pair sum Q.pdf
├── 02. Stack
│ ├── Basic Operation
│ │ ├── 3. Stack using Queue
│ │ │ └── README.md
│ │ ├── 4. Stack using Priority Queue
│ │ │ └── README.md
│ │ ├── 2. Stack using LinkList
│ │ │ └── README.md
│ │ └── 1. Stack using Array
│ │ │ └── README.md
│ ├── LeetCode Questions
│ │ ├── 04. Baseball Game #682
│ │ │ └── Resources.md
│ │ ├── 06. Crawler Log Folder #1598
│ │ │ ├── Resources.md
│ │ │ └── Solution.java
│ │ ├── 15. Online Stock Span #901
│ │ │ ├── README.md
│ │ │ └── Solution.cpp
│ │ ├── 01. Remove Outermost Parentheses #1021
│ │ │ └── Remove Outermost Parentheses.md
│ │ ├── 03. Build an Array With Stack Operations #1441
│ │ │ ├── Resources.md
│ │ │ └── Solution.java
│ │ ├── 07. Make The String Great #1544
│ │ │ ├── Resources.md
│ │ │ ├── Solution_Vikash.java
│ │ │ └── Solution_self.java
│ │ ├── 10. Design a Stack With Increment Operation #1381
│ │ │ └── Resources.md
│ │ ├── 08. Backspace String Compare #844
│ │ │ ├── Resources.md
│ │ │ └── Solution_self.java
│ │ ├── 09. Valid Parentheses #20
│ │ │ ├── Resources.md
│ │ │ └── Solution.java
│ │ ├── 12. Score of Parentheses #856
│ │ │ ├── Resources.md
│ │ │ └── Solution.java
│ │ ├── 14. Validate Stack Sequences #946
│ │ │ ├── Resources.md
│ │ │ └── Solution.java
│ │ ├── 05. Next Greater Element I #496
│ │ │ ├── Resources.md
│ │ │ └── Solution_Optimised.java
│ │ ├── 02. Remove All Adjacent Duplicates In String #1047
│ │ │ ├── Remove All Adjacent Duplicates In String.md
│ │ │ ├── Solution_Vikash.java
│ │ │ └── Solution.java
│ │ ├── 13. Daily Temperatures #739
│ │ │ ├── Resources.md
│ │ │ └── Solution.java
│ │ ├── 18. Maximum Nesting Depth of the Parentheses #1614
│ │ │ ├── Resources.md
│ │ │ └── solution.cpp
│ │ └── 11. Minimum Add to Make Parentheses Valid #921
│ │ │ ├── Resources.md
│ │ │ └── Solution.java
│ └── Infix Prefix Postfix
│ │ ├── 01. Infix to Postfix
│ │ ├── pseudocode.png
│ │ └── README.md
│ │ ├── 02. Infix to Prefix
│ │ ├── pseudocode.png
│ │ └── README.md
│ │ ├── 03. Postfix to Infix
│ │ ├── pseudocode.png
│ │ ├── 01. Postfix to Infix In C++
│ │ │ ├── output_01.png
│ │ │ └── output_02.png
│ │ ├── README.md
│ │ └── PostfixInfix.java
│ │ ├── 04. Prefix to Infix
│ │ ├── pseudocode.png
│ │ ├── README.md
│ │ └── PrefixInfix.java
│ │ ├── 05. Postfix to Prefix
│ │ ├── pseudocode.png
│ │ ├── README.md
│ │ └── PostfixPrefix.java
│ │ └── 06. Prefix to Postfix
│ │ ├── pseudocode.png
│ │ ├── README.md
│ │ └── PrefixPostfix.java
├── 08. Graph
│ ├── Leetcode Questions
│ │ ├── 01. Rotting Oranges #994
│ │ │ └── Rotting Oranges.md
│ │ ├── 03. Is Graph Bipartite #785
│ │ │ └── Is Graph Bipartite.md
│ │ ├── 02. Minimize Malware Spread #924
│ │ │ └── Minimize Malware Spread.md
│ │ └── 04. Shortest Path In Binary Matrix #1091
│ │ │ └── Shortest Path In Binary Matrix.md
│ └── 02. DFS Traversal
│ │ └── DFS.cpp
├── 06. HashMap
│ └── README.md
├── 05. ArrayList
│ └── AddOddAndEvenNums
│ │ ├── README.md
│ │ └── Solution.java
└── 07. Set
│ └── SubarrayWith0Sum.java
├── Love Babbar DSA Sheet Solutions
├── Graph
│ └── README.md
├── Greedy
│ └── README.md
├── Heap
│ └── README.md
├── Trie
│ └── README.md
├── Matrix
│ └── README.md
├── String
│ ├── README.md
│ ├── Reverse string.java
│ └── Palindrome_String.java
├── LinkedList
│ └── README.md
├── BackTracking
│ └── README.md
├── Binary Trees
│ ├── README.md
│ ├── Top View Of Binary tree
│ │ └── readme.md
│ ├── Bottom View Of a Binary Tree
│ │ └── readme.md
│ ├── Level-order-traversal
│ │ ├── readme.md
│ │ └── solution.cpp
│ ├── Maximum Depth of a binary tree
│ │ ├── readme.md
│ │ └── solution.cpp
│ ├── height-of-a-binary-tree
│ │ ├── readme.md
│ │ └── solution.cpp
│ ├── Diameter of Binary Tree
│ │ ├── readme.md
│ │ └── solution.cpp
│ ├── reverse-level-order
│ │ ├── readme.md
│ │ └── solution.cpp
│ └── Flatten a Binary Tree
│ │ └── README.md
├── Bit Manipulation
│ ├── README.md
│ ├── Count set bits
│ │ ├── set_bits.java
│ │ └── README.md
│ ├── Count number of bits to be flipped to convert A to B
│ │ └── README.md
│ └── Find position of set bit
│ │ └── README.md
├── Stacks & Queues
│ └── README.md
├── Binary Search Trees
│ └── README.md
├── Dynamic Programming
│ ├── README.md
│ ├── Coin Change
│ │ └── Readme.md
│ ├── Edit Distance
│ │ └── README.md
│ ├── Knapsack
│ │ └── README.md
│ └── Longest Common Subsequence.py
│ │ └── README.md
├── Searching & Sorting
│ └── README.md
├── README.md
└── Arrays
│ ├── README.md
│ ├── Largest Sum Contiguous Subarray
│ ├── README.md
│ └── largestSumContiguousSubarray.java
│ ├── Next Permutation
│ └── .DS_Store
│ ├── Kadane's Algorithm
│ └── Solution.cpp
│ ├── duplicate_array
│ └── duplicates.cpp
│ └── Trapping Rain Water
│ └── trappingRainWaterLeetcodeHard.java
├── 02. Algorithms
├── 01. Arrays
│ ├── 12. 3Sum #15
│ │ └── 3Sum.md
│ ├── 13. 4Sum #18
│ │ └── 4Sum.md
│ ├── 03. 215. Kth Largest Element in an Array
│ │ ├── brute_force.java
│ │ └── Using_priorityQueue.java
│ ├── 25. Array Partition 1
│ │ └── array-partition-i.cpp
│ ├── 10. 45. Jump Game II
│ │ ├── Solution.java
│ │ └── README.md
│ ├── 08. Kadane's Algorithm
│ │ ├── 53. Maximum Subarray
│ │ │ ├── Solution.java
│ │ │ └── README.md
│ │ └── 918. Maximum Sum Circular Subarray
│ │ │ └── Solution2.java
│ ├── 11. Jumping on the Clouds
│ │ ├── Solution.java
│ │ ├── README.md
│ │ └── Solution2.java
│ ├── 04. 75. Sort Colors OR Sort012
│ │ ├── Solution.cpp
│ │ ├── brute_force.java
│ │ └── Optimal_Solution.java
│ ├── 07. Cyclically Rotate an Array
│ │ ├── Brute_Force.java
│ │ ├── cyclic_rotate.cpp
│ │ └── Optimal_Solution.java
│ ├── 09. Minimize the Heights II
│ │ ├── Solution.java
│ │ └── README.md
│ ├── 21. Rotate the 2D Array
│ │ └── Rotate the 2D array.cpp
│ ├── 05. Move all negative numbers to one side
│ │ └── Solution.java
│ ├── 01. Reverse an Array
│ │ ├── ReverseArray.cpp
│ │ └── Reverse_Array.py
│ ├── 19. Wave Array
│ │ ├── readme.md
│ │ └── wave_array.java
│ ├── 02. Find the maximum and minimum element in an array
│ │ └── find_max_min.cpp
│ ├── 24. Pick from both sides
│ │ └── Solution.cpp
│ ├── 17. Pascal's Triangle
│ │ ├── pascalTriangle.java
│ │ └── pascalTriangle.cpp
│ └── 06. Find the Union and Intersection of Two Arrays
│ │ └── Union_and_Intersection.java
├── 03. Searching
│ ├── 01. Linear Search
│ │ ├── Notes.pdf
│ │ ├── Linear_Search.py
│ │ ├── README.md
│ │ ├── Linear_Search.cpp
│ │ └── Linear_Search.java
│ ├── 02. Binary Search
│ │ ├── Notes.pdf
│ │ ├── Binary_search_(Recursive).cpp
│ │ └── Binary_Search.py
│ ├── 03. Depth First Search
│ │ └── Images
│ │ │ ├── Screenshot 1.png
│ │ │ └── Screenshot 2.png
│ └── Problems
│ │ ├── 13. 410. Split Array Largest Sum
│ │ └── Dry run.pdf
│ │ ├── 08. 852. Peak Index in a Mountain Array
│ │ ├── Solution1.java
│ │ └── Solution2.java
│ │ ├── 02. 1672. Richest Customer Wealth
│ │ └── Solution.java
│ │ ├── 01. 1295. Find Numbers with Even Number of Digits
│ │ ├── Solution.java
│ │ └── README.md
│ │ ├── 05. 744. Find Smallest Letter Greater Than Target
│ │ ├── Solution.java
│ │ └── README.md
│ │ ├── 16. 167. Two Sum II - Input array is sorted
│ │ ├── optimal_approach.java
│ │ └── brute-force.java
│ │ ├── 14. 69. Sqrt(x)
│ │ └── Solution.java
│ │ ├── 15. 278. First Bad Version
│ │ ├── Solution.java
│ │ └── README.md
│ │ ├── 04. Ceil in a Sorted Array
│ │ ├── Solution.java
│ │ └── README.md
│ │ ├── 03. Floor in a Sorted Array
│ │ ├── Solution.java
│ │ └── README.md
│ │ ├── 17. Maximum Value at a Given Index in a Bounded Array
│ │ └── README.md
│ │ ├── 07. Find position of an element in an infinite sorted array
│ │ └── README.md
│ │ ├── 11. 33. Search in Rotated Sorted Array
│ │ └── Solution.java
│ │ └── 09. 162. Find Peak Element
│ │ └── Solution.java
├── 05. Graphs
│ ├── 01. Depth First Search
│ │ └── output.png
│ └── BreathFirstSearch
│ │ └── Screenshot 2021-10-03 235854.png
├── 02. Sorting
│ ├── 08. Heap Sort
│ │ └── README.md
│ ├── 02. InsertionSort
│ │ ├── README.md
│ │ ├── Insertion_Sort.java
│ │ ├── insertionSort.py
│ │ ├── Insertion_Sort.c
│ │ └── Insertion_Sort.cpp
│ ├── 10. TimSort
│ │ └── README.md
│ ├── 03. Selection Sort
│ │ ├── README.md
│ │ ├── selectioSort.py
│ │ └── Selection_Sort.java
│ ├── 09. Cyclic Sort
│ │ └── CyclicSort.py
│ ├── 01. Bubble Sort
│ │ ├── buubleSort.py
│ │ └── BubbleSort.java
│ ├── 11. Bitonic Sort
│ │ └── README.md
│ └── 06. Count Sort
│ │ └── Count_sort.c
├── 09. Dynamic Programming
│ ├── 001. Target Sum
│ │ └── README.md
│ ├── 002. Climbing Stairs
│ │ ├── README.md
│ │ └── code.cpp
│ ├── 005. House Robber
│ │ ├── README.md
│ │ └── code.cpp
│ ├── 006. House Robber II
│ │ ├── README.md
│ │ └── code.cpp
│ ├── 010. Delete and Earn
│ │ ├── README.md
│ │ └── code.cpp
│ ├── 007. Fibonacci Number
│ │ ├── README.md
│ │ └── code.cpp
│ ├── 008. Nth Tribonacci Number
│ │ ├── README.md
│ │ └── code.cpp
│ ├── 009. Min Cost Climbing Stairs
│ │ ├── README.md
│ │ └── code.cpp
│ └── 003. Best time to Buy and Sell Stock
│ │ ├── README.md
│ │ └── code.cpp
├── 04. Recursion
│ ├── README.md
│ └── Problems
│ │ ├── 01. 509. Fibonacci Number
│ │ ├── fibonacci_cache.py
│ │ ├── fibonacci.py
│ │ ├── Fibonacci_SeriesUptoN.cpp
│ │ └── README.md
│ │ ├── 06. Permutations
│ │ ├── README.md
│ │ └── permutations.py
│ │ ├── 07. Memoization
│ │ ├── 03. Unique Path
│ │ │ └── Solution.cpp
│ │ ├── 02. Climbing Stairs
│ │ │ └── Solution.cpp
│ │ └── 01. Game of Chef
│ │ │ └── Solution.cpp
│ │ ├── 02. 344. Reverse String
│ │ ├── Solution.java
│ │ └── README.md
│ │ ├── 03.Towers of Hanoi
│ │ ├── TowerOfHanoi.java
│ │ ├── TowerOfHanoi.c
│ │ ├── Towers_of_Hanoi.cpp
│ │ └── TowerOfHanoi.py
│ │ ├── 08. Factorial of N
│ │ ├── Solution.py
│ │ └── README.md
│ │ ├── 00. Maximum and Minimum Value in an Array
│ │ ├── README.md
│ │ └── Solution.java
│ │ ├── 00. Sum triangle from array
│ │ ├── Solution.java
│ │ └── README.md
│ │ ├── 04. Recursive Insertion Sort
│ │ ├── Solution.java
│ │ └── README.md
│ │ └── 05.Recursive Bubble Sort
│ │ ├── RecursiveBubbleSort.java
│ │ └── README.md
├── 09. Checksum Calc
│ ├── README.md
│ └── ones_complement_checksum16bit.cpp
├── 08. Strings
│ ├── Reverse a String
│ │ ├── Reverse_ String.java
│ │ └── reverse_string.py
│ └── Palindrome String
│ │ ├── palindrome_string.py
│ │ └── Palindrome String.java
└── 06. Number Theory
│ └── Sieve Of Eratosthenes
│ ├── Sieve_Of_Eratosthenes.py
│ └── SieveOfEratosthenes.cpp
└── CodeChef DSA Learning series Solutions
├── Assets
├── cc1.jpg
└── cc2.jpg
├── Easy Problems to get started
├── Buy please
│ ├── Solution.cpp
│ └── README.md
├── Add Natural Numbers
│ ├── Solution.cpp
│ └── README.md
├── Range Odd
│ ├── Solution.cpp
│ └── README.md
├── Valid Triangle Or Not
│ ├── Solution.cpp
│ └── README.md
├── Triangle With Angle
│ ├── Solution.cpp
│ └── README.md
├── Sum Is Everywhere
│ ├── Solution.cpp
│ └── README.md
├── Raju and His Trip
│ ├── Solution.cpp
│ └── README.md
├── Reverse Me
│ ├── Solution.cpp
│ └── README.md
├── Is Both or Not
│ ├── Solution.cpp
│ └── README.md
├── Triangle Everywhere
│ ├── Solution.cpp
│ └── README.md
├── Reverse Star Pattern
│ ├── Solution.cpp
│ └── README.md
├── Factors Finding
│ ├── Solution.cpp
│ └── README.md
├── Find Me
│ ├── Solution.cpp
│ └── README.md
├── Find Second Larget
│ ├── Solution.c
│ └── README.md
└── Alternative Square Pattern
│ ├── Solution.cpp
│ └── README.md
└── README.md
/_config.yml:
--------------------------------------------------------------------------------
1 | theme: jekyll-theme-minimal
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/08. Linked Queue/Images/README:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Graph/README.md:
--------------------------------------------------------------------------------
1 | # Solutions to Graph Problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Greedy/README.md:
--------------------------------------------------------------------------------
1 | # Solutions to Greedy Problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Heap/README.md:
--------------------------------------------------------------------------------
1 | # Solutions to Heap Problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Trie/README.md:
--------------------------------------------------------------------------------
1 | # Solutions to Trie Problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Matrix/README.md:
--------------------------------------------------------------------------------
1 | ## Solutions to Matrix Problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/String/README.md:
--------------------------------------------------------------------------------
1 | ## Solutions to String Problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/LinkedList/README.md:
--------------------------------------------------------------------------------
1 | ## Solutions to LinkedList Problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/BackTracking/README.md:
--------------------------------------------------------------------------------
1 | # Solutions to BackTracking problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Binary Trees/README.md:
--------------------------------------------------------------------------------
1 | ## Solutions to Binary Trees Problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Bit Manipulation/README.md:
--------------------------------------------------------------------------------
1 | # Solutions to Bit Manipulation Problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Stacks & Queues/README.md:
--------------------------------------------------------------------------------
1 | # Solutions to Stacks & Queues problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Binary Search Trees/README.md:
--------------------------------------------------------------------------------
1 | # Solutions to Binary Search Trees problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Dynamic Programming/README.md:
--------------------------------------------------------------------------------
1 | # Solutions to Dynamic Programming Problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Searching & Sorting/README.md:
--------------------------------------------------------------------------------
1 | ## Solutions to Searching & Sorting Problems
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Binary Trees/Top View Of Binary tree/readme.md:
--------------------------------------------------------------------------------
1 | https://practice.geeksforgeeks.org/problems/top-view-of-binary-tree/1
--------------------------------------------------------------------------------
/01. DataStructures/04. Tree/01. Introduction to Binary Tree/Tree Operations/Readme.md:
--------------------------------------------------------------------------------
1 | Here you can find the Operations can be done in Binary Tree
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Binary Trees/Bottom View Of a Binary Tree/readme.md:
--------------------------------------------------------------------------------
1 | https://practice.geeksforgeeks.org/problems/bottom-view-of-binary-tree/
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/Problems/Add Two Numbers/README.md:
--------------------------------------------------------------------------------
1 | # Add Two Numbers
2 | https://leetcode.com/problems/add-two-numbers/
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/12. 3Sum #15/3Sum.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [3Sum #15](https://leetcode.com/problems/3sum/)
2 |
3 | **Solution Link:** [3Sum #15](./Solution.java)
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/13. 4Sum #18/4Sum.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [4Sum #18](https://leetcode.com/problems/4sum/)
2 |
3 | **Solution Link:** [4Sum #18](./Solution.java)
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/README.md:
--------------------------------------------------------------------------------
1 | ## All Solutions of Love Babbar's [450 DSA Sheet](https://drive.google.com/file/d/1FMdN_OCfOI0iAeDlqswCiC2DZzD4nPsb/view)
2 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/Problems/Merge Two Sorted Lists/README.md:
--------------------------------------------------------------------------------
1 | # Merge Two Sorted Lists
2 | https://leetcode.com/problems/merge-two-sorted-lists/
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Assets/cc1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/CodeChef DSA Learning series Solutions/Assets/cc1.jpg
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Assets/cc2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/CodeChef DSA Learning series Solutions/Assets/cc2.jpg
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Binary Trees/Level-order-traversal/readme.md:
--------------------------------------------------------------------------------
1 | # Level order traversal
2 | link:- https://practice.geeksforgeeks.org/problems/level-order-traversal/1
3 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Binary Trees/Maximum Depth of a binary tree/readme.md:
--------------------------------------------------------------------------------
1 | # Maximum Depth of Binary Tree
2 | link:- https://leetcode.com/problems/maximum-depth-of-binary-tree/
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Binary Trees/height-of-a-binary-tree/readme.md:
--------------------------------------------------------------------------------
1 | # Height of Binary Tree
2 | link: https://practice.geeksforgeeks.org/problems/height-of-binary-tree/1
3 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/01. Linear Search/Notes.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/02. Algorithms/03. Searching/01. Linear Search/Notes.pdf
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/02. Binary Search/Notes.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/02. Algorithms/03. Searching/02. Binary Search/Notes.pdf
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Arrays/README.md:
--------------------------------------------------------------------------------
1 | ## Solutions to Array Problems
2 |
3 | - Reverse the Array: [Submit here](https://practice.geeksforgeeks.org/problems/reverse-a-string/1)
4 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Binary Trees/Diameter of Binary Tree/readme.md:
--------------------------------------------------------------------------------
1 | # Diameter of Binary Tree
2 | link:- https://practice.geeksforgeeks.org/problems/diameter-of-binary-tree/1
3 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Binary Trees/reverse-level-order/readme.md:
--------------------------------------------------------------------------------
1 | # Reverse Level Order Traversal
2 | link: https://practice.geeksforgeeks.org/problems/reverse-level-order-traversal/1
3 |
--------------------------------------------------------------------------------
/02. Algorithms/05. Graphs/01. Depth First Search/output.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/02. Algorithms/05. Graphs/01. Depth First Search/output.png
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Arrays/Largest Sum Contiguous Subarray/README.md:
--------------------------------------------------------------------------------
1 | ## Question link available here 👇
2 |
3 | https://practice.geeksforgeeks.org/problems/kadanes-algorithm-1587115620/1
--------------------------------------------------------------------------------
/01. DataStructures/04. Tree/Problems/Path Sum #112/PathSum.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Path Sum #112](https://leetcode.com/problems/path-sum/)
2 |
3 | **Solution Link:** [Path Sum #112 Solution](./Solution.java)
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/08. Heap Sort/README.md:
--------------------------------------------------------------------------------
1 | ## [Already Covered](https://github.com/thepranaygupta/Data-Structures-and-Algorithms/tree/main/01.%20DataStructures/04.%20Tree/05.%20Heap#7---heap-sort)
2 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/Problems/Remove Nth Node From End of List/README.md:
--------------------------------------------------------------------------------
1 | # Remove Nth Node From End of List
2 | https://leetcode.com/problems/remove-nth-node-from-end-of-list/
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Arrays/Next Permutation/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/Love Babbar DSA Sheet Solutions/Arrays/Next Permutation/.DS_Store
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/11. Rotate LL by K place/README.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Rotate List by K places](https://www.interviewbit.com/problems/rotate-list/)
2 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/03. Kth Node From Middle/README.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Kth Node From Middle](https://www.interviewbit.com/problems/kth-node-from-middle/)
2 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Basic Operation/3. Stack using Queue/README.md:
--------------------------------------------------------------------------------
1 | ### **Step 1 :** [Visualize here by codeWhoop](https://youtu.be/kKjYSBeDpFA)
2 |
3 | ### **Step 2:** [See my code here](./Stack_Queue.java)
4 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Basic Operation/4. Stack using Priority Queue/README.md:
--------------------------------------------------------------------------------
1 | ### **Step 1:** [Vizualize here](https://youtu.be/A1fc2Robnk0)
2 |
3 | ### **Step 2:** [See my code here](./Stack_Priority.java)
4 |
5 |
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/001. Target Sum/README.md:
--------------------------------------------------------------------------------
1 | ## Set of Leetcode Problems based on basic Dynamic Programming Approach
2 |
3 | # Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/12. Reverse LL-ii/README.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Reverse-link-list-II InterviewBit](https://www.interviewbit.com/problems/reverse-link-list-ii/)
2 |
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/002. Climbing Stairs/README.md:
--------------------------------------------------------------------------------
1 | ## Set of Leetcode Problems based on basic Dynamic Programming Approach
2 |
3 | # Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/005. House Robber/README.md:
--------------------------------------------------------------------------------
1 | ## Set of Leetcode Problems based on basic Dynamic Programming Approach
2 |
3 | # Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/006. House Robber II/README.md:
--------------------------------------------------------------------------------
1 | ## Set of Leetcode Problems based on basic Dynamic Programming Approach
2 |
3 | # Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/010. Delete and Earn/README.md:
--------------------------------------------------------------------------------
1 | ## Set of Leetcode Problems based on basic Dynamic Programming Approach
2 |
3 | # Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/LeetCode Questions/02. Sort List #148/SortList.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Sort List #148](https://leetcode.com/problems/sort-list/)
2 |
3 | **Solution Link:** [Sort List #148 Solution](./Solution.java)
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/03. Depth First Search/Images/Screenshot 1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/02. Algorithms/03. Searching/03. Depth First Search/Images/Screenshot 1.png
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/03. Depth First Search/Images/Screenshot 2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/02. Algorithms/03. Searching/03. Depth First Search/Images/Screenshot 2.png
--------------------------------------------------------------------------------
/02. Algorithms/05. Graphs/BreathFirstSearch/Screenshot 2021-10-03 235854.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/02. Algorithms/05. Graphs/BreathFirstSearch/Screenshot 2021-10-03 235854.png
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/007. Fibonacci Number/README.md:
--------------------------------------------------------------------------------
1 | ## Set of Leetcode Problems based on basic Dynamic Programming Approach
2 |
3 | # Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/008. Nth Tribonacci Number/README.md:
--------------------------------------------------------------------------------
1 | ## Set of Leetcode Problems based on basic Dynamic Programming Approach
2 |
3 | # Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/009. Min Cost Climbing Stairs/README.md:
--------------------------------------------------------------------------------
1 | ## Set of Leetcode Problems based on basic Dynamic Programming Approach
2 |
3 | # Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Dynamic Programming/Coin Change/Readme.md:
--------------------------------------------------------------------------------
1 | # Problem Name - Coin Change problem
2 | # Problem Link - https://practice.geeksforgeeks.org/problems/coin-change2448/1
3 |
4 | # TC - O(N * S)
5 | # SC - O(N * S)
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/10. Reverse LL in K group GQ/Iterative Solution Self.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [K reverse linked list](https://www.interviewbit.com/problems/k-reverse-linked-list/)
2 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Dynamic Programming/Edit Distance/README.md:
--------------------------------------------------------------------------------
1 | # Problem Name - Edit Distance problem
2 | # Problem Link - https://practice.geeksforgeeks.org/problems/edit-distance3702/1
3 |
4 | # TC = o(M * N)
5 | # SC = o(M * N)
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Dynamic Programming/Knapsack/README.md:
--------------------------------------------------------------------------------
1 | # Problem Name - Knapsack problem
2 | # Problem Link - https://practice.geeksforgeeks.org/problems/0-1-knapsack-problem0945/1
3 |
4 | # TC - O(N * W)
5 | # SC - O(N * W)
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/04. Baseball Game #682/Resources.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Baseball Game #682](https://leetcode.com/problems/baseball-game/)
2 |
3 | **Solution Link:** [Baseball Game #682 Solution](./Solution.java)
4 |
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/01. Queue using Array/pseudocode.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/01. DataStructures/03. Queue/Basic Operations/01. Queue using Array/pseudocode.png
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/13. 410. Split Array Largest Sum/Dry run.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/02. Algorithms/03. Searching/Problems/13. 410. Split Array Largest Sum/Dry run.pdf
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/003. Best time to Buy and Sell Stock/README.md:
--------------------------------------------------------------------------------
1 | ## Set of Leetcode Problems based on basic Dynamic Programming Approach
2 |
3 | # Problem Link - https://leetcode.com/study-plan/dynamic-programming/?progress
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/01. Infix to Postfix/pseudocode.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/01. DataStructures/02. Stack/Infix Prefix Postfix/01. Infix to Postfix/pseudocode.png
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/02. Infix to Prefix/pseudocode.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/01. DataStructures/02. Stack/Infix Prefix Postfix/02. Infix to Prefix/pseudocode.png
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/03. Postfix to Infix/pseudocode.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/01. DataStructures/02. Stack/Infix Prefix Postfix/03. Postfix to Infix/pseudocode.png
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/04. Prefix to Infix/pseudocode.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/01. DataStructures/02. Stack/Infix Prefix Postfix/04. Prefix to Infix/pseudocode.png
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/05. Postfix to Prefix/pseudocode.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/01. DataStructures/02. Stack/Infix Prefix Postfix/05. Postfix to Prefix/pseudocode.png
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/06. Prefix to Postfix/pseudocode.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/01. DataStructures/02. Stack/Infix Prefix Postfix/06. Prefix to Postfix/pseudocode.png
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/08. Linked Queue/Images/Screenshot_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/01. DataStructures/03. Queue/Basic Operations/08. Linked Queue/Images/Screenshot_1.png
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/08. Linked Queue/Images/Screenshot_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/01. DataStructures/03. Queue/Basic Operations/08. Linked Queue/Images/Screenshot_2.png
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/3. Circular-Linked-List/README.md:
--------------------------------------------------------------------------------
1 | **Step 1:** [Circular Linked List by Dinesh Varyani.](https://youtube.com/playlist?list=PLFiCMqYy50jG4Cd3t9Hj92gDHiKvdz2tO)
2 |
3 | **Step 2:** [See my code here.](./CircularLL.java)
4 |
--------------------------------------------------------------------------------
/01. DataStructures/08. Graph/Leetcode Questions/01. Rotting Oranges #994/Rotting Oranges.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Rotting Oranges #994](https://leetcode.com/problems/rotting-oranges/)
2 |
3 | **Solution Link:** [Rotting Oranges #994 Solution](./Solution.java)
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/04. Circular Queue using Array/README.md:
--------------------------------------------------------------------------------
1 | ### **Step 1:** [Visualize Circular Queue using array ](https://youtu.be/KSvMGwc9dN8)
2 |
3 | ### **Step 2:** [Try to understand my code here.](./CircularQueue_Array.java)
4 |
--------------------------------------------------------------------------------
/01. DataStructures/08. Graph/Leetcode Questions/03. Is Graph Bipartite #785/Is Graph Bipartite.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Is Graph Bipartite #785](https://leetcode.com/problems/is-graph-bipartite/)
2 |
3 | **Solution Link:** [Is Graph Bipartite #785](./Solution.java)
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/2. Doubly-Linked-List/Basic Operations/README.md:
--------------------------------------------------------------------------------
1 | **Step 1:** [DoublyLL playlist by Dinesh Varyani](https://youtube.com/playlist?list=PLFiCMqYy50jGaEUri0wNiHtBqlxO_jell)
2 |
3 | **Step 2:** [See my code here.](./DoublyLL.java)
4 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/06. Crawler Log Folder #1598/Resources.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Crawler Log Folder #1598](https://leetcode.com/problems/crawler-log-folder/)
2 |
3 | **Solution Link :** [Crawler Log Folder #1598 Solution](./Solution.java)
4 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/README.md:
--------------------------------------------------------------------------------
1 | # Video Tutorials:
2 | + ### Kunal's [Introduction to Recursion - Learn In The Best Way](https://youtu.be/M2uO2nMT0Bk)
3 | + ### Striver's [Recursion Masterclass](https://youtube.com/playlist?list=PLN4aKSfpk8TR-A59O9qR2VL0wqt3LrrnK)
4 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/2. Doubly-Linked-List/Questions/1. Two Pair Sum/2 pair sum Q.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thepranaygupta/Data-Structures-and-Algorithms/HEAD/01. DataStructures/01. Linked List/2. Doubly-Linked-List/Questions/1. Two Pair Sum/2 pair sum Q.pdf
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/LeetCode Questions/01. Reverse Linked List #206/ReverseLinkedList.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Reverse Linked List #206](https://leetcode.com/problems/reverse-linked-list/)
2 |
3 | **Solution Link:** [Reverse Linked List #206 Solution](./Solution.java)
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/05. Circular Queue using Linked List/README.md:
--------------------------------------------------------------------------------
1 | ### **Step 1:** [Visualize Circular Queue using Linked List](https://youtu.be/HsJc7a6NoTE)
2 |
3 | ### **Step 2:** [Try to understand my code here.](./CircularQueue_LinkedList.java)
4 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Buy please/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main() {
5 | int a,b,x,y;
6 | cin>>a>>b>>x>>y;
7 | cout<<(a*x)+(b*y)<Finding Height of a Binary Tree
2 |
3 | Link of question https://leetcode.com/problems/maximum-depth-of-binary-tree/
4 |
5 | Time Complexity = O(n)
6 | Space Complexity = O(n)
7 |
--------------------------------------------------------------------------------
/01. DataStructures/04. Tree/Problems/All Nodes At Distance K #863/AllNodesAtDistanceK.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [All Nodes Distance K in Binary Tree #863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/)
2 |
3 | **Solution Link:** [All Nodes Distance K in Binary Tree #863 Solution](./Solution.java)
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/01. ReverseLL/README.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Reverse Link List-InterviewBit](https://www.interviewbit.com/problems/reverse-linked-list/)
2 |
3 | **Tutorial Link:** [Reverse a Singly Linked List in Java](https://youtu.be/jY-EUKXYT20)
4 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/04. Palindrome List GQ/README.md:
--------------------------------------------------------------------------------
1 | **ProblemLink:** [Palindrome-list-InterviewBit](https://www.interviewbit.com/problems/palindrome-list/)
2 |
3 | **Tutorial Link:** [Check if palindrome-take U forward](https://youtu.be/-DtNInqFUXs)
4 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/03. 215. Kth Largest Element in an Array/brute_force.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/kth-largest-element-in-an-array/
2 | class Solution {
3 | public int findKthLargest(int[] nums, int k) {
4 | Arrays.sort(nums);
5 | return nums[nums.length - k];
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/02. Queue using Linked List/README.md:
--------------------------------------------------------------------------------
1 | ### **Step 1:** [Visualize Queue using linklist here by Apna College in 6min only](https://youtu.be/Yi8Im4_eSQI)
2 |
3 | ### **Step 2:** [Recall your Linklist knowledge and try to understand my code here.](./Queue_LinkedList.java)
4 |
--------------------------------------------------------------------------------
/01. DataStructures/04. Tree/05. Heap/Leetcode Questions/02. K Closest Points to Origin #973/K Closest Points to Origin.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [K Closest Points to Origin #973](https://leetcode.com/problems/k-closest-points-to-origin/)
2 |
3 | **Solution Link:** [K Closest Points to Origin #973 Solution](./Solution.java)
--------------------------------------------------------------------------------
/01. DataStructures/05. ArrayList/AddOddAndEvenNums/README.md:
--------------------------------------------------------------------------------
1 | # Using two ArrayLists, add all even numbers to one list and all odds to another. Do another pass to insert them at the correct indices.
2 |
3 | https://leetcode.com/problems/sort-array-by-parity-ii/discuss/194416/java-easy-to-understand-arraylist-solution
4 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/01. 509. Fibonacci Number/fibonacci_cache.py:
--------------------------------------------------------------------------------
1 | from functools import cache
2 |
3 | @cache
4 | def fibonacci(n):
5 | if n < 2:
6 | return n
7 | return fibonacci(n-1) + fibonacci(n-2)
8 |
9 | for i in range(100):
10 | print("Fibonacci number ", i, ": ", fibonacci(i))
11 |
--------------------------------------------------------------------------------
/01. DataStructures/04. Tree/Problems/Lowest Common Ancestor #236/LowestCommonAncestor.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Lowest Common Ancestor of a Binary Tree #236](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)
2 |
3 | **Solution Link:** [Lowest Common Ancestor of a Binary Tree #236 Solution](./Solution.java)
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Add Natural Numbers/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main() {
5 | long long N,sum=0;
6 |
7 | cin>>N;
8 | for(int i =1 ;i<=N;i++){
9 | sum = sum+i;
10 | }
11 | cout<
2 | using namespace std;
3 | int main()
4 | {
5 | int l,r;
6 | cin>>l>>r;
7 | if(l%2==0){
8 | l++;
9 | }
10 | for(int i=l;i<=r;i+=2){
11 | cout<left),maxDepth(root->right)));
8 | }
9 | };
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/08. Remove nth Node from List end GQ/README.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Remove-nth-node-from-list-end-InterviewBit](https://www.interviewbit.com/problems/remove-nth-node-from-list-end/)
2 |
3 | **Tutorial Link:** [Remove nth from list end -takeUforwad](https://youtu.be/Lhu3MsXZy-Q)
4 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/05. Merge Two Sorted List GQ/Apna College Solution (Good Explaination).md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Merge Two Sorted Lists InterviewBit](https://www.interviewbit.com/problems/merge-two-sorted-lists/)
2 |
3 | **Tutorial Link:** [Merge-two-sorted-List Apna College](https://youtu.be/n5_9DMCX0Yk)
4 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Valid Triangle Or Not/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main()
5 | {
6 | int a, b, c;
7 | cin >> a >> b >> c;
8 | if (a + b < c a + c < b b + c < a)
9 | cout << "NO";
10 | else
11 | cout << "YES";
12 | return 0;
13 | }
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Triangle With Angle/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main() {
5 |
6 | int a,b,c;
7 | cin>>a>>b>>c;
8 | if(a+b+c==180&& a>0 && b>0 && c>0){
9 | cout<<"YES";
10 | }
11 | else{
12 | cout<<"NO";
13 | }
14 | return 0;
15 | }
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Sum Is Everywhere/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #define ll long long
3 | using namespace std;
4 |
5 | int main() {
6 | // your code goes here
7 | ll n,even,odd;
8 |
9 | cin>>n;
10 | even = n(n+1);
11 | odd = nn;
12 | cout<
2 | using namespace std;
3 |
4 | int main() {
5 | // your code goes here
6 | int n;
7 | cin>>n;
8 |
9 | if(n%5==0 || n%6==0){
10 | cout<<"YES";
11 | }
12 | else{
13 | cout<<"NO";
14 | }
15 |
16 | return 0;
17 | }
18 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/07. Remove Duplicates From Sorted List- II GQ/README.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Interviewbit remove-duplicates-from-sorted-list II](https://www.interviewbit.com/problems/remove-duplicates-from-sorted-list-ii/)
2 |
3 | **Tutorial Link:** [ Remove Dublicate From Sorted List-II-Algorithms Made Easy](https://youtu.be/R6-PnHODewY)
4 |
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/06. Dequeue using circular array/README.md:
--------------------------------------------------------------------------------
1 | ### **Step 1:** [Visualize Dequeue using circular array](https://www.studytonight.com/data-structures/double-ended-queue)
2 |
3 | ### **Step 2:** [Try to understand my code here.](./Dequeue_CircularArray.java)
4 |
5 | ### **Step 3:** [Learn about builtIn ArrayDequeue class in java](https://youtu.be/V_NVMqxcNaI)
6 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Reverse Me/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | int main() {
6 | // your code goes here
7 | int n;
8 |
9 | cin>>n;
10 | int a[n];
11 | for(int i=0 ;i>a[i];
13 | }
14 | for(int i=n-1;i>=0;i--){
15 | cout<& prices) {
4 | int cost = INT_MAX, sell=0;
5 |
6 | for(int i:prices)
7 | {
8 | cost = min(cost,i);
9 | sell = max(sell,i-cost) ;
10 | }
11 |
12 | return sell ;
13 | }
14 | };
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Binary Trees/height-of-a-binary-tree/solution.cpp:
--------------------------------------------------------------------------------
1 | class Solution{
2 | public:
3 | //Function to find the height of a binary tree.
4 | int height(struct Node* node){
5 | if(node == NULL) return 0;
6 | int lh = height(node->left);
7 | int rh = height(node->right);
8 |
9 | return 1 + max(lh,rh);
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/007. Fibonacci Number/code.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | int fib(int n) {
4 |
5 | if(n==0 || n==1)
6 | return n;
7 | int dp[n+1];
8 | dp[0] = 0;
9 | dp[1] = 1;
10 |
11 | for(int i=2;i<=n;i++)
12 | dp[i] = dp[i-1]+dp[i-2] ;
13 |
14 | return dp[n] ;
15 | }
16 | };
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/09. Valid Parentheses #20/Resources.md:
--------------------------------------------------------------------------------
1 | ### **Problem Link:**
2 |
3 | - #### [Valid Parentheses #20](https://leetcode.com/problems/valid-parentheses/)
4 |
5 | ### **Solution Link:**
6 |
7 | - #### [Valid Parentheses #20 solution](./Solution.java)
8 |
9 | ### **Tutorial Link:**
10 |
11 | - #### [Visualization by Prakash Shukhla](https://youtu.be/cnjxnFDNie8)
12 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/12. Score of Parentheses #856/Resources.md:
--------------------------------------------------------------------------------
1 | ### **Problem Link:**
2 |
3 | - #### [Score of Parentheses #856](https://leetcode.com/problems/score-of-parentheses/)
4 |
5 | ### **Solution Link:**
6 |
7 | - #### [Score of Parentheses #856 solution](./Solution.java )
8 |
9 | ### **Tutorial Link:** [Score of Parentheses by Nick White](https://youtu.be/jfmJusJ0qKM)
10 |
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/02. InsertionSort/README.md:
--------------------------------------------------------------------------------
1 | ### [Video Reference](https://youtu.be/wWhAhp6PIuQ)
2 |
3 | ### Time Complexity: O(n²)
4 | #### but less number of swaps than Bubble Sort
5 |
6 | ```java
7 | for (int i = 1; i < n; i++) {
8 | int temp = arr[i];
9 | int j = i - 1;
10 | while (j >= 0 && arr[j] > temp) {
11 | arr[j + 1] = arr[j];
12 | j--;
13 | }
14 | arr[j + 1] = temp;
15 | }
16 | ```
17 |
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/10. TimSort/README.md:
--------------------------------------------------------------------------------
1 | [Video Reference](https://www.youtube.com/watch?v=emeME__917E)
2 |
3 | TimSort is a sorting algorithm based on Insertion Sort and Merge Sort.
4 |
5 | 1) A stable sorting algorithm works in O(n Log n) time
6 | 2) Used in Java’s Arrays.sort() as well as Python’s sorted() and sort().
7 | 3) First sort small pieces using Insertion Sort, then merges the pieces using merge of merge sort.
8 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/14. Validate Stack Sequences #946/Resources.md:
--------------------------------------------------------------------------------
1 | ### **Problem Link:**
2 |
3 | - #### [Validate Stack Sequences #946](https://leetcode.com/problems/validate-stack-sequences/)
4 |
5 | ### **Solution Link:**
6 |
7 | - #### [Validate Stack Sequences #946 solution](./Solution.java)
8 |
9 | ### **Tutorial Link:** [Validate Stack Sequences by AlgorithmMEasy](https://youtu.be/SeTsK_aNUWI)
10 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/06. Permutations/README.md:
--------------------------------------------------------------------------------
1 | # 6: Permutations
2 |
3 | [Inspired by this Leetcode question.](https://leetcode.com/problems/permutations/)
4 |
5 | Problem:
6 |
7 | Given an array of integers, return all possible configurations that the original array can be in.
8 |
9 | For example, given [1,2,3].
10 | -> [ [1,2,3], [1,3,2], [2,3,1], [2,1,3], [3,2,1] ]
11 |
12 | Use recursion in your solution.
13 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/03. Postfix to Infix/README.md:
--------------------------------------------------------------------------------
1 | **NOTE:** [You should know Step 2 from here.](../01.%20Infix%20to%20Postfix)
2 |
3 | ### **Step 1:** [Understand the working of pseudocode](https://youtu.be/OKdMY9oYkTg)
4 |
5 | 
6 |
7 | ### **Step 2:** [See my code here in java to convert posfix to infix using stack.](./PostfixInfix.java)
8 |
9 |
10 | Thank You
11 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/04. Prefix to Infix/README.md:
--------------------------------------------------------------------------------
1 | **NOTE:** [You should know Step 3 from here.](../01.%20Infix%20to%20Postfix)
2 |
3 | ### **Step 1:** [Understand the working of pseudocode](https://youtu.be/sevlImM3Onc)
4 |
5 | 
6 |
7 | ### **Step 2:** [See my code here in java to convert prefix to infix using stack.](./PrefixInfix.java)
8 |
9 |
10 | Thank You
11 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/README.md:
--------------------------------------------------------------------------------
1 | Here I will Upload all the answers realted to the codechef DSA learning series
2 |
3 | 
4 |
5 | These are the Topics realted to this series and I will Upload all the Problems in this.
6 | 
7 |
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/03. Queue using Stacks/README.md:
--------------------------------------------------------------------------------
1 | ### **Step 1:** [Visualize Queue using stack here in 10min only](https://youtu.be/ma1S6vtkw9I)
2 |
3 | ### **Step 2:** [Try to understand my code here.](./Queue_Stack.java)
4 |
5 |
6 | ### **Step 3:** [Visualize Queue using stack here in 10min only](https://www.youtube.com/watch?v=iEwX3XA5SDo)
7 |
8 | ### **Step 4:** [Try to understand my code here.] (./queue using two stack.cpp)
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/07. Memoization/03. Unique Path/Solution.cpp:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/unique-paths/
2 |
3 | #include
4 | using namespace std;
5 |
6 | int factorial(int n){
7 | if(n==1 || n==0)
8 | return 1;
9 | return n*factorial(n-1);
10 | }
11 |
12 | int main(){
13 | int n,m;
14 | cin>>n>>m;
15 | cout<<(factorial(n+m-2))/(factorial(n-1)*factorial(m-1));
16 | return 0;
17 | }
18 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/08. 852. Peak Index in a Mountain Array/Solution1.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/peak-index-in-a-mountain-array/
2 | // Using linear search
3 |
4 | class Solution {
5 | public int peakIndexInMountainArray(int[] arr) {
6 | int i;
7 | for(i = 0; i < arr.length; i++) {
8 | if(arr[i + 1] < arr[i])
9 | break;
10 | }
11 | return i;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/25. Array Partition 1/array-partition-i.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | int arrayPairSum(vector& nums) {
4 | int n = nums.size();
5 | sort(nums.begin(),nums.end());
6 | int total=0;
7 | for(int i=0;i= j)
9 | return;
10 | char temp = s[i];
11 | s[i] = s[j];
12 | s[j] = temp;
13 | reverse(s, i+1, j-1);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/05. Postfix to Prefix/README.md:
--------------------------------------------------------------------------------
1 | ### **Step 1:** [Postfix to Prefix and viceversa **manual conversion**.](https://youtu.be/tuRwmc6Jo1U)
2 |
3 | ### **Step 2:** [Visualize pseudocode to convert postfix to prefix.](https://youtu.be/4GTqpV_dqKY)
4 |
5 | 
6 |
7 | ### **Step 3:** [See my code here in java to convert postfix to prefix using stack.](./PostfixPrefix.java)
8 |
9 |
10 | Thank You
11 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/02. Remove All Adjacent Duplicates In String #1047/Remove All Adjacent Duplicates In String.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Remove All Adjacent Duplicates In String #1047](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)
2 |
3 | **Solution Link:** [Remove All Adjacent Duplicates In String #1047 Solution](./Solution.java)
4 |
5 | **Vikash's Solution Link:** [Remove All Adjacent Duplicates In String #1047 Solution](./Solution_Vikash.java)
6 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/String/Reverse string.java:
--------------------------------------------------------------------------------
1 | //question link 1. https://leetcode.com/problems/reverse-string/
2 |
3 | class Solution
4 | {
5 | public void reverseString(char[] s)
6 | {
7 |
8 | char temp=' ';
9 | for(int i=0; i
10 | Thank You
11 |
--------------------------------------------------------------------------------
/02. Algorithms/08. Strings/Reverse a String/Reverse_ String.java:
--------------------------------------------------------------------------------
1 | //question link- https://leetcode.com/problems/reverse-string/
2 |
3 | class Solution
4 | {
5 | public void reverseString(char[] s)
6 | {
7 |
8 | char temp=' ';
9 | for(int i=0; i& nums) {
4 | vector cnt(10002);
5 | for(int i=0;i dp(10002);
11 | dp[1]=cnt[1];
12 | dp[2]=max(cnt[1],cnt[2]);
13 | for(int i=3;imaxHeight)
14 | maxHeight= height;
15 | depth(root.left, height+1);
16 | depth(root.right, height+1);
17 |
18 | }
19 | }
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/03.Towers of Hanoi/TowerOfHanoi.java:
--------------------------------------------------------------------------------
1 | public class TowerOfHanoi {
2 | public static void main(String[] args) {
3 | towerOfHanoi(3, 'A','B', 'C');
4 | }
5 |
6 | static void towerOfHanoi(int n, char from, char aux, char to) {
7 | if (n>0) {
8 | towerOfHanoi(n-1, from, to, aux);
9 | System.out.println("Move " + n + " from " + from + " to " + to );
10 | towerOfHanoi(n-1, aux, from, to);
11 | }
12 | }
13 | }
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/06. Permutations/permutations.py:
--------------------------------------------------------------------------------
1 |
2 | array = [1,2,3]
3 |
4 | def permute(array: list) -> list:
5 | if len(array) == 1: # base case
6 | return [array]
7 |
8 | # recursive case
9 |
10 | perms=[]
11 |
12 | for i in range(len(array)):
13 | first = [array[i]]
14 | minilists = permute(array[:i]+array[i+1:])
15 |
16 | for p in minilists:
17 | perms.append(first+p)
18 |
19 | return perms
20 |
21 | print(permute(array))
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/07. Memoization/02. Climbing Stairs/Solution.cpp:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/climbing-stairs/
2 |
3 | #include
4 | using namespace std;
5 |
6 | int memoization(int n){
7 | int memo[n+1];
8 | memo[0]=1;
9 | memo[1]=1;
10 | for(int i=2; i<=n; i++){
11 | memo[i]= memo[i-1]+memo[i-2];
12 | }
13 | return memo[n];
14 | }
15 |
16 | int main(){
17 | int n;
18 | cin>>n;
19 | cout<
2 | using namespace std;
3 |
4 | int main()
5 | {
6 | int n;
7 | cin>>n;
8 |
9 | if(n%5==0 && n%11==0)
10 | {
11 | cout<<"BOTH";
12 | }
13 | if (n%5==0 && n%11!=0 || n%11==0 && n%5!=0 )
14 | {
15 | cout<<"ONE";
16 |
17 | }
18 | if(n%5!=0 && n%11!=0)
19 | {
20 | cout<<"NONE";
21 | }
22 | return 0;
23 | }
24 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/18. Maximum Nesting Depth of the Parentheses #1614/Resources.md:
--------------------------------------------------------------------------------
1 | **Problem Link:** [Maximum Nesting Depth of the Parentheses #1614](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/)
2 | **Solution Link:** [Maximum Nesting Depth of the Parentheses #1614 solution](https://github.com/gaurav23122001/Data-Structures-and-Algorithms/blob/main/01.%20DataStructures/02.%20Stack/LeetCode%20Questions/18.%20Maximum%20Nesting%20Depth%20of%20the%20Parentheses%20%231614/solution.cpp)
3 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/14. Validate Stack Sequences #946/Solution.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public boolean validateStackSequences(int[] pushed, int[] popped) {
3 | Stack st=new Stack();
4 | int j=0;
5 | for(int pu:pushed){
6 | st.push(pu);
7 | while(!st.isEmpty() && j maxWealth)
10 | maxWealth = iwealth;
11 | }
12 | return maxWealth;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Triangle Everywhere/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main()
5 | {
6 | int a, b, c;
7 | cin >> a >> b >> c;
8 | if (a + b > c && b + c > a && c + a > b)
9 | {
10 | if (a == b && b == c)
11 | cout << "1";
12 | else if (a == b b == c c == a)
13 | cout << "2";
14 | else
15 | cout << "3";
16 | }
17 | else
18 | cout << "-1";
19 | return 0;
20 | }
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/06. Crawler Log Folder #1598/Solution.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public int minOperations(String[] logs) {
3 | Stack st = new Stack();
4 | for(String i : logs){
5 | if(i.equals("../")){
6 | if(!st.empty())
7 | st.pop();
8 | }
9 | else if(i.equals("./"))
10 | continue;
11 | else
12 | st.push(i);
13 | }
14 | return st.size();
15 | }
16 | }
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/03.Towers of Hanoi/TowerOfHanoi.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | int main()
4 | {
5 | hanoi(3, 'A', 'B', 'C');
6 | return 0;
7 | }
8 | void hanoi(int n, char rodFrom, char rodMiddle, char rodTo){
9 | if(n==1){
10 | printf("Disk 1 moved from %c to %c \n",rodFrom,rodTo);
11 | return;
12 | }
13 | hanoi(n-1,rodFrom,rodTo,rodMiddle);
14 | printf("Disk %d moved from %c to %c \n",n,rodFrom,rodTo);
15 | hanoi(n-1,rodMiddle,rodFrom,rodTo);
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Reverse Star Pattern/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main() {
5 | // your code goes here
6 | int n;
7 | cin>>n;
8 |
9 | for(int i=n;i>0;i--){
10 | for(int j = 0;j<=n;j++){
11 | if(j>=i){
12 | cout<<"*";
13 | }
14 | else{
15 | cout<<" ";
16 | }
17 | }
18 | cout< st=new Stack();
4 | int score=0;
5 | for(char c: S.toCharArray()){
6 |
7 | if(c=='('){
8 | st.push(score);
9 | score=0;
10 | }
11 | else{
12 | score=st.pop()+Math.max(2*score,1);
13 | }
14 | }
15 | return score;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/02. Algorithms/08. Strings/Palindrome String/palindrome_string.py:
--------------------------------------------------------------------------------
1 | # The function to find if the string is a palindrome or not
2 | def is_palindrome(s):
3 | # Check if string is equal to its reverse.
4 | s = s.lower()
5 | return s == s[::-1]
6 |
7 | # The driver code
8 | if __name__ == '__main__':
9 | s = str(raw_input("Ask user for something: "))
10 | IS_PALINDROME = is_palindrome(s)
11 |
12 | if IS_PALINDROME:
13 | print("Yes, the input is a palindrome")
14 | else:
15 | print("No, the input in not a palindrome")
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Factors Finding/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main() {
5 | int n;
6 | cin>>n;
7 | int count = 0;
8 |
9 | for(int i=1;i<=n;i++)
10 | {
11 | if(n%i==0){
12 | count++;
13 | }
14 | }
15 | cout<left , d);
7 | int rh = height(root->right , d);
8 | d = max(d ,1 + lh + rh);
9 | return 1 + max(lh , rh);
10 | }
11 | int diameter(Node* root) {
12 | int d = 0;
13 | height(root , d);
14 | return d;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/005. House Robber/code.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | int rob(vector& nums) {
4 | int sz = nums.size();
5 | vector dp(sz);
6 | for (int i = 0; i < sz; i++) {
7 | if (i == 0) {
8 | dp[i] = nums[0];
9 | } else if (i == 1) {
10 | dp[i] = max(nums[1], nums[0]);
11 | } else {
12 | dp[i] = max(dp[i-1], dp[i-2] + nums[i]);
13 | }
14 | }
15 | return dp[sz-1];
16 | }
17 | };
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/02. Binary Search/Binary_search_(Recursive).cpp:
--------------------------------------------------------------------------------
1 |
2 |
3 | int binsearch(int A[], int low, int high, int key){
4 |
5 | if (low<=high){
6 | int mid = low + (high-low)/2;
7 |
8 | if(key== A[mid]){
9 | return mid;
10 | }
11 |
12 | else if (key
2 | using namespace std;
3 |
4 | int main() {
5 | // your code goes here
6 | int n,k,count=0,p=0;
7 | cin>>n>>k;
8 | int a[n];
9 | for(int i=0;i>a[i];
11 | }
12 | for(int i=0;i0){
21 | cout<<"1";
22 | }
23 | else if(p>0){
24 | cout<<"-1";
25 |
26 | }
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/03. 215. Kth Largest Element in an Array/Using_priorityQueue.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public int findKthLargest(int[] arr, int k) {
3 | int n=arr.length;
4 | PriorityQueue minPq=new PriorityQueue<>();
5 |
6 | for(int i=0;iminPq.peek()) {
10 | minPq.remove();
11 | minPq.add(arr[i]);
12 | }
13 | }
14 | return minPq.remove();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/09. Cyclic Sort/CyclicSort.py:
--------------------------------------------------------------------------------
1 | # Creating a cyclic sort function
2 | def cyclic_sort(a):
3 | i=0
4 | while(i ls[m]:
11 | l = m + 1
12 | else:
13 | h= m - 1
14 | return -1
15 |
16 | numbers = [0, 12, 13, 34, 42, 69, 420, 435, 634, 6755, 9999, 34567, 953285]
17 | number = 9999
18 |
19 | ans = binary_search(number, numbers)
20 | if ans == -1:
21 | print("Element not found")
22 | else:
23 | print("Index of", number, "in list:", binary_search(number, numbers))
24 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Bit Manipulation/Count set bits/README.md:
--------------------------------------------------------------------------------
1 | # [ Count set bits ](https://practice.geeksforgeeks.org/problems/set-bits0143/1)
2 | ## Given a positive integer N, print count of set bits in it.
3 | ### Example 1:
4 | **Input:**
5 | N = 6
6 | **Output:**
7 | 2
8 |
9 | ### Example 2:
10 | **Input:**
11 | N = 8
12 | **Output:**
13 | 1
14 |
15 | ### Solution in C++:
16 | ```
17 | int setBits(int N) {
18 | int count = 0;
19 | while(N!=0)
20 | {
21 | if((N&1)==1) count++;
22 | N = N>>1;
23 | }
24 | return count;
25 | }
26 | ```
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/07. Make The String Great #1544/Solution_Vikash.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public String makeGood(String S) {
3 | int l=S.length();
4 | char st[]=new char[l];
5 | int top=-1;
6 | for(int i=0;i-1 && Math.abs(st[top]-S.charAt(i)) == 32)
9 | top--;
10 | else{
11 | top++;
12 | st[top]=currCh;
13 | }
14 | }
15 | return new String(st,0,top+1);
16 | }
17 | }
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/08. Kadane's Algorithm/53. Maximum Subarray/Solution.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/maximum-subarray/
2 | class Solution {
3 | public int maxSubArray(int[] arr) {
4 | int csum = arr[0]; //current sum
5 | int osum = arr[0]; //overall sum
6 | for(int i = 1; i < arr.length; i++) {
7 | if(csum > 0)
8 | csum += arr[i];
9 | else
10 | csum = arr[i];
11 |
12 | if(csum > osum)
13 | osum = csum;
14 | }
15 | return osum;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/01. ReverseLL/solution_in_cpp.cpp:
--------------------------------------------------------------------------------
1 | //linked list node
2 | /*
3 | struct Node{
4 | int data;
5 | struct Node *next;
6 | }
7 | */
8 |
9 | struct Node *reverseList(struct Node *head)
10 | {
11 | struct Node *p = head;
12 | struct Node *q = nullptr;
13 | struct Node *r = nullptr;
14 |
15 | while (p != NULL)
16 | {
17 | r = q;
18 | q = p;
19 | p = p->next;
20 | q->next = r;
21 | }
22 | head = q;
23 | return head;
24 | }
25 |
26 | //time = O(n)
27 | //space = O(1)
28 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/01. 509. Fibonacci Number/fibonacci.py:
--------------------------------------------------------------------------------
1 |
2 | ## Finds the Nth number in the Fibonacci Series
3 | def fib(n: int) -> int:
4 |
5 | if n <= 1: # returns 0 if fib(0), and 1 if fib(1)
6 | return n
7 |
8 | return fib(n-1) + fib(n-2)
9 |
10 | print(fib(12))
11 |
12 |
13 |
14 |
15 | ## Generates a Fibonacci Series with two starting numbers, up until the Nth number.
16 | def fib_sequence(n: int, x: int = 0,y: int = 1) -> list:
17 |
18 | if n == 0:
19 | return [y]
20 |
21 | return [y]+fib_sequence(n-1,y,x+y)
22 |
23 | print(fib_sequence(15,1,1))
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/03. Build an Array With Stack Operations #1441/Solution.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public List buildArray(int[] target, int n) {
3 | ArrayList arr = new ArrayList();
4 | int input = 1; //1,2,3,4,....,n
5 | for(int i=0; i
2 | using namespace std;
3 | void toh(int n,char sou,char aux,char des);
4 | int main(){
5 | int n;
6 | cout<<"enter the number of disks "<>n;
8 | toh(n,'A','B','C');
9 | return 0;
10 | }
11 | void toh(int n,char sou,char aux,char des){
12 | if (n==1)
13 | {
14 | cout<<"move disk "<st=new Stack();
4 | for(char c : S.toCharArray()){
5 | if(c=='(')
6 | st.push(c);
7 | else {
8 | if(c==')' && !st.isEmpty() && st.peek()=='(')
9 | st.pop();
10 | else
11 | st.push(c);
12 | }
13 | }
14 | return st.size();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/01. Bubble Sort/buubleSort.py:
--------------------------------------------------------------------------------
1 | # Creating a bubble sort function
2 | def bubble_sort(list1):
3 | # Outer loop for traverse the entire list
4 | for i in range(0,len(list1)-1):
5 | for j in range(len(list1)-1):
6 | if(list1[j]>list1[j+1]):
7 | temp = list1[j]
8 | list1[j] = list1[j+1]
9 | list1[j+1] = temp
10 | return list1
11 |
12 | list1 = [4,5,6,2,1,8,3]
13 | print("The unsorted list is: ", list1)
14 | # Calling the bubble sort function
15 | print("The sorted list is: ", bubble_sort(list1))
16 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/01. 1295. Find Numbers with Even Number of Digits/Solution.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public int findNumbers(int[] arr) {
3 | int length = arr.length;
4 | int evencount = 0;
5 | for(int element: arr){
6 | int digitcount = 0;
7 | if(element < 0)
8 | element *= -1;
9 | while(element > 0){
10 | digitcount++;
11 | element /= 10;
12 | }
13 | if(digitcount%2 == 0)
14 | evencount++;
15 | }
16 | return evencount;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Add Natural Numbers/README.md:
--------------------------------------------------------------------------------
1 | Add Natural Numbers Problem Code: ADDNATRL
2 | Add problem to Todo list
3 |
4 | You are given a number N. Find the sum of all numbers from 1 to N.
5 |
6 | Input:
7 | First-line will contain the number N.
8 | Output:
9 | Print the answer in a single line.
10 |
11 | Constraints
12 | 1≤N≤109
13 | Sample Input 1:
14 | 4
15 | Sample Output 1:
16 | 10
17 | Sample Input 2:
18 | 8
19 | Sample Output 2:
20 | 36
21 | EXPLANATION:
22 | In the first example, (1 + 2 + 3 + 4) = 10.
23 | In the second example, (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8) = 36.
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/11. Jumping on the Clouds/Solution.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class Solution {
4 | public static void main(String[] args) {
5 | Scanner in = new Scanner(System.in);
6 | int n = in.nextInt();
7 | int c[] = new int[n];
8 | for(int i=0; i < n; i++){
9 | c[i] = in.nextInt();
10 | }
11 | int jumps = 0;
12 | for(int i = 0; i < n-1; i++){
13 | jumps++;
14 | if( i + 2 < n && c[i + 2] == 0){
15 | i++;
16 | }
17 | }
18 | System.out.println(jumps);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/05. 744. Find Smallest Letter Greater Than Target/Solution.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/find-smallest-letter-greater-than-target/
2 | class Solution {
3 | public char nextGreatestLetter(char[] letters, char target) {
4 | int start = 0;
5 | int end = letters.length - 1;
6 |
7 | while (start <= end) {
8 | int mid = start + (end - start) / 2;
9 | if (target < letters[mid]) {
10 | end = mid - 1;
11 | } else {
12 | start = mid + 1;
13 | }
14 | }
15 | return letters[start % letters.length];
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/16. 167. Two Sum II - Input array is sorted/optimal_approach.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
2 | class Solution {
3 | public int[] twoSum(int[] arr, int target) {
4 | int i = 0, j = arr.length - 1;
5 | while(i<=j) {
6 | if(arr[i] + arr[j] > target)
7 | j--; // need to decrease the sum
8 | else if(arr[i] + arr[j] < target)
9 | i++; // need to increase the sum
10 | else
11 | break;
12 | }
13 | return new int[]{i + 1, j + 1};
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/02. Algorithms/08. Strings/Reverse a String/reverse_string.py:
--------------------------------------------------------------------------------
1 | """
2 | The idea:
3 | 1. s = s.split(' ') -> splits the string with the space delim
4 | 2. [x for x in s.split(' ') if x != ""] -> filters leading, trailing or extra spaces (if any)
5 | 3. rev_s = list(reversed(s)) -> returns a reversed list
6 | 4. " ".join(rev_s) -> create a string again from the rev_s with the space delim
7 | """
8 | class Solution(object):
9 | def reverseWords(self, s):
10 | """
11 | :type s: str
12 | :rtype: str
13 | """
14 |
15 | return " ".join(list(reversed([x for x in s.split(' ') if x != ""])))
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/13. Daily Temperatures #739/Solution.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public int[] dailyTemperatures(int[] T) {
3 | int[] ans = new int[T.length];
4 | Stack stack = new Stack();
5 | for (int i = T.length - 1; i >= 0; --i) {
6 | while (!stack.isEmpty() && T[i] >= T[stack.peek()])
7 | stack.pop();
8 | if(stack.isEmpty())
9 | ans[i]=0;
10 | else
11 | ans[i]=stack.peek() - i;
12 |
13 | stack.push(i);
14 | }
15 | return ans;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/04. 75. Sort Colors OR Sort012/Solution.cpp:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/sort-colors/
2 | class Solution {
3 | public:
4 | void sortColors(vector& a) {
5 | int n = a.size();
6 | int low=0, mid=0, high=n-1;
7 | while(mid<=high) {
8 | if(a[mid]==0)
9 | {
10 | swap(a[mid++],a[low++]);
11 | }
12 | else if(a[mid]==1)
13 | {
14 | mid++;
15 | }
16 | else
17 | {
18 | swap(a[mid],a[high--]);
19 | }
20 | }
21 | }
22 | };
23 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Basic Operation/1. Stack using Array/README.md:
--------------------------------------------------------------------------------
1 | ## in JAVA
2 |
3 | **Step 1:** [Stack DS by Apna College(10min only)](https://youtu.be/JvuaAgDar1c)
4 |
5 | **Step 2:** [Implementation of Stack DS in CPP by Simple Snippets](https://youtu.be/08QSylWv6jM)
6 |
7 | **Step 3:** [See my Implemented code in java](https://github.com/thepranaygupta/Data-Structures-and-Algorithms/blob/main/02.%20Stack/Basic%20Operation/1.%20Stack%20using%20Array/Stack_Array.java)
8 |
9 | ## in C++
10 |
11 | **Step 1:** [Visualize here](https://www.youtube.com/watch?v=sFVxsglODoo)
12 |
13 | **Step 2:** [See my code](./stack_using_array.cpp)
14 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/01. Linear Search/README.md:
--------------------------------------------------------------------------------
1 | ### [Video Reference](https://youtu.be/_HRA37X8N_Q)
2 |
3 | ### Time Complexity: O(n), where n is the length of the array
4 |
5 | ```java
6 | //initial arguments will be: (array, element to be searched)
7 | static int linearSearch(int[] arr, int element) {
8 | if(arr.length == 0)
9 | return -1;
10 |
11 | for(int i = 0; i < arr.length; i++)
12 | {
13 | // find the first occurrence of the element and return it's position
14 | if(arr[i] == element) {
15 | return i + 1;
16 | }
17 | }
18 | // return -1 if the element is not found in the array
19 | return -1;
20 | }
21 | ```
22 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/18. Maximum Nesting Depth of the Parentheses #1614/solution.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | int maxDepth(string s) {
4 | stack a;
5 | int count=0;
6 | int max=0;
7 | for(int i=0;imax){
13 | max=count;
14 | }
15 | if(s[i]==')'){
16 | a.pop();
17 | count--;
18 | }
19 | }
20 | return max;
21 | }
22 | };
23 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/14. 69. Sqrt(x)/Solution.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/sqrtx/
2 | class Solution {
3 | public int mySqrt(int x) {
4 | if (x == 0)
5 | return 0;
6 | int left = 1;
7 | int right = x; // here right = x/2 is also correct
8 | while (true) {
9 | int mid = left + (right - left)/2;
10 | if (mid > x/mid) {
11 | right = mid - 1;
12 | } else {
13 | if (mid + 1 > x/(mid + 1))
14 | return mid;
15 | left = mid + 1;
16 | }
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/16. 167. Two Sum II - Input array is sorted/brute-force.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
2 | class Solution {
3 | public int[] twoSum(int[] arr, int target) {
4 | int answer[] = {0 , 0};
5 | for(int i = 0; i < arr.length; i++) {
6 | for(int j = i + 1; j < arr.length; j++) {
7 | if((arr[i] + arr[j]) == target) {
8 | answer[0] = i + 1;
9 | answer[1] = j + 1;
10 | break;
11 | }
12 | }
13 | }
14 | return answer;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/02. 344. Reverse String/README.md:
--------------------------------------------------------------------------------
1 | ## 344. Reverse String
2 | ## https://leetcode.com/problems/reverse-string/
3 |
4 | ## Using 2 pointer in recursion
5 |
6 | - ### Time Complexity: O(N/2)
7 | - ### Auxiliary Space Complexity: O(N/2)
8 |
9 | ```java
10 | class Solution {
11 | public void reverseString(char[] s) {
12 | reverse(s, 0, s.length - 1);
13 | }
14 |
15 | void reverse(char[] s, int i, int j) {
16 | if(i >= j)
17 | return;
18 | char temp = s[i];
19 | s[i] = s[j];
20 | s[j] = temp;
21 | reverse(s, i+1, j-1);
22 | }
23 | }
24 | ```
25 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/00. Maximum and Minimum Value in an Array/README.md:
--------------------------------------------------------------------------------
1 | ## Program to find the minimum (or maximum) element of an array
2 | ## https://www.geeksforgeeks.org/program-find-minimum-maximum-element-array/
3 |
4 | ```java
5 | // initial arguments: (array, 0, array.length)
6 | static int getMin(int[] arr, int i, int n) {
7 | if (n == 1)
8 | return arr[i];
9 | int min = Math.min(arr[i], getMin(arr, i + 1, n - 1));
10 | return min;
11 | }
12 |
13 | static int getMax(int[] arr, int i, int n) {
14 | if (n == 1)
15 | return arr[i];
16 | int max = Math.max(arr[i], getMax(arr, i + 1, n - 1));
17 | return max;
18 | }
19 | ```
20 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Find Second Larget/Solution.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main()
4 | {
5 | int a, b, c;
6 | // printf("Values: ");
7 | scanf("%d%d%d", &a, &b, &c);
8 | if(a>b && a>c)
9 | {
10 | if(b>c)
11 | printf("%d", b);
12 | else
13 | printf("%d", c);
14 | }
15 | else if(b>c && b>a)
16 | {
17 | if(c>a)
18 | printf("%d", c);
19 | else
20 | printf("%d", a);
21 | }
22 | else if(a>b)
23 | printf("%d", a);
24 | else
25 | printf("%d", b);
26 | return 0;
27 | }
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/07. Cyclically Rotate an Array/Brute_Force.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/rotate-array/
2 | class Solution {
3 | public void rotate(int[] a, int k) {
4 | int temp = a[0];
5 | for(int i = 1; i <= k; i++) {
6 | for(int j = 0; j < a.length; j++) {
7 | if(j == 0) {
8 | temp = a[0];
9 | a[0] = a[a.length - 1];
10 | }
11 | else {
12 | int temp2 = a[j];
13 | a[j] = temp;
14 | temp = temp2;
15 | }
16 | }
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/07. Cyclically Rotate an Array/cyclic_rotate.cpp:
--------------------------------------------------------------------------------
1 | using namespace std;
2 | int main()
3 | {
4 | int t;
5 | cin >> t;
6 | while(t--)
7 | {
8 | int N,D,i;
9 | cin >> N;
10 | cin >> D;
11 | int a[N],b[N];
12 | for(i=0;i> a[i];
14 | for(i=0;i= k) { //if height is not negative
10 | max = Math.max(arr[i-1]+k, arr[n-1]-k);
11 | min = Math.min(arr[i]-k, arr[0]+k);
12 | ans = Math.min(ans, max-min);
13 | }
14 | }
15 | return ans;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/21. Rotate the 2D Array/Rotate the 2D array.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | int main()
6 | {
7 | int a[3][3]={1,2,3,4,5,6,7,8,9};
8 | int i,j;
9 | cout<<("Original Array\n");
10 | for(i=0;i<3;i++)
11 | {
12 | for(j=0;j<3;j++)
13 | {
14 | cout<<("%d ",a[i][j]);
15 | }
16 | cout<<("\n");
17 | }
18 | cout<<("Rotate Matrix by 90 degrees\n");
19 | for(i=0;i<3;i++)
20 | {
21 | for(j=2;j>=0;j--)
22 | {
23 | cout<<("%d ",a[j][i]);
24 | }
25 | cout<<("\n");
26 | }
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/07. Make The String Great #1544/Solution_self.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public String makeGood(String s) {
3 | Stack stack = new Stack();
4 | for(int i=0;i 0 ? Math.max(maxSum, total - minSum) : maxSum;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/00. Sum triangle from array/Solution.java:
--------------------------------------------------------------------------------
1 | // TODO: https://www.geeksforgeeks.org/sum-triangle-from-array/
2 |
3 | package recursion;
4 |
5 | import java.util.Arrays;
6 |
7 | public class Sum_triangle_from_array {
8 |
9 | public static void main(String[] args) {
10 | int arr[] = { 1, 2, 3, 4, 5 };
11 | int n = arr.length;
12 | sum(arr, n);
13 | }
14 |
15 | static void sum(int[] arr, int n) {
16 | if (n < 1)
17 | return;
18 | int temp[] = new int[n - 1];
19 | for (int i = 0; i < n - 1; i++) {
20 | temp[i] = arr[i] + arr[i + 1];
21 | }
22 | sum(temp, n - 1);
23 | System.out.println(Arrays.toString(arr));
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Alternative Square Pattern/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main() {
5 | int n;
6 | cin>>n;
7 | int sumi = 0, sumj = 10;
8 | for(int i=1; i<=n; i++){
9 | if(i%2!=0){
10 | for(int j=sumi+1; j<=sumi+5; j++){
11 | cout<=sumj-4; j--){
17 | cout< arr[mid]) {
15 | start = mid + 1;
16 | } else if (target < arr[mid]) {
17 | end = mid - 1;
18 | }
19 | }
20 | return start;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/04. Recursive Insertion Sort/Solution.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/sort-an-array/
2 | class Solution {
3 | public int[] sortArray(int[] nums)
4 | {
5 | insertionSortRecursive(nums, nums.length);
6 | return nums;
7 | }
8 | static void insertionSortRecursive(int arr[], int n)
9 | {
10 | if (n <= 1)
11 | return;
12 | insertionSortRecursive( arr, n-1 );
13 | int last = arr[n-1];
14 | int j = n-2;
15 | while (j >= 0 && arr[j] > last)
16 | {
17 | arr[j+1] = arr[j];
18 | j--;
19 | }
20 | arr[j+1] = last;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/01. ReverseLL/solution 1.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for singly-linked list.
3 | * class ListNode {
4 | * public int val;
5 | * public ListNode next;
6 | * ListNode(int x) { val = x; next = null; }
7 | * }
8 | */
9 | public class Solution {
10 | public ListNode reverseList(ListNode A) {
11 | ListNode curr=A;
12 | ListNode previous=null;
13 | ListNode nex=null;
14 | while(curr!=null) {
15 | nex=curr.next;
16 | curr.next=previous;
17 | previous=curr;
18 | curr=nex;
19 | }
20 | return previous;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/05. Move all negative numbers to one side/Solution.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 |
3 | public class NegativePositiveArray {
4 |
5 | public static void main(String[] args) {
6 | int arr[] = { -12, 11, -13, -5, 6, -7, 5, -3, -6 };
7 | int n = arr.length;
8 |
9 | int low = 0;
10 | int mid = 0;
11 | while (mid <= n - 1) {
12 | if (arr[mid] < 0) {
13 | if (mid != low)
14 | swap(arr, mid, low);
15 | low++;
16 | mid++;
17 | } else
18 | mid++;
19 | }
20 | System.out.println(Arrays.toString(arr));
21 | }
22 |
23 | public static void swap(int a[], int i, int j) {
24 | int temp = a[i];
25 | a[i] = a[j];
26 | a[j] = temp;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/LeetCode Questions/01. Reverse Linked List #206/Solution.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 |
12 | class Solution {
13 | public ListNode reverseList(ListNode head) {
14 | if (head == null || head.next == null)
15 | return head;
16 | ListNode p = reverseList(head.next);
17 | head.next.next = head;
18 | head.next = null;
19 | return p;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/01. Linear Search/Linear_Search.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | void linear(int a[], int n) {
4 | int t = -1;
5 | for (int i = 0; i < 5; i++) {
6 | if (a[i] == n) {
7 | cout << "Element found at position: " << i + 1 << endl;
8 | t = 0;
9 | break;
10 | }
11 | }
12 | if (t == -1) {
13 | cout << "No Element Found" << endl;
14 | }}
15 | int main() {
16 | int arr[5];
17 | cout << "Please enter 5 elements of the Array" << endl;
18 | for (int i = 0; i < 5; i++) {
19 | cin >> arr[i];
20 | }
21 | cout << "Please enter an element to search" << endl;
22 | int num;
23 | cin >> num;
24 | linear(arr, num);
25 | return 0;
26 | }
27 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/01. 509. Fibonacci Number/Fibonacci_SeriesUptoN.cpp:
--------------------------------------------------------------------------------
1 |
2 | #include
3 | using namespace std;
4 |
5 | int main()
6 | {
7 |
8 | int limit, first=0, second=1, next, num;
9 | cout <<“Enter the limit of Fibonacci series”<> num;
12 | cout << “First “< st = new Stack();
6 | for(char c : s.toCharArray()){
7 | if(c=='(' || c=='[' || c=='{')
8 | st.push(c);
9 | else if(c==']' && !st.isEmpty() && st.peek()=='[') st.pop();
10 | else if(c==')' && !st.isEmpty() && st.peek()=='(') st.pop();
11 | else if(c=='}' && !st.isEmpty() && st.peek()=='{') st.pop();
12 | else st.push(c);
13 | }
14 | return st.isEmpty();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/05. 744. Find Smallest Letter Greater Than Target/README.md:
--------------------------------------------------------------------------------
1 | ## https://leetcode.com/problems/find-smallest-letter-greater-than-target/
2 |
3 | ## [Video Tutorial](https://youtu.be/W9QJ8HaRvJQ?t=2815)
4 |
5 | ```java
6 | class Solution {
7 | public char nextGreatestLetter(char[] letters, char target) {
8 | int start = 0;
9 | int end = letters.length - 1;
10 |
11 | while (start <= end) {
12 | int mid = start + (end - start) / 2;
13 | if (target < letters[mid]) {
14 | end = mid - 1;
15 | } else {
16 | start = mid + 1;
17 | }
18 | }
19 | return letters[start % letters.length];
20 | }
21 | }
22 | ```
23 |
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/02. InsertionSort/Insertion_Sort.java:
--------------------------------------------------------------------------------
1 | // TODO Sort an Array using Insertion Sort Technique
2 | package array.sorting;
3 |
4 | import java.util.Arrays;
5 |
6 | public class Insertion_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 | for (int i = 1; i < n; i++) {
14 | int temp = arr[i];
15 | int j = i - 1;
16 | while (j >= 0 && arr[j] > temp) {
17 | arr[j + 1] = arr[j];
18 | j--;
19 | }
20 | arr[j + 1] = temp;
21 | }
22 |
23 | System.out.println("Array after Sorting: " + Arrays.toString(arr));
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/08. Factorial of N/README.md:
--------------------------------------------------------------------------------
1 | ### Intuition:
2 | - Base Case: If array size is less than 0, return.
3 | - Base Case: If array size is 1 or smaller, return 1.
4 | - Recursion to N elements. Retrun last values after Multiplication.
5 |
6 | ### Time Complexity: O(n)
7 |
8 | ### Space Complexity: O(N)
9 | ```Python
10 | def recur_factorial(n):
11 | if n == 1:
12 | return n
13 | else:
14 | return n*recur_factorial(n-1)
15 |
16 | num = 7
17 |
18 |
19 | if num < 0:
20 | print("Sorry, factorial does not exist for negative numbers")
21 | elif num == 0:
22 | print("The factorial of 0 is 1")
23 | else:
24 | print("The factorial of", num, "is", recur_factorial(num))
25 | ```
26 |
27 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/02. Infix to Prefix/README.md:
--------------------------------------------------------------------------------
1 | ### **Step 1:** [Learn manual conversion of Infix to Postfix/Prefix by GFS.](https://youtu.be/q75VAGSwL0U)
2 |
3 | ### **Step 2:** [Learn Rules && Visualize pseudo code to convert Infix to prefix using stack.](https://youtu.be/gmlVZ68KRD8)
4 |
5 | 
6 |
7 | ### **Step 3:** [Implementation of pseudo code in C++ to convert Infix to prefix using stack.](https://youtu.be/-vZA4qdDxAg)
8 |
9 | ### **Step 4:** [See my code here in java to convert Infix to prefix using stack.](./InfixPrefix.java)
10 |
11 | **NOTE:** [You must know Infix to Postfix before doing this.](../01.%20Infix%20to%20Postfix)
12 |
13 |
14 | Thank You
15 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/15. 278. First Bad Version/README.md:
--------------------------------------------------------------------------------
1 | ## 278. First Bad Version
2 | ## https://leetcode.com/problems/first-bad-version/
3 |
4 | ### using binary search
5 | ```java
6 | /* The isBadVersion API is defined in the parent class VersionControl.
7 | boolean isBadVersion(int version); */
8 |
9 | public class Solution extends VersionControl {
10 | public int firstBadVersion(int n) {
11 | int start = 0, end = n;
12 | while(start < end) {
13 | int mid = start + (end - start)/2;
14 | if(isBadVersion(mid) == false)
15 | start = mid + 1;
16 | else
17 | end = mid;
18 | }
19 | return start;
20 | }
21 | }
22 | ```
23 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Reverse Star Pattern/README.md:
--------------------------------------------------------------------------------
1 | Reverse Star Pattern Problem Code: REVSTRPT
2 | You're given a number N. Print the first N lines of the below-given pattern.
3 |
4 | *
5 | **
6 | ***
7 | ****
8 | *****
9 | Input:
10 | First-line will contain the number N.
11 | Output:
12 | Print the first N lines of the given pattern.
13 |
14 | Constraints
15 | 1≤N≤200
16 | Sample Input 1:
17 | 4
18 | Sample Output 1:
19 | *
20 | **
21 | ***
22 | ****
23 | Sample Input 2:
24 | 2
25 | Sample Output 2:
26 | *
27 | **
28 | EXPLANATION:
29 | In the first example, we'll print the first 4 lines of the given pattern.
30 | In the second example, we'll print the first 2 lines of the given pattern
--------------------------------------------------------------------------------
/02. Algorithms/06. Number Theory/Sieve Of Eratosthenes/SieveOfEratosthenes.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int N = 1000000;
5 | bool sieve[1000001];
6 |
7 | void createSieve() {
8 | for (int i = 2; i < N; i++) {
9 | sieve[i] = true;
10 | }
11 | for (int i = 2; i * i <= N; i++) {
12 | if (sieve[i]) {
13 | for (int j = i * i; j <= N; j += i) {
14 | sieve[j] = false;
15 | }
16 | }
17 | }
18 | }
19 |
20 | int main() {
21 | #ifndef ONLINE_JUDGE
22 | freopen("input.txt", "r", stdin);
23 | freopen("output.txt", "w", stdout);
24 | #endif
25 |
26 | createSieve();
27 | int n;
28 | cin >> n;
29 |
30 | if (sieve[n])
31 | cout << "Prime";
32 | else
33 | cout << "Not Prime";
34 |
35 | return 0;
36 | }
37 |
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/07. Priority Queue/KLargestElement.java:
--------------------------------------------------------------------------------
1 | class Solution
2 | {
3 | //Function to return k largest elements from an array.
4 | public static ArrayList kLargest(int a[], int n, int k)
5 | {
6 | PriorityQueue pq=new PriorityQueue<>();
7 | for(int i=0;i ans =new ArrayList<>(pq);
18 | Collections.sort(ans,Collections.reverseOrder());
19 | return ans;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/11. Bitonic Sort/README.md:
--------------------------------------------------------------------------------
1 | [Bitonic Sort Video Reference](https://www.youtube.com/watch?v=uEfieI0MumY)
2 |
3 |
4 | -Bitonic sort does O(n Log 2n) comparisons.
5 |
6 | -The number of comparisons done by Bitonic sort are more than popular sorting algorithms like Merge Sort [ does O(nLogn) comparisons], but Bitonice sort is better for parallel implementation because we always compare elements in predefined sequence and the sequence of comparison doesn’t depend on data. Therefore it is suitable for implementation in hardware and parallel processor array.
7 |
8 | -Bitonic Sort must be done if number of elements to sort are 2^n. The procedure of bitonic sequence fails if the number of elements are not in the aforementioned quantity precisely.
9 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Reverse Me/README.md:
--------------------------------------------------------------------------------
1 | Reverse Me Problem Code: REVMEE
2 | Add problem to Todo list
3 | Submit
4 | You are given a list of N integers and you need to reverse it and print the reversed list in a new line.
5 |
6 | Input:
7 | First-line will contain the number N.
8 | Second line will contain N space-separated integers.
9 | Output:
10 | Print the reversed list in a single line.
11 |
12 | Constraints
13 | 1≤N,Ai≤105
14 | Sample Input 1:
15 | 4
16 | 1 3 2 4
17 | Sample Output 1:
18 | 4 2 3 1
19 | Sample Input 2:
20 | 2
21 | 9 8
22 | Sample Output 2:
23 | 8 9
24 | EXPLANATION:
25 | In the first example, the reverse of the [1,3,2,4] is [4,2,3,1].
26 | In the second example, the reverse of [9,8] is [8,9]
--------------------------------------------------------------------------------
/01. DataStructures/07. Set/SubarrayWith0Sum.java:
--------------------------------------------------------------------------------
1 | // Problem Link: https://practice.geeksforgeeks.org/problems/subarray-with-0-sum-1587115621/1#
2 | // Editorial Link: https://youtu.be/PSpuM9cimxA
3 |
4 | class Solution{
5 | //Function to check whether there is a subarray present with 0-sum or not.
6 | static boolean findsum(int arr[],int n)
7 | {
8 | Set set = new HashSet<>();
9 | int sum=0;
10 | boolean found = false;
11 | for(int ele: arr)
12 | {
13 | set.add(sum);
14 | sum += ele;
15 | if(set.contains(sum))
16 | {
17 | found = true;
18 | break;
19 | }
20 | }
21 | return found;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/03. Kth Node From Middle/solution 3.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for singly-linked list.
3 | * class ListNode {
4 | * public int val;
5 | * public ListNode next;
6 | * ListNode(int x) { val = x; next = null; }
7 | * }
8 | */
9 | public class Solution {
10 | public int solve(ListNode A, int B) {
11 | ListNode t=A;int n=0;
12 | while(t!=null){
13 | n++;
14 | t=t.next;
15 | }
16 | int mn=(n/2)+1;
17 | t=A;
18 | int nd=mn-B;
19 | if(nd<1){return -1;}
20 | else{
21 | for(int i=1;i map=new HashMap(); // hash map is use to store greatestRightSide element(as a value) for each element in nums2(as a key)
4 | Stack st=new Stack();
5 |
6 | for(int ele:nums2){
7 | while(!st.isEmpty() && ele>st.peek()){
8 | map.put(st.pop(),ele);
9 | }
10 | st.push(ele);
11 | }
12 |
13 | for(int i=0;i
2 | Click Here
3 |
4 | Problem:Given a binary tree, flatten it into linked list in-place.After flattening, left of each node should point to NULL and right should contain next node in preorder.
5 |
6 | Algorithm to solve:
7 |
8 | - Recursively flatten the left and the right subtree
9 | - Store the left and the right tail
10 | - Start the right subtree in temp and make left subtree as right subtree
11 | - Join right subtree with left tail
12 | - Return right tail
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/01. DataStructures/04. Tree/05. Heap/Leetcode Questions/02. K Closest Points to Origin #973/Solution.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public int[][] kClosest(int[][] points, int k) {
3 | PriorityQueue heap = new PriorityQueue<>((a, b) -> {
4 | int d1 = a[0] * a[0] + a[1] * a[1];
5 | int d2 = b[0] * b[0] + b[1] * b[1];
6 | return d2 - d1;
7 | });
8 | for (int[] point : points) {
9 | heap.add(point);
10 | if (heap.size() > k) {
11 | heap.remove();
12 | }
13 | }
14 | int[][] ans = new int[k][2];
15 | int i = 0;
16 | while (k-- > 0) {
17 | ans[i++] = heap.remove();
18 | }
19 | return ans;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/15. Online Stock Span #901/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | vector stockSpan(vector& arr){
5 |
6 | int n = arr.size();
7 | vector ans(n);
8 | stack s;
9 |
10 | for (int i = 0 ;i < n ; i++)
11 | {
12 | while (!s.empty() && arr[s.top()] <= arr[i])
13 | s.pop();
14 |
15 | if(s.empty())
16 | ans[i]= i+1;
17 | else
18 | ans[i]= i - s.top();
19 |
20 | s.push(i);
21 | }
22 | return ans;
23 | }
24 |
25 | int main()
26 | {
27 | vector arr = {100,80,60,70,60,75,85};
28 | vector out = stockSpan(arr);
29 |
30 | for(auto x : out){
31 | cout<
2 | using namespace std;
3 | int main()
4 | {
5 | int arr[100], tot, i, j, temp;
6 | cout<<"Enter the Size for Array: ";
7 | cin>>tot;
8 | cout<<"Enter "<>arr[i];
11 | cout<<"\nThe Original Array is:\n";
12 | for(i=0; i 0){
15 | digitcount++;
16 | element /= 10;
17 | }
18 | if(digitcount%2 == 0)
19 | evencount++;
20 | }
21 | return evencount;
22 | }
23 | }
24 | ```
25 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/04. Ceil in a Sorted Array/README.md:
--------------------------------------------------------------------------------
1 | ### [Video Tutorial](https://youtu.be/W9QJ8HaRvJQ?t=625)
2 |
3 | ```java
4 | class Solution{
5 |
6 | // Function to find floor of x
7 | // arr: input array
8 | // n is the size of array
9 | static int findFloor(long arr[], int n, long target){
10 |
11 | int start = 0;
12 | int end = n - 1;
13 | while (start <= end) {
14 | int mid = start + (end - start) / 2;
15 | if(arr[mid] == target)
16 | return mid;
17 | else if (target > arr[mid]) {
18 | start = mid + 1;
19 | } else if (target < arr[mid]) {
20 | end = mid - 1;
21 | }
22 | }
23 | return start;
24 | }
25 | }
26 | ```
27 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/07. Cyclically Rotate an Array/Optimal_Solution.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/rotate-array/
2 | class Solution {
3 | public void rotate(int[] arr, int k) {
4 | int len = arr.length;
5 | k = k % len;
6 | // reverse part1
7 | reverse(arr, 0, len - k - 1);
8 |
9 | // reverse part2
10 | reverse(arr, len - k, len - 1);
11 |
12 | // reverse the whole array
13 | reverse(arr, 0, len-1);
14 | }
15 |
16 | public void reverse(int[] arr, int li, int ri) {
17 | while(li < ri) {
18 | int temp = arr[li];
19 | arr[li] = arr[ri];
20 | arr[ri] = temp;
21 | li++;
22 | ri--;
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/19. Wave Array/readme.md:
--------------------------------------------------------------------------------
1 | # WAVE ARRAY
2 |
3 |
4 | Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array and return it
5 | In other words, arrange the elements into a sequence such that arr[1] >= arr[2] <= arr[3] >= arr[4] <= arr[5].....
6 |
7 | ### Example 1:
8 |
9 | Input:
10 | n = 5
11 | arr[] = {1,2,3,4,5}
12 | Output: 2 1 4 3 5
13 |
14 | Explanation: Array elements after
15 | sorting it in wave form are
16 | 2 1 4 3 5.
17 |
18 | ### Example 2:
19 |
20 | Input:
21 | n = 6
22 | arr[] = {2,4,7,8,9,10}
23 | Output: 4 2 8 7 10 9
24 |
25 | Explanation: Array elements after
26 | sorting it in wave form are
27 | 4 2 8 7 10 9.
28 |
29 | [question link](https://practice.geeksforgeeks.org/problems/wave-array-1587115621/1)
30 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/03. Floor in a Sorted Array/Solution.java:
--------------------------------------------------------------------------------
1 | // https://practice.geeksforgeeks.org/problems/floor-in-a-sorted-array-1587115620/1#
2 |
3 | class Solution{
4 |
5 | // Function to find floor of x
6 | // arr: input array
7 | // n is the size of array
8 | static int findFloor(long arr[], int n, long target){
9 |
10 | int start = 0;
11 | int end = n - 1;
12 | while (start <= end) {
13 | int mid = start + (end - start) / 2;
14 | if(arr[mid] == target)
15 | return mid;
16 | else if (target > arr[mid]) {
17 | start = mid + 1;
18 | } else if (target < arr[mid]) {
19 | end = mid - 1;
20 | }
21 | }
22 | return end;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/01. DataStructures/05. ArrayList/AddOddAndEvenNums/Solution.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 |
3 | class Solution {
4 | public int[] sortArrayByParityII(int[] A) {
5 | ArrayList evens = new ArrayList();
6 | ArrayList odds = new ArrayList();
7 | for(int i = 0; i < A.length; i++){
8 | if(A[i]%2==0)
9 | evens.add(A[i]);
10 | else
11 | odds.add(A[i]);
12 | }
13 | int index = 0;
14 | for(Integer val: evens){
15 | A[index] = val;
16 | index += 2;
17 | }
18 | index = 1;
19 | for(Integer val: odds){
20 | A[index] = val;
21 | index += 2;
22 | }
23 | return A;
24 | }
25 | }
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/01. 509. Fibonacci Number/README.md:
--------------------------------------------------------------------------------
1 | ## 509. Fibonacci Number
2 | ## https://leetcode.com/problems/fibonacci-number/
3 |
4 | ### Time Complexity: O(2^N)
5 | ### Auxiliary Space Complexity: O(N)
6 | ```java
7 | class Solution {
8 | public int fib(int n) {
9 | if (n < 2)
10 | return n;
11 | return (fib(n - 1) + fib(n - 2));
12 | }
13 | }
14 | ```
15 |
16 | ## Print Fibonacci Number Series upto N
17 | ```java
18 | public class Fibonacci {
19 |
20 | public static void main(String[] args) {
21 | int n = 10;
22 | for (int i = 0; i <= n; i++)
23 | System.out.println(fibo(i));
24 | }
25 |
26 | static int fibo(int n) {
27 | if (n < 2) {
28 | return n;
29 | }
30 | return (fibo(n - 1) + fibo(n - 2));
31 | }
32 | }
33 | ```
34 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Range Odd/README.md:
--------------------------------------------------------------------------------
1 | Range Odd Problem Code: RNGEODD
2 | Add problem to Todo list
3 | Submit
4 | You're given two numbers L and R. Print all odd numbers between L and R (both inclusive) in a single line separated by space, in ascending (increasing) order.
5 |
6 | Input:
7 | First-line will contain two numbers L and R.
8 | Output:
9 | Print all odd numbers in a single line separated by space, in ascending (increasing) order.
10 |
11 | Constraints
12 | 1≤L levelOrder(Node* node)
6 | {
7 | vectorans;
8 | if(node == NULL) return ans;
9 | queueq;
10 | q.push(node);
11 | while(!q.empty()){
12 | int size = q.size();
13 | for(int i = 0 ; i < size ; i++){
14 | Node* curr = q.front();
15 | q.pop();
16 | if(curr->left!=NULL) q.push(curr->left);
17 | if(curr->right!=NULL) q.push(curr->right);
18 | ans.push_back(curr->data);
19 | }
20 | }
21 | return ans;
22 | }
23 | };
24 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/02. Find the maximum and minimum element in an array/find_max_min.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | int main ()
4 | {
5 | int arr[10], n, i, max, min;
6 | cout << "Enter the size of the array : ";
7 | cin >> n;
8 | cout << "Enter the elements of the array : ";
9 | for (i = 0; i < n; i++)
10 | cin >> arr[i];
11 | max = arr[0];
12 | for (i = 0; i < n; i++)
13 | {
14 | if (max < arr[i])
15 | max = arr[i];
16 | }
17 | min = arr[0];
18 | for (i = 0; i < n; i++)
19 | {
20 | if (min > arr[i])
21 | min = arr[i];
22 | }
23 | cout << "Largest element : " << max << endl;
24 | cout << "Smallest element : " << min;
25 | return 0;
26 | }
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/00. Sum triangle from array/README.md:
--------------------------------------------------------------------------------
1 | ## Sum triangle from array
2 | ## https://www.geeksforgeeks.org/sum-triangle-from-array/
3 |
4 | ### Approach:
5 | + At each iteration create a new array which contains the Sum of consecutive elements in the array passes as parameter.
6 | + Make a recursive call and pass the newly created array in the previous step.
7 | + While back tracking print the array (for printing in reverse order).
8 |
9 | ```java
10 | // initial arguments: (arr, arr.length)
11 | static void sum(int[] arr, int n) {
12 | if(n < 1)
13 | return;
14 | int temp[] = new int[n-1];
15 | for(int i = 0; i < n-1; i++) {
16 | temp[i] = arr[i] + arr[i + 1];
17 | }
18 | sum(temp, n - 1);
19 | System.out.println(Arrays.toString(arr));
20 | }
21 | ```
22 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Is Both or Not/README.md:
--------------------------------------------------------------------------------
1 | Is Both Or Not | Problem Code: ISBOTH
2 |
3 | You're given a number N. If N is divisible by 5 or 11 but not both then print "ONE"(without quotes). If N is divisible by both 5 and 11 then print "BOTH"(without quotes). If N is not divisible by 5 or 11 then print "NONE"(without quotes).
4 |
5 | Input:
6 | First-line will contain the number N
7 | Output:
8 | Print the answer in a newline.
9 | Constraints
10 | 1≤N≤103
11 |
12 | Sample Input 1:
13 | 50
14 | Sample Output 1:
15 | ONE
16 | Sample Input 2:
17 | 110
18 |
19 | EXPLANATION:
20 | In the first example, 50 is divisible by 5, but not 11.
21 | In the second example, 110 is divisible by both 5 and 11.
22 | In the third example, 16 is not divisible by 5 or 11.
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Sum Is Everywhere/README.md:
--------------------------------------------------------------------------------
1 | Sum Is Everywhere Problem Code: SUMEVOD
2 | Add problem to Todo list
3 |
4 | You are given a number N and find the sum of the first N odd and even numbers in a line separated by space. All even and odd numbers should be greater than 0.
5 |
6 | Input:
7 | First-line will contain the number N.
8 | Output:
9 | Print the sum of the first N odd and even numbers in a line separated by space.
10 |
11 | Constraints
12 | 1≤N≤106
13 | Sample Input 1:
14 | 4
15 | Sample Output 1:
16 | 16 20
17 | Sample Input 2:
18 | 1
19 | Sample Output 2:
20 | 1 2
21 | EXPLANATION:
22 | In the first example, (1 + 3 + 5 + 7) = 16 and (2 + 4 + 6 + 8) = 20.
23 | In the second example, only one odd that is 1 and only one even that is 2.
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/11. Rotate LL by K place/solution_11.py:
--------------------------------------------------------------------------------
1 | class Solution:
2 |
3 | def rotateRight(self, A, B):
4 | aa = []
5 | if A == None:
6 | return A
7 | while(A):
8 | aa.append(A.val)
9 | A = A.next
10 | bb = []
11 | bc = []
12 | B = (B % len(aa))
13 | i = len(aa)-B
14 | while(i < len(aa)):
15 | bb.append(aa[i])
16 | i += 1
17 | i = 0
18 | while(i < len(aa)-B):
19 | bb.append(aa[i])
20 | i += 1
21 | head = ListNode(bb[0])
22 | cur = head
23 | for i in range(1, len(bb)):
24 | cur.next = ListNode(bb[i])
25 | cur = cur.next
26 | return head
27 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/04. 75. Sort Colors OR Sort012/Optimal_Solution.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/sort-colors/
2 | class Solution {
3 | public void sortColors(int[] arr) {
4 | int low = 0, mid = 0, high = arr.length - 1;
5 | while(mid <= high) {
6 | if(arr[mid] == 0) {
7 | swap(arr, low, mid);
8 | low++;
9 | mid++;
10 | }
11 | else if(arr[mid] == 1) {
12 | mid++;
13 | }
14 | else if(arr[mid] == 2) {
15 | swap(arr, high, mid);
16 | high--;
17 | }
18 | }
19 | }
20 | public void swap(int[] arr, int i, int j) {
21 | int temp = arr[i];
22 | arr[i] = arr[j];
23 | arr[j] = temp;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/08. Kadane's Algorithm/53. Maximum Subarray/README.md:
--------------------------------------------------------------------------------
1 | ## 53. Maximum Subarray
2 | ## https://leetcode.com/problems/maximum-subarray/
3 |
4 | ### [Video Tutorial](https://youtu.be/VMtyGnNcdPw)
5 | 
6 | ```java
7 | class Solution {
8 | public int maxSubArray(int[] arr) {
9 | int csum = arr[0]; //current sum
10 | int osum = arr[0]; //overall sum
11 | for(int i = 1; i < arr.length; i++) {
12 | if(csum > 0)
13 | csum += arr[i];
14 | else
15 | csum = arr[i];
16 |
17 | if(csum > osum)
18 | osum = csum;
19 | }
20 | return osum;
21 | }
22 | }
23 | ```
24 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/11. Jumping on the Clouds/README.md:
--------------------------------------------------------------------------------
1 | ## Jumping on the Clouds
2 | ## https://www.hackerrank.com/challenges/jumping-on-the-clouds/
3 |
4 |
5 | ```java
6 | int jumps = 0;
7 | for(int i = 0; i < n-1; i++){
8 | if( i + 2 < n && c[i + 2] == 0){
9 | i++;
10 | }
11 | jumps++;
12 | }
13 | System.out.println(jumps);
14 | ```
15 |
16 | ## Jumping on the Clouds: Revisited
17 | ## https://www.hackerrank.com/challenges/jumping-on-the-clouds-revisited/problem
18 |
19 | ```java
20 | public static void jumpingOnClouds(int c[],int k)
21 | {
22 | int curr=0;
23 | int e=100;
24 | int len=c.length;
25 | do{
26 | curr=(curr+k)%len;
27 | e--;
28 | if(c[curr]==1)
29 | e-=2;
30 | }while(curr!=0);
31 | System.out.println(e);
32 | }
33 | ```
34 |
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/01. Bubble Sort/BubbleSort.java:
--------------------------------------------------------------------------------
1 | // TODO Sort an Array using Bubble-Sort Algorithm
2 | package array.sorting;
3 |
4 | import java.util.Arrays;
5 |
6 | public class BubbleSort {
7 | public static void main(String[] args) {
8 | int arr[] = { 4, 21, 1, 9, -12, 5, 32, 2, 6 };
9 | System.out.println("Array before Sorting: " + Arrays.toString(arr));
10 |
11 | int n = arr.length;
12 | for (int i = 0; i < n - 1; i++) {
13 | boolean swapped = false;
14 | for (int j = 0; j < n - 1 - i; j++) {
15 | if (arr[j + 1] < arr[j]) {
16 | swapped = true;
17 | int temp = arr[j];
18 | arr[j] = arr[j+1];
19 | arr[j+1] = temp;
20 | }
21 | }
22 | if (!swapped)
23 | break;
24 | }
25 | System.out.println("Array after Sorting: " + Arrays.toString(arr));
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/07. Memoization/01. Game of Chef/Solution.cpp:
--------------------------------------------------------------------------------
1 | // https://www.codechef.com/problems/CHGM1
2 | // Game of Chef
3 |
4 | #include
5 | using namespace std;
6 |
7 | int kadaneAlgo(int n, int a[]){
8 | int maxTillNow=0, maxFinal=0;
9 | for(int i=0;imaxFinal)
12 | maxFinal=maxTillNow;
13 | if(maxTillNow<0)
14 | maxTillNow=0;
15 | }
16 | return maxFinal;
17 | }
18 |
19 | int main(){
20 | int k;
21 | cin>>k;
22 | for(int i=0;i>n;
25 | int a[n];
26 | for(int j=0;j>a[j];
28 | int sum=kadaneAlgo(n,a);
29 | cout<-1 && st[top]==currCh)
9 | top--;
10 | else{
11 | top++;
12 | st[top]=currCh;
13 | }
14 | }
15 | return new String(st,0,top+1);
16 | }
17 | }
18 |
19 | /** Logic Used here is :-
20 |
21 | for(i=0 to i=s.length()){
22 | if(st.peek()==S.charAt(i))
23 | POP;
24 | else
25 | PUSH;
26 | }
27 | return stackAsString
28 | **/
29 |
--------------------------------------------------------------------------------
/02. Algorithms/09. Checksum Calc/ones_complement_checksum16bit.cpp:
--------------------------------------------------------------------------------
1 | //16bit binary adder with carry
2 | #include
3 | using namespace std;
4 |
5 | uint16_t b16_adder(uint16_t a,uint16_t b)
6 | {
7 | uint32_t total32=0;
8 | total32=a+b;
9 | uint16_t rem=(total32>>16);
10 | uint16_t total16=(total32);
11 | uint16_t real_total=(total16+rem);
12 |
13 | return real_total;
14 | }
15 |
16 | //pointer to data array and array size
17 | uint16_t checksum_ans16(uint16_t *arr,uint data_size)
18 | {
19 | uint sz=((data_size%2)>0?data_size+1:data_size);
20 | uint16_t current_ans=0;
21 | int x=0;
22 | while(x<(sz/2))
23 | {
24 | uint16_t temp_ans=0;
25 | uint16_t no=(arr[x]);
26 | temp_ans=b16_adder(current_ans,no);
27 | current_ans=(uint16_t)temp_ans;
28 | }
29 | x++;
30 | return (uint16_t)(~current_ans);
31 | }
32 |
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/002. Climbing Stairs/code.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | /*vectordp ;
4 |
5 | Solution(): dp(46,-1){
6 | dp[0]=1 ;
7 | }
8 |
9 | int climbStairs(int n) {
10 |
11 | if(n<0)
12 | return 0;
13 |
14 | if(dp[n]!=-1)
15 | return dp[n] ;
16 |
17 | return dp[n] = climbStairs(n-1)+climbStairs(n-2);
18 | }*/
19 |
20 | int climbStairs(int n)
21 | {
22 | if(n==1)
23 | return 1 ;
24 |
25 | int dp[n+1];
26 |
27 | dp[0] = 1;
28 | dp[1] = 1;
29 |
30 | for(int i=2;i<=n;i++)
31 | {
32 | dp[i] = dp[i-1]+dp[i-2] ;
33 | }
34 |
35 | return dp[n] ;
36 | }
37 | };
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/02. InsertionSort/insertionSort.py:
--------------------------------------------------------------------------------
1 | # creating a function for insertion
2 | def insertion_sort(list1):
3 |
4 | # Outer loop to traverse through 1 to len(list1)
5 | for i in range(1, len(list1)):
6 |
7 | value = list1[i]
8 |
9 | # Move elements of list1[0..i-1], that are greater than value, to one position ahead of their current position
10 | j = i - 1
11 | while j >= 0 and value < list1[j]:
12 | list1[j + 1] = list1[j]
13 | j -= 1
14 | list1[j + 1] = value
15 | return list1
16 | # Driver code to test above
17 |
18 | list1 = [25,6,3,9,1,17]
19 | print("The unsorted list is:", list1)
20 |
21 | print("The sorted list1 is:", insertion_sort(list1))
22 |
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/006. House Robber II/code.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 | int rob(vector& nums) {
4 | int n = nums.size() ;
5 | if(n==1)
6 | return nums[0] ;
7 | if(n==2)
8 | return max(nums[0],nums[1]) ;
9 |
10 | int dp1[n-1] ;
11 |
12 | dp1[0] = nums[0] ;
13 | dp1[1] = max(nums[0],nums[1]) ;
14 | for(int i=2;i 0) {
22 | x = x & (x - 1);
23 | count++;
24 | }
25 | return count;
26 | }
27 | ```
--------------------------------------------------------------------------------
/01. DataStructures/04. Tree/05. Heap/Leetcode Questions/03. Top K Frequent Elements #347/Solution.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public int[] topKFrequent(int[] nums, int k) {
3 | HashMap map = new HashMap<>();
4 | for (int ele : nums) {
5 | map.put(ele, map.getOrDefault(ele, 0) + 1);
6 | }
7 | PriorityQueue heap = new PriorityQueue<>((a, b) -> {
8 | return map.get(a) - map.get(b);
9 | });
10 | for (int key : map.keySet()) {
11 | heap.add(key);
12 | if (heap.size() > k) {
13 | heap.remove();
14 | }
15 | }
16 | int[] ans = new int[k];
17 | int i = 0;
18 | while (k-- > 0) {
19 | ans[i++] = heap.remove();
20 | }
21 | return ans;
22 | }
23 | }
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Raju and His Trip/README.md:
--------------------------------------------------------------------------------
1 | Raju and His Trip Problem Code: VALTRI
2 | Add problem to Todo list
3 | Submit
4 | Raju is planning to visit his favourite restaurant. He shall travel to it by bus. Only the buses whose numbers are divisible by 5 or by 6 shall take him to his destination. You are given a bus number N. Find if Raju can take the bus or not. Print YES if he can take the bus, otherwise print NO.
5 |
6 | Input:
7 | The first and only line of the input shall contain an integer N, denoting the bus number.
8 | Output:
9 | Print YES if Raju can take that bus, else print NO.
10 |
11 | Constraints
12 | 1≤N≤106
13 | Sample Input 1:
14 | 60
15 | Sample Output 1:
16 | YES
17 | Sample Input 2:
18 | 16
19 | Sample Output 2:
20 | NO
21 | Sample Input 3:
22 | 20
23 | Sample Output 3:
24 | YES
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/24. Pick from both sides/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 | int solve(vector &A, int B) {
5 | if(B > A.size())
6 | return -1;
7 |
8 | int max_val = 0;
9 | for(int i=0;iA.size()-B-1;i--){
16 | ans = max(ans, max_val - A[j] + A[i]);
17 | max_val = max_val - A[j] + A[i];
18 | j--;
19 | }
20 | return ans;
21 |
22 | }
23 | int main(){
24 | int n;
25 | cin>>n;
26 | int number;
27 | vector a(n,0);
28 | for(int i=0;i>a[i];
30 | }
31 | int b;
32 | cin>>b;
33 | cout<maxSum){
9 | maxSum=presentSum;
10 | }
11 | else if(presentSum<0){
12 | presentSum=0;
13 | }
14 |
15 | }
16 | return maxSum;
17 | }
18 | public static void main(String[] args) {
19 | int[] nums = new int[]{1, -2, 3, 2, -6, 4, 100, -200};
20 | maxSubArray(nums);
21 | System.out.println(maxSubArray(nums));
22 | }
23 | }
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/01. Linear Search/Linear_Search.java:
--------------------------------------------------------------------------------
1 | // TODO: Search an Element in the array using Linear Search Technique
2 | package array.searching;
3 |
4 | public class Linear_Search {
5 |
6 | static int linearSearch(int[] arr, int element) {
7 | if(arr.length == 0)
8 | return -1;
9 |
10 | for(int i = 0; i < arr.length; i++)
11 | {
12 | // find the first occurrence of the element and return it's position
13 | if(arr[i] == element) {
14 | return i + 1;
15 | }
16 | }
17 | return -1;
18 | }
19 | public static void main(String[] args) {
20 | int[] arr = { 18, 12, 9, 14, 77, 50, 45, 2, 91, 40};
21 | int element = 9;
22 | int res = linearSearch(arr, element);
23 | if(res == -1)
24 | System.out.println("Element not Found");
25 | else
26 | System.out.println("Element Found at position " + res);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/05.Recursive Bubble Sort/RecursiveBubbleSort.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 |
3 | public class RecursiveBubbleSort {
4 | public static void main(String[] args) {
5 | int[] arr = {3,1,5,4,2};
6 |
7 | // Before Sorting
8 | System.out.println(Arrays.toString(arr));
9 |
10 | // Sort Function
11 | sort(arr, arr.length);
12 |
13 | // After Sorting
14 | System.out.println(Arrays.toString(arr));
15 | }
16 |
17 | static void sort(int[] arr, int n) {
18 | if (n==1) return;
19 |
20 | for(int i=0; iarr[i+1]){
22 | int temp = arr[i];
23 | arr[i] = arr[i+1];
24 | arr[i+1] = temp;
25 | }
26 | }
27 |
28 | sort(arr, n-1);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Binary Trees/reverse-level-order/solution.cpp:
--------------------------------------------------------------------------------
1 | vector reverseLevelOrder(Node *root)
2 | {
3 | vectorans;
4 | vector>ans1;
5 | if(!root) return ans;
6 | queueq;
7 | q.push(root);
8 | while(!q.empty()){
9 | int s = q.size();
10 | vectorlevel;
11 | for(int i = 0 ; i < s ; i++){
12 | Node* curr = q.front();
13 | q.pop();
14 | if(curr->left) q.push(curr->left);
15 | if(curr->right) q.push(curr->right);
16 | level.push_back(curr->data);
17 | }
18 | ans1.push_back(level);
19 | }
20 | for(int i = ans1.size()-1 ; i >= 0 ; i--){
21 | for(int j = 0 ; j < ans1[i].size() ; j++){
22 | ans.push_back(ans1[i][j]);
23 | }
24 | }
25 | return ans;
26 | }
27 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/07. Remove Duplicates From Sorted List- II GQ/solution.js:
--------------------------------------------------------------------------------
1 | // Definition for singly-linked list.
2 | // function Node(data){
3 | // this.data = data
4 | // this.next = null
5 | // }
6 |
7 | module.exports = {
8 | //param A : head node of linked list
9 | //return the head node in the linked list
10 | deleteDuplicates: function (A) {
11 | let previousPointer = new Node();
12 | previousPointer.next = A;
13 | let temp = previousPointer;
14 | while (A) {
15 | if (A && A.next && A.next.data == A.data) {
16 | while (A.next && A.data == A.next.data) A = A.next;
17 |
18 | temp.next = A.next;
19 | } else {
20 | temp = temp.next;
21 | }
22 | A = A.next;
23 | }
24 | return previousPointer.next;
25 | },
26 | };
27 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/17. Maximum Value at a Given Index in a Bounded Array/README.md:
--------------------------------------------------------------------------------
1 | You are given three positive integers: n, index, and maxSum. You want to construct an array nums (0-indexed) that satisfies the following conditions:
2 |
3 | nums.length == n
4 |
5 | nums[i] is a positive integer where 0 <= i < n.
6 | abs(nums[i] - nums[i+1]) <= 1 where 0 <= i < n-1.
7 | The sum of all the elements of nums does not exceed maxSum.
8 | nums[index] is maximized.
9 | Return nums[index] of the constructed array.
10 |
11 | Note that abs(x) equals x if x >= 0, and -x otherwise.
12 |
13 | ## Example 1
14 |
15 | Input: n = 4, index = 2, maxSum = 6
16 |
17 | Output: 2
18 |
19 | Explanation: nums = [1,2,2,1] is one array that satisfies all the conditions.
20 | There are no arrays that satisfy all the conditions and have nums[2] == 3, so 2 is the maximum nums[2].
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/02. InsertionSort/Insertion_Sort.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int printArray(int array[], int n)
4 | {
5 | for (int i = 0; i < n - 1; i++)
6 | {
7 | printf("%d ", array[i]);
8 | }
9 | printf("\n\n");
10 | }
11 |
12 | int main(){
13 | int arr[] = {7,98,79,-2,8,98,7,-7,4,54,87,8,45,9};
14 |
15 |
16 | int n = ((sizeof arr) /( sizeof arr[0]));
17 |
18 |
19 | printf("before sorting: \n");
20 | printArray(arr, n);
21 |
22 | for (int i = 1; i < n-1; i++)
23 | {
24 | while (arr[i] < arr[i-1] && i-1 >= 0)
25 | {
26 | int temp = arr[i-1];
27 | arr[i-1] = arr[i];
28 | arr[i] = temp;
29 | i--;
30 | }
31 |
32 | }
33 | printf("After sorting: \n");
34 | printArray(arr, n);
35 | }
36 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Find Second Larget/README.md:
--------------------------------------------------------------------------------
1 | Find Second Largest | Problem Code: SECLAR
2 | Given three distinct integers A, B and C, print the second largest number among them.
3 | Input:
4 | The input consists of three lines.
5 | The first line contains a single integer A.
6 | The second line contains a single integer B.
7 | The third line contains a single integer C
8 | Output:
9 | Print the second largest number among A, B and C, in a separate line.
10 |
11 | Constraints
12 | 1≤A,B,C≤109
13 | Sample Input 1:
14 | 2
15 | 7
16 | 21
17 | Sample Output 1:
18 | 7
19 | Sample Input 2:
20 | 14
21 | 28
22 | 16
23 | Sample Output 2:
24 | 16
25 | EXPLANATION:
26 | In the first example, 7 is the second largest number among the given three numbers.
27 | In the second example, 16 is the second largest number among the given three numbers.
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/10. Reverse LL in K group GQ/solution 10-AC.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for singly-linked list.
3 | * class ListNode {
4 | * public int val;
5 | * public ListNode next;
6 | * ListNode(int x) { val = x; next = null; }
7 | * }
8 | */
9 | public class Solution {
10 | public ListNode reverseList(ListNode head, int k) {
11 | ListNode prev=null;
12 | ListNode curr=head;
13 | ListNode next=null;
14 |
15 | int count=0;
16 |
17 | while(curr!=null && count a[j]:
6 | small = j
7 | # Swap the found minimum element with
8 | # the first element
9 | a[i], a[small] = a[small], a[i]
10 |
11 | def printArr(a): # function to print the array
12 |
13 | for i in range(len(a)):
14 | print (a[i], end = " ")
15 |
16 |
17 |
18 | a = [69,55,31,99,43,56,1]
19 | print("Before sorting array elements are - ")
20 | printArr(a)
21 | selection(a)
22 | print("\nAfter sorting array elements are - ")
23 | selection(a)
24 | printArr(a)
25 |
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Valid Triangle Or Not/README.md:
--------------------------------------------------------------------------------
1 | You're given the length of three sides a, b, and c respectively. Now check if these three sides can form a triangle or not. Print "YES"(without quotes) if it can form a valid triangle with an area greater than 0, otherwise print "NO" (without quotes).
2 |
3 | Input:
4 | First-line will contain three numbers a, b, and c separated by space.
5 | Output:
6 | Print "YES"(without quotes) if these sides can form a valid triangle, otherwise print "NO" (without quotes).
7 |
8 | Constraints
9 | 1≤a,b,c≤106
10 | Sample Input 1:
11 | 2 4 3
12 | Sample Output 1:
13 | YES
14 | Sample Input 2:
15 | 1 1 4
16 | Sample Output 2:
17 | NO
18 | EXPLANATION:
19 | In the first example, (2, 4, 3) can form a triangle with an area greater than 0.
20 | In the second example, (1, 1, 4) will never form a valid triangle.
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/Problems/Reverse Singly LinkedList/README.md:
--------------------------------------------------------------------------------
1 |
2 | # Reverse a linked list
3 |
4 |
5 | ## Demo
6 | Input: Head of following linked list
7 | 1->2->3->4->NULL
8 | Output: Linked list should be changed to,
9 | 4->3->2->1->NULL
10 |
11 | Input: Head of following linked list
12 | 1->2->3->4->5->NULL
13 | Output: Linked list should be changed to,
14 | 5->4->3->2->1->NULL
15 |
16 | ## Algorithms
17 | Initialize three pointers prev as NULL, curr as head and next as NULL.
18 | Iterate through the linked list. In loop, do following.
19 | // Before changing next of current,
20 | // store next node
21 | next = curr->next
22 | // Now change next of current
23 | // This is where actual reversing happens
24 | curr->next = prev
25 | // Move prev and curr one step forward
26 | prev = curr
27 | curr = next
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/05.Recursive Bubble Sort/README.md:
--------------------------------------------------------------------------------
1 | ### Recursive Bubble Sort
2 | #### Time Complexity: O(n²)
3 | ```java
4 | public class RecursiveBubbleSort {
5 | public static void main(String[] args) {
6 | int[] arr = {3,1,5,4,2};
7 |
8 | // Before Sorting
9 | System.out.println(Arrays.toString(arr));
10 |
11 | // Sort Function
12 | sort(arr, arr.length);
13 |
14 | // After Sorting
15 | System.out.println(Arrays.toString(arr));
16 | }
17 |
18 | static void sort(int[] arr, int n) {
19 | if (n==1) return;
20 |
21 | for(int i=0; iarr[i+1]){
23 | int temp = arr[i];
24 | arr[i] = arr[i+1];
25 | arr[i+1] = temp;
26 | }
27 | }
28 |
29 | sort(arr, n-1);
30 | }
31 | }
32 | ```
--------------------------------------------------------------------------------
/CodeChef DSA Learning series Solutions/Easy Problems to get started/Alternative Square Pattern/README.md:
--------------------------------------------------------------------------------
1 | Alternative Square Pattern Problem Code: SQALPAT
2 | Add problem to Todo list
3 |
4 | You're given a number N. Print the first N lines of the below-given pattern.
5 |
6 | 1 2 3 4 5
7 | 10 9 8 7 6
8 | 11 12 13 14 15
9 | 20 19 18 17 16
10 | 21 22 23 24 25
11 | 30 29 28 27 26
12 | Input:
13 | First-line will contain the number N.
14 | Output:
15 | Print the first N lines of the given pattern.
16 |
17 | Constraints
18 | 1≤N≤200
19 | Sample Input 1:
20 | 4
21 | Sample Output 1:
22 | 1 2 3 4 5
23 | 10 9 8 7 6
24 | 11 12 13 14 15
25 | 20 19 18 17 16
26 | Sample Input 2:
27 | 2
28 | Sample Output 2:
29 | 1 2 3 4 5
30 | 10 9 8 7 6
31 | EXPLANATION:
32 | In the first example, we'll print the first 4 lines of the given pattern.
33 | In the second example, we'll print the first 2 lines of the given pattern.
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Arrays/Kadane's Algorithm/Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | using namespace std;
5 |
6 | //Function to calculate maximum sum subarray
7 | int maximum_sum_subarray(vector& arr){
8 |
9 | int max_so_far=arr[0], max_till_here=arr[0];
10 |
11 | for(int i=1;i>n;
24 | vector arr(n);
25 |
26 | for(int i=0;i>arr[i];
28 | }
29 | sum=maximum_sum_subarray(arr);
30 | cout< B.data) {
16 | ans.next = B;
17 |
18 | B = B.next;
19 | } else {
20 | ans.next = A;
21 | A = A.next;
22 | }
23 | ans = ans.next;
24 | }
25 |
26 | if (!A) {
27 | ans.next = B;
28 | } else {
29 | ans.next = A;
30 | }
31 |
32 | return head.next;
33 | },
34 | };
35 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/10. 45. Jump Game II/README.md:
--------------------------------------------------------------------------------
1 | ## 45. Jump Game II
2 | ## https://leetcode.com/problems/jump-game-ii/
3 |
4 | Let's say the range of the current jump is `[curBegin, curEnd]`, curFarthest is the farthest point that all points in `[curBegin, curEnd]` can reach. Once the current point reaches curEnd, then trigger another jump, and set the new curEnd with curFarthest, then keep the above steps, as the following:
5 |
6 | ```java
7 | class Solution {
8 | public int jump(int[] A) {
9 | int jumps = 0, curEnd = 0, curFarthest = 0;
10 | for (int i = 0; i < A.length - 1; i++) {
11 | curFarthest = Math.max(curFarthest, i + A[i]);
12 | if (i == curEnd) {
13 | jumps++;
14 | curEnd = curFarthest;
15 | }
16 | }
17 | return jumps;
18 | }
19 | }
20 | ```
21 |
22 | ## Time Complexity: O(n)
23 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/12. Reverse LL-ii/solution_12.py:
--------------------------------------------------------------------------------
1 | class Solution:
2 |
3 | def reverseBetween(self, A, B, C):
4 |
5 |
6 | head = A
7 | r = C-B
8 |
9 | c=1
10 | prev =None
11 | curr = head
12 | while c < B:
13 | prev = curr
14 | curr = curr.next
15 | c += 1
16 |
17 | tail = prev
18 | next_tail = curr
19 | prev = None
20 | i=0
21 | while curr != None and i <= r:
22 | nxt = curr.next
23 | curr.next = prev
24 | prev = curr
25 | curr = nxt
26 | i += 1
27 | if tail != None:
28 | tail.next = prev
29 | else:
30 | head = prev
31 |
32 | next_tail.next = curr
33 |
34 | return head
35 |
--------------------------------------------------------------------------------
/02. Algorithms/04. Recursion/Problems/00. Maximum and Minimum Value in an Array/Solution.java:
--------------------------------------------------------------------------------
1 | // TODO: https://www.geeksforgeeks.org/program-find-minimum-maximum-element-array/
2 | package recursion;
3 |
4 | public class Min_Max_of_Array {
5 |
6 | public static void main(String[] args) {
7 | int arr[] = { 12, 43, 45, 23, -420, -8, 69, 67, 1 };
8 | int n = arr.length;
9 | System.out.println("Minimum Element of Array: " + getMin(arr, 0, n));
10 | System.out.println("Maximum Element of Array: " + getMax(arr, 0, n));
11 | }
12 |
13 | static int getMin(int[] arr, int i, int n) {
14 | if (n == 1)
15 | return arr[i];
16 | int min = Math.min(arr[i], getMin(arr, i + 1, n - 1));
17 | return min;
18 | }
19 |
20 | static int getMax(int[] arr, int i, int n) {
21 | if (n == 1)
22 | return arr[i];
23 | int max = Math.max(arr[i], getMax(arr, i + 1, n - 1));
24 | return max;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/07. Priority Queue/README.md:
--------------------------------------------------------------------------------
1 | ### **Step 1:** [Watch priority Queue basic explanation here.](https://youtu.be/FdObb76AmzM)
2 |
3 | # **Implementation Using Heap**
4 |
5 | ## **VI Prerequisites:** [Comparable in PQ for generic PQ](https://youtu.be/DE835GvxZMQ)
6 |
7 | ## [**My Implementation of PQ**](./PriorityQueue.java)
8 |
9 | ## Output of above implementation:-
10 |
11 | 
12 |
13 | ## **NOTE:** Implementation ()s of priority Queue is totally same as heap DS function which is covered in heap DS.
14 |
15 | ## **Priority Queue --------- Heap**
16 |
17 | 1. **add() = minInsert()**
18 | 2. **peek() = getMin()**
19 | 3. **remove() = extractMin()**
20 |
21 | ### **Step 2:** [Problem Link.](https://practice.geeksforgeeks.org/problems/k-largest-elements3736/1)
22 |
23 | ### **Step 3:** [Solution Link.](./KLargestElement.java)
24 |
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/03. Selection Sort/Selection_Sort.java:
--------------------------------------------------------------------------------
1 | // TODO: Sort an Array using Selection Sort Technique
2 | package array.sorting;
3 |
4 | import java.util.Arrays;
5 |
6 | public class Selection_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 | for (int i = 0; i < n - 1; i++) {
14 | int min = i;
15 |
16 | // search for the smallest element
17 | for (int j = i + 1; j < n; j++) {
18 | if (arr[j] < arr[min])
19 | min = j;
20 | }
21 |
22 | // swap the smallest element with the element at i
23 | if (min != i) {
24 | int temp = arr[i];
25 | arr[i] = arr[min];
26 | arr[min] = temp;
27 | }
28 | }
29 | System.out.println("Array after Sorting: " + Arrays.toString(arr));
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Arrays/duplicate_array/duplicates.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 |
5 | class Solution{
6 | public:
7 | vector duplicates(int arr[], int n) {
8 |
9 | vector v;
10 | int last;
11 | sort(arr,arr+n);
12 | for(int i=0;i> n;
29 | int a[n];
30 | for (int i = 0; i < n; i++) cin >> a[i];
31 | Solution obj;
32 | vector ans = obj.duplicates(a, n);
33 | for (int i : ans) cout << i << ' ';
34 | cout << endl;
35 |
36 | return 0;
37 | }
38 |
39 |
40 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/08. Backspace String Compare #844/Solution_self.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public boolean backspaceCompare(String s, String t) {
3 | String a=operate(s);
4 | String b=operate(t);
5 | return a.equals(b);
6 | }
7 | String operate(String check) {
8 | Stack stack=new Stack();
9 | for(int x=0; x
2 | using namespace std;
3 |
4 |
5 | const int N = 1e+5 +2;
6 | bool vis[N];
7 |
8 | vector adj [N];
9 |
10 |
11 |
12 | void dfs(int node){
13 |
14 | vis[node] = true;
15 | cout<::iterator it;
18 |
19 | for(it = adj[node].begin();it!=adj[node].end();it++){
20 | if(!vis[*it])
21 | dfs(*it);
22 | }
23 | }
24 |
25 | int main(){
26 |
27 | for (int i=0;i>n>>m;
33 |
34 | int x,y;
35 | for(int i=0;i>x>>y;
37 |
38 | adj[x].push_back(y);
39 | adj[y].push_back(x);
40 | }
41 |
42 | for(int i=1;i<=N;i++){
43 | if(!vis[i])
44 | dfs(i);
45 | }
46 | return 0;
47 | }
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/11. Jumping on the Clouds/Solution2.java:
--------------------------------------------------------------------------------
1 | // https://www.hackerrank.com/challenges/jumping-on-the-clouds-revisited/problem
2 | import java.io.*;
3 | import java.util.*;
4 |
5 | public class Solution {
6 |
7 | public static void main(String[] args) {
8 | Scanner in = new Scanner(System.in);
9 | int n = in.nextInt();
10 | int k = in.nextInt();
11 | int c[] = new int[n];
12 | for(int i=0; i < n; i++){
13 | c[i] = in.nextInt();
14 | }
15 | jumpingOnClouds(c, k);
16 | }
17 | public static void jumpingOnClouds(int c[],int k)
18 | {
19 | int curr=0;
20 | int e=100;
21 | int len=c.length;
22 | do{
23 | curr=(curr+k)%len;
24 | e--;
25 | if(c[curr]==1)
26 | e-=2;
27 | }while(curr!=0);
28 | System.out.println(e);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/17. Pascal's Triangle/pascalTriangle.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class PascalTriangel {
3 | public static void main(String[] args) {
4 | Scanner sc = new Scanner(System.in);
5 | int numRows = sc.nextInt();
6 | System.out.println(generateTriangel(numRows));
7 | }
8 |
9 | // method for Pascal's Triangel generation using ArrayList as auxiliary space and nested for loop
10 | public static List> generateTriangel(int numRows) {
11 | List> res = new ArrayList<>();
12 |
13 | for(int line=1;line<=numRows;line++){
14 | int c = 1;
15 | ArrayList ans = new ArrayList<>();
16 | for(int i =1;i<=line;i++){
17 | ans.add(c);
18 | c = c*(line-i)/i;
19 | }
20 | res.add(ans);
21 | }
22 | return res;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/01. Infix to Postfix/README.md:
--------------------------------------------------------------------------------
1 | ### **Step 1:** [Learn manual conversion of Infix to Postfix/Prefix by GFS.](https://youtu.be/q75VAGSwL0U)
2 |
3 | ### **Step 2:** [Learn postfix to Infix manually.](https://youtu.be/qqagmeTN0p4?t=1031)
4 |
5 | ### **Step 3:** [Learn prefix to Infix manually.](https://youtu.be/lT4-mJAF5UA?t=1119)
6 |
7 | ### **Step 4:** [Learn Rules to convert Infix to postfix using stack.](https://youtu.be/8wQ7JE5pFXU)
8 |
9 | ### **Step 5:** [Visualize pseudo code to convert Infix to postfix using stack.](https://youtu.be/BeRM6DzdCBg)
10 |
11 | 
12 |
13 | ### **Step 6:** [Implementation of pseudo code in C++ to convert Infix to postfix using stack.](https://youtu.be/dJESbyFR1sU)
14 |
15 | ### **Step 7:** [See my code here in Java to convert Infix to postfix using stack.](./InfixPostfix.java)
16 |
17 |
18 | Thank You
19 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/03. Postfix to Infix/PostfixInfix.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class PostfixInfix {
4 |
5 | public static void main(String[] args) {
6 | Scanner sc=new Scanner(System.in);
7 | String postfix_exp="";
8 | System.out.println("Enter Postfix Expression");
9 | postfix_exp=sc.next();
10 | System.out.println("Equivalent infix Expression for "+postfix_exp+" is= "+PostfixToInfix(postfix_exp));
11 | }
12 |
13 | private static String PostfixToInfix(String postfix_exp) {
14 | Stack st=new Stack();
15 | for(char ch:postfix_exp.toCharArray()) {
16 | if((ch >= 'a' && ch <= 'z') ||(ch >= 'A' && ch <= 'Z')) {
17 | st.push(Character.toString(ch));
18 | }
19 | else {
20 | String op2=st.pop();
21 | String op1=st.pop();
22 | String exp='('+op1+ch+op2+')';
23 | st.push(exp);
24 | }
25 | }
26 |
27 | return st.peek();
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/05. Postfix to Prefix/PostfixPrefix.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class PostfixPrefix {
4 |
5 | public static void main(String[] args) {
6 | Scanner sc=new Scanner(System.in);
7 | String postfix_exp="";
8 | System.out.println("Enter postfix Expression");
9 | postfix_exp=sc.next();
10 | System.out.println("Equivalent prefix Expression for "+postfix_exp+" is= "+PostfixToInfix(postfix_exp));
11 |
12 | }
13 |
14 | private static String PostfixToInfix(String postfix_exp) {
15 | Stack st=new Stack();
16 | for(char ch:postfix_exp.toCharArray()) {
17 | if((ch >= 'a' && ch <= 'z') ||(ch >= 'A' && ch <= 'Z')) {
18 | st.push(Character.toString(ch));
19 | }
20 | else {
21 | String op2=st.pop();
22 | String op1=st.pop();
23 | String exp=ch+op1+op2;
24 | st.push(exp);
25 | }
26 | }
27 |
28 | return st.peek();
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/LeetCode Questions/02. Remove All Adjacent Duplicates In String #1047/Solution.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public String removeDuplicates(String S) {
3 | Stack st=new Stack();
4 | StringBuilder ans=new StringBuilder(S.length());
5 | for(int i=0; i
2 |
3 | using namespace std;
4 |
5 | int printArray(int array[], int n)
6 | {
7 | for (int i = 0; i < n - 1; i++)
8 | {
9 | cout<< array[i] << " ";
10 | }
11 | cout << "\n\n";
12 |
13 | return 0;
14 | }
15 |
16 | int main(){
17 | int arr[] = {7,98,79,-2,8,98,7,-7,4,54,87,8,45,9};
18 |
19 |
20 | int n = ((sizeof arr) /( sizeof arr[0]));
21 |
22 | cout << "before sorting: \n";
23 | printArray(arr, n);
24 |
25 | for (int i = 1; i < n-1; i++)
26 | {
27 | while (arr[i] < arr[i-1] && i-1 >= 0)
28 | {
29 | int temp = arr[i-1];
30 | arr[i-1] = arr[i];
31 | arr[i] = temp;
32 | i--;
33 | }
34 |
35 | }
36 |
37 | cout << "after sorting: \n";
38 |
39 | printArray(arr, n);
40 | }
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Arrays/Trapping Rain Water/trappingRainWaterLeetcodeHard.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | import java.io.*;
3 | import java.lang.*;
4 | public class solution{
5 | int traprain(int arr[])
6 | {
7 | int n=arr.length;
8 | int right[]=new int[n];
9 | int left[]=new int[n];
10 | left[0]=arr[0];
11 | for(int i=1;i=0;i--)
17 | {
18 | right[i]=Math.max(right[i+1],arr[i]);
19 | }
20 | int res=0;
21 | for(int i=0;i union=new HashSet<>();
10 | for(int val:arr1) {
11 | union.add(val);
12 | }
13 | for(int val:arr2) {
14 | union.add(val);
15 | }
16 | System.out.println("Union= "+union);
17 | union.clear();// clearing union set to reuse for finding intersection
18 | for(int val:arr1) {
19 | union.add(val);
20 | }
21 | Set intersection=new HashSet<>();
22 | for(int val:arr2) {
23 | intersection.add(val);
24 | }
25 | intersection.retainAll(union); // https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
26 | System.out.println("Intersection= "+intersection);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/03. Floor in a Sorted Array/README.md:
--------------------------------------------------------------------------------
1 | ### https://practice.geeksforgeeks.org/problems/floor-in-a-sorted-array-1587115620/1#
2 |
3 | ### [Video Tutorial](https://youtu.be/W9QJ8HaRvJQ?t=1981)
4 | ### [Editorial](https://www.geeksforgeeks.org/floor-in-a-sorted-array/)
5 |
6 | ```java
7 | class Solution{
8 |
9 | // Function to find floor of x
10 | // arr: input array
11 | // n is the size of array
12 | static int findFloor(long arr[], int n, long target){
13 |
14 | int start = 0;
15 | int end = n - 1;
16 | while (start <= end) {
17 | int mid = start + (end - start) / 2;
18 | if(arr[mid] == target)
19 | return mid;
20 | else if (target > arr[mid]) {
21 | start = mid + 1;
22 | } else if (target < arr[mid]) {
23 | end = mid - 1;
24 | }
25 | }
26 | return end;
27 | }
28 | }
29 | ```
30 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/14. Palindrome List/README.md:
--------------------------------------------------------------------------------
1 | ### Sort Binary Linked List
2 |
3 | ### https://www.interviewbit.com/problems/sort-binary-linked-list/
4 |
5 | Problem Description
6 |
7 | Given a Linked List A consisting of N nodes.
8 |
9 | The Linked List is binary i.e data values in the linked list nodes consist of only 0's and 1's.
10 |
11 | You need to sort the linked list and return the new linked list.
12 |
13 | NOTE:
14 |
15 | Try to do it in constant space.
16 |
17 |
18 | ```c++
19 | ListNode* Solution::solve(ListNode* A) {
20 | if(A==NULL||A->next==NULL){
21 | return A;
22 | }
23 | ListNode* count = A;
24 | ListNode* curr = A;
25 | while(curr!=NULL){
26 | if(curr->val!=1){
27 | swap(curr->val , count->val);
28 | count = count->next;
29 | }
30 | curr = curr->next;
31 | }
32 | return A;
33 | }
34 | ```
35 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/07. Find position of an element in an infinite sorted array/README.md:
--------------------------------------------------------------------------------
1 | ## Find position of an element in a sorted array of infinite numbers
(basically without using arr.length)
2 | ## https://www.geeksforgeeks.org/find-position-element-sorted-array-infinite-numbers/
3 |
4 | ## [Video Tutorial](https://youtu.be/W9QJ8HaRvJQ?t=5283)
5 |
6 | ```java
7 | //initial arguments will be: (array, element to be searched)
8 | static int ans(int[] arr, int target) {
9 | // first find the range
10 | // first start with a box of size 2
11 | int start = 0;
12 | int end = 1;
13 |
14 | // condition for the target to lie in the range
15 | while (target > arr[end]) {
16 | int temp = end + 1; // this is my new start
17 | // double the box value
18 | // end = previous end + sizeofbox*2
19 | end = end + (end - start + 1) * 2;
20 | start = temp;
21 | }
22 | return binarySearch(arr, target, start, end);
23 |
24 | }
25 | ```
26 |
--------------------------------------------------------------------------------
/01. DataStructures/01. Linked List/1. Singly-Linked-List/InterviewBit Questions/13. Sort Binary Linked List/README.md:
--------------------------------------------------------------------------------
1 | ### Sort Binary Linked List
2 |
3 | ### https://www.interviewbit.com/problems/sort-binary-linked-list/
4 |
5 | Problem Description
6 |
7 | Given a Linked List A consisting of N nodes.
8 |
9 | The Linked List is binary i.e data values in the linked list nodes consist of only 0's and 1's.
10 |
11 | You need to sort the linked list and return the new linked list.
12 |
13 | NOTE:
14 |
15 | Try to do it in constant space.
16 |
17 |
18 | ```c++
19 | ListNode* Solution::solve(ListNode* A) {
20 | if(A==NULL||A->next==NULL){
21 | return A;
22 | }
23 | ListNode* count = A;
24 | ListNode* curr = A;
25 | while(curr!=NULL){
26 | if(curr->val!=1){
27 | swap(curr->val , count->val);
28 | count = count->next;
29 | }
30 | curr = curr->next;
31 | }
32 | return A;
33 | }
34 | ```
35 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/04. Prefix to Infix/PrefixInfix.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class PrefixInfix {
3 |
4 | public static void main(String[] args) {
5 | Scanner sc=new Scanner(System.in);
6 | String prefix_exp="";
7 | System.out.println("Enter Prefix Expression");
8 | prefix_exp=sc.next();
9 | System.out.println("Equivalent infix Expression for "+prefix_exp+" is= "+PrefixToInfix(prefix_exp));
10 |
11 | }
12 |
13 | private static String PrefixToInfix(String prefix_exp) {
14 | Stack st=new Stack();
15 | for(int i=prefix_exp.length()-1;i>=0;i--) {
16 | char ch=prefix_exp.charAt(i);
17 | if((ch >= 'a' && ch <= 'z') ||(ch >= 'A' && ch <= 'Z')) {
18 | st.push(Character.toString(ch));
19 | }
20 | else {
21 | String op1=st.pop();
22 | String op2=st.pop();
23 | String exp='('+op1+ch+op2+')';
24 | st.push(exp);
25 | }
26 | }
27 |
28 | return st.peek();
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/01. Reverse an Array/Reverse_Array.py:
--------------------------------------------------------------------------------
1 | #Method 1
2 | #The original array
3 | arr = [11, 22, 33, 44, 55]
4 | print("Array is :",arr)
5 | res = arr[::-1] #reversing using list slicing
6 | print("Resultant new reversed array:",res)
7 |
8 |
9 | #Method 2
10 | #The original array
11 | arr = [11, 22, 33, 44, 55]
12 | print("Before reversal Array is :",arr)
13 | #reversing using reverse()
14 | arr.reverse()
15 | print("After reversing Array:",arr)
16 |
17 | #Method 3
18 | #The original array
19 | arr = [12, 34, 56, 78]
20 | print("Original Array is :",arr)
21 | #reversing using reversed()
22 | result=list(reversed(arr))
23 | print("Resultant new reversed Array:",result)
24 |
25 | #Method 4
26 | #Using reverse() Method
27 | import array
28 | #The original array
29 | new_arr=array.array('i',[2,4,6,8,10,12])
30 | print("Original Array is :",new_arr)
31 |
32 | #reversing using reverse()
33 | new_arr.reverse()
34 | print("Reversed Array:",new_arr)
35 |
--------------------------------------------------------------------------------
/02. Algorithms/02. Sorting/06. Count Sort/Count_sort.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #define RANGE 255
4 |
5 | void countSort(char arr[])
6 | {
7 | char output[strlen(arr)];
8 |
9 | int count[RANGE + 1], i;
10 | memset(count, 0, sizeof(count));
11 |
12 |
13 | for (i = 0; arr[i]; ++i)
14 | ++count[arr[i]];
15 |
16 |
17 | for (i = 1; i <= RANGE; ++i)
18 | count[i] += count[i - 1];
19 |
20 |
21 | for (i = 0; arr[i]; ++i) {
22 | output[count[arr[i]] - 1] = arr[i];
23 | --count[arr[i]];
24 | }
25 |
26 |
27 | for (i = sizeof(arr)-1; i>=0; --i)
28 | {
29 | output[count[arr[i]]-1] = arr[i];
30 | --count[arr[i]];
31 | }
32 |
33 |
34 | for (i = 0; arr[i]; ++i)
35 | arr[i] = output[i];
36 | }
37 |
38 | int main()
39 | {
40 | char arr[] = "geeksforgeeks"; //"applepp";
41 |
42 | countSort(arr);
43 |
44 | printf("Sorted character array is %sn", arr);
45 | return 0;
46 | }
47 |
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/17. Pascal's Triangle/pascalTriangle.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 | vector > solve(int A) {
5 |
6 | vector< vector > v;
7 | if(A == 0)
8 | return v;
9 | vector v1;
10 | v1.push_back(1);
11 | v.push_back(v1);
12 |
13 | int x;
14 | for(int i=2;i<=A;i++)
15 | {
16 | vector v2(v1.size(),0);
17 | x=1;
18 | v2[0] = v1[0];
19 | while(x>n;
33 | vector< vector > v = solve(n);
34 | for(int i=0;i= 0 && arr[j] > last)
26 | {
27 | arr[j+1] = arr[j];
28 | j--;
29 | }
30 | arr[j+1] = last;
31 | }
32 | }
33 | ```
34 |
--------------------------------------------------------------------------------
/01. DataStructures/02. Stack/Infix Prefix Postfix/06. Prefix to Postfix/PrefixPostfix.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class PrefixPostfix {
4 |
5 | public static void main(String[] args) {
6 | Scanner sc=new Scanner(System.in);
7 | String prefix_exp="";
8 | System.out.println("Enter prefix Expression");
9 | prefix_exp=sc.next();
10 | System.out.println("Equivalent postfix Expression for "+prefix_exp+" is= "+PrefixToPostfix(prefix_exp));
11 |
12 | }
13 |
14 | private static String PrefixToPostfix(String prefix_exp) {
15 | Stack st=new Stack();
16 |
17 | for(int i=prefix_exp.length()-1;i>=0;i--) {
18 | char ch=prefix_exp.charAt(i);
19 | if((ch >= 'a' && ch <= 'z') ||(ch >= 'A' && ch <= 'Z')) {
20 | st.push(Character.toString(ch));
21 | }
22 | else {
23 | String op1=st.pop();
24 | String op2=st.pop();
25 | String exp=op1+op2+ch;
26 | st.push(exp);
27 | }
28 | }
29 |
30 | return st.peek();
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/Queue Reversal/Recursion implementation.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | void queue_reverse_recursion_implementation(queue*q2){
5 | int size = (*q2).size() , data = (*q2).front();
6 | if(size == 0){
7 | return;
8 | }else{
9 | (*q2).pop();
10 | queue_reverse_recursion_implementation(q2);
11 | (*q2).push(data);
12 | return;
13 | }
14 | }
15 |
16 | int main(){
17 | queueq1;
18 | q1.push(10);
19 | q1.push(20);
20 | q1.push(30);
21 | q1.push(40);
22 | q1.push(50);
23 | q1.push(60);
24 | q1.push(70);
25 | q1.push(80);
26 | q1.push(90);
27 | q1.push(100);
28 | cout << "Queue Size -> " << q1.size() << endl;
29 | queue_reverse_recursion_implementation(&q1);
30 | for(int i=q1.size();i>0;i--){
31 | cout << q1.front() << " ";
32 | q1.pop();
33 | }
34 | cout << endl;
35 | return 0;
36 | }
--------------------------------------------------------------------------------
/02. Algorithms/01. Arrays/09. Minimize the Heights II/README.md:
--------------------------------------------------------------------------------
1 | ## Minimize the Heights II
2 | ## https://practice.geeksforgeeks.org/problems/minimize-the-heights3351/1#
3 | ## Similar Question on [LeetCode](https://leetcode.com/problems/smallest-range-ii/)
4 | ### [Video Tutorial](https://youtu.be/o9WG7t6EKZo)
5 |
6 | ## Complexity:
7 | - Time Complexity = O(nlog(n)), since we are using Arrays.sort() function
8 | - Space Complexity = O(1)
9 |
10 | ```java
11 | class Solution {
12 | int getMinDiff(int[] arr, int n, int k) {
13 | Arrays.sort(arr);
14 | int min, max;
15 | int ans = arr[n - 1] - arr[0];
16 |
17 | for(int i = 1; i < n; i++){
18 | if(arr[i] >= k) { //if height is not negative
19 | max = Math.max(arr[i-1]+k, arr[n-1]-k);
20 | min = Math.min(arr[i]-k, arr[0]+k);
21 | ans = Math.min(ans, max-min);
22 | }
23 | }
24 | return ans;
25 | }
26 | }
27 | ```
28 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/Bit Manipulation/Find position of set bit/README.md:
--------------------------------------------------------------------------------
1 | # [ Find position of set bit ](https://practice.geeksforgeeks.org/problems/find-position-of-set-bit3706/1)
2 | ## Given a number N having only one ‘1’ and all other ’0’s in its binary representation, find position of the only set bit. If there are 0 or more than 1 set bit the answer should be -1. Position of set bit '1' should be counted starting with 1 from LSB side in binary representation of the number.
3 | ### Example 1:
4 | **Input:**
5 | N = 2
6 | **Output:**
7 | 2
8 |
9 | ### Example 2:
10 | **Input:**
11 | N = 5
12 | **Output:**
13 | -1
14 |
15 | ### Solution in C++:
16 | ```
17 | int findPosition(int n) {
18 | int pos = 0;
19 | if(n<=0) return -1;;
20 | if((n&(n-1))==0)
21 | {
22 | int i = 1;
23 | while(n!=0)
24 | {
25 | n=n>>1;
26 | pos++;
27 | }
28 | return pos;
29 | }
30 | else return -1;
31 | }
32 | ```
33 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/11. 33. Search in Rotated Sorted Array/Solution.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/search-in-rotated-sorted-array/
2 | class Solution {
3 | public int search(int[] arr, int target) {
4 | int start = 0, end = arr.length - 1;
5 | while (start <= end) {
6 | int mid = start + (end - start) / 2;
7 |
8 | int num = arr[mid];
9 | // if arr[mid] and target are "on the same side" of arr[0], we just take arr[mid].
10 | if ((arr[mid] < arr[0]) == (target < arr[0])) {
11 | num = arr[mid];
12 | } else {
13 | num = target < arr[0] ? Integer.MIN_VALUE : Integer.MAX_VALUE;
14 | }
15 |
16 | if (num < target)
17 | start = mid + 1;
18 | else if (num > target)
19 | end = mid - 1;
20 | else
21 | return mid;
22 | }
23 | return -1;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/02. Algorithms/03. Searching/Problems/08. 852. Peak Index in a Mountain Array/Solution2.java:
--------------------------------------------------------------------------------
1 | //https://leetcode.com/problems/peak-index-in-a-mountain-array/
2 | // using binary search
3 | class Solution {
4 | public int peakIndexInMountainArray(int[] arr) {
5 | int start = 0;
6 | int end = arr.length - 1;
7 | while(start < end) {
8 | int mid = start + (end - start)/2;
9 | if(arr[mid] > arr[mid + 1]) {
10 | // you're in decreasing part of the array
11 | // arr[mid] may be the answer, but look at the left of mid
12 | end = mid;
13 | } else {
14 | // you're in ascending part of the array
15 | // because we know that arr[mid+1] > arr[mid]
16 | start = mid + 1;
17 | }
18 | }
19 | // in the end start == end point to the peak of the mountain
20 | return start; // return start or end since both are same
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/02. Algorithms/08. Strings/Palindrome String/Palindrome String.java:
--------------------------------------------------------------------------------
1 | //question link https://practice.geeksforgeeks.org/problems/palindrome-string0817/1
2 |
3 | import java.io.*;
4 | import java.util.*;
5 |
6 | class GFG {
7 | public static void main(String args[]) throws IOException {
8 | BufferedReader read =
9 | new BufferedReader(new InputStreamReader(System.in));
10 | int t = Integer.parseInt(read.readLine());
11 | while (t-- > 0) {
12 | String S = read.readLine();
13 |
14 | Solution ob = new Solution();
15 | System.out.println(ob.isPalindrome(S));
16 | }
17 | }
18 | }// } Driver Code Ends
19 |
20 |
21 | //User function Template for Java
22 |
23 | class Solution {
24 | int isPalindrome(String S)
25 | {
26 | for(int i=0;i 0 && arr[i - 1] > arr[i])
15 | {
16 | t = arr[i];
17 | arr[i] = arr[i - 1];
18 | arr[i - 1] = t;
19 | }
20 | if (i < n - 1 && arr[i] < arr[i + 1])
21 | {
22 | t = 0;
23 | t = arr[i];
24 | arr[i] = arr[i + 1];
25 | arr[i + 1] = t;
26 | }
27 | }
28 | for (int i = 0; i < n; i++)
29 | System.out.print(arr[i] + " ");
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Love Babbar DSA Sheet Solutions/String/Palindrome_String.java:
--------------------------------------------------------------------------------
1 | //Palindrome String link- https://practice.geeksforgeeks.org/problems/palindrome-string0817/1
2 |
3 | import java.io.*;
4 | import java.util.*;
5 |
6 | class GFG {
7 | public static void main(String args[]) throws IOException {
8 | BufferedReader read =
9 | new BufferedReader(new InputStreamReader(System.in));
10 | int t = Integer.parseInt(read.readLine());
11 | while (t-- > 0) {
12 | String S = read.readLine();
13 |
14 | Solution ob = new Solution();
15 | System.out.println(ob.isPalindrome(S));
16 | }
17 | }
18 | }// } Driver Code Ends
19 |
20 |
21 | //User function Template for Java
22 |
23 | class Solution {
24 | int isPalindrome(String S)
25 | {
26 | for(int i=0;i arr[mid + 1]) {
10 | // you're in decreasing part of the array
11 | // arr[mid] may be the answer, but look at the left of mid
12 | end = mid;
13 | } else {
14 | // you're in ascending part of the array
15 | // because we know that arr[mid+1] > arr[mid]
16 | start = mid + 1;
17 | }
18 | }
19 | // in the end start == end point to the peak of the mountain
20 | return start; // return start or end since both are same
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/01. DataStructures/03. Queue/Basic Operations/Queue Reversal/Stack Implementation.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | void queue_reverse_stack_implementation(queue*q2){ // Time Complexity is O(n), space complexity is O(n)
5 | stacks1;
6 | int size = (*q2).size();
7 | for(int i=size;i>0;i--){
8 | s1.push((*q2).front());
9 | (*q2).pop();
10 | }
11 | for(int i=size;i>0;i--){
12 | (*q2).push(s1.top());
13 | s1.pop();
14 | }
15 | }
16 |
17 | int main(){
18 | queueq1;
19 | q1.push(10);
20 | q1.push(20);
21 | q1.push(30);
22 | q1.push(40);
23 | q1.push(50);
24 | q1.push(60);
25 | q1.push(70);
26 | q1.push(80);
27 | q1.push(90);
28 | q1.push(100);
29 | cout << "Queue Size -> " << q1.size() << endl;
30 | queue_reverse_stack_implementation(&q1);
31 | for(int i=q1.size();i>0;i--){
32 | cout << q1.front() << " ";
33 | q1.pop();
34 | }
35 | cout << endl;
36 | return 0;
37 | }
--------------------------------------------------------------------------------
/02. Algorithms/09. Dynamic Programming/009. Min Cost Climbing Stairs/code.cpp:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public:
3 |
4 | int min(int a, int b) {
5 | return acost, vector&dp, int index) {
9 | if(index>=cost.size())
10 | return 0 ;
11 |
12 | if(dp[index]!=-1)
13 | return dp[index] ;
14 |
15 | return dp[index] = cost[index] + min(solve(cost,dp,index+1), solve(cost,dp,index+2)) ;
16 | }
17 |
18 | int minCostClimbingStairs(vector& cost) {
19 | int n = cost.size() ;
20 |
21 | /*vectordp(n+1,-1) ;
22 | return min(solve(cost,dp,0), solve(cost,dp,1)) ;*/
23 |
24 | int dp[n] ;
25 |
26 | dp[0] = cost[0] ;
27 | dp[1] = cost[1] ;
28 |
29 | for(int i=2;i