├── 01-Java
├── Dynamic Programming
│ └── G
├── Key Concepts
│ ├── Prefix Sum
│ │ └── Readme.md
│ ├── BIT Manipulation
│ │ ├── Theory
│ │ │ ├── Gg
│ │ │ ├── GetBit.java
│ │ │ ├── ClearBits.java
│ │ │ ├── UpdateBit.java
│ │ │ └── SetBits.java
│ │ └── Applications
│ │ │ ├── OptimizedOddEven.java
│ │ │ └── FastExponentation.java
│ ├── Important Algorithms
│ │ ├── ACIIcodeOfCharacter.java
│ │ ├── SwappingAlgorithm.java
│ │ └── FrequencyCounterUsingHashMap.java
│ ├── Kadane'sAlgorithm.java
│ ├── Two Pointer Approach
│ │ ├── Linked List
│ │ │ ├── FindMiddleOfList.java
│ │ │ └── FindKthNodefromLast.java
│ │ └── String
│ │ │ └── PalindromeString.java
│ ├── Sliding Window
│ │ └── MAXsubArraySUM.java
│ └── Divide And Conquer
│ │ ├── RotatedBinarySearch.java
│ │ ├── MergeSort.java
│ │ └── QuickSort.java
├── OOPs
│ ├── Interfaces
│ │ ├── Default Method
│ │ │ ├── Readme.md
│ │ │ └── DefaultMethods.java
│ │ ├── Interfaces.java
│ │ ├── InheritanceInInterfaces.java
│ │ └── InterfaceDemonstration.java
│ ├── Abstract Classes
│ │ ├── AbstractClasses.java
│ │ ├── Readme.md
│ │ └── AbstractMethod.java
│ ├── Key Concepts
│ │ ├── MethodOveriding.java
│ │ ├── MethodOverloading.java
│ │ ├── DynamicDispatch.java
│ │ ├── Getter&Putter.java
│ │ └── MethodOverriding2.java
│ ├── Basics
│ │ ├── OOPsBasics1.java
│ │ ├── OOPsBasics3.java
│ │ ├── OOPsBasics2.java
│ │ └── OOPsBasics4.java
│ ├── Inheritance
│ │ ├── ThisAndSuper.java
│ │ └── Inheritance.java
│ ├── Constructors
│ │ ├── Readme.md
│ │ └── Constructors.java
│ └── Assessment
│ │ ├── RPSGameusingOOPs.java
│ │ └── GuessTheNumberGame.java
├── Data Structure
│ ├── Linked List
│ │ ├── LeetCode Top Questions
│ │ │ └── Coming soon
│ │ ├── LLReveralusingCOLLECTION.java
│ │ ├── LLClass.java
│ │ ├── LLusingCOLLECTIONS.java
│ │ ├── LLRecursiveReversal.java
│ │ ├── LLIterativeReversal.java
│ │ └── LLMiddleOperations.java
│ ├── Stack
│ │ ├── Custom Stack Class
│ │ │ ├── Exception.java
│ │ │ ├── StackMain.java
│ │ │ ├── DynamicStack.java
│ │ │ └── CustomStack.java
│ │ ├── StackUsingCOLLECTIONS.java
│ │ ├── LeetCode Top Questions
│ │ │ ├── Problem1.java
│ │ │ └── Problem2.java
│ │ ├── StackUsingArrayList.java
│ │ └── Applications
│ │ │ └── STACKasENCODER.java
│ ├── Trees
│ │ ├── AVL Trees
│ │ │ └── Main.java
│ │ ├── Segment Tree
│ │ │ └── Main.java
│ │ ├── Important Tree Algorithms
│ │ │ ├── SumOfNode.java
│ │ │ ├── HeightsCalculation.java
│ │ │ ├── NumberOfNode.java
│ │ │ └── DiameterCalculation.java
│ │ ├── Breathe First Search
│ │ │ └── Breathe-First-Search.java
│ │ ├── Building A Tree
│ │ │ └── FromPreOrderSequence.java
│ │ ├── Tree Traversals
│ │ │ ├── InOrderTraversal.java
│ │ │ ├── PostOrderTranversal.java
│ │ │ └── PreOrderTraversal.java
│ │ └── LeetCode Questions
│ │ │ └── 105. Construct Binary Tree from Preorder and Inorder Traversal.md
│ ├── Heap ( PriorityQueue )
│ │ ├── Heaps Custom Class
│ │ │ └── HeapMain.java
│ │ └── Priority Queue
│ │ │ ├── MinHeapwithPriorityQueue.java
│ │ │ ├── MaxHeapwithPriorityQueue.java
│ │ │ └── HeapSortWithPriorityQueue.java
│ ├── String
│ │ ├── String Builder
│ │ │ ├── AppendCharacter.java
│ │ │ ├── DeleteCharacter.java
│ │ │ ├── StringReversal.java
│ │ │ ├── InsertCharacter.java
│ │ │ └── StringBuilderAllOperations.java
│ │ ├── LeetCode Top Question
│ │ │ ├── PalindromeString.java
│ │ │ ├── ShortestPath.java
│ │ │ ├── StringComparison.java
│ │ │ └── ConvertStringToUppercase.java
│ │ └── StringComparison.java
│ ├── Arrays
│ │ ├── 1D Arrays
│ │ │ ├── LargestNumInArray.java
│ │ │ ├── ArrayBasics.java
│ │ │ ├── MSSKadane'sAlgorithm.java
│ │ │ ├── MaxSumSubArray(MSS).java
│ │ │ ├── ArrayReversal.java
│ │ │ └── MSSOptimized.java
│ │ └── 2D Arrays
│ │ │ ├── 2DArraysBasics.java
│ │ │ ├── 2DArrayDiagonalSum.java
│ │ │ └── 2DArraySeraching.java
│ ├── Hash Map
│ │ ├── IteratingOverHastMap.java
│ │ ├── Top Leetcode Questions
│ │ │ ├── SubArraySumPrefixSum.java
│ │ │ └── GroupAnagrams.java
│ │ └── HashMapOperation.java
│ ├── Queue
│ │ ├── Custom Queue Class
│ │ │ ├── QueueMain.java
│ │ │ └── DynamicQueue.java
│ │ ├── QueueUsingCOLLECTIONS.java
│ │ └── LeetCode Top Questions
│ │ │ └── Problem1.java
│ ├── ArrayList
│ │ ├── ArrayListSorting.java
│ │ └── MultidimensionalArrayList.java
│ ├── Hash Set
│ │ ├── HashSetIterator.java
│ │ ├── HashSet Algorithms
│ │ │ └── RemoveDuplicates.java
│ │ └── HashSetBasics.java
│ ├── Graphs
│ │ └── Creating a Graph
│ │ │ └── UsingAdjacencyList.java
│ └── Readme.md
├── Recursion
│ ├── Readme.md
│ ├── Simple Recursion Problems
│ │ ├── SumOfDigits.java
│ │ ├── ProductOfDigits.java
│ │ ├── CountZerosInNumber.java
│ │ └── ReverseTheNumber.java
│ ├── Concepts
│ │ ├── 2-RecursionBasics.java
│ │ └── 3-RecursionBasics.java
│ ├── Recursive Sorting
│ │ ├── BubbleSort.java
│ │ ├── SelectionSort.java
│ │ ├── MergeSort.java
│ │ └── QuickSort.java
│ ├── Patterns Using Recursion
│ │ ├── PatternUsingRecursion2.java
│ │ └── PatternUsingRecursion.java
│ ├── Subsets
│ │ ├── SubsetsOfArray.java
│ │ └── SubsetsOfArrayWithDuplicates.java
│ └── Backtracking
│ │ └── Maze Problems
│ │ ├── MazeWithPathRestrictions.java
│ │ └── MazeThreeSide.java
├── Essentials
│ ├── Lists.jpg
│ ├── Logo.jpg
│ ├── Logo.webp
│ ├── Maps.jpg
│ ├── Queue.jpg
│ ├── Sets.jpg
│ ├── Javacup.jpg
│ ├── Flowchart.png
│ ├── Framework.jpg
│ ├── JavaBook.jpg
│ ├── ListFlow.jpeg
│ ├── Recurrflow.png
│ ├── Recursion.png
│ ├── SetFlow1.png
│ ├── SlidingWindow.webp
│ └── Readme.md
├── Time & Space Complexity
│ ├── TimeComplexityAnalysis.pdf
│ └── Space Complexity Analysis.pdf
├── Java Basics
│ ├── Iterative Statements
│ │ ├── Loops
│ │ │ ├── ForLoop.java
│ │ │ ├── WhileLoop.java
│ │ │ ├── DoWhileLoop.java
│ │ │ ├── NestedForLoop.java
│ │ │ ├── ForEachLoop.java
│ │ │ └── Break&Continue.java
│ │ └── Patterns
│ │ │ ├── Pattern-3.java
│ │ │ ├── Pattern-4.java
│ │ │ ├── Pattern-7.java
│ │ │ ├── Pattern-2.java
│ │ │ ├── Patterns-10.java
│ │ │ ├── Pattern-5.java
│ │ │ ├── Pattern-1.java
│ │ │ ├── Patterns-11.java
│ │ │ ├── Pattern-8.java
│ │ │ ├── Pattern-9.java
│ │ │ ├── Pattern-6.java
│ │ │ └── Readme.md
│ ├── Conditional Statements
│ │ ├── If..Statement.java
│ │ ├── If...else..Statement.java
│ │ └── If...elseif...else.java
│ └── Switch-Case Statements
│ │ └── Swtich-Case.java
├── COLLECTIONS Framework
│ └── Implementations
│ │ ├── LLReveralusingCOLLECTION.java
│ │ ├── StackUsingCOLLECTIONS.java
│ │ ├── LLusingCOLLECTIONS.java
│ │ └── QueueUsingCOLLECTIONS.java
├── Methods(Functions)
│ ├── Basics
│ │ ├── BasicMethod.java
│ │ ├── MultipleTwoNumber.java
│ │ └── CalculateFactorial.java
│ ├── MethodWithParameters.java
│ ├── MethodWithReturnValue.java
│ ├── RecursiveMethod.java
│ ├── OverloadedMethod.java
│ └── Readme.md
├── Sorting Algorithms
│ ├── Readme.md
│ ├── CountingSort.java
│ ├── MergeSort.java
│ ├── InserationSort.java
│ ├── QuickSort.java
│ └── SelectionSort.java
├── Searching Algorithms
│ ├── LinearSearch.java
│ ├── OrderAgnosticBinarySearch.java
│ └── BinarySearch.java
└── Problem Solving Patterns
│ └── Two Pointer Approach
│ ├── Linked List
│ ├── FindMiddleOfList.java
│ └── FindKthNodefromLast.java
│ ├── String
│ └── PalindromeString.java
│ └── Arrays
│ ├── DeleteDuplicatesInSortedArray.java
│ ├── DeleteDuplicatesInSortedArray-ll.java
│ └── MovingZeros.java
├── 02-C and C++
├── C++
│ ├── Data Structures
│ │ ├── Strings
│ │ │ └── String
│ │ │ │ └── Teml
│ │ └── Arrays
│ │ │ ├── 1D Arrays
│ │ │ ├── Kadane's_Algorithm.cpp
│ │ │ └── ArraysBasic.cpp
│ │ │ └── Matrix ( 2D Array )
│ │ │ ├── Accept_and_Display_Matrix.cpp
│ │ │ └── Matrix_Addition.cpp
│ ├── Operators
│ │ ├── 02_Extraction_Operator.cpp
│ │ ├── 05_Logical_Operator.cpp
│ │ ├── 01_Insertion_Operator.cpp
│ │ ├── 06_Comparison_Operator.cpp
│ │ └── 03_Arthematic_Operator.cpp
│ ├── Searching Algorithms
│ │ └── Linear_Search.cpp
│ └── Sorting Algorithms
│ │ ├── Bubble_Sort.cpp
│ │ ├── Insertion_Sort.cpp
│ │ └── Selection_Sort.cpp
└── C
│ ├── Assests
│ ├── Readme.md
│ └── C.png
│ ├── Time & Space Complexity
│ ├── TimeComplexityAnalysis.pdf
│ └── Time & Space Complexity.pdf
│ ├── Control Statement
│ ├── Conditional Statement
│ │ ├── ifStatement.c
│ │ ├── if-elseStatement.c
│ │ ├── ConditionalOperator.c
│ │ ├── Ladder..if..else.c
│ │ ├── FizzBuzzAssignment.c
│ │ ├── TriangleAssignment.c
│ │ └── ResultsAssessments.c
│ ├── Iterative Statements
│ │ ├── ForLoop.c
│ │ ├── DoWhileLoop.c
│ │ ├── WhileLoop.c
│ │ ├── Patterns
│ │ │ ├── Pattern-1.c
│ │ │ ├── Pattern-3.c
│ │ │ └── Pattern-2.c
│ │ ├── FactorialUsingLoop.c
│ │ ├── NestedForLoop.c
│ │ ├── SummationUsingLoops.c
│ │ ├── MultipleTableUsingNesredForLoop.c
│ │ ├── GCD&LCMusingLoops.c
│ │ ├── DoWhileLoop...ExitLogic.c
│ │ └── TablesUsingLoops.c
│ └── Switch-case
│ │ └── Switch-case.c
│ ├── Important Algorithms
│ ├── FactorialCalculation.c
│ ├── SwapingAlgorithm.c
│ └── FibonacciSeries.c
│ ├── Array
│ ├── 1D Array
│ │ ├── TraversingArray.c
│ │ └── ArrayBasics.c
│ └── Structure Array
│ │ ├── TraversingStructureArray.c
│ │ └── StructureArrayBasics.c
│ ├── Sorting Algorithm
│ ├── BubbleSort.c
│ ├── SelectionSort.c
│ └── InserationSort.c
│ ├── Stack
│ ├── STACKasDCBconverter.c
│ ├── STACKAsADT.c
│ └── STACKforWellFormatednessOfparenthsis.c
│ ├── String
│ ├── StringOperations-2.c
│ └── StringOperations.c
│ ├── Searching Algorithms
│ ├── LinearSearch.c
│ └── BinarySearch.c
│ ├── LinkedList
│ └── Singly LinkedList
│ │ ├── BasicLLCreations.c
│ │ ├── IterativeLLCreations.c
│ │ └── CountingNode.c
│ ├── Functions
│ └── FunctionConcepts.c
│ └── Special Codes
│ └── Binary_Adder.c
├── Assests
├── 106.jpg
├── 116.png
├── DSA.jpg
├── BigO.png
├── BigO2.webp
├── DataSA.png
├── Time.webp
├── keypad.png
├── queens.jpg
├── BSvsLS.webp
├── DsaRoad.jpg
├── dsa_logo.jpg
├── mpsexam1.jpg
├── mpsexam2.jpg
├── DSA_Roadmap.jpg
├── dsa_logo_2.jpg
└── dsa_logo_3.jpg
└── Roadmap
└── DSA Roadmap.pdf
/01-Java/Dynamic Programming/G:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/01-Java/Key Concepts/Prefix Sum/Readme.md:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/01-Java/Key Concepts/BIT Manipulation/Theory/Gg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Interfaces/Default Method/Readme.md:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/02-C and C++/C++/Data Structures/Strings/String/Teml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/02-C and C++/C/Assests/Readme.md:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Linked List/LeetCode Top Questions/Coming soon:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Assests/106.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/106.jpg
--------------------------------------------------------------------------------
/Assests/116.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/116.png
--------------------------------------------------------------------------------
/Assests/DSA.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/DSA.jpg
--------------------------------------------------------------------------------
/01-Java/Recursion/Readme.md:
--------------------------------------------------------------------------------
1 | >[!WARNING]
2 | > # Don't Take This Shit Casually , Will Regret a Lot !!!!!!!!!
3 |
--------------------------------------------------------------------------------
/Assests/BigO.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/BigO.png
--------------------------------------------------------------------------------
/Assests/BigO2.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/BigO2.webp
--------------------------------------------------------------------------------
/Assests/DataSA.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/DataSA.png
--------------------------------------------------------------------------------
/Assests/Time.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/Time.webp
--------------------------------------------------------------------------------
/Assests/keypad.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/keypad.png
--------------------------------------------------------------------------------
/Assests/queens.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/queens.jpg
--------------------------------------------------------------------------------
/Assests/BSvsLS.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/BSvsLS.webp
--------------------------------------------------------------------------------
/Assests/DsaRoad.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/DsaRoad.jpg
--------------------------------------------------------------------------------
/Assests/dsa_logo.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/dsa_logo.jpg
--------------------------------------------------------------------------------
/Assests/mpsexam1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/mpsexam1.jpg
--------------------------------------------------------------------------------
/Assests/mpsexam2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/mpsexam2.jpg
--------------------------------------------------------------------------------
/Assests/DSA_Roadmap.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/DSA_Roadmap.jpg
--------------------------------------------------------------------------------
/Assests/dsa_logo_2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/dsa_logo_2.jpg
--------------------------------------------------------------------------------
/Assests/dsa_logo_3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Assests/dsa_logo_3.jpg
--------------------------------------------------------------------------------
/Roadmap/DSA Roadmap.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/Roadmap/DSA Roadmap.pdf
--------------------------------------------------------------------------------
/01-Java/Essentials/Lists.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/Lists.jpg
--------------------------------------------------------------------------------
/01-Java/Essentials/Logo.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/Logo.jpg
--------------------------------------------------------------------------------
/01-Java/Essentials/Logo.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/Logo.webp
--------------------------------------------------------------------------------
/01-Java/Essentials/Maps.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/Maps.jpg
--------------------------------------------------------------------------------
/01-Java/Essentials/Queue.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/Queue.jpg
--------------------------------------------------------------------------------
/01-Java/Essentials/Sets.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/Sets.jpg
--------------------------------------------------------------------------------
/02-C and C++/C/Assests/C.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/02-C and C++/C/Assests/C.png
--------------------------------------------------------------------------------
/01-Java/Essentials/Javacup.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/Javacup.jpg
--------------------------------------------------------------------------------
/01-Java/Essentials/Flowchart.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/Flowchart.png
--------------------------------------------------------------------------------
/01-Java/Essentials/Framework.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/Framework.jpg
--------------------------------------------------------------------------------
/01-Java/Essentials/JavaBook.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/JavaBook.jpg
--------------------------------------------------------------------------------
/01-Java/Essentials/ListFlow.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/ListFlow.jpeg
--------------------------------------------------------------------------------
/01-Java/Essentials/Recurrflow.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/Recurrflow.png
--------------------------------------------------------------------------------
/01-Java/Essentials/Recursion.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/Recursion.png
--------------------------------------------------------------------------------
/01-Java/Essentials/SetFlow1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/SetFlow1.png
--------------------------------------------------------------------------------
/01-Java/Essentials/SlidingWindow.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Essentials/SlidingWindow.webp
--------------------------------------------------------------------------------
/01-Java/Time & Space Complexity/TimeComplexityAnalysis.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Time & Space Complexity/TimeComplexityAnalysis.pdf
--------------------------------------------------------------------------------
/01-Java/Time & Space Complexity/Space Complexity Analysis.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/01-Java/Time & Space Complexity/Space Complexity Analysis.pdf
--------------------------------------------------------------------------------
/02-C and C++/C/Time & Space Complexity/TimeComplexityAnalysis.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/02-C and C++/C/Time & Space Complexity/TimeComplexityAnalysis.pdf
--------------------------------------------------------------------------------
/02-C and C++/C/Time & Space Complexity/Time & Space Complexity.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Atharvkote/data-structures-and-algorithms/HEAD/02-C and C++/C/Time & Space Complexity/Time & Space Complexity.pdf
--------------------------------------------------------------------------------
/01-Java/Key Concepts/Important Algorithms/ACIIcodeOfCharacter.java:
--------------------------------------------------------------------------------
1 | public class Ascii {
2 | public static void main(String[] args) {
3 | char ch = 'a';
4 | System.out.println((char)(ch + 1));
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Stack/Custom Stack Class/Exception.java:
--------------------------------------------------------------------------------
1 | package com.kunal;
2 |
3 | public class StackException extends Exception{
4 |
5 | public StackException(String message) {
6 | super(message);
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Conditional Statement/ifStatement.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int a=10;
5 | int b=20;
6 | if(a
2 | int main()
3 | {
4 | int i ;
5 | for(i=1 ; i<10 ; i++){
6 | printf("Hello World !!");
7 | //This Loop will print Hello World Prompt 10 times
8 | }
9 | return 0 ;
10 | }
--------------------------------------------------------------------------------
/01-Java/Data Structure/Trees/AVL Trees/Main.java:
--------------------------------------------------------------------------------
1 | class Main {
2 | public static void main(String[] args) {
3 | AVL tree = new AVL();
4 |
5 | for(int i=0; i < 1000; i++) {
6 | tree.insert(i);
7 | }
8 | System.out.println(tree.height());
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Loops/WhileLoop.java:
--------------------------------------------------------------------------------
1 | public class WhileLoop {
2 | public static void main(String[] args) {
3 | int i = 10;
4 | while (i>0) {
5 | System.out.println(i);
6 | i--;
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Loops/DoWhileLoop.java:
--------------------------------------------------------------------------------
1 | public class DoWhileLoop {
2 | public static void main(String[] args) {
3 | int i = 10;
4 | do {
5 | System.out.println(i);
6 | i--;
7 | } while (i>0);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Conditional Statement/if-elseStatement.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int a=10;
5 | int b=20;
6 | if(a list2 = new LinkedList<>();
5 | list2.add(1);
6 | list2.add(2);
7 | Collections.reverse(list2);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Iterative Statements/DoWhileLoop.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int i;
5 | i = 1;
6 | do{
7 | printf("Hello World\n");
8 | i++;
9 | //this loop will print "Hello World " Prompt 10 times
10 | }while(i !=10);
11 | return 0;
12 | }
--------------------------------------------------------------------------------
/01-Java/COLLECTIONS Framework/Implementations/LLReveralusingCOLLECTION.java:
--------------------------------------------------------------------------------
1 | import java.util.Collections;
2 | class LL {
3 | public static void main(String[] args){
4 | LinkedList list2 = new LinkedList<>();
5 | list2.add(1);
6 | list2.add(2);
7 | Collections.reverse(list2);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Iterative Statements/WhileLoop.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int i;
5 | i = 1;
6 | while (i != 10)
7 | {
8 | printf("Hello World\n");
9 | i++;
10 | //this loop will print "Hello World " Prompt 10 times
11 | }
12 | return 0;
13 | }
--------------------------------------------------------------------------------
/01-Java/OOPs/Abstract Classes/AbstractClasses.java:
--------------------------------------------------------------------------------
1 | abstract class Phone{
2 | abstract void on();
3 | }
4 | class SmartPhone extends Phone{
5 | void run(){
6 | System.out.println("Turning on...");
7 | }
8 | public static void main(String args[]){
9 | Phone obj = new SmartPhone();
10 | obj.on();
11 | }
12 | }
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Conditional Statement/ConditionalOperator.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 |
4 | {
5 | int a, b;
6 | printf("Enter Your Age");
7 | scanf("%d", &a);
8 | (a >=18) ? printf("candidate is eligible for voting") : printf("candidate is not eligible for voting");
9 | return 0;
10 | }
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Iterative Statements/Patterns/Pattern-1.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int i, j;
5 | for (i = 0; i <= 4; i++)
6 | {
7 | for (j = 0; j <= i; j++)
8 | {
9 | printf("%d", j);
10 | }
11 | printf("\n");
12 | }
13 | return 0;
14 | }
--------------------------------------------------------------------------------
/02-C and C++/C++/Operators/02_Extraction_Operator.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main() {
5 | int a, b;
6 |
7 | cout << "Enter Two Numbers: ";
8 | cin >> a >> b;
9 |
10 | cout << "Value of a is " << a << endl;
11 | cout << "Value of b is " << b << endl;
12 | return 0;
13 | }
14 |
--------------------------------------------------------------------------------
/02-C and C++/C/Important Algorithms/FactorialCalculation.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int factorial(int n) {
4 | if (n == 0) {
5 | return 1;
6 | }
7 | return n * factorial(n - 1);
8 | }
9 |
10 | int main() {
11 | int n = 5;
12 | printf("Factorial of %d is %d\n", n, factorial(n));
13 | return 0;
14 | }
15 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Trees/Important Tree Algorithms/SumOfNode.java:
--------------------------------------------------------------------------------
1 | public static int sumOfNodes(Node root) {
2 |
3 | if(root == null) {
4 | return 0;
5 | }
6 |
7 | int leftSum = sumOfNodes(root.left);
8 | int rightSum = sumOfNodes(root.right);
9 |
10 | return leftSum + rightSum + root.data;
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/01-Java/Methods(Functions)/Basics/BasicMethod.java:
--------------------------------------------------------------------------------
1 | public class BasicFunction {
2 | // Method that prints a greeting
3 | public static void printGreeting() {
4 | System.out.println("Hello, World!");
5 | }
6 |
7 | public static void main(String[] args) {
8 | // Calling the method
9 | printGreeting();
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Iterative Statements/FactorialUsingLoop.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int fact = 1, i, a;
5 | printf("Enter your number");
6 | scanf("%d", &a);
7 | for (i = 1; i < a + 1; i++)
8 | {
9 | fact *= i;
10 | }
11 | printf("Factorial of %d is %d ", a, fact);
12 | return 0;
13 | }
--------------------------------------------------------------------------------
/01-Java/Data Structure/Trees/Important Tree Algorithms/HeightsCalculation.java:
--------------------------------------------------------------------------------
1 | public static int height(Node root) {
2 |
3 | if(root == null) {
4 | return 0;
5 | }
6 |
7 | int leftHeight = height(root.left);
8 | int rightHeight = height(root.right);
9 |
10 | return Math.max(leftHeight, rightHeight) + 1;
11 | }
12 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Trees/Important Tree Algorithms/NumberOfNode.java:
--------------------------------------------------------------------------------
1 | public static int countOfNodes(Node root) {
2 |
3 | if(root == null) {
4 | return 0;
5 | }
6 |
7 | int leftNodes = countOfNodes(root.left);
8 | int rightNodes = countOfNodes(root.right);
9 |
10 | return leftNodes + rightNodes + 1;
11 | }
12 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Loops/NestedForLoop.java:
--------------------------------------------------------------------------------
1 | public class Nested {
2 | public static void main(String[] args) {
3 | for (int i=1; i<5; i++) {
4 | for (int j=1; j<5; j++) {
5 | System.out.println(i+" * "+j+" = "+i*j);
6 | }
7 | System.out.println();
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Iterative Statements/NestedForLoop.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int i, j;
5 | for (i = 0; i <= 2; i++)
6 | {
7 | for (j = 0; j <= 2; j++)
8 | {
9 | printf("%d ", j);
10 | //this Code will print a 3x3 Matrix
11 | }
12 | printf("\n");
13 | }
14 | }
--------------------------------------------------------------------------------
/01-Java/Key Concepts/BIT Manipulation/Applications/OptimizedOddEven.java:
--------------------------------------------------------------------------------
1 | public class OddEven {
2 | public static void main(String args[]) {
3 | int n = 10;
4 | int bitMask = 1;
5 | if((n & bitMask) == 0) {
6 | System.out.println("even");
7 | } else {
8 | System.out.println("odd");
9 | }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Simple Recursion Problems/SumOfDigits.java:
--------------------------------------------------------------------------------
1 | public class DigitSum {
2 | public static void main(String[] args) {
3 | int ans = sum(1342);
4 | System.out.println(ans);
5 | }
6 |
7 | static int sum(int n) {
8 | if (n == 0) {
9 | return 0;
10 | }
11 | return (n % 10) + sum(n / 10);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Heap ( PriorityQueue )/Heaps Custom Class/HeapMain.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 |
3 | class Main {
4 | public static void main(String[] args) throws Exception{
5 | Heap heap = new Heap<>();
6 |
7 | heap.insert(34);
8 | heap.insert(45);
9 | heap.insert(22);
10 | heap.insert(89);
11 | heap.insert(76);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Iterative Statements/SummationUsingLoops.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int sum = 0, i, a;
5 | printf(" • Enter The Last Number Till You Want Sum : ");
6 | scanf("%d", &a);
7 | for (i = 1; i < a + 1; i++)
8 | {
9 | sum += i;
10 | }
11 | printf("SUM Of 1 To %d is %d ", a, sum);
12 | return 0;
13 | }
--------------------------------------------------------------------------------
/02-C and C++/C/Important Algorithms/SwapingAlgorithm.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 |
4 | {
5 | int b, c;
6 | printf("enter value of a and b");
7 | scanf("%d%d", &a, &b);
8 | c = a;
9 | a = b; //Temporary Variable "c".....
10 | b = c;
11 | printf("value of a is %d", a);
12 |
13 | printf("\nvalue of b is %d", b);
14 | return 0;
15 | }
16 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/String/String Builder/AppendCharacter.java:
--------------------------------------------------------------------------------
1 | // Append a char
2 | //Append means to add something at the end.
3 |
4 | import java.util.*;
5 | public class Strings {
6 | public static void main(String args[]) {
7 | StringBuilder sb = new StringBuilder("Tony");
8 | sb.append(" Stark");
9 | System.out.println(sb);
10 | }
11 |
12 | }
13 |
14 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Simple Recursion Problems/ProductOfDigits.java:
--------------------------------------------------------------------------------
1 | public class DigitProduct {
2 | public static void main(String[] args) {
3 | int ans = prod(505);
4 | System.out.println(ans);
5 | }
6 |
7 | static int prod(int n) {
8 | if (n%10 == n) {
9 | return n;
10 | }
11 | return (n%10) * prod(n/10);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Loops/ForEachLoop.java:
--------------------------------------------------------------------------------
1 | public class ForeachExample {
2 | public static void main(String[] args) {
3 | int[] numbers = {1, 2, 3, 4, 5};
4 |
5 | // Using foreach loop to iterate over the array
6 | for (int number : numbers) {
7 | System.out.println(number);
8 | // Output :[ 1 2 3 4 5 ]
9 | }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Concepts/2-RecursionBasics.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | // Compiler version JDK 11.0.2
4 |
5 | public class Recursion
6 | {
7 | public static void hello(int n){
8 | if(n==0){
9 | return;
10 | }
11 | System.out.println(n);
12 | hello(n-1);
13 | }
14 | public static void main(String args[])
15 | {
16 | int n=10;
17 | hello(n);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Conditional Statement/Ladder..if..else.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int a=10;
5 |
6 | if(a<0)
7 | {
8 | printf("Number is Negative");
9 | }
10 | else if(a>0){
11 | printf("Number is Positive");
12 |
13 | }
14 | else{
15 | printf("Number is Zero");
16 | // Only Remaining case that is (a=0)
17 | }
18 |
19 | return 0 ;
20 | }
21 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Patterns/Pattern-3.java:
--------------------------------------------------------------------------------
1 | public class Patterns {
2 | public static void inverted_halfPyramid_withNumbers(int n) {
3 | for(int i=1; i<=n; i++) {
4 | //numbers
5 | for(int j=1; j<=n-i+1; j++) {
6 | System.out.print(j);
7 | }
8 | //spaces...
9 | System.out.println();
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/02-C and C++/C/Important Algorithms/FibonacciSeries.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int fibonacci(int n) {
4 | if (n <= 1) {
5 | return n;
6 | }
7 | return fibonacci(n-1) + fibonacci(n-2);
8 | }
9 |
10 | int main() {
11 | int n = 10;
12 | printf("Fibonacci Series up to %d terms: ", n);
13 | for (int i = 0; i < n; i++) {
14 | printf("%d ", fibonacci(i));
15 | }
16 | return 0;
17 | }
18 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Conditional Statements/If..Statement.java:
--------------------------------------------------------------------------------
1 | /*
2 | if Statement Syntax : ------>
3 |
4 | if (condition) {
5 | //block of code
6 |
7 | */
8 |
9 | public class JavaIf {
10 | public static void main(String[] args) {
11 | String name = "Mohan";
12 | int Roll = 25;
13 | if (name == "Mohan" && Roll == 25) {
14 | System.out.println("Details of Mohan.");
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/01-Java/Methods(Functions)/MethodWithParameters.java:
--------------------------------------------------------------------------------
1 | public class MethodWithParameters {
2 | // Method that prints a personalized greeting
3 | public static void printPersonalizedGreeting(String name) {
4 | System.out.println("Hello, " + name + "!");
5 | }
6 |
7 | public static void main(String[] args) {
8 | // Calling the method with a parameter
9 | printPersonalizedGreeting("Alice");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Iterative Statements/Patterns/Pattern-3.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | int main()
4 | {
5 | int a, i, j;
6 | char b[100]="AtharvaKote";
7 |
8 | a = strlen(b);
9 |
10 | for (i = 0; i <= a; i++)
11 | {
12 | for (j = 0; j < i; j++)
13 | {
14 | printf(" %c", b[j]);
15 | }
16 | printf("\n");
17 | }
18 |
19 | return 0;
20 | }
21 |
--------------------------------------------------------------------------------
/01-Java/Methods(Functions)/MethodWithReturnValue.java:
--------------------------------------------------------------------------------
1 | public class MethodWithReturnValue {
2 | // Method that adds two numbers and returns the result
3 | public static int add(int a, int b) {
4 | return a + b;
5 | }
6 |
7 | public static void main(String[] args) {
8 | // Calling the method and storing the result
9 | int result = add(5, 3);
10 | System.out.println("The sum is: " + result);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Linked List/LLClass.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | class LL {
4 | public static void main(String args[]) {
5 | LinkedList list = new LinkedList();
6 |
7 | list.addFirst("Atharva");
8 | list.addFirst("name");
9 | list.addFirst("my");
10 | System.out.println(list);
11 |
12 | // Add First Operation
13 | list.add(2, "is");
14 | System.out.println(list);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/String/String Builder/DeleteCharacter.java:
--------------------------------------------------------------------------------
1 | // Delete char at some Index
2 |
3 | import java.util.*;
4 | public class Strings {
5 | public static void main(String args[]) {
6 | StringBuilder sb = new StringBuilder("tony");
7 | //Insert char
8 | sb.insert(0, 'S');
9 | System.out.println(sb);
10 | //delete char
11 | sb.delete(0, 1);
12 | System.out.println(sb);
13 | }
14 |
15 | }
16 |
17 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Stack/StackUsingCOLLECTIONS.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class StackJCF {
4 | public static void main(String args[]) {
5 | Stack stack = new Stack<>();
6 | stack.push(1);
7 | stack.push(2);
8 | stack.push(3);
9 | stack.push(4);
10 |
11 | while(!stack.isEmpty()) {
12 | System.out.println(stack.peek());
13 | stack.pop();
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Patterns/Pattern-4.java:
--------------------------------------------------------------------------------
1 | public class Patterns {
2 | public static void floyds_triangle(int n) {
3 | int number = 1;
4 | for(int i=1; i<=n; i++) {
5 | //numbers
6 | for(int j=1; j<=i; j++) {
7 | System.out.print(number +" ");
8 | number++;
9 | }
10 | //spaces ...
11 | System.out.println();
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/02-C and C++/C/Array/1D Array/TraversingArray.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | // Declaring an array of integers with size 5
5 | int numbers[5] = {10, 20, 30, 40, 50};
6 |
7 | // Traversing the array using a for loop
8 | printf("Array elements:\n");
9 | for (int i = 0; i < 5; i++) {
10 | printf("Element %d: %d\n", i, numbers[i]);
11 | }
12 |
13 | return 0;
14 | }
15 | //Output :
16 | 10
17 | 20
18 | 30
19 | 40
20 | 50
21 |
--------------------------------------------------------------------------------
/01-Java/COLLECTIONS Framework/Implementations/StackUsingCOLLECTIONS.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class StackJCF {
4 | public static void main(String args[]) {
5 | Stack stack = new Stack<>();
6 | stack.push(1);
7 | stack.push(2);
8 | stack.push(3);
9 | stack.push(4);
10 |
11 | while(!stack.isEmpty()) {
12 | System.out.println(stack.peek());
13 | stack.pop();
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Arrays/1D Arrays/LargestNumInArray.java:
--------------------------------------------------------------------------------
1 | // Print largest number in array
2 | package Arrays;
3 |
4 | public class LargestNumber {
5 | public static void main(String args[]) {
6 | int arr[] = {1, 2, 3, 4, 8, 3, 2, 5};
7 | int largest = arr[0];
8 | for(int i=0; i
2 | int main()
3 | {
4 | int i, j, k, a;
5 | printf("enter them number till you want tables");
6 | scanf("%d", &a);
7 | for (i =1; i < a + 1; i++)
8 | {
9 | printf("\tTable of %d\n", i);
10 | for (j = 1; j < 11; j++)
11 | {
12 | k = i * j;
13 | printf("\t\t%dx%d=%d\n", i, j, k);
14 | }
15 | printf("\n");
16 | }
17 | return 0;
18 | }
19 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Patterns/Pattern-5.java:
--------------------------------------------------------------------------------
1 | public class Patterns {
2 | public static void zero_one_triangle(int n) {
3 | for(int i=1; i<=n; i++) {
4 | //binary bits
5 | for(int j=1; j<=i; j++) {
6 | if((i+j) % 2 == 0) { //even
7 | System.out.print("1");
8 | } else { //odd
9 | System.out.print("0");
10 | }
11 | }
12 | System.out.println();
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Patterns/Pattern-1.java:
--------------------------------------------------------------------------------
1 | public class Patterns {
2 | //Hollow Rectangle
3 | public static void hollowRectangle(int n, int m) {
4 | for(int i=1; i<=n; i++) {
5 | for(int j=1; j<=m; j++) {
6 | if(i == 1 || j == 1 || i == n || j == m) {
7 | System.out.print("*");
8 | } else {
9 | System.out.print(" ");
10 | }
11 | }
12 | System.out.println();
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Iterative Statements/GCD&LCMusingLoops.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void main()
4 | {
5 | int i, n1, n2, LCM, GCD;
6 |
7 | printf("Values of n1 and n2 are ");
8 | scanf("%d%d", &n1, &n2);
9 |
10 | for (i = 1; i <= n1 && i <= n2; i++)
11 | {
12 | if (n1 % i == 0 && n2 % i == 0)
13 | {
14 | GCD = i;
15 | }
16 | }
17 | LCM = (n1 * n2) / GCD;
18 | printf("GCD of %d and %d is %d\n", n1, n2, GCD);
19 | printf("LCM of %d and %d is %d", n1, n2, LCM);
20 | }
--------------------------------------------------------------------------------
/01-Java/Key Concepts/BIT Manipulation/Applications/FastExponentation.java:
--------------------------------------------------------------------------------
1 | public class FastExponentiation {
2 | //to calculate a^n
3 | public static int pow(int a, int n) {
4 | int ans = 1;
5 |
6 | while(n > 0) {
7 | if((n&1) != 0) { //checking bit is 0 or 1
8 | ans = ans * a;
9 | }
10 | a = a*a;
11 | n = n>>1;
12 | }
13 |
14 | return ans;
15 | }
16 | public static void main(String args[]) {
17 | System.out.println(pow(5,3));
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/01-Java/Methods(Functions)/RecursiveMethod.java:
--------------------------------------------------------------------------------
1 | public class RecursiveMethod {
2 | // Method to calculate factorial of a number
3 | public static int factorial(int n) {
4 | if (n <= 1) {
5 | return 1; // Base case
6 | } else {
7 | return n * factorial(n - 1); // Recursive case
8 | }
9 | }
10 |
11 | public static void main(String[] args) {
12 | // Calling the recursive method
13 | int result = factorial(5);
14 | System.out.println("Factorial of 5 is: " + result);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Interfaces/Default Method/DefaultMethods.java:
--------------------------------------------------------------------------------
1 | interface Animal{
2 | // Default method
3 | default void say(){
4 | System.out.println("Hello, this is default method");
5 | }
6 | // Abstract method
7 | void bark();
8 | }
9 | public class CWH implements Animal{
10 |
11 | @Override
12 | public void bark() {
13 | System.out.println("Dog barks!");
14 | }
15 | public static void main(String[] args) {
16 | CWH obj1 = new CWH();
17 | obj1.bark();
18 | obj1.say();
19 |
20 | }
21 |
22 |
23 | }
--------------------------------------------------------------------------------
/01-Java/OOPs/Key Concepts/MethodOverloading.java:
--------------------------------------------------------------------------------
1 | class calculate{
2 | int multiply(int a,int b){
3 | return a*b;
4 | }
5 | double multiply(double a,double b){
6 | return a*b;
7 | }
8 |
9 | public static void main(String[] args) {
10 |
11 | calculate obj = new calculate();
12 | int c = obj.multiply(5,4);
13 | double d = obj.multiply(5.1,4.2);
14 | System.out.println("Mutiply method : returns integer : " + c);
15 | System.out.println("Mutiply method : returns double : " + d);
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Iterative Statements/Patterns/Pattern-2.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main()
4 |
5 | {
6 | int i, j, k, l;
7 | for (i = 0; i < 5; i++)
8 | {
9 | for (j = 4 - i; j > 0; j--)
10 | {
11 | printf(" ");
12 | }
13 |
14 | for (k = i; k >= 0; k--)
15 | {
16 | printf("%d", k);
17 | }
18 |
19 | for (l = 1; l <= i; l++)
20 | {
21 | printf("%d", l);
22 | }
23 | printf("\n");
24 | }
25 |
26 | return 0;
27 | }
28 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Iterative Statements/DoWhileLoop...ExitLogic.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int fact = 1, i, a, v;
5 | do
6 | {
7 | printf(" • Enter your number Whose Factorial You Want To Calculate..");
8 | scanf("%d", &a);
9 | for (i = 1; i < a + 1; i++)
10 | {
11 | fact *= i;
12 | }
13 | printf("Factorial of %d is %d\n ", a, fact);
14 | printf("》 If You Want to Continue [Press 0]");
15 | scanf("%d", &v);
16 | } while (v == 0);
17 | return 0;
18 | }
19 |
--------------------------------------------------------------------------------
/02-C and C++/C++/Operators/05_Logical_Operator.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | int main() {
6 | int x = 10;
7 | int y = 20;
8 |
9 | // Logical AND
10 | if (x > 5 && y > 15) {
11 | cout << "Both conditions are true." << endl;
12 | }
13 |
14 | // Logical OR
15 | if (x > 15 || y > 15) {
16 | cout << "At least one condition is true." << endl;
17 | }
18 |
19 | // Logical NOT
20 | if (!(x > 15)) {
21 | cout << "x is not greater than 15." << endl;
22 | }
23 |
24 | return 0;
25 | }
26 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Iterative Statements/TablesUsingLoops.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int i, a, k, w;
5 | do
6 | {
7 | printf("Enter The Number Of Which Table You Want : ");
8 | scanf("%d", &a);
9 | printf("\t\t • Table of %d\n", a);
10 | for (i = 1; i < 11; i++)
11 | {
12 | k = a * i;
13 | printf("\t\t %d x %d = %d\n", a, i, k);
14 | }
15 | printf("》 If You Want to Continue [Press 0]");
16 | scanf("%d", &w);
17 | } while (w == 0);
18 | return 0;
19 | }
--------------------------------------------------------------------------------
/01-Java/Data Structure/String/LeetCode Top Question/PalindromeString.java:
--------------------------------------------------------------------------------
1 | //Problem : Check if a string is a palindrome
2 | public class Palindrome {
3 | public static boolean isPalindrome(String str) {
4 | for(int i=0; i e : map.entrySet()) {
9 |
10 | System.out.println(e.getKey());
11 | System.out.println(e.getValue());
12 |
13 | }
14 | //Iteration Method-(2)
15 |
16 | Set keys = map.keySet();
17 | for(String key : keys) {
18 | System.out.println(key+ " " + map.get(key));
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/String/String Builder/StringReversal.java:
--------------------------------------------------------------------------------
1 | //Reverse a String (using StringBuilder class)
2 |
3 | import java.util.*;
4 | public class Strings {
5 | public static void main(String args[]) {
6 | StringBuilder sb = new StringBuilder("HelloWorld");
7 | for(int i=0; i
2 | using namespace std;
3 |
4 | int main() {
5 |
6 | // Print a Prompt Message
7 | cout << "Hello World!";
8 |
9 | // Print On New Line
10 | cout << "Hello World!" << "\n";
11 | cout << "I am learning C++";
12 |
13 |
14 | // Printing on NewLine using endl
15 | cout << "Hello World!" << endl;
16 | cout << "I am learning C++";
17 |
18 | // Printing Values
19 | int a=10;
20 | int b=20;
21 |
22 | cout << "Value of a is " << a << endl;
23 | cout << "Value of b is " << b << endl;
24 | return 0;
25 | }
26 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Linked List/LLusingCOLLECTIONS.java:
--------------------------------------------------------------------------------
1 | import java.util.Collections;
2 | class LL {
3 | public static void main(String args[]) {
4 | LinkedList list = new LinkedList();
5 | list.add("is");
6 | list.add("a");
7 | list.addLast("list");
8 | list.addFirst("this");
9 | list.add(3, "linked");
10 | System.out.println(list);
11 | System.out.println(list.get(0));
12 | System.out.println(list.size());
13 | list.remove(3);
14 | list.removeFirst();
15 | list.removeLast();
16 | System.out.println(list);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Simple Recursion Problems/CountZerosInNumber.java:
--------------------------------------------------------------------------------
1 | public class CountZeros {
2 | public static void main(String[] args) {
3 | System.out.println(count(30210004));
4 | }
5 |
6 | static int count(int n) {
7 | return helper(n, 0);
8 | }
9 |
10 | // special pattern, how to pass a value to above calls
11 | private static int helper(int n, int c) {
12 | if (n == 0) {
13 | return c;
14 | }
15 |
16 | int rem = n % 10;
17 | if (rem == 0) {
18 | return helper(n/10, c+1);
19 | }
20 | return helper(n/10, c);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Queue/Custom Queue Class/QueueMain.java:
--------------------------------------------------------------------------------
1 |
2 |
3 | public class QueueMain {
4 | public static void main(String[] args) throws Exception {
5 | CircularQueue queue = new CircularQueue(5);
6 | queue.insert(3);
7 | queue.insert(6);
8 | queue.insert(5);
9 | queue.insert(19);
10 | queue.insert(1);
11 |
12 | queue.display();
13 |
14 | System.out.println(queue.remove());
15 | queue.insert(133);
16 | queue.display();
17 |
18 | System.out.println(queue.remove());
19 | queue.insert(99);
20 | queue.display();
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/01-Java/Key Concepts/BIT Manipulation/Theory/ClearBits.java:
--------------------------------------------------------------------------------
1 | class ClearBits{
2 | //Clear ith Bit
3 | public static int clearIthBit(int n, int i) {
4 | int bitMask = ~(1< list = new LinkedList();
5 | list.add("is");
6 | list.add("a");
7 | list.addLast("list");
8 | list.addFirst("this");
9 | list.add(3, "linked");
10 | System.out.println(list);
11 | System.out.println(list.get(0));
12 | System.out.println(list.size());
13 | list.remove(3);
14 | list.removeFirst();
15 | list.removeLast();
16 | System.out.println(list);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/01-Java/Methods(Functions)/OverloadedMethod.java:
--------------------------------------------------------------------------------
1 | public class OverloadedMethods {
2 | // Method to add two integers
3 | public static int add(int a, int b) {
4 | return a + b;
5 | }
6 |
7 | // Method to add three integers
8 | public static int add(int a, int b, int c) {
9 | return a + b + c;
10 | }
11 |
12 | public static void main(String[] args) {
13 | // Calling the overloaded methods
14 | int sum1 = add(2, 3);
15 | int sum2 = add(1, 2, 3);
16 | System.out.println("Sum of two numbers: " + sum1);
17 | System.out.println("Sum of three numbers: " + sum2);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/ArrayList/ArrayListSorting.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 | import java.util.Collections;
3 |
4 | public class Sorting {
5 |
6 | public static void main(String args[]) {
7 | ArrayList list = new ArrayList<>();
8 | list.add(5);
9 | list.add(3);
10 | list.add(6);
11 | list.add(7);
12 | list.add(1);
13 |
14 | //ascending order
15 | Collections.sort(list);
16 | System.out.println(list);
17 |
18 | //descending order
19 | Collections.sort(list, Collections.reverseOrder());
20 | System.out.println(list);
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Hash Set/HashSetIterator.java:
--------------------------------------------------------------------------------
1 | import java.util.HashSet;
2 |
3 | public class Hashing {
4 | public static void main(String args[]) {
5 |
6 | HashSet set = new HashSet<>();
7 | //Add
8 |
9 | set.add(1);
10 | set.add(2);
11 | set.add(3);
12 | set.add(1);
13 | //Iteration - HashSet does not have an order
14 |
15 | set.add(0);
16 |
17 | Iterator it = set.iterator();// Declaration
18 |
19 | while (it.hasNext()) {
20 |
21 | System.out.print(it.next() + ", ");
22 | }
23 | System.out.println();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Conditional Statements/If...else..Statement.java:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | if...else Statement Syntax: ----->
4 |
5 | if(Condition){
6 | //If Condition true
7 | //This Block of Code is executed
8 | }
9 | esle{
10 | //Condition Flase
11 | //this Block of Code is Executed
12 | }
13 |
14 | */
15 |
16 | public class JavaIf {
17 | public static void main(String[] args) {
18 | String name = "Mohan";
19 | int Roll = 25;
20 | if (name == "Mohan" && Roll == 26) {
21 | System.out.println("Details of Mohan.");
22 | } else {
23 | System.out.println("Invalid details.");
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Basics/OOPsBasics1.java:
--------------------------------------------------------------------------------
1 | class Animal {
2 | private String Name;
3 | private String color; // Private - Access Modifyiers
4 | void print_attri() {
5 | System.out.println(Name + " " + color);
6 | }
7 | void setname(String name) {
8 | Name = name;
9 | }
10 | String getname() {
11 | return Name ;
12 |
13 | }
14 | }
15 | public class Main {
16 | public static void main(String[] args) {
17 | Animal tiger = new Animal();
18 | //tiger.Name= "Tiger"; Throws Error Due to Private Access Modifyeirs ......
19 | //tiger.color="Yellow";
20 | tiger.setname("CodeWithHarry");
21 | System.out.println(tiger.getname());
22 |
23 | }
24 | }
--------------------------------------------------------------------------------
/02-C and C++/C/Array/1D Array/ArrayBasics.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main() {
4 | // Declaring an array of integers with size 5
5 | int numbers[5];
6 |
7 | // Assigning values to the array elements
8 | numbers[0] = 10;
9 | numbers[1] = 20;
10 | numbers[2] = 30;
11 | numbers[3] = 40;
12 | numbers[4] = 50;
13 |
14 | // Accessing and printing array elements
15 | printf("Element 0: %d\n", numbers[0]);
16 | printf("Element 1: %d\n", numbers[1]);
17 | printf("Element 2: %d\n", numbers[2]);
18 | printf("Element 3: %d\n", numbers[3]);
19 | printf("Element 4: %d\n", numbers[4]);
20 |
21 | return 0;
22 | }
23 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Patterns/Patterns-11.java:
--------------------------------------------------------------------------------
1 | public class Patterns {
2 | public static void palindromic_pattern_withNumbers(int n) {
3 | for(int i=1; i<=n; i++) {
4 | //spaces
5 | for(int j=1; j<=n-i; j++) {
6 | System.out.print(" ");
7 | }
8 |
9 | //descending
10 | for(int j=i; j>=1; j--) {
11 | System.out.print(j);
12 | }
13 |
14 | //ascending
15 | for(int j=2; j<=i; j++) {
16 | System.out.print(j);
17 | }
18 |
19 | System.out.println();
20 | }
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/02-C and C++/C/Sorting Algorithm/BubbleSort.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main (){
4 | int a[10],size,temp,o;
5 | printf("ENTER ARRAY SIZE ");
6 | scanf("%d",&size);
7 | for(int i=0;ia[j+1]){
16 | temp=a[j];
17 | a[j]=a[j+1];
18 | a[j+1]=temp;
19 | }
20 | printf("[ ");
21 | for(int i=0;i=1; i--) {
12 | factorial = factorial * i;
13 | }
14 | System.out.println(factorial);
15 | return;
16 | }
17 |
18 | public static void main(String args[]) {
19 |
20 | Scanner sc = new Scanner(System.in);
21 | int n = sc.nextInt();
22 | printFactorial(n);
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/01-Java/Sorting Algorithms/CountingSort.java:
--------------------------------------------------------------------------------
1 | public class Sorting {
2 | public static void countingSort(int[] arr) {
3 | int largest = Integer.MIN_VALUE;
4 | for (int i = 0; i < arr.length; i++) {
5 | largest = Math.max(largest, arr[i]);
6 | }
7 | int count[] = new int[largest + 1];
8 | for (int i = 0; i < arr.length; i++) {
9 | count[arr[i]]++;
10 | }
11 | //sorting
12 | int j = 0;
13 | for (int i = 0; i < count.length; i++) {
14 | while (count[i] > 0) {
15 | arr[j] = i;
16 | j++;
17 | count[i]--;
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Patterns/Pattern-8.java:
--------------------------------------------------------------------------------
1 | public class Patterns {
2 | public static void hollow_rhombus(int n) {
3 | for(int i=1; i<=n; i++) {
4 | //spaces
5 | for(int j=1; j<=n-i; j++) {
6 | System.out.print(" ");
7 | }
8 |
9 | //stars
10 | for(int j=1; j<=n; j++) {
11 | if(i == 1 || j == 1 || i == n || j == n) {
12 | System.out.print("*");
13 | } else {
14 | System.out.print(" ");
15 | }
16 | }
17 |
18 | System.out.println();
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Conditional Statement/FizzBuzzAssignment.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main ()
3 | {
4 | int i;
5 | for (i=1;i<=100;i++)
6 | {
7 | if (i%3==0&&i%5==0)
8 | {
9 |
10 | printf(" fizz bazz");
11 |
12 | }
13 | else if(i%3==0)
14 | {
15 | printf(" fizz"
16 | "z");
17 | }
18 | else if(i%5==0)
19 | {
20 | printf(" bazz");
21 | }
22 | else
23 | {
24 | printf(" %d",i);
25 | }
26 |
27 |
28 |
29 | }
30 | return 0 ;
31 | }
32 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Linked List/LLRecursiveReversal.java:
--------------------------------------------------------------------------------
1 | class LL{
2 | public Node reverseListRecursive(Node head) {
3 | //empty node || last node or only one node
4 | if(head == null || head.next == null) {
5 | return head;
6 | }
7 | Node newHead = reverseListRecursive(head.next);
8 | head.next.next = head;
9 | head.next = null;
10 | return newHead;
11 | }
12 | public static void main(String args[]) {
13 | LL list = new LL();
14 | list.addLast(1);
15 | list.addLast(2);
16 | list.addLast(3);
17 | list.printList();
18 | list.reverseListRecursive();
19 | list.printList();
20 |
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/01-Java/Key Concepts/BIT Manipulation/Theory/UpdateBit.java:
--------------------------------------------------------------------------------
1 | //Update Bit
2 | import java.util.*;
3 | public class Bits {
4 | public static void main(String args[]) {
5 | Scanner sc = new Scanner(System.in);
6 | int oper = sc.nextInt();
7 | // oper=1 -> set; oper=0 -> clear
8 | int n = 5;
9 | int pos = 1;
10 | int bitMask = 1< used for giving Reference
30 | //super----->To pass super a Value
31 |
32 | }
33 | }
--------------------------------------------------------------------------------
/01-Java/Essentials/Readme.md:
--------------------------------------------------------------------------------
1 | # Essentials Image
2 |
3 | 
4 | 
5 |
6 |
7 |
8 | 




9 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Arrays/1D Arrays/MSSKadane'sAlgorithm.java:
--------------------------------------------------------------------------------
1 | //Question : Print maximum subarray sum
2 |
3 | //Kadane's Algorithm
4 | public static void maxSubarraySum3(int arr[]) {
5 | int cs = 0;
6 | int ms = 0;
7 |
8 | for(int i=0; i minHeap = new PriorityQueue<>();
7 |
8 | // Add elements to the min heap
9 | minHeap.add(10);
10 | minHeap.add(20);
11 | minHeap.add(5);
12 | minHeap.add(1);
13 |
14 | // Print and remove elements in ascending order
15 | System.out.println("Min heap elements in ascending order:");
16 | while (!minHeap.isEmpty()) {
17 | System.out.println(minHeap.poll());
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/01-Java/Methods(Functions)/Readme.md:
--------------------------------------------------------------------------------
1 | # Note 📝 :
2 | ### Introduction to Functions In Java
3 |
4 | A function (also called a method) is a block of code designed to perform a specific task. Functions help in breaking down complex problems into simpler pieces and promote code reusability.Defining a FunctionTo define a function in Java, you need to specify the return type, function name, and parameters (if any). The general syntax is:
5 |
6 | ```java
7 | returnType functionName(parameterList) {
8 | // body of the function
9 | }
10 | ```
11 |
12 | ### Calling Static Methods
13 |
14 | Static methods belong to the class rather than any specific instance of the class. They can be called using the class name.Syntax:
15 | ```java
16 | ClassName.methodName(arguments);
17 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Recursive Sorting/BubbleSort.java:
--------------------------------------------------------------------------------
1 | public class BubbleSort {
2 | public static void main(String[] args) {
3 |
4 | int[] arr = {1, 4, 3, 5};
5 | bubble(arr, arr.length, 0, 0);
6 | System.out.println(Arrays.toString(arr));
7 |
8 | }
9 |
10 | static void bubble(int[] arr, int r, int c) {
11 | if (r == 0) {
12 | return;
13 | }
14 | if (c < r) {
15 |
16 | if (arr[c] > arr[c+1]) {
17 | // swap
18 | int temp = arr[c];
19 | arr[c] = arr[c+1];
20 | arr[c+1] = temp;
21 | }
22 |
23 | bubble(arr, r, c+1);
24 | } else {
25 | bubble(arr, r-1, 0);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Switch-case/Switch-case.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main()
3 | {
4 | int a;
5 | printf("Enter Number Between 1 and 7\n");
6 | scanf("%d", &a);
7 | switch (a)
8 | {
9 | case 1:
10 | printf("Sunday");
11 |
12 | case 2:
13 | printf("\nMonday");
14 | break;
15 | case 3:
16 | printf("\nTuesday");
17 | break;
18 | case 4:
19 | printf("\nWednesday");
20 | break;
21 | case 5:
22 | printf("\nThursday");
23 | break;
24 | case 6:
25 | printf("\nFriday");
26 | break;
27 | case 7:
28 | printf("\nSaturday");
29 | break;
30 | default:
31 | printf("\nInvalid Input ");
32 | }
33 |
34 | return 0;
35 | }
--------------------------------------------------------------------------------
/01-Java/Data Structure/Linked List/LLIterativeReversal.java:
--------------------------------------------------------------------------------
1 | class LL{
2 | public void reverseList() {
3 | if(head == null || head.next == null) {
4 | return;
5 | }
6 | Node prevNode = head;
7 | Node currNode = head.next;
8 | while(currNode != null) {
9 | Node nextNode = currNode.next;
10 | currNode.next = prevNode;
11 | prevNode = currNode;
12 | currNode = nextNode;
13 | }
14 | head.next = null;
15 | head = prevNode;
16 | }
17 | public static void main(String args[]) {
18 | LL list = new LL();
19 | list.addLast(1);
20 | list.addLast(2);
21 | list.addLast(3);
22 | list.printList();
23 | list.reverseList();
24 | list.printList();
25 | }
26 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Stack/LeetCode Top Questions/Problem1.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | //To push an element at the bottom of a stack
3 | public class StackProblem1 {
4 | public static void pushAtBottom(Stack s, int data) {
5 | if(s.isEmpty()) {
6 | s.push(data);
7 | return;
8 | }
9 |
10 | int temp = s.pop();
11 | pushAtBottom(s, data);
12 | s.push(temp);
13 | }
14 |
15 | public static void main(String args[]) {
16 | Stack stack = new Stack<>();
17 | stack.push(1);
18 | stack.push(2);
19 | stack.push(3);
20 | pushAtBottom(stack, 4);
21 |
22 | while(!stack.isEmpty()) {
23 | System.out.println(stack.pop());
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Patterns Using Recursion/PatternUsingRecursion2.java:
--------------------------------------------------------------------------------
1 | public class TrianglePattern{
2 | public static void main(String[] args) {
3 |
4 | TrianglePattern(4, 0);
5 |
6 | /* Output :
7 |
8 | * * * *
9 | * * *
10 | * *
11 | *
12 |
13 | */
14 | }
15 |
16 | static void TrianglePattern(int r, int c) {
17 | if (r == 0) {
18 | return;
19 | }
20 |
21 | if (c < r) {
22 |
23 | System.out.print("*");
24 | TrianglePattern(r, c+1);
25 |
26 | } else {
27 |
28 | System.out.println();
29 | TrianglePattern(r-1, 0);
30 |
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/02-C and C++/C++/Data Structures/Arrays/1D Arrays/Kadane's_Algorithm.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include // For std::max
3 | using namespace std;
4 |
5 | // Kadane's Algorithm to find the maximum subarray sum
6 | void maxSubarraySum3(int arr[], int size) {
7 | int currentSum = 0;
8 | int maxSum = 0;
9 |
10 | for(int i = 0; i < size; i++) {
11 | currentSum += arr[i];
12 | if (currentSum < 0) {
13 | currentSum = 0;
14 | }
15 | maxSum = max(maxSum, currentSum);
16 | }
17 |
18 | cout << "Max subarray sum is: " << maxSum << endl;
19 | }
20 |
21 | int main() {
22 | int arr[] = {1, -2, 6, -1, 3};
23 | int size = sizeof(arr)/sizeof(arr[0]);
24 |
25 | maxSubarraySum3(arr, size);
26 |
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Patterns Using Recursion/PatternUsingRecursion.java:
--------------------------------------------------------------------------------
1 | public class TrianglePattern{
2 | public static void main(String[] args) {
3 |
4 | TrianglePattern(4, 0);
5 |
6 | /* Output :
7 |
8 | *
9 | * *
10 | * * *
11 | * * * *
12 |
13 | */
14 | }
15 |
16 | static void TrianglePattern(int r, int c) {
17 | if (r == 0) {
18 | return;
19 | }
20 |
21 | if (c < r) {
22 |
23 | TrianglePattern(r, c+1);
24 | System.out.print("*");
25 |
26 | } else {
27 |
28 | TrianglePattern(r-1, 0);
29 | System.out.println();
30 |
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/01-Java/Data Structure/Linked List/LLMiddleOperations.java:
--------------------------------------------------------------------------------
1 | public void addInMiddle(int index, String data) {
2 | if(index > size || index < 0) {
3 | System.out.println("Invalid Index value");
4 | return;
5 | }
6 | size++;
7 | Node newNode = new Node(data);
8 | if(head == null || index == 0) {
9 | newNode.next = head;
10 | head = newNode;
11 | return;
12 | }
13 | Node currNode = head;
14 | for(int i=1; i q = new LinkedList();
6 | Queue q = new ArrayDeque();
7 | q.add(1);
8 | q.add(2);
9 | q.add(3);
10 | q.add(4);
11 | q.add(5);
12 | while(!q.isEmpty()) {
13 | System.out.println(q.peek());
14 | q.remove();
15 | }
16 | }
17 | }
18 | /*
19 | Note :
20 | 1. Queue is A Interface Which is Implemented using the classes like Linked List of ArrayDeque
21 | 2. Therefore in Line Number 6 we created of object of reference to Queue(interface)
22 | 3. And Object is Created of classes Linked List/ArrayDeque
23 | */
24 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Loops/Break&Continue.java:
--------------------------------------------------------------------------------
1 | // Break Statement----->
2 |
3 | public class JavaBreak {
4 | public static void main(String[] args) {
5 | for(int n=1; n<=20; n++) {
6 | if(n%2 == 0) {
7 | System.out.println(n);
8 | if (n == 12) {
9 | break;
10 | }
11 | }
12 | }
13 | }
14 | }
15 |
16 |
17 | // Continue------->
18 |
19 | public class JavaContinue {
20 | public static void main(String[] args) {
21 | for(int n=1; n<=20; n++) {
22 | if(n%2 == 0) {
23 | if (n == 12) {
24 | continue;
25 | }
26 | System.out.println(n);
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/02-C and C++/C/Array/Structure Array/TraversingStructureArray.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | // Define a structure
4 | struct Person {
5 | char name[50];
6 | int age;
7 | float height;
8 | };
9 |
10 | int main() {
11 | // Declare an array of structures
12 | struct Person people[3] = {
13 | {"Alice", 25, 5.6},
14 | {"Bob", 30, 6.0},
15 | {"Charlie", 35, 5.8}
16 | };
17 |
18 | // Traverse the array of structures
19 | printf("People Information:\n");
20 | for (int i = 0; i < 3; i++) {
21 | printf("Person %d:\n", i+1);
22 | printf("Name: %s\n", people[i].name);
23 | printf("Age: %d\n", people[i].age);
24 | printf("Height: %.2f\n", people[i].height);
25 | printf("\n");
26 | }
27 |
28 | return 0;
29 | }
30 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Key Concepts/DynamicDispatch.java:
--------------------------------------------------------------------------------
1 | class Phone{
2 | public void showTime(){
3 | System.out.println("Time is 8 am");
4 | }
5 | public void on(){
6 | System.out.println("Turning on Phone...");
7 | }
8 | }
9 |
10 | class SmartPhone extends Phone{
11 | public void music(){
12 | System.out.println("Playing music...");
13 | }
14 | public void on(){
15 | System.out.println("Turning on SmartPhone...");
16 | }
17 | }
18 | public class CWH {
19 | public static void main(String[] args) {
20 |
21 | Phone obj = new SmartPhone(); // Yes it is allowed
22 | // SmartPhone obj2 = new Phone(); // Not allowed
23 |
24 | obj.showTime();
25 | obj.on();
26 | // obj.music(); Not Allowed
27 |
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/01-Java/COLLECTIONS Framework/Implementations/QueueUsingCOLLECTIONS.java:
--------------------------------------------------------------------------------
1 | //queue using Java Collection Framework
2 | import java.util.*;
3 | public class QueueB {
4 | public static void main(String args[]) {
5 | //Queue q = new LinkedList();
6 | Queue q = new ArrayDeque();
7 | q.add(1);
8 | q.add(2);
9 | q.add(3);
10 | q.add(4);
11 | q.add(5);
12 | while(!q.isEmpty()) {
13 | System.out.println(q.peek());
14 | q.remove();
15 | }
16 | }
17 | }
18 | /*
19 | Note :
20 | 1. Queue is A Interface Which is Implemented using the classes like Linked List of ArrayDeque
21 | 2. Therefore in Line Number 6 we created of object of reference to Queue(interface)
22 | 3. And Object is Created of classes Linked List/ArrayDeque
23 | */
24 |
--------------------------------------------------------------------------------
/01-Java/Searching Algorithms/LinearSearch.java:
--------------------------------------------------------------------------------
1 | public class LinearSearch {
2 | public static int linearSearch(int[] arr, int target) {
3 | for (int i = 0; i < arr.length; i++) {
4 | if (arr[i] == target) {
5 | return i; // return the index of the target element if found
6 | }
7 | }
8 | return -1; // return -1 if the target element is not found
9 | }
10 |
11 | public static void main(String[] args) {
12 | int[] arr = { 5, 3, 8, 1, 9, 2 };
13 | int target = 8;
14 | int result = linearSearch(arr, target);
15 | if (result != -1) {
16 | System.out.println("Element found at index: " + result);
17 | } else {
18 | System.out.println("Element not found in the array.");
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/01-Java/Key Concepts/Important Algorithms/FrequencyCounterUsingHashMap.java:
--------------------------------------------------------------------------------
1 | public class FrequencyCounter {
2 | public static void main(String[] args) {
3 | int[] array = {3,2,1,2,4,1,3,4,3,2}
4 | // Print Frequency for each Element:
5 | Map< Integer,Integer >map = new HashMap();
6 | // Method-1 :
7 | for(int num : array){
8 | if(map.containsKey(num)){
9 | map.put(num,map.get(num)+1);
10 | }else{
11 | map.put(num,1);
12 | }
13 | }
14 |
15 | //Method-2
16 | for(int num : array){
17 | map.put(num,map.getOrDefault(num,0)+1);
18 | }
19 |
20 | /* Output will be same :
21 | map={
22 | 3 : 3 ,
23 | 2 : 3 ,
24 | 1 : 2 ,
25 | 4 : 2 ,
26 | }
27 | */
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Recursive Sorting/SelectionSort.java:
--------------------------------------------------------------------------------
1 | public class Triangle {
2 | public static void main(String[] args) {
3 |
4 | int[] arr = {1, 4, 3, 5};
5 | selection(arr, arr.length, 0, 0);
6 | System.out.println(Arrays.toString(arr));
7 | }
8 | static void selection(int[] arr, int r, int c, int max) {
9 | if (r == 0) {
10 | return;
11 | }
12 | if (c < r) {
13 | if (arr[c] > arr[max]) {
14 | selection(arr, r, c+1, c);
15 | } else {
16 | selection(arr, r, c+1, max);
17 | }
18 | } else {
19 | int temp = arr[max];
20 | arr[max] = arr[r-1];
21 | arr[r-1] = temp;
22 | selection(arr, r-1, 0, 0);
23 | }
24 | }
25 |
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/02-C and C++/C++/Searching Algorithms/Linear_Search.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | // Function to perform linear search
5 | int linearSearch(int arr[], int size, int target) {
6 | for (int i = 0; i < size; i++) {
7 | if (arr[i] == target) {
8 | return i; // Return the index of the target element
9 | }
10 | }
11 | return -1; // Return -1 if the target is not found
12 | }
13 |
14 | int main() {
15 | int arr[] = {2, 4, 6, 8, 10};
16 | int size = sizeof(arr) / sizeof(arr[0]);
17 | int target = 8;
18 |
19 | int result = linearSearch(arr, size, target);
20 |
21 | if (result != -1) {
22 | cout << "Element found at index " << result << endl;
23 | } else {
24 | cout << "Element not found" << endl;
25 | }
26 |
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/String/String Builder/InsertCharacter.java:
--------------------------------------------------------------------------------
1 | /*
2 | String Builder
3 | - Declaration
4 | StringBuilder sb = new StringBuilder("Atharva");
5 | System.out.println(sb);
6 |
7 | // Get A Character from Index
8 | StringBuilder sb = new StringBuilder("Tony");
9 |
10 | //Set Char
11 | System.out.println(sb.charAt(0));
12 | Set a Character at Index
13 | StringBuilder sb = new StringBuilder("Tony");
14 |
15 | //Get Char
16 | sb.setCharAt(0, 'P');
17 | System.out.println(sb);
18 |
19 | */
20 |
21 | //Insert a Character at Some Index
22 | class StringBuilder{
23 | import java.util.*;
24 | public class Strings {
25 | public static void main(String args[]) {
26 | StringBuilder sb = new StringBuilder("tony");
27 | //Insert char
28 | sb.insert(0, 'S');
29 | System.out.println(sb);
30 | }
31 | }
32 |
33 |
--------------------------------------------------------------------------------
/01-Java/Key Concepts/Two Pointer Approach/Linked List/FindMiddleOfList.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public ListNode middleNode(ListNode head) {
3 | // Initialize two pointers, slow and fast, both starting at the head of the list
4 | ListNode slow = head;
5 | ListNode fast = head;
6 |
7 | // Traverse the list with the two pointers
8 | // The fast pointer moves two steps at a time, and the slow pointer moves one step at a time
9 | while (fast != null && fast.next != null) {
10 | slow = slow.next; // Move slow pointer one step
11 | fast = fast.next.next; // Move fast pointer two steps
12 | }
13 |
14 | // When the fast pointer reaches the end of the list, the slow pointer will be at the middle
15 | return slow;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Arrays/1D Arrays/MaxSumSubArray(MSS).java:
--------------------------------------------------------------------------------
1 | //Question : Print maximum subarray sum
2 | public class MaxSubarraySum {
3 | //Bruteforce
4 | public static void maxSubarraySum1(int arr[]) {
5 | int largestSum = Integer.MIN_VALUE;
6 |
7 | for(int i=0; i maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
8 |
9 | // Add elements to the max heap
10 | maxHeap.add(10);
11 | maxHeap.add(20);
12 | maxHeap.add(5);
13 | maxHeap.add(1);
14 |
15 | // Print and remove elements in descending order
16 | System.out.println("Max heap elements in descending order:");
17 | while (!maxHeap.isEmpty()) {
18 | System.out.println(maxHeap.poll());
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/01-Java/Problem Solving Patterns/Two Pointer Approach/Linked List/FindMiddleOfList.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public ListNode middleNode(ListNode head) {
3 | // Initialize two pointers, slow and fast, both starting at the head of the list
4 | ListNode slow = head;
5 | ListNode fast = head;
6 |
7 | // Traverse the list with the two pointers
8 | // The fast pointer moves two steps at a time, and the slow pointer moves one step at a time
9 | while (fast != null && fast.next != null) {
10 | slow = slow.next; // Move slow pointer one step
11 | fast = fast.next.next; // Move fast pointer two steps
12 | }
13 |
14 | // When the fast pointer reaches the end of the list, the slow pointer will be at the middle
15 | return slow;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/02-C and C++/C/Stack/STACKasDCBconverter.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int top = -1;
4 | int size = 5;
5 | int a[5];
6 |
7 | void PrintArray(int a[5]) {
8 | printf("[ ");
9 | for (int i = top; i >= 0; i--) {
10 | printf("%d ", a[i]);
11 | }
12 | printf(" ] ");
13 | }
14 |
15 | void push(int re) {
16 | if (top == size - 1) {
17 | printf("STACK OVERFLOW !!!!\n");
18 | } else {
19 | top++;
20 | a[top] = re;
21 | printf("%d PUSHED to [%d] Index\n", re, top);
22 | }
23 | }
24 |
25 | void main() {
26 | int a[5], deci, re;
27 | printf("Enter a decimal number: ");
28 | scanf("%d", &deci);
29 |
30 | while (deci > 0) {
31 | re = deci % 2;
32 | push(re);
33 | deci /= 2;
34 | }
35 |
36 | printf("Binary equivalents are\n");
37 | PrintArray(a);
38 | }
39 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Arrays/2D Arrays/2DArraysBasics.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class Basics {
4 | public static void main(String args[]) {
5 | int n = 5; //rows
6 | int m = 3; //cols
7 | int marks[][] = new int [n][m];
8 |
9 | //Taking Input
10 | Scanner sc = new Scanner(System.in);
11 |
12 | for(int i=0; i
2 | void PrintArray(int a[10],int size)
3 | {
4 | printf("[ ");
5 | for(int i=0;ia[j]){
32 | temp=a[i];
33 | a[i]=a[j];
34 | a[j]=temp;
35 | }
36 |
37 | PrintArray(a,size);
38 | }
39 | o=i;
40 | }
41 |
42 |
43 | printf("Number of Passes = %d",o+1);
44 | return 0;
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Constructors/Readme.md:
--------------------------------------------------------------------------------
1 | # Constructors In Java
2 |
3 | In Java, a Constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method that is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called.
4 |
5 | ### Constructor Exmaple :
6 |
7 | ```bash
8 |
9 | // Java Program to demonstrate
10 | // Constructor
11 | import java.io.*;
12 | // Driver Class
13 | class Geeks {
14 | // Constructor
15 | Geeks()
16 | {
17 | super();
18 | System.out.println("Constructor Called");
19 | }
20 | // main function
21 | public static void main(String[] args){
22 | Geeks geek = new Geeks();
23 | }
24 | }
25 | ```
26 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Hash Map/Top Leetcode Questions/SubArraySumPrefixSum.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 |
3 | public int subarraySum(int[] nums, int k) {
4 |
5 | Mapmap=new HashMap();
6 |
7 | int ans = 0 ;
8 | int sum = 0 ;
9 |
10 | map.put(0,1);
11 |
12 | for(int i = 0 ;i
2 | using namespace std;
3 |
4 | int main() {
5 | // Creating an array
6 |
7 | int marks[50]; // Uninitialized array of size 50
8 |
9 | int numbers[] = {1, 2, 3}; // Array with initialized elements
10 |
11 | int moreNumbers[] = {4, 5, 6}; // Another array with initialized elements
12 |
13 | string fruits[] = {"apple", "mango", "orange"}; // Array of strings
14 |
15 | cout << marks[0] << endl; // Accessing the first element of 'marks' (uninitialized, may contain garbage value)
16 | cout << numbers[0] << endl; // Accessing the first element of 'numbers'
17 | cout << moreNumbers[0] << endl; // Accessing the first element of 'moreNumbers'
18 | cout << fruits[0] << endl; // Accessing the first element of 'fruits'
19 |
20 | return 0;
21 | }
22 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Interfaces/InheritanceInInterfaces.java:
--------------------------------------------------------------------------------
1 | package com.company;
2 |
3 | interface sampleInterface{
4 | void meth1();
5 | void meth2();
6 | }
7 | interface childSampleInterface extends sampleInterface{
8 | void meth3();
9 | void meth4();
10 | }
11 | class MySampleClass implements childSampleInterface{
12 | public void meth1(){
13 | System.out.println("meth1");
14 | }
15 | public void meth2(){
16 | System.out.println("meth2");
17 | }
18 | public void meth3(){
19 | System.out.println("meth3");
20 | }
21 | public void meth4(){
22 | System.out.println("meth4");
23 | }
24 | }
25 | public class cwh_58_inheritance_interfaces {
26 | public static void main(String[] args) {
27 | MySampleClass obj = new MySampleClass();
28 | obj.meth1();
29 | obj.meth2();
30 | obj.meth3();
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Arrays/1D Arrays/ArrayReversal.java:
--------------------------------------------------------------------------------
1 | //Question : Reverse an Array
2 | package Arrays;
3 |
4 | public class ReverseArray {
5 | public static void reverse(int arr[]) {
6 | int start = 0;
7 | int end = arr.length-1;
8 |
9 | while(start <= end) {
10 | //swap
11 | int t = arr[start];
12 | arr[start] = arr[end];
13 | arr[end] = t;
14 |
15 | start++;
16 | end--;
17 | }
18 | }
19 |
20 | public static void printArray(int arr[]) {
21 | for(int i=0; i=0; i--) {
19 | //spaces
20 | for(int j=1; j<=n-i; j++) {
21 | System.out.print(" ");
22 | }
23 |
24 | //stars
25 | for(int j=1; j<=(2*i)-1; j++) {
26 | System.out.print("*");
27 | }
28 | System.out.println();
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/02-C and C++/C++/Operators/06_Comparison_Operator.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | int main() {
6 | int x = 10;
7 | int y = 20;
8 |
9 | // Equal to
10 | if (x == y) {
11 | cout << "x is equal to y." << endl;
12 | }
13 |
14 | // Not equal to
15 | if (x != y) {
16 | cout << "x is not equal to y." << endl;
17 | }
18 |
19 | // Greater than
20 | if (x > y) {
21 | cout << "x is greater than y." << endl;
22 | }
23 |
24 | // Less than
25 | if (x < y) {
26 | cout << "x is less than y." << endl;
27 | }
28 |
29 | // Greater than or equal to
30 | if (x >= y) {
31 | cout << "x is greater than or equal to y." << endl;
32 | }
33 |
34 | // Less than or equal to
35 | if (x <= y) {
36 | cout << "x is less than or equal to y." << endl;
37 | }
38 |
39 | return 0;
40 | }
41 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Arrays/2D Arrays/2DArrayDiagonalSum.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | //Problem : Calculate Diagonal Sum
4 |
5 | public class DiagonalSum {
6 | public static int calcSum(int matrix[][]) {
7 | int sum = 0;
8 | //primary
9 | for(int i=0, j=0; i=0; j--,i++) {
15 | if(i == j) {
16 | continue;
17 | }
18 | sum += matrix[i][j];
19 | }
20 |
21 | return sum;
22 | }
23 | public static void main(String args[]) {
24 | int matrix[][] = {{1, 2, 3, 4},
25 | {5, 6, 7, 8},
26 | {9, 10, 11, 12},
27 | {13, 14, 15, 16}};
28 | calcSum(matrix);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Subsets/SubsetsOfArray.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 | import java.util.Arrays;
3 | import java.util.List;
4 |
5 | public class SubSet {
6 | public static void main(String[] args) {
7 | int[] arr = {1, 2, 2};
8 | List> ans = subsetDuplicate(arr);
9 | for (List list : ans) {
10 | System.out.println(list);
11 | }
12 | }
13 |
14 | static List> subset(int[] arr) {
15 | List> outer = new ArrayList<>();
16 | outer.add(new ArrayList<>());
17 | for (int num : arr) {
18 | int n = outer.size();
19 | for (int i = 0; i < n; i++) {
20 | List internal = new ArrayList<>(outer.get(i));
21 | internal.add(num);
22 | outer.add(internal);
23 | }
24 | }
25 | return outer;
26 | }
27 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Abstract Classes/Readme.md:
--------------------------------------------------------------------------------
1 | # Abstract Classes and Methods
2 |
3 | Data abstraction is the process of hiding certain details and showing only essential information to the user.
4 | Abstraction can be achieved with either abstract classes or interfaces (which you will learn more about in the next chapter).
5 |
6 | The abstract keyword is a non-access modifier, used for classes and methods:
7 |
8 | `Abstract class` : is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
9 |
10 | `Abstract method` : can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
11 |
12 | ## Abstract Classes and Methods Example
13 |
14 | ```sudo
15 |
16 | abstract class Animal {
17 | public abstract void animalSound();
18 | public void sleep() {
19 | System.out.println("Zzz");
20 | }
21 | }
22 |
23 | ```
24 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Stack/Custom Stack Class/DynamicStack.java:
--------------------------------------------------------------------------------
1 | public class DynamicStack extends CustomStack{
2 |
3 | public DynamicStack() {
4 | super(); // it will call CustomStack()
5 | }
6 |
7 | public DynamicStack(int size) {
8 | super(size); // it will call CustomStack(int size)
9 | }
10 |
11 | @Override
12 | public boolean push(int item) {
13 | // this takes care of it being full
14 | if (this.isFull()) {
15 | // double the array size
16 | int[] temp = new int[data.length * 2];
17 |
18 | // copy all previous items in new data
19 | for (int i = 0; i < data.length; i++) {
20 | temp[i] = data[i];
21 | }
22 |
23 | data = temp;
24 | }
25 |
26 | // at this point we know that array is not full
27 | // insert item
28 | return super.push(item);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/String/LeetCode Top Question/ShortestPath.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | //Problem : Print the displacement (Shortest Path)
3 | public class ShortestPath {
4 | public static float shortestPath(String path) {
5 | int x = 0, y = 0;
6 | for(int i=0; i> levelOrder(TreeNode root) {
3 | List> result = new ArrayList<>();
4 |
5 | if (root == null) {
6 | return result;
7 | }
8 |
9 | Queue queue = new LinkedList<>();
10 | queue.offer(root);
11 |
12 | while (!queue.isEmpty()) {
13 | int levelSize = queue.size();
14 | List currentLevel = new ArrayList<>(levelSize);
15 | for (int i=0; i < levelSize; i++) {
16 | TreeNode currentNode = queue.poll();
17 | currentLevel.add(currentNode.val);
18 | if (currentNode.left != null) {
19 | queue.offer(currentNode.left);
20 | }
21 | if (currentNode.right != null) {
22 | queue.offer(currentNode.right);
23 | }
24 | }
25 | result.add(currentLevel);
26 | }
27 | return result;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Simple Recursion Problems/ReverseTheNumber.java:
--------------------------------------------------------------------------------
1 | public class Reverse {
2 | static int sum = 0;
3 | static void rev1(int n) {
4 | if (n == 0) {
5 | return;
6 | }
7 | int rem = n % 10;
8 | sum = sum * 10 + rem;
9 | rev1(n/10);
10 | }
11 |
12 | static int rev2(int n) {
13 | // sometimes you might need some additional variables in the argument
14 | // in that case, make another function
15 | int digits = (int)(Math.log10(n)) + 1;
16 | return helper(n, digits);
17 | }
18 |
19 | private static int helper(int n, int digits) {
20 | if (n%10 == n) {
21 | return n;
22 | }
23 | int rem = n % 10;
24 | return rem * (int)(Math.pow(10, digits-1)) + helper(n/10, digits-1);
25 | }
26 |
27 | public static void main(String[] args) {
28 | System.out.println(rev2(1234));
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Key Concepts/Getter&Putter.java:
--------------------------------------------------------------------------------
1 | class Employee {
2 |
3 | private int id; //access modifier : private
4 | private String name;// Only Accesable in Class
5 |
6 | public String getName(){
7 | return name;
8 | }
9 | public void setName(String n){
10 | name = n;
11 | }
12 | public void setId(int i){
13 | id = i;
14 | }
15 | public int getId(){
16 | return id;
17 | }
18 | }
19 |
20 | public class CWH {
21 | public static void main(String[] args) {
22 | Employee emp1 = new Employee();
23 |
24 | emp1.setName("Shubham");
25 | System.out.println(emp1.getName());
26 | emp1.setId(1);
27 | System.out.println(emp1.getId());
28 |
29 | //1.within class 2.within package 3.outside package by subclass only 4.outside package
30 | //public Y Y Y Y
31 | //protected Y Y Y N
32 | //Default Y Y N N
33 | //private Y N N N
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/02-C and C++/C/Sorting Algorithm/InserationSort.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void Print_Array(int a[10],int size)
4 | {
5 | printf("[ ");
6 | for(int i=0;ia[i])
37 | {
38 | temp=a[j];
39 | a[j]=a[i];
40 |
41 | for(k=i;k>j;k--)
42 | {
43 | a[k]=a[k-1];
44 | }
45 | a[k+1]=temp;
46 | }
47 | }
48 | Print_Array(a,size);
49 | o=i;
50 | }
51 | printf("Number of Passes : %d\n",o);
52 | return 0;
53 | }
54 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Arrays/1D Arrays/MSSOptimized.java:
--------------------------------------------------------------------------------
1 | //Question : Print maximum subarray sum
2 |
3 |
4 | //optimization1 : Prefix Sum array
5 | public static void maxSubarraySum2(int arr[]) {
6 | int maxSum = Integer.MIN_VALUE;
7 |
8 | int prefix[] = new int[arr.length];
9 | prefix[0] = arr[0];
10 | for(int i=1; i uniqueNumbers = new HashSet<>(Arrays.asList(numbers));
12 | // Or You can Populate the HashSet using a for loop to remove duplicates
13 | for (Integer number : numbers) {
14 | uniqueNumbers.add(number);
15 | }
16 |
17 | // Convert back to an array if needed
18 | Integer[] uniqueArray = uniqueNumbers.toArray(new Integer[0]);
19 |
20 | // Print the unique array
21 | System.out.println("Array with duplicates removed: " + Arrays.toString(uniqueArray));
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Queue/Custom Queue Class/DynamicQueue.java:
--------------------------------------------------------------------------------
1 |
2 |
3 | public class DynamicQueue extends CircularQueue{
4 | public DynamicQueue() {
5 | super();
6 | }
7 |
8 | public DynamicQueue(int size) {
9 | super(size);
10 | }
11 |
12 | @Override
13 | public boolean insert(int item) {
14 |
15 | // this takes care of it being full
16 | if (this.isFull()) {
17 | // double the array size
18 | int[] temp = new int[data.length * 2];
19 |
20 | // copy all previous items in new data
21 | for (int i = 0; i < data.length; i++) {
22 | temp[i] = data[(front + i) % data.length];
23 | }
24 | front = 0;
25 | end = data.length;
26 | data = temp;
27 | }
28 |
29 | // at this point we know that array is not full
30 | // insert item
31 | return super.insert(item);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/02-C and C++/C/String/StringOperations-2.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | int main()
4 | {
5 | int len1, len2, m;
6 | char a[] = "OmSai"; //String Delcaration
7 | char b[] = "Ram"; //Intialization
8 | char c[10];
9 | puts(" • First string is : \n");
10 | puts(a); // To Print the String
11 | printf(" • Second string is %s\n", b);
12 |
13 | len1 = strlen(a); // Length Calculation
14 | len2 = strlen(b);
15 | printf(" • Length of First String is %d\n", len1);
16 | printf(" • Length of Second String is %d\n", len2);
17 | strcat(a, b); //String Joining
18 | printf(" • Joined String is %s\n", a);
19 | strcpy(c, a); // String Copy
20 | printf(" • Copied String is %s\n", c);
21 | m = strcmp(a, b); //comparing String
22 | if (m == 0)
23 | {
24 | printf(" • String are Equal");
25 | }
26 | else
27 | {
28 | printf(" • String are Not Equal");
29 | }
30 | return 0;
31 | }
--------------------------------------------------------------------------------
/01-Java/Data Structure/Stack/LeetCode Top Questions/Problem2.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | //Code to Reverse a Stack
3 |
4 | public class StackProblem2 {
5 | public static void pushAtBottom(Stack s, int data) {
6 | if(s.isEmpty()) {
7 | s.push(data);
8 | return;
9 | }
10 |
11 | int temp = s.pop();
12 | pushAtBottom(s, data);
13 | s.push(temp);
14 | }
15 |
16 | public static void reverse(Stack s) {
17 | if(s.isEmpty()) {
18 | return;
19 | }
20 |
21 | int top = s.pop();
22 | reverse(s);
23 | pushAtBottom(s, top);
24 | }
25 |
26 | public static void main(String args[]) {
27 | Stack stack = new Stack<>();
28 | stack.push(1);
29 | stack.push(2);
30 | stack.push(3);
31 |
32 | while(!stack.isEmpty()) {
33 | System.out.println(stack.pop());
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/02-C and C++/C/Searching Algorithms/LinearSearch.c:
--------------------------------------------------------------------------------
1 | #include
2 | void ReadArray(int a[10],int size)
3 | {
4 | for(int i=0;i
2 | int main()
3 | {
4 | int a, b, c;
5 | printf("enter value of the side of trinagle ");
6 | scanf("%d%d%d", &a, &b, &c);
7 |
8 | if (a * a == b * b + c * c || b * b == a * a + c * c || c * c == b * b + a * a)
9 | {
10 | printf("Given Trinagle is Right Angled Triangle");
11 | }
12 |
13 | else if (a * a > b * b + c * c || b * b > c * c + a * a || c * c > b * b + a * a)
14 | {
15 | printf("Given Trinangle is Obtuse angled Triagle ");
16 | }
17 | else
18 | {
19 | printf("Given Triangle is Acute Angled Triangle ");
20 | }
21 | if (a == b && b == c)
22 | {
23 | printf("And Equilateral Triangle aslo");
24 | }
25 |
26 | else if (a == b || b == c || a == c)
27 | {
28 | printf(" And Isoceles Triangle also");
29 | }
30 | else
31 | {
32 | printf(" And Scalar Triangle");
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/01-Java/Key Concepts/BIT Manipulation/Theory/SetBits.java:
--------------------------------------------------------------------------------
1 | class SetBits{
2 | //Set ith Bit
3 | public static int setIthBit(int n, int i) {
4 | int bitMask = 1<>1;
17 | }
18 | return setBits;
19 | }
20 |
21 | //a faster method to count set bits
22 | public static int countSetBits2(int n) {
23 | int setBits = 0;
24 | while(n>0) {
25 | // removes the last set bit from curr number
26 | n = n & (n-1);
27 | setBits++;
28 | }
29 | return setBits;
30 | }
31 | public static void main(String args[]) {
32 | System.out.println(countSetBits(9));
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Key Concepts/MethodOverriding2.java:
--------------------------------------------------------------------------------
1 | // A Simple Java program to demonstrate
2 | // Overriding and Access-Modifiers
3 |
4 | class Parent {
5 | // private methods are not overridden
6 | private void m1()
7 | {
8 | System.out.println("From parent m1()");
9 | }
10 | protected void m2()
11 | {
12 | System.out.println("From parent m2()");
13 | }
14 | }
15 | class Child extends Parent {
16 | // new m1() method
17 | // unique to Child class
18 | private void m1()
19 | {
20 | System.out.println("From child m1()");
21 | }
22 | // overriding method
23 | // with more accessibility
24 | @Override public void m2()
25 | {
26 | System.out.println("From child m2()");
27 | }
28 | }
29 | // Main class
30 | class Main {
31 | public static void main(String[] args)
32 | {
33 | Parent obj1 = new Parent();
34 | obj1.m2();
35 | Parent obj2 = new Child();
36 | obj2.m2();
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/String/StringComparison.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class CompareStrings {
4 | public static void main(String args[]) {
5 | String name1 = "Tony";
6 | String name2 = "Tony";
7 |
8 | if(name1.equals(name2)) {
9 | System.out.println("They are the same string");
10 | } else {
11 | System.out.println("They are different strings");
12 | }
13 |
14 | //DO NOT USE == to check for string equality
15 | //Gives correct answer here
16 | if(name1 == name2) {
17 | System.out.println("They are the same string");
18 | } else {
19 | System.out.println("They are different strings");
20 | }
21 |
22 | //Gives incorrect answer here
23 | if(new String("Tony") == new String("Tony")) {
24 | System.out.println("They are the same string");
25 | } else {
26 | System.out.println("They are different strings");
27 | }
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/02-C and C++/C/String/StringOperations.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | int main()
4 | {
5 | int len1, len2, m;
6 | char a[100];
7 | char b[100];
8 | char c[100];
9 | printf("Enter First String");
10 | scanf("%s", &a);
11 | printf("Enter Second String");
12 | scanf("%s", &b);
13 |
14 | printf(" • First String is %s\n", a);
15 | printf(" • Second string is %s\n", b);
16 |
17 | len1 = strlen(a); // Length Calculation
18 | len2 = strlen(b);
19 | printf(" • Length of First String is %d\n", len1);
20 | printf(" • Length of Second String is %d\n", len2);
21 | strcat(a, b); //String Joining
22 | printf(" • Joined String is %s\n", a);
23 | strcpy(c, a); // String Copy
24 | printf(" • Copied String is %s\n", c);
25 | m = strcmp(a, b); //comparing String
26 | if (m == 0)
27 | {
28 | printf(" • String are Equal");
29 | }
30 | else
31 | {
32 | printf(" • String are Not Equal");
33 | }
34 |
35 | return 0;
36 | }
--------------------------------------------------------------------------------
/01-Java/Java Basics/Conditional Statements/If...elseif...else.java:
--------------------------------------------------------------------------------
1 | /*
2 | Ladder If Else Syntax: ------>
3 | if (condition 1) {
4 | //block of code
5 | } else if (condition 2) {
6 | //block of code
7 | } else if (condition 3) {
8 | //block of code
9 | } else if (condition 4) {
10 | //block of code
11 | } else {
12 | //if no condition matches
13 | //block of code
14 | }
15 | */
16 |
17 | public class JavaIf {
18 | public static void main(String[] args) {
19 | String name[] = {"Mohan", "Rohan", "Soham"};
20 | int Roll[] = {25, 36, 71};
21 | if (name[0] == "Mohan" && Roll[1] == 25) {
22 | System.out.println("Details of Mohan.");
23 | } else if (name[2] == "Rohan" && Roll[1] == 36) {
24 | System.out.println("Details of Rohan.");
25 | } else if (name[2] == "Soham" && Roll[2] == 71) {
26 | System.out.println("Details of Soham.");
27 | } else {
28 | System.out.println("Invalid details.");
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/02-C and C++/C/Array/Structure Array/StructureArrayBasics.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | // Define a structure
4 | struct Person {
5 | char name[50];
6 | int age;
7 | };
8 |
9 | int main() {
10 | // Declare an array of structures
11 | struct Person people[3];
12 |
13 | // Assign values to the elements of the array
14 | strcpy(people[0].name, "Alice");
15 | people[0].age = 25;
16 |
17 | strcpy(people[1].name, "Bob");
18 | people[1].age = 30;
19 |
20 | strcpy(people[2].name, "Charlie");
21 | people[2].age = 35;
22 |
23 | // Access and print the values of elements in the array
24 | printf("Person 1:\n");
25 | printf("Name: %s\n", people[0].name);
26 | printf("Age: %d\n\n", people[0].age);
27 |
28 | printf("Person 2:\n");
29 | printf("Name: %s\n", people[1].name);
30 | printf("Age: %d\n\n", people[1].age);
31 |
32 | printf("Person 3:\n");
33 | printf("Name: %s\n", people[2].name);
34 | printf("Age: %d\n\n", people[2].age);
35 |
36 | return 0;
37 | }
38 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/ArrayList/MultidimensionalArrayList.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 | public class Multidimenional {
3 | public static void main(String args[]) {
4 | ArrayList> mainList = new ArrayList<>();
5 | ArrayList list1 = new ArrayList<>();
6 | ArrayList list2 = new ArrayList<>();
7 | ArrayList list3 = new ArrayList<>();
8 |
9 | mainList.add(list1);
10 | mainList.add(list2);
11 | mainList.add(list3);
12 |
13 | for(int i=1; i<=5; i++) {
14 | list1.add(i*1);
15 | list2.add(i*2);
16 | list3.add(i*3);
17 | }
18 |
19 | //print all numbers
20 | for(int i=0; i currList = mainList.get(i);
22 | for(int j=0; j set = new HashSet<>();
7 | //Add
8 |
9 | set.add(1);
10 | set.add(2);
11 | set.add(3);
12 | set.add(1);
13 |
14 | //Size
15 | System.out.println("size of set is : " + set.size());
16 |
17 | //Search
18 | if(set.contains(1)) {
19 | System.out.println("present");
20 | }
21 | if(!set.contains(6)) {
22 | System.out.println("absent");
23 | }
24 |
25 | //Delete
26 | set.remove(1);
27 | if(!set.contains(1)) {
28 | System.out.println("absent");
29 | }
30 |
31 | //Print all elements
32 | System.out.println(set);
33 | //isEmpty
34 |
35 | if(!set.isEmpty()) {
36 |
37 | System.out.println("set is not empty");
38 |
39 | }
40 |
41 | }
42 |
43 | }
44 |
45 |
46 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/String/LeetCode Top Question/StringComparison.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class CompareStrings {
4 | public static void main(String args[]) {
5 | String name1 = "Tony";
6 | String name2 = "Tony";
7 |
8 | if(name1.equals(name2)) {
9 | System.out.println("They are the same string");
10 | } else {
11 | System.out.println("They are different strings");
12 | }
13 |
14 | //DO NOT USE == to check for string equality
15 | //Gives correct answer here
16 | if(name1 == name2) {
17 | System.out.println("They are the same string");
18 | } else {
19 | System.out.println("They are different strings");
20 | }
21 |
22 | //Gives incorrect answer here
23 | if(new String("Tony") == new String("Tony")) {
24 | System.out.println("They are the same string");
25 | } else {
26 | System.out.println("They are different strings");
27 | }
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/02-C and C++/C++/Operators/03_Arthematic_Operator.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main() {
5 | int a, b;
6 |
7 | // Input two integers
8 | cout << "Enter two integers: ";
9 | cin >> a >> b;
10 |
11 | // Addition
12 | int sum = a + b;
13 | cout << "Sum: " << sum << endl;
14 |
15 | // Subtraction
16 | int difference = a - b;
17 | cout << "Difference: " << difference << endl;
18 |
19 | // Multiplication
20 | int product = a * b;
21 | cout << "Product: " << product << endl;
22 |
23 | // Division
24 | if (b != 0) {
25 | int quotient = a / b;
26 | cout << "Quotient: " << quotient << endl;
27 | } else {
28 | cout << "Division by zero is not allowed." << endl;
29 | }
30 |
31 | // Modulus
32 | if (b != 0) {
33 | int remainder = a % b;
34 | cout << "Remainder: " << remainder << endl;
35 | } else {
36 | cout << "Modulus by zero is not allowed." << endl;
37 | }
38 |
39 | return 0;
40 | }
41 |
--------------------------------------------------------------------------------
/02-C and C++/C++/Sorting Algorithms/Bubble_Sort.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | // Function to perform Bubble Sort
5 | void bubbleSort(int arr[], int n) {
6 | for(int i = 0; i < n-1; i++) {
7 | for(int j = 0; j < n-i-1; j++) {
8 | if(arr[j] > arr[j+1]) {
9 | // Swap arr[j] and arr[j+1]
10 | int temp = arr[j];
11 | arr[j] = arr[j+1];
12 | arr[j+1] = temp;
13 | }
14 | }
15 | }
16 | }
17 |
18 | // Function to print an array
19 | void printArray(int arr[], int size) {
20 | for(int i = 0; i < size; i++) {
21 | cout << arr[i] << " ";
22 | }
23 | cout << endl;
24 | }
25 |
26 | int main() {
27 | int arr[] = {64, 34, 25, 12, 22, 11, 90};
28 | int n = sizeof(arr)/sizeof(arr[0]);
29 | cout << "Original array: ";
30 | printArray(arr, n);
31 |
32 | bubbleSort(arr, n);
33 |
34 | cout << "Sorted array: ";
35 | printArray(arr, n);
36 |
37 | return 0;
38 | }
39 |
--------------------------------------------------------------------------------
/02-C and C++/C/LinkedList/Singly LinkedList/BasicLLCreations.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | struct Node
5 | {
6 | int data;
7 | struct Node *next;
8 | };
9 | void linkedlist(struct Node *ptr)
10 | {
11 | while(ptr!=NULL)
12 | {
13 | printf("Element : %d\n",ptr->data);
14 | ptr=ptr->next;
15 | }
16 | }
17 |
18 | int main()
19 | {
20 | struct Node *head;
21 | struct Node *second;
22 | struct Node *third;
23 | struct Node *fourth;
24 |
25 | head=(struct Node*)malloc(sizeof(struct Node));
26 | second=(struct Node*)malloc(sizeof(struct Node));
27 | third=(struct Node*)malloc(sizeof(struct Node));
28 | fourth=(struct Node*)malloc(sizeof(struct Node));
29 |
30 | head->data=7;
31 | head->next=second;
32 |
33 | second->data=23;
34 | second->next=third;
35 |
36 | third->data=45;
37 | third->next=fourth;
38 |
39 | fourth->data=2;
40 | fourth->next=NULL;
41 |
42 | linkedlist(head);
43 | return 0;
44 | }
45 |
--------------------------------------------------------------------------------
/01-Java/Key Concepts/Two Pointer Approach/Linked List/FindKthNodefromLast.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 |
3 | // Function to find the data of kth node from
4 | // the end of a linked list.
5 | int getKthFromLast(Node head, int k) {
6 | if (head == null || k <= 0) {
7 | return -1; // Assuming -1 as an error code, can be adjusted based on requirement
8 | }
9 |
10 | Node fast = head;
11 | Node slow = head;
12 |
13 | // Move fast k nodes ahead
14 | for (int i = 0; i < k; i++) {
15 | if (fast == null) {
16 | return -1; // k is larger than the length of the list
17 | }
18 | fast = fast.next;
19 | }
20 |
21 | // Move both fast and slow pointers until fast reaches the end
22 | while (fast != null) {
23 | fast = fast.next;
24 | slow = slow.next;
25 | }
26 |
27 | // Slow is now at the k-th node from the end
28 | return slow.data;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/01-Java/Key Concepts/Sliding Window/MAXsubArraySUM.java:
--------------------------------------------------------------------------------
1 | // Java code O(n*k) solution for finding maximum sum of
2 | // a subarray of size k
3 | class SlidingWindow {
4 | // Returns maximum sum in
5 | // a subarray of size k.
6 | static int maxSum(int arr[], int n, int k)
7 | {
8 | // Initialize result
9 | int max_sum = Integer.MIN_VALUE;
10 |
11 | // Consider all blocks starting with i.
12 | for (int i = 0; i < n - k + 1; i++) {
13 | int current_sum = 0;
14 | for (int j = 0; j < k; j++)
15 | current_sum = current_sum + arr[i + j];
16 |
17 | // Update result if required.
18 | max_sum = Math.max(current_sum, max_sum);
19 | }
20 |
21 | return max_sum;
22 | }
23 |
24 | // Driver code
25 | public static void main(String[] args)
26 | {
27 | int arr[] = { 1, 4, 2, 10, 2, 3, 1, 0, 20 };
28 | int k = 4;
29 | int n = arr.length;
30 | System.out.println(maxSum(arr, n, k));
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Basics/OOPsBasics3.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | class Employee {
3 | private int Id ;
4 | private String Name;
5 | void setname(String name) {
6 | Name = name;
7 | }
8 | String getname() {
9 | return Name ;
10 |
11 | }
12 | void setId(int id) {
13 | Id = id;
14 | }
15 | int getId() {
16 | return Id;
17 |
18 | }
19 | Employee() { // Constructor with Defualt value
20 | Id = 0;
21 | Name = "No_Name_Entered_!!";
22 | }
23 | void print_details(){
24 | System.out.println("Name : " +Name + "\nId : " +Id);
25 | }
26 | }
27 | public class Main {
28 | public static void main(String[] args) {
29 | Scanner sc = new Scanner(System.in);
30 | Employee Ep1 = new Employee();
31 | // System.out.println("Enter Your ID !!");
32 | // int Id_in = sc.nextInt();
33 | //Ep1.setId(Id_in);
34 | //System.out.println("Enter Your Name !!");
35 | //String ep_name = sc.next();
36 | //Ep1.setname(ep_name);
37 | // int a=Ep1.getId();
38 | // String b=Ep1.getname();
39 | Ep1.print_details();
40 |
41 | }
42 | }
--------------------------------------------------------------------------------
/01-Java/Problem Solving Patterns/Two Pointer Approach/Linked List/FindKthNodefromLast.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 |
3 | // Function to find the data of kth node from
4 | // the end of a linked list.
5 | int getKthFromLast(Node head, int k) {
6 | if (head == null || k <= 0) {
7 | return -1; // Assuming -1 as an error code, can be adjusted based on requirement
8 | }
9 |
10 | Node fast = head;
11 | Node slow = head;
12 |
13 | // Move fast k nodes ahead
14 | for (int i = 0; i < k; i++) {
15 | if (fast == null) {
16 | return -1; // k is larger than the length of the list
17 | }
18 | fast = fast.next;
19 | }
20 |
21 | // Move both fast and slow pointers until fast reaches the end
22 | while (fast != null) {
23 | fast = fast.next;
24 | slow = slow.next;
25 | }
26 |
27 | // Slow is now at the k-th node from the end
28 | return slow.data;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Concepts/3-RecursionBasics.java:
--------------------------------------------------------------------------------
1 |
2 |
3 | public class Reverse {
4 | static int sum = 0;
5 | static void rev1(int n) {
6 | if (n == 0) {
7 | return;
8 | }
9 | int rem = n % 10;
10 | sum = sum * 10 + rem;
11 | rev1(n/10);
12 | }
13 |
14 | static int rev2(int n) {
15 | // concept: with need a variable in agrument but cant change defualt functions arguments :
16 | // sometimes you might need some additional variables in the argument
17 | // in that case, make another function
18 | int digits = (int)(Math.log10(n)) + 1;
19 | return helper(n, digits);
20 | }
21 |
22 | private static int helper(int n, int digits) {
23 | if (n%10 == n) {
24 | return n;
25 | }
26 | int rem = n % 10;
27 | return rem * (int)(Math.pow(10, digits-1)) + helper(n/10, digits-1);
28 | }
29 |
30 | public static void main(String[] args) {
31 | System.out.println(rev2(1234));
32 | }
33 |
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Constructors/Constructors.java:
--------------------------------------------------------------------------------
1 | class Employee {
2 | // First constructor
3 | Employee(String s, int i){
4 | System.out.println("The name of the first employee is : " + s);
5 | System.out.println("The id of the first employee is : " + i);
6 | }
7 | // Constructor overloaded
8 | Employee(String s, int i, int salary){
9 | System.out.println("The name of the second employee is : " + s);
10 | System.out.println("The id of the second employee is : " + i);
11 | System.out.println("The salary of second employee is : " + salary);
12 | }
13 |
14 | }
15 | public class constructors {
16 | public static void main(String[] args) {
17 | Employee shubham = new Employee("Shubham",1);
18 | Employee harry = new Employee("Harry",2,70000);
19 |
20 |
21 | /*
22 | The name of the first employee is : Shubham
23 | The id of the first employee is : 1
24 | The name of the second employee is : Harry
25 | The id of the second employee is : 2
26 | The salary of second employee is : 70000
27 | */
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/02-C and C++/C++/Sorting Algorithms/Insertion_Sort.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | // Function to perform Insertion Sort
5 | void insertionSort(int arr[], int n) {
6 | for(int i = 1; i < n; i++) {
7 | int key = arr[i];
8 | int j = i - 1;
9 |
10 | // Move elements of arr[0..i-1], that are greater than key,
11 | // to one position ahead of their current position
12 | while(j >= 0 && arr[j] > key) {
13 | arr[j + 1] = arr[j];
14 | j = j - 1;
15 | }
16 | arr[j + 1] = key;
17 | }
18 | }
19 |
20 | // Function to print an array
21 | void printArray(int arr[], int size) {
22 | for(int i = 0; i < size; i++) {
23 | cout << arr[i] << " ";
24 | }
25 | cout << endl;
26 | }
27 |
28 | int main() {
29 | int arr[] = {12, 11, 13, 5, 6};
30 | int n = sizeof(arr) / sizeof(arr[0]);
31 | cout << "Original array: ";
32 | printArray(arr, n);
33 |
34 | insertionSort(arr, n);
35 |
36 | cout << "Sorted array: ";
37 | printArray(arr, n);
38 |
39 | return 0;
40 | }
41 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Trees/Building A Tree/FromPreOrderSequence.java:
--------------------------------------------------------------------------------
1 | public class BinaryTreesYT {
2 |
3 | static class Node {
4 | int data;
5 | Node left;
6 | Node right;
7 |
8 | Node(int data) {
9 | this.data = data;
10 | this.left = null;
11 | this.right = null;
12 | }
13 | }
14 |
15 | static class BinaryTree {
16 | static int idx = -1;
17 | public static Node buildTree(int nodes[]) {
18 | idx++;
19 | if(nodes[idx] == -1) {
20 | return null;
21 | }
22 | Node newNode = new Node(nodes[idx]);
23 | newNode.left = buildTree(nodes);
24 | newNode.right = buildTree(nodes);
25 | return newNode;
26 | }
27 | }
28 |
29 | public static void main(String args[]) {
30 | int nodes[] = {1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1};
31 | BinaryTree tree = new BinaryTree()
32 |
33 | Node root = tree.buildTree(nodes);
34 | System.out.println(root.data); // output : 1(Root nodes of the tree)
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/01-Java/Searching Algorithms/OrderAgnosticBinarySearch.java:
--------------------------------------------------------------------------------
1 | class BinarySearch{
2 | static int orderAgnosticBS(int[] arr, int target) {
3 | int leftIndex = 0;
4 | int rightIndex = arr.length - 1;
5 |
6 | // Determine if the array is sorted in ascending or descending order
7 | boolean isAscendingOrder = arr[leftIndex] < arr[rightIndex];
8 |
9 | while (leftIndex <= rightIndex) {
10 | // Find the middle element
11 | int middleIndex = leftIndex + (rightIndex - leftIndex) / 2;
12 |
13 | if (arr[middleIndex] == target) {
14 | return middleIndex;
15 | }
16 |
17 | if (isAscendingOrder) {
18 | if (target < arr[middleIndex]) {
19 | rightIndex = middleIndex - 1;
20 | } else {
21 | leftIndex = middleIndex + 1;
22 | }
23 | } else {
24 | if (target > arr[middleIndex]) {
25 | rightIndex = middleIndex - 1;
26 | } else {
27 | leftIndex = middleIndex + 1;
28 | }
29 | }
30 | }
31 | return -1;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Backtracking/Maze Problems/MazeWithPathRestrictions.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 |
3 | public class Maze {
4 | public static void main(String[] args) {
5 | boolean[][] board = {
6 | {true, true, true},
7 | {true, false, true},
8 | {true, true, true},
9 | };
10 | // false Symbolizes Obstacles you can access the cell you have to avoid that path.
11 | pathRestrictions("", board, 0, 0);
12 | }
13 | static void pathRestrictions(String p, boolean[][] maze, int r, int c) {
14 | if (r == maze.length - 1 && c == maze[0].length - 1) {
15 | System.out.println(p);
16 | return;
17 | }
18 |
19 | if (!maze[r][c]) {
20 | return;
21 | }
22 |
23 | if (r < maze.length - 1) {
24 | pathRestrictions(p + 'D', maze, r+1, c);
25 | }
26 |
27 | if (c < maze[0].length - 1) {
28 | pathRestrictions(p + 'R', maze, r, c+1);
29 | }
30 | }
31 | /* Output :
32 | DDRR
33 | RRDD
34 | [Program finished]
35 | */
36 | }
37 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/String/LeetCode Top Question/ConvertStringToUppercase.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | //Problem : Convert all first letters of words to UPPERCASE
4 | public class ConvertToUppercase {
5 | public static String getUppercase(String str) {
6 | StringBuilder newStr = new StringBuilder("");
7 | char firstChar = Character.toUpperCase(str.charAt(0));
8 | newStr.append(firstChar);
9 | for(int i=0; i
2 | using namespace std;
3 |
4 | int main() {
5 | int rows1, cols1;
6 |
7 | // Input dimensions of the matrices
8 | cout << "Enter the number of rows and columns of the first matrix: ";
9 | cin >> rows1 >> cols1;
10 | cout << "Enter the number of rows and columns of the second matrix: ";
11 | cin >> rows2 >> cols2;
12 |
13 | // Declare matrices
14 | int matrix1[rows1][cols1], matrix2[rows2][cols2], product[rows1][cols2];
15 |
16 | // Input elements for the first matrix
17 | cout << "Enter elements of the first matrix:" << endl;
18 | for (int i = 0; i < rows1; i++) {
19 | for (int j = 0; j < cols1; j++) {
20 | cin >> matrix1[i][j];
21 | }
22 | }
23 | // Display the product matrix
24 | cout << "Product of the matrices:" << endl;
25 | for (int i = 0; i < rows1; i++) {
26 | for (int j = 0; j < cols2; j++) {
27 | cout << product[i][j] << " ";
28 | }
29 | cout << endl;
30 | }
31 |
32 | return 0;
33 | }
34 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Hash Map/HashMapOperation.java:
--------------------------------------------------------------------------------
1 | import java.util.HashMap;
2 | import java.util.HashSet;
3 |
4 |
5 | public class Hashing {
6 |
7 | public static void main(String args[]) {
8 |
9 | //Creation
10 |
11 | HashMap map = new HashMap<>();
12 |
13 |
14 | //Insertion
15 |
16 | map.put("India", 120);
17 |
18 | map.put("US", 30);
19 |
20 | map.put("China", 150);
21 |
22 |
23 | System.out.println(map);
24 |
25 |
26 | map.put("China", 180);
27 |
28 | System.out.println(map);
29 |
30 |
31 |
32 | //Searching
33 |
34 | if(map.containsKey("Indonesia")) {
35 |
36 | System.out.println("key is present in the map");
37 |
38 | } else {
39 |
40 | System.out.println("key is not present in the map");
41 |
42 | }
43 |
44 |
45 | System.out.println(map.get("China")); //key exists
46 |
47 | System.out.println(map.get("Indonesia")); //key doesn't exist
48 |
49 | //Removing
50 |
51 | map.remove("China");
52 |
53 | System.out.println(map);
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Inheritance/Inheritance.java:
--------------------------------------------------------------------------------
1 | class Base{
2 | public int x;
3 |
4 | public int getX() {
5 | return x;
6 | }
7 |
8 | public void setX(int x) {
9 | System.out.println("I am in base and setting x now");
10 | this.x = x;
11 | }
12 |
13 | public void printMe(){
14 | System.out.println("I am a constructor");
15 | }
16 | }
17 |
18 | class Derived extends Base{
19 | public int y;
20 |
21 | public int getY() {
22 | return y;
23 | }
24 |
25 | public void setY(int y) {
26 | this.y = y;
27 | }
28 | }
29 |
30 | public class Inheritance {
31 | public static void main(String[] args) {
32 | // Creating an Object of base class
33 | Base b = new Base();
34 | b.setX(4);
35 | System.out.println(b.getX());
36 |
37 | // Creating an object of derived class
38 | Derived d = new Derived();
39 | d.setY(43);
40 | System.out.println(d.getY());
41 | // You can Set/Get X in Derived class object as well but you can't access Y from Base class because base is a super class
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Stack/StackUsingArrayList.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 |
3 | public class StackAL {
4 | static class Stack {
5 | ArrayList list = new ArrayList<>();
6 |
7 | public void push(int data) {
8 | list.add(data);
9 | }
10 |
11 | public boolean isEmpty() {
12 | return list.size() == 0;
13 | }
14 |
15 | public int pop() {
16 | if(isEmpty()) {
17 | return -1;
18 | }
19 | int top = list.remove(list.size()-1);
20 | return top;
21 | }
22 |
23 | public int peek() {
24 | if(isEmpty()) {
25 | return -1;
26 | }
27 | return list.get(list.size()-1);
28 | }
29 | }
30 | public static void main(String args[]) {
31 | Stack stack = new Stack();
32 | stack.push(1);
33 | stack.push(2);
34 | stack.push(3);
35 | stack.push(4);
36 |
37 | while(!stack.isEmpty()) {
38 | System.out.println(stack.peek());
39 | stack.pop();
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/02-C and C++/C++/Sorting Algorithms/Selection_Sort.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | // Function to perform Selection Sort
5 | void selectionSort(int arr[], int n) {
6 | for(int i = 0; i < n-1; i++) {
7 | // Find the minimum element in unsorted array
8 | int minIndex = i;
9 | for(int j = i+1; j < n; j++) {
10 | if(arr[j] < arr[minIndex]) {
11 | minIndex = j;
12 | }
13 | }
14 | // Swap the found minimum element with the first element
15 | int temp = arr[minIndex];
16 | arr[minIndex] = arr[i];
17 | arr[i] = temp;
18 | }
19 | }
20 |
21 | // Function to print an array
22 | void printArray(int arr[], int size) {
23 | for(int i = 0; i < size; i++) {
24 | cout << arr[i] << " ";
25 | }
26 | cout << endl;
27 | }
28 |
29 | int main() {
30 | int arr[] = {64, 25, 12, 22, 11};
31 | int n = sizeof(arr)/sizeof(arr[0]);
32 | cout << "Original array: ";
33 | printArray(arr, n);
34 |
35 | selectionSort(arr, n);
36 |
37 | cout << "Sorted array: ";
38 | printArray(arr, n);
39 |
40 | return 0;
41 | }
42 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Backtracking/Maze Problems/MazeThreeSide.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 |
3 | public class Maze {
4 | public static void main(String[] args) {
5 |
6 | System.out.println(pathRetDiagonal("", 3, 3));
7 | // Print path when you can move in three directions
8 | // D - Diagonal
9 | // R - Right
10 | // V - Down
11 | }
12 | static ArrayList pathRetDiagonal(String p, int r, int c) {
13 | if (r == 1 && c == 1) {
14 | ArrayList list = new ArrayList<>();
15 | list.add(p);
16 | return list;
17 | }
18 |
19 | ArrayList list = new ArrayList<>();
20 |
21 | if (r > 1 && c > 1) {
22 | list.addAll(pathRetDiagonal(p + 'D', r-1, c-1));
23 | }
24 |
25 | if (r > 1) {
26 | list.addAll(pathRetDiagonal(p + 'V', r-1, c));
27 | }
28 |
29 | if (c > 1) {
30 | list.addAll(pathRetDiagonal(p + 'H', r, c-1));
31 | }
32 |
33 | return list;
34 | }
35 |
36 | /*
37 | Output ;
38 | [DD, DVH, DHV, VDH, VVHH, VHD, VHVH, VHHV, HDV, HVD, HVVH, HVHV, HHVV]
39 | */
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Arrays/2D Arrays/2DArraySeraching.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | //Problem : Search for key in sorted matrix
4 |
5 | public class SearchInSorted {
6 | public static boolean staircaseSearch(int matrix[][], int key) {
7 | int n = matrix.length, m = matrix[0].length;
8 | int i = 0, j = n-1;
9 |
10 | while(j>=0 && i key) {
21 | j--;
22 | }
23 |
24 | }
25 |
26 | System.out.println("NOT Found");
27 | return false;
28 | }
29 | public static void main(String args[]) {
30 | int matrix[][] = {{10, 20, 30, 40},
31 | {15, 25, 35, 45},
32 | {27, 29, 37, 48},
33 | {32, 33, 39, 50}};
34 | int key = 33;
35 | staircaseSearch(matrix, key);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Patterns/Pattern-6.java:
--------------------------------------------------------------------------------
1 | public class Patterns {
2 | public static void butterfly(int n) {
3 | //1st half
4 | for(int i=1; i<=n; i++) {
5 | //stars
6 | for(int j=1; j<=i; j++) {
7 | System.out.print("*");
8 | }
9 |
10 | //spaces
11 | for(int j=1; j<=2*(n-i); j++) {
12 | System.out.print(" ");
13 | }
14 |
15 | //stars
16 | for(int j=1; j<=i; j++) {
17 | System.out.print("*");
18 | }
19 |
20 | System.out.println();
21 | }
22 |
23 | //2nd half
24 | for(int i=n; i>=1; i--) {
25 | //stars
26 | for(int j=1; j<=i; j++) {
27 | System.out.print("*");
28 | }
29 |
30 | //spaces
31 | for(int j=1; j<=2*(n-i); j++) {
32 | System.out.print(" ");
33 | }
34 |
35 | //stars
36 | for(int j=1; j<=i; j++) {
37 | System.out.print("*");
38 | }
39 |
40 | System.out.println();
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/02-C and C++/C/LinkedList/Singly LinkedList/IterativeLLCreations.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | struct Node {
4 | int data ;
5 | struct Node *next;
6 | struct Node *prev;
7 | };
8 |
9 | struct Node *head=NULL;
10 | struct Node *temp;
11 |
12 | void Display(){
13 |
14 | temp=head;
15 | printf("[ ");
16 | while(temp!=NULL){
17 | printf("%d ",temp->data);
18 | temp=temp->next;
19 | }
20 | printf(" ]\n");
21 | }
22 |
23 | void main()
24 | {
25 | int ch,Num,w;
26 | struct Node *Newnode;
27 | printf("First Create A Linked :\nEnter Number of Nodes You Want : ");
28 | scanf("%d",&Num);
29 | for (int i =1;i<=Num;i++){
30 | Newnode=(struct Node *)malloc(sizeof(struct Node));
31 | printf("Enter Data as a Number : ");
32 | scanf("%d",&Newnode->data);
33 | Newnode->next=NULL;
34 |
35 | if(head==NULL){
36 | head=Newnode;
37 | temp=Newnode;
38 | Newnode->prev=NULL;
39 | }
40 | else{
41 | temp->next=Newnode;
42 | Newnode->prev=temp;
43 | temp=Newnode;
44 | }
45 | }
46 | printf("Created Linked List Is : ");
47 | Display();
48 | }
49 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Basics/OOPsBasics2.java:
--------------------------------------------------------------------------------
1 | class Employee{
2 | int id;
3 | int salary;
4 | String name;
5 | public void printDetails(){
6 | System.out.println("My id is " + id);
7 | System.out.println("and my name is "+ name);
8 | }
9 |
10 | public int getSalary(){
11 | return salary;
12 | }
13 | }
14 |
15 | public class Class {
16 | public static void main(String[] args) {
17 | System.out.println("This is our custom class");
18 | Employee harry = new Employee(); // Instantiating a new Employee Object
19 | Employee john = new Employee(); // Instantiating a new Employee Object
20 |
21 | // Setting Attributes for Harry
22 | harry.id = 12;
23 | harry.salary = 34;
24 | harry.name = "HelloWorld";
25 |
26 | // Setting Attributes for John
27 | john.id = 17;
28 | john.salary = 12;
29 | john.name = "John Cena";
30 |
31 | // Printing the Attributes
32 | harry.printDetails();
33 | john.printDetails();
34 | int salary = john.getSalary();
35 | System.out.println(salary);
36 | // System.out.println(harry.id);
37 | // System.out.println(harry.name);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Basics/OOPsBasics4.java:
--------------------------------------------------------------------------------
1 | class Employee{
2 | int id;
3 | int salary;
4 | String name;
5 | public void printDetails(){
6 | System.out.println("My id is " + id);
7 | System.out.println("and my name is "+ name);
8 | }
9 |
10 | public int getSalary(){
11 | return salary;
12 | }
13 | }
14 |
15 | public class Class {
16 | public static void main(String[] args) {
17 | System.out.println("This is our custom class");
18 | Employee harry = new Employee(); // Instantiating a new Employee Object
19 | Employee john = new Employee(); // Instantiating a new Employee Object
20 |
21 | // Setting Attributes for Harry
22 | harry.id = 12;
23 | harry.salary = 34;
24 | harry.name = "HelloWorld";
25 |
26 | // Setting Attributes for John
27 | john.id = 17;
28 | john.salary = 12;
29 | john.name = "John Cena";
30 |
31 | // Printing the Attributes
32 | harry.printDetails();
33 | john.printDetails();
34 | int salary = john.getSalary();
35 | System.out.println(salary);
36 | // System.out.println(harry.id);
37 | // System.out.println(harry.name);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Switch-Case Statements/Swtich-Case.java:
--------------------------------------------------------------------------------
1 | /*
2 | Switch Case Declaration Syntax ------>
3 |
4 | switch (expression) {
5 | case value1:
6 | // code
7 | break;
8 | case value2:
9 | // code
10 | break;
11 | ...
12 | ...
13 | default:
14 | // default statements
15 | }
16 |
17 | */
18 | public class JavaSwitch {
19 | public static void main(String[] args) {
20 | String day = "Wednesday";
21 | switch (day) {
22 | case "Sunday":
23 | System.out.println("Today is Sunday");
24 | break;
25 | case "Monday":
26 | System.out.println("Today is Monday");
27 | break;
28 | case "Tuesday":
29 | System.out.println("Today is Tuesday");
30 | break;
31 | case "Wednesday":
32 | System.out.println("Today is Wednesday");
33 | break;
34 | case "Thursday":
35 | System.out.println("Today is Thursday");
36 | break;
37 | case "Friday":
38 | System.out.println("Today is Friday");
39 | break;
40 | case "Saturday":
41 | System.out.println("Today is Saturday");
42 | break;
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/01-Java/Searching Algorithms/BinarySearch.java:
--------------------------------------------------------------------------------
1 | public class BinarySearch {
2 | public static int binarySearch(int[] arr, int target) {
3 | int left = 0;
4 | int right = arr.length - 1;
5 |
6 | while (left <= right) {
7 | int mid = left + (right - left) / 2;
8 |
9 | // Check if target is present at mid
10 | if (arr[mid] == target) {
11 | return mid;
12 | }
13 |
14 | // If target greater, ignore left half
15 | if (arr[mid] < target) {
16 | left = mid + 1;
17 | }
18 |
19 | // If target is smaller, ignore right half
20 | else {
21 | right = mid - 1;
22 | }
23 | }
24 |
25 | // If we reach here, then element was not present
26 | return -1;
27 | }
28 |
29 | public static void main(String[] args) {
30 | int[] arr = { 2, 3, 4, 10, 40 };
31 | int target = 10;
32 | int result = binarySearch(arr, target);
33 | if (result != -1) {
34 | System.out.println("Element found at index: " + result);
35 | } else {
36 | System.out.println("Element not found in the array.");
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/02-C and C++/C/Control Statement/Conditional Statement/ResultsAssessments.c:
--------------------------------------------------------------------------------
1 |
2 | #include
3 | int main()
4 | {
5 | int a, b, c, d, e;
6 | float f, g;
7 | printf("\n\t\t\t●| |RESULT| | ●");
8 | printf("\n Enter EM-1 marks");
9 | scanf("%d", &a);
10 | printf(" Enter EC marks");
11 | scanf("%d", &b);
12 | printf(" Enter BXE marks");
13 | scanf("%d", &c);
14 | printf(" Enter BEE marks");
15 | scanf("%d", &d);
16 | printf(" Enter CFP marks ");
17 | scanf("%d", &e);
18 | f = a + b + c + d + e;
19 | g = (f * 100) / 500;
20 | printf(" • Total Agregratte marks obtained out off 500 : %.1f", f);
21 | printf("\n • Precentage : %.2f ", g);
22 |
23 | if (g > 75)
24 | {
25 | printf("\n • Remark :Frist class with distingustion");
26 | }
27 | else if (74 > g && g > 65)
28 | {
29 | printf("\n • Remark : Frist class");
30 | }
31 | else if (64 > g && g > 54)
32 | {
33 | printf("\n • Remark : Higher Second class");
34 | }
35 |
36 | else if (54 > g && g > 45)
37 | {
38 | printf("\n • Remark : Lower second class");
39 | }
40 |
41 | else
42 | {
43 | printf("\n • Remark : Kangaroolation Tum Fail ho..!!!");
44 | }
45 | return 0;
46 | }
--------------------------------------------------------------------------------
/01-Java/Key Concepts/Two Pointer Approach/String/PalindromeString.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public boolean isPalindrome(String s) {
3 | // Initialize two pointers, one at the start and one at the end of the string
4 | int left = 0;
5 | int right = s.length() - 1;
6 |
7 | // Traverse the string from both ends towards the middle
8 | while (left < right) {
9 | // Skip non-alphanumeric characters from the left side
10 | while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
11 | left++;
12 | }
13 | // Skip non-alphanumeric characters from the right side
14 | while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
15 | right--;
16 | }
17 | // Compare the characters at the current pointers
18 | if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
19 | return false; // If they don't match, it's not a palindrome
20 | }
21 | // Move the pointers closer to the center
22 | left++;
23 | right--;
24 | }
25 |
26 | // If all characters match, the string is a palindrome
27 | return true;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Graphs/Creating a Graph/UsingAdjacencyList.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class buildGraph {
4 | static class Edge {
5 | int src;
6 | int dest;
7 | int wt;
8 |
9 | public Edge(int s, int d, int w) {
10 | this.src = s;
11 | this.dest = d;
12 | this.wt = w;
13 | }
14 | }
15 |
16 | static void createGraph(ArrayList graph[]) {
17 | for (int i = 0; i < graph.length; i++) {
18 | graph[i] = new ArrayList<>();
19 | }
20 |
21 | graph[0].add(new Edge(0, 1, 1));
22 | graph[0].add(new Edge(0, 2, 1));
23 | graph[1].add(new Edge(1, 0, 1));
24 | graph[1].add(new Edge(1, 3, 1));
25 | graph[2].add(new Edge(2, 0, 1));
26 | graph[2].add(new Edge(2, 4, 1));
27 | graph[3].add(new Edge(3, 1, 1));
28 | graph[3].add(new Edge(3, 4, 1));
29 | graph[3].add(new Edge(3, 5, 1));
30 | graph[4].add(new Edge(4, 2, 1));
31 | graph[4].add(new Edge(4, 3, 1));
32 | graph[4].add(new Edge(4, 5, 1));
33 | graph[5].add(new Edge(5, 3, 1));
34 | graph[5].add(new Edge(5, 4, 1));
35 | graph[5].add(new Edge(5, 6, 1));
36 | graph[6].add(new Edge(6, 5, 1));
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Subsets/SubsetsOfArrayWithDuplicates.java:
--------------------------------------------------------------------------------
1 |
2 | import java.util.ArrayList;
3 | import java.util.Arrays;
4 | import java.util.List;
5 |
6 | public class SubSet {
7 | public static void main(String[] args) {
8 | int[] arr = {1, 2, 2};
9 | List> ans = subsetDuplicate(arr);
10 | for (List list : ans) {
11 | System.out.println(list);
12 | }
13 | }
14 |
15 | static List> subsetDuplicate(int[] arr) {
16 | Arrays.sort(arr);
17 | List> outer = new ArrayList<>();
18 | outer.add(new ArrayList<>());
19 | int start = 0;
20 | int end = 0;
21 | for (int i = 0; i < arr.length; i++) {
22 | start = 0;
23 | // if current and previous element is same, s = e + 1
24 | if (i > 0 && arr[i] == arr[i-1]) {
25 | start = end + 1;
26 | }
27 | end = outer.size() - 1;
28 | int n = outer.size();
29 | for (int j = start; j < n; j++) {
30 | List internal = new ArrayList<>(outer.get(j));
31 | internal.add(arr[i]);
32 | outer.add(internal);
33 | }
34 | }
35 | return outer;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/01-Java/Problem Solving Patterns/Two Pointer Approach/String/PalindromeString.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public boolean isPalindrome(String s) {
3 | // Initialize two pointers, one at the start and one at the end of the string
4 | int left = 0;
5 | int right = s.length() - 1;
6 |
7 | // Traverse the string from both ends towards the middle
8 | while (left < right) {
9 | // Skip non-alphanumeric characters from the left side
10 | while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
11 | left++;
12 | }
13 | // Skip non-alphanumeric characters from the right side
14 | while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
15 | right--;
16 | }
17 | // Compare the characters at the current pointers
18 | if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
19 | return false; // If they don't match, it's not a palindrome
20 | }
21 | // Move the pointers closer to the center
22 | left++;
23 | right--;
24 | }
25 |
26 | // If all characters match, the string is a palindrome
27 | return true;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/02-C and C++/C/Functions/FunctionConcepts.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | float add()
4 | {
5 | float a, b, c;
6 | printf("Enter Two Number");
7 | scanf("%f%f", &a, &b);
8 | c = a + b;
9 | printf("Addition is %f", c);
10 | }
11 | float sub()
12 | {
13 | float a, b, c;
14 | printf("Enter Two Number");
15 | scanf("%f%f", &a, &b);
16 | c = a - b;
17 | printf(" Substraction is %f", c);
18 | }
19 | float multi()
20 | {
21 | float a, b, c;
22 | printf("Enter Two Number");
23 | scanf("%f%f", &a, &b);
24 | c = a * b;
25 | printf("Multiplication is %f", c);
26 | }
27 |
28 | float div(float a, float b)
29 | {
30 | float c;
31 | printf("Enter Two Number");
32 | scanf("%f%f", &a, &b);
33 | c = a / b;
34 | printf(" Division is %f", c);
35 | }
36 |
37 | void main()
38 | {
39 | int a, b;
40 | printf("\t\tOperation Menu\n\t\t1.Addition\n\t\t2.Substraction\n\t\t3.Multiplication\n\t\t4.Division\n");
41 | scanf("\t\t%d", &a);
42 | switch (a)
43 | {
44 | case 1:
45 | add();
46 | break;
47 | case 2:
48 | sub();
49 | break;
50 | case 3:
51 | multi();
52 | break;
53 | case 4:
54 | div(a, b);
55 | break;
56 | default:
57 | printf("\t\tInvaild Input");
58 | }
59 | }
60 |
61 |
--------------------------------------------------------------------------------
/02-C and C++/C/Stack/STACKAsADT.c:
--------------------------------------------------------------------------------
1 | #include
2 | int top;
3 | int size=5;
4 | void push (int a[10]){
5 | int num;
6 | if(top==size){
7 | printf("STACK OVERFLOW !!!!");
8 | }
9 | else {
10 | printf("Enter A NUMBER ");
11 | scanf("%d",&num);
12 | top=-1;
13 | a[top]=num;
14 | printf("%d PUSHED to [%d] Index",num,top);
15 | }
16 | }
17 | void pop(int a[10]){
18 | if(top<0){
19 | printf("STCK UNDERFLOW !!!!");
20 | }
21 | else {
22 | printf("Number TO DELETED is %d",a[top]);
23 | top--;
24 | }
25 | }
26 | int Full(){
27 | if(top==size ){
28 | return 0;
29 | }
30 | }
31 | int Empty(){
32 | if(top==-1){
33 | return 1;
34 | }
35 | }
36 | void main (){
37 | int a[20],Choice;
38 | printf("STACK OPERATION Menu\n1.PUSH\n2.POP\n3.Check Empty Or NOT\n4.Check FULL or NOT");
39 | scanf("%d",&Choice );
40 | int full_v=Full();
41 | int epty_v=Empty();
42 | switch(Choice){
43 | case 1 : push(a);
44 | break;
45 | case 2 : pop(a);
46 | break;
47 | case 3 :
48 |
49 | if(epty_v==1)
50 | {
51 | printf("STACK EMPTY!!!!!!");
52 | }
53 | else {
54 | printf("Some ELEMENT are Present !!!!!");
55 | }
56 | break;
57 | case 4 :
58 | if(full_v==0)
59 | {
60 | printf("STACK FULL!!!!!!");
61 | }
62 | else {
63 | printf("STACK is NOT FULL !!!!!");
64 | }
65 | break;
66 | default : printf("Invalid CHOICE ");
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Queue/LeetCode Top Questions/Problem1.java:
--------------------------------------------------------------------------------
1 | //queue using 2 stacks
2 |
3 | import java.util.*;
4 |
5 |
6 | public class QueueB {
7 |
8 | static class Queue {
9 |
10 | static Stack s1 = new Stack<>();
11 |
12 | static Stack s2 = new Stack<>();
13 |
14 |
15 | public static boolean isEmpty() {
16 |
17 | return s1.isEmpty();
18 |
19 | }
20 |
21 |
22 | public static void add(int data) {
23 |
24 | while(!s1.isEmpty()) {
25 |
26 | s2.push(s1.pop());
27 |
28 | }
29 |
30 | s1.push(data);
31 |
32 | while(!s2.isEmpty()) {
33 |
34 | s1.push(s2.pop());
35 |
36 | }
37 |
38 | }
39 |
40 |
41 | public static int remove() {
42 |
43 | return s1.pop();
44 |
45 | }
46 |
47 |
48 | public static int peek() {
49 |
50 | return s1.peek();
51 |
52 | }
53 |
54 | }
55 |
56 | public static void main(String args[]) {
57 |
58 | Queue q = new Queue();
59 |
60 | q.add(1);
61 |
62 | q.add(2);
63 |
64 | q.add(3);
65 |
66 |
67 | while(!q.isEmpty()) {
68 |
69 | System.out.println(q.peek());
70 |
71 | q.remove();
72 |
73 | }
74 |
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/01-Java/Problem Solving Patterns/Two Pointer Approach/Arrays/DeleteDuplicatesInSortedArray.java:
--------------------------------------------------------------------------------
1 |
2 | public class Main {
3 | class Solution {
4 | public int removeDuplicates(int[] nums) {
5 | int n = nums.length;
6 | if (n < 2) return n;
7 |
8 | int L = 0, R = 1;
9 |
10 | while (R < n) {
11 | if (nums[R] != nums[L]) {
12 | L++;
13 | nums[L] = nums[R];
14 | }
15 | R++;
16 | }
17 |
18 | return L + 1;
19 | }
20 | }
21 | public static void main(String[] args) {
22 | // Example sorted array with duplicates
23 | int[] nums = {1, 1, 2, 2, 3, 4, 4, 5};
24 |
25 | // Create an instance of the Solution class
26 | Solution solution = new Solution();
27 |
28 | // Call the removeDuplicates method and get the new length
29 | int newLength = solution.removeDuplicates(nums);
30 |
31 | // Print the results
32 | System.out.println("New length after removing duplicates: " + newLength);
33 | System.out.print("Array after removing duplicates: ");
34 |
35 | // Only print the first newLength elements of the array
36 | for (int i = 0; i < newLength; i++) {
37 | System.out.print(nums[i] + " ");
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/02-C and C++/C/Searching Algorithms/BinarySearch.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void Read_Array(int arr[20], int size)
4 | { // Function To Read Array
5 | for (int i = 0; i < size; i++)
6 | {
7 | printf("a[%d] = ", i);
8 | scanf("%d", &arr[i]);
9 | }
10 | }
11 |
12 | void Binary_Search(int arr[20], int size, int high, int low, int key) //Recurrive Function To Search Element
13 | if (low > high)
14 | {
15 | printf("Number Not Found\n");
16 | return;
17 | }
18 | int mid = (low + high) / 2; //Computing Mid
19 | if (key == arr[mid])
20 | {
21 |
22 | printf("%d Found at index %d\n", key, mid);
23 | return;
24 | }
25 | else if (key < arr[mid])
26 | {
27 | Binary_Search(arr, size, mid - 1, low, key); //Recurrsive Caliing To function Binary Search
28 | }
29 | else
30 | {
31 | Binary_Search(arr, size, high, mid + 1, key); //Recurrsive Caliing To function Binary Search
32 | }
33 | }
34 |
35 | int main()
36 | {
37 | int arr[20], size, key;
38 | printf("Enter array size: ");
39 | scanf("%d", &size);
40 | Read_Array(arr, size);
41 | printf(" Enter number to search: ");
42 | scanf("%d", &key);
43 | Binary_Search(arr, size, size - 1, 0, key); //Passing Value To function Binary Search
44 | return 0;
45 | }
46 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Stack/Custom Stack Class/CustomStack.java:
--------------------------------------------------------------------------------
1 | public class CustomStack {
2 | protected int[] data;
3 | private static final int DEFAULT_SIZE = 10;
4 |
5 | int ptr = -1;
6 |
7 | public CustomStack(){
8 | this(DEFAULT_SIZE);
9 | }
10 |
11 | public CustomStack(int size) {
12 | this.data = new int[size];
13 | }
14 |
15 | public boolean push(int item) {
16 | if (isFull()) {
17 | System.out.println("Stack is full!!");
18 | return false;
19 | }
20 | ptr++;
21 | data[ptr] = item;
22 | return true;
23 | }
24 |
25 | public int pop() throws StackException {
26 | if (isEmpty()) {
27 | throw new StackException("Cannot pop from an empty stack!!");
28 | }
29 | // int removed = data[ptr];
30 | // ptr--;
31 | // return removed;
32 | return data[ptr--];
33 | }
34 |
35 | public int peek() throws StackException {
36 | if (isEmpty()) {
37 | throw new StackException("Cannot peek from an empty stack!!");
38 | }
39 | return data[ptr];
40 | }
41 |
42 | public boolean isFull() {
43 | return ptr == data.length - 1; // ptr is at last index
44 | }
45 |
46 | public boolean isEmpty() {
47 | return ptr == -1;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Trees/Tree Traversals/InOrderTraversal.java:
--------------------------------------------------------------------------------
1 | public class BinaryTreesYT {
2 |
3 | static class Node {
4 | int data;
5 | Node left;
6 | Node right;
7 |
8 | Node(int data) {
9 | this.data = data;
10 | this.left = null;
11 | this.right = null;
12 | }
13 | }
14 |
15 | static class BinaryTree {
16 | static int idx = -1;
17 | public static Node buildTree(int nodes[]) {
18 | idx++;
19 | if(nodes[idx] == -1) {
20 | return null;
21 | }
22 | Node newNode = new Node(nodes[idx]);
23 | newNode.left = buildTree(nodes);
24 | newNode.right = buildTree(nodes);
25 | return newNode;
26 | }
27 | }
28 |
29 | public static void inorder(Node root) {
30 |
31 | if(root == null) {
32 |
33 | System.out.print(-1+" ");
34 |
35 | return;
36 |
37 | }
38 |
39 | inorder(root.left);
40 |
41 | System.out.print(root.data+" ");
42 |
43 | inorder(root.right);
44 |
45 | }
46 | public static void main(String args[]) {
47 | int nodes[] = {1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1};
48 | BinaryTree tree = new BinaryTree()
49 |
50 | Node root = tree.buildTree(nodes);
51 | tree.inorder();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/02-C and C++/C/Special Codes/Binary_Adder.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | // Function to perform binary addition of two binary numbers
4 | int binaryAddition(int binary1, int binary2) {
5 | int result = 0;
6 | int carry = 0;
7 | int placeValue = 1;
8 |
9 | while (binary1 > 0 || binary2 > 0 || carry > 0) {
10 | // Extract the last bit of each binary number
11 | int bit1 = binary1 % 10;
12 | int bit2 = binary2 % 10;
13 |
14 | // Calculate the sum of bits and carry
15 | int sumBits = bit1 + bit2 + carry;
16 |
17 | // Determine the resulting bit at the current place value
18 | result += (sumBits % 2) * placeValue;
19 |
20 | // Update the carry for the next iteration
21 | carry = sumBits / 2;
22 |
23 | // Move to the next higher bit
24 | binary1 /= 10;
25 | binary2 /= 10;
26 | placeValue *= 10;
27 | }
28 |
29 | return result;
30 | }
31 |
32 | int main() {
33 | // Example binary numbers for testing
34 | int binary1 = 1011; // Binary for 11 in decimal
35 | int binary2 = 1101; // Binary for 13 in decimal
36 |
37 | // Perform binary addition
38 | int result = binaryAddition(binary1, binary2);
39 |
40 | // Output the result
41 | printf("The sum of %d and %d in binary is: %d\n", binary1, binary2, result);
42 |
43 | return 0;
44 | }
45 |
--------------------------------------------------------------------------------
/01-Java/Key Concepts/Divide And Conquer/RotatedBinarySearch.java:
--------------------------------------------------------------------------------
1 | public class DividenConquer {
2 | public static int search(int arr[], int tar, int si, int ei) {
3 | if(si > ei) {
4 | return -1;
5 | }
6 | //kaam
7 | int mid = si + (ei-si)/2; //(si+ei)/2
8 |
9 | //case FOUND
10 | if(arr[mid] == tar) {
11 | return mid;
12 | }
13 |
14 | //mid on L1
15 | if(arr[si] <= arr[mid]) {
16 | //case a : left
17 | if(arr[si] <= tar && tar <= arr[mid]) {
18 | return search(arr, tar, si, mid-1);
19 | } else {
20 | //case b : right
21 | return search(arr, tar, mid+1, ei);
22 | }
23 | }
24 |
25 | //mid on L2
26 | else {
27 | //case c : right
28 | if(arr[mid] <= tar && tar <= arr[ei]) {
29 | return search(arr, tar, mid+1, ei);
30 | } else {
31 | //case d : left
32 | return search(arr, tar, si, mid-1);
33 | }
34 | }
35 | }
36 | public static void main(String args[]) {
37 | int arr[] = {4, 5, 6, 7, 0, 1, 2};
38 | int target = 6; //output -> 4
39 | int tarIdx = search(arr, target, 0, arr.length-1);
40 | System.out.println(tarIdx);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/String/String Builder/StringBuilderAllOperations.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | /*METHODS USED
4 | 1.Append()
5 | 2.setCharAt()
6 | 3.insert()
7 | 4.Delete()
8 | */
9 | public class Strings {
10 | public static void main( String args[]){
11 | Scanner sc = new Scanner(System.in);
12 | StringBuilder sb = new StringBuilder("JaiShriRam JaiShriRam");
13 | System.out.println("● Length of String is "+sb.length());
14 | System.out.println("● Substring is "+sb.substring(3,10));
15 | StringBuilder sbb = new StringBuilder("Hel");
16 | sbb.append("lo");
17 | sbb.append("Wor");
18 | sbb.append("ld....!");
19 | System.out.println("● Appended String is "+sbb);
20 | StringBuilder sb2 = new StringBuilder("SaiRam");
21 | sb2.insert(0,'O');
22 | sb2.insert(1,'m');
23 | System.out.println("● Instered String is "+sb2);
24 | StringBuilder sb3 = new StringBuilder("Athxyva");
25 | sb3.setCharAt(3,'a');
26 | sb3.setCharAt(4,'r');
27 | System.out.println("● CharAt String is "+sb3);
28 | StringBuilder sb4 = new StringBuilder("AtharvaxxxxKote");
29 | sb4.delete(7,11);
30 | sb4.insert(7," ");
31 | System.out.println("● Deleted String is "+sb4);
32 | System.out.println("● Reversed String is "+sb3.reverse());
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Trees/Tree Traversals/PostOrderTranversal.java:
--------------------------------------------------------------------------------
1 | public class BinaryTreesYT {
2 |
3 | static class Node {
4 | int data;
5 | Node left;
6 | Node right;
7 |
8 | Node(int data) {
9 | this.data = data;
10 | this.left = null;
11 | this.right = null;
12 | }
13 | }
14 |
15 | static class BinaryTree {
16 | static int idx = -1;
17 | public static Node buildTree(int nodes[]) {
18 | idx++;
19 | if(nodes[idx] == -1) {
20 | return null;
21 | }
22 | Node newNode = new Node(nodes[idx]);
23 | newNode.left = buildTree(nodes);
24 | newNode.right = buildTree(nodes);
25 | return newNode;
26 | }
27 | }
28 |
29 | public static void postorder(Node root) {
30 |
31 | if(root == null) {
32 |
33 | System.out.print(-1+" ");
34 |
35 | return;
36 |
37 | }
38 |
39 | postorder(root.left);
40 |
41 | postorder(root.right);
42 |
43 | System.out.print(root.data+" ");
44 |
45 | }
46 |
47 | public static void main(String args[]) {
48 | int nodes[] = {1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1};
49 | BinaryTree tree = new BinaryTree()
50 |
51 | Node root = tree.buildTree(nodes);
52 | tree.postorder();
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/01-Java/OOPs/Assessment/RPSGameusingOOPs.java:
--------------------------------------------------------------------------------
1 | package com.company;
2 |
3 | import java.util.Random;
4 | import java.util.Scanner;
5 |
6 | public class cwh_41_ex2sol {
7 | public static void main(String[] args) {
8 | // 0 for Rock
9 | // 1 for Paper
10 | // 2 for Scissor
11 |
12 | Scanner sc = new Scanner(System.in);
13 | System.out.print("Enter 0 for Rock, 1 for Paper, 2 for Scissor ");
14 | int userInput = sc.nextInt();
15 |
16 | Random random = new Random();
17 | int computerInput = random.nextInt(3);
18 |
19 | if (userInput == computerInput) {
20 | System.out.println("Draw");
21 | }
22 | else if (userInput == 0 && computerInput == 2 || userInput == 1 && computerInput == 0
23 | || userInput == 2 && computerInput == 1) {
24 | System.out.println("You Win!");
25 | } else {
26 | System.out.println("Computer Win!");
27 | }
28 | // System.out.println("Computer choice: " + computerInput);
29 | if(computerInput==0){
30 | System.out.println("Computer choice: Rock");
31 | }
32 | else if(computerInput==1){
33 | System.out.println("Computer choice: Paper");
34 | }
35 | else if(computerInput==2){
36 | System.out.println("Computer choice: Scissors");
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/01-Java/Java Basics/Iterative Statements/Patterns/Readme.md:
--------------------------------------------------------------------------------
1 | # Outputs 🔎
2 |
3 | ## Pattern 1
4 |
5 | ```sudo
6 |
7 | *****
8 | * *
9 | * *
10 | *****
11 |
12 | ```
13 | ## Pattern 2
14 | ```sudo
15 | *
16 | **
17 | ***
18 | ****
19 | ```
20 | ## Pattern 3
21 |
22 | ```sudo
23 | 12345
24 | 1234
25 | 123
26 | 12
27 | 1
28 | ```
29 | ## Pattern 4
30 |
31 | ```sudo
32 |
33 | 1
34 | 2 3
35 | 4 5 6
36 | 7 8 9 10
37 | 11 12 13 14 15
38 | ```
39 | ## Pattern 5
40 |
41 | ```sudo
42 |
43 | 1
44 | 01
45 | 101
46 | 0101
47 | 10101
48 | ```
49 | ## Pattern 6
50 |
51 | ```sudo
52 |
53 | * *
54 | ** **
55 | *** ***
56 | ********
57 | ********
58 | *** ***
59 | ** **
60 | * *
61 | ```
62 | ## Pattern 7
63 |
64 | ```sudo
65 |
66 | *****
67 | *****
68 | *****
69 | *****
70 | *****
71 | ```
72 | ## Pattern 8
73 |
74 | ```sudo
75 |
76 | *****
77 | * *
78 | * *
79 | * *
80 | *****
81 | ```
82 | ## Pattern 9
83 |
84 | ```sudo
85 |
86 | *
87 | ***
88 | *****
89 | *******
90 | *******
91 | *****
92 | ***
93 | *
94 | ```
95 | ## Pattern 10
96 |
97 | ```sudo
98 |
99 | 1
100 | 2 2
101 | 3 3 3
102 | 4 4 4 4
103 | 5 5 5 5 5
104 | ```
105 | ## Pattern 10
106 |
107 | ```sudo
108 |
109 | 1
110 | 212
111 | 32123
112 | 4321234
113 | 543212345
114 | ```
115 |
--------------------------------------------------------------------------------
/01-Java/Problem Solving Patterns/Two Pointer Approach/Arrays/DeleteDuplicatesInSortedArray-ll.java:
--------------------------------------------------------------------------------
1 | public class Main {
2 |
3 | class Solution {
4 | public int removeDuplicates(int[] nums) {
5 | int n = nums.length;
6 | if (n < 2) return n;
7 |
8 | int L = 0, R = 1;
9 |
10 | while (R < n) {
11 | if (nums[R] != nums[L]) {
12 | L += 1; // Move to the next position
13 | nums[L] = nums[R]; // Update the array with the unique element
14 | }
15 | R++;
16 | }
17 |
18 | return L + 1;
19 | }
20 | }
21 | public static void main(String[] args) {
22 | // Example sorted array with duplicates
23 | int[] nums = {1, 1, 2, 2, 3, 4, 4, 5, 5};
24 |
25 | // Create an instance of the Solution class
26 | Solution solution = new Solution();
27 |
28 | // Call the removeDuplicates method and get the new length
29 | int newLength = solution.removeDuplicates(nums);
30 |
31 | // Print the results
32 | System.out.println("New length after removing duplicates: " + newLength);
33 | System.out.print("Array after removing duplicates: ");
34 |
35 | // Only print the first newLength elements of the array
36 | for (int i = 0; i < newLength; i++) {
37 | System.out.print(nums[i] + " ");
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Heap ( PriorityQueue )/Priority Queue/HeapSortWithPriorityQueue.java:
--------------------------------------------------------------------------------
1 | import java.util.PriorityQueue;
2 |
3 | public class HeapSort {
4 | // Method to perform heap sort using PriorityQueue
5 | public static void heapSort(int[] arr) {
6 | // Create a PriorityQueue (Min-Heap)
7 | PriorityQueue minHeap = new PriorityQueue<>();
8 |
9 | // Insert all elements into the PriorityQueue
10 | for (int num : arr) {
11 | minHeap.offer(num);
12 | }
13 |
14 | // Extract elements from the PriorityQueue and place them back into the array
15 | int index = 0;
16 | while (!minHeap.isEmpty()) {
17 | arr[index++] = minHeap.poll(); // poll() retrieves and removes the head of the queue
18 | }
19 | }
20 |
21 | public static void main(String[] args) {
22 | int[] arr = {5, 3, 8, 4, 1, 9, 2};
23 |
24 | System.out.println("Original array:");
25 | printArray(arr);
26 |
27 | // Perform Heap Sort
28 | heapSort(arr);
29 |
30 | System.out.println("Sorted array using Heap Sort:");
31 | printArray(arr);
32 | }
33 |
34 | // Utility method to print an array
35 | public static void printArray(int[] arr) {
36 | for (int num : arr) {
37 | System.out.print(num + " ");
38 | }
39 | System.out.println();
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Trees/Tree Traversals/PreOrderTraversal.java:
--------------------------------------------------------------------------------
1 | public class BinaryTreesYT {
2 |
3 | static class Node {
4 | int data;
5 | Node left;
6 | Node right;
7 |
8 | Node(int data) {
9 | this.data = data;
10 | this.left = null;
11 | this.right = null;
12 | }
13 | }
14 |
15 | static class BinaryTree {
16 | static int idx = -1;
17 | public static Node buildTree(int nodes[]) {
18 | idx++;
19 | if(nodes[idx] == -1) {
20 | return null;
21 | }
22 | Node newNode = new Node(nodes[idx]);
23 | newNode.left = buildTree(nodes);
24 | newNode.right = buildTree(nodes);
25 | return newNode;
26 | }
27 | }
28 |
29 | public static void preorder(Node root) {
30 |
31 | if(root == null) {
32 |
33 | System.out.print(-1+" ");
34 |
35 | return;
36 |
37 | }
38 |
39 | System.out.print(root.data+" ");
40 |
41 | preorder(root.left);
42 |
43 | preorder(root.right);
44 |
45 | }
46 |
47 | public static void main(String args[]) {
48 | int nodes[] = {1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1};
49 | BinaryTree tree = new BinaryTree()
50 |
51 | Node root = tree.buildTree(nodes);
52 | tree.preorder();// output : 1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Trees/LeetCode Questions/105. Construct Binary Tree from Preorder and Inorder Traversal.md:
--------------------------------------------------------------------------------
1 |
2 | ## Solution :
3 |
4 | ```java
5 | /**
6 | * Definition for a binary tree node.
7 | */
8 | public class TreeNode {
9 | int val;
10 | TreeNode left;
11 | TreeNode right;
12 |
13 | TreeNode() {}
14 |
15 | TreeNode(int val) {
16 | this.val = val;
17 | }
18 |
19 | TreeNode(int val, TreeNode left, TreeNode right) {
20 | this.val = val;
21 | this.left = left;
22 | this.right = right;
23 | }
24 | }
25 |
26 | class Solution {
27 | public TreeNode buildTree(int[] preorder, int[] inorder) {
28 | if (preorder.length == 0) {
29 | return null;
30 | }
31 |
32 | int root_val = preorder[0];
33 | int indx = 0;
34 |
35 | for (indx = 0; indx < inorder.length; indx++) {
36 | if (inorder[indx] == root_val) {
37 | break;
38 | }
39 | }
40 |
41 | TreeNode root = new TreeNode(root_val);
42 |
43 | root.left = buildTree(Arrays.copyOfRange(preorder, 1, indx + 1),
44 | Arrays.copyOfRange(inorder, 0, indx));
45 |
46 | root.right = buildTree(Arrays.copyOfRange(preorder, indx + 1, preorder.length),
47 | Arrays.copyOfRange(inorder, indx + 1, inorder.length));
48 |
49 | return root;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Readme.md:
--------------------------------------------------------------------------------
1 | > [!TIP]
2 | > 1. **Iteration Over a Data Structures :**
3 | > - To Iterate over a Data Structures (Array,LinkedList,Queue, Stack) or a Collection ( ArrayList , HashSet , HastMap ,etc)
4 | > - We Generally use for loop with a `Iterative Variable [i]`,
5 |
6 | - **For Example**
7 |
8 | ```java
9 | // For Loop Iterating over ArrayList :
10 | List myList = new ArrayList();
11 |
12 | // Adding elements to the list
13 | // Custom inputs
14 | myList.add("A");
15 | myList.add("B");
16 | myList.add("C");
17 | myList.add("D");
18 |
19 | // For loop for iterating over the List
20 |
21 | for (int i = 0; i < myList.size(); i++) {
22 |
23 | // Print all elements of List
24 | System.out.println(myList.get(i));
25 |
26 | }
27 | ```
28 | >[!IMPORTANT]
29 | > - But This can Done in a More Simple way By Using a
30 | ```For Each Loop```
31 | - **For Example :**
32 | ```java
33 | // Creating an array list
34 | ArrayList languages = new ArrayList<>();
35 |
36 | languages.add("Java");
37 | languages.add("JavaScript");
38 | languages.add("Python");
39 |
40 | System.out.println("ArrayList:
41 | " + languages);
42 |
43 | // Using forEach loop
44 |
45 | for(String language : languages) {
46 | System.out.print(language);
47 | System.out.print(", ");
48 | }
49 | ```
50 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Trees/Important Tree Algorithms/DiameterCalculation.java:
--------------------------------------------------------------------------------
1 | class TreeNode {
2 | int val;
3 | TreeNode left;
4 | TreeNode right;
5 | TreeNode(int x) { val = x; }
6 | }
7 |
8 | class Solution {
9 | // Method to calculate the height of a tree
10 | public static int height(TreeNode root) {
11 | if (root == null) {
12 | return 0;
13 | }
14 |
15 | int Lheight = height(root.left);
16 | int Rheight = height(root.right);
17 |
18 | return Math.max(Lheight, Rheight) + 1;
19 | }
20 |
21 | // Method to calculate the diameter of a tree
22 | public int diameter(TreeNode root) {
23 | if (root == null) {
24 | return 0;
25 | }
26 |
27 | int Lheight = height(root.left);
28 | int Rheight = height(root.right);
29 |
30 | // Diameter passing through the root
31 | int rootDiameter = Lheight + Rheight;
32 |
33 | // Diameter not passing through the root
34 | int leftDiameter = diameter(root.left);
35 | int rightDiameter = diameter(root.right);
36 |
37 | // The diameter is the maximum of the above three values
38 | return Math.max(rootDiameter, Math.max(leftDiameter, rightDiameter));
39 | }
40 |
41 | // Main method to be called to get the diameter of a binary tree
42 | public int diameterOfBinaryTree(TreeNode root) {
43 | return diameter(root);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/02-C and C++/C++/Data Structures/Arrays/Matrix ( 2D Array )/Matrix_Addition.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int main() {
5 | int rows, cols;
6 |
7 | // Input the dimensions of the matrices
8 | cout << "Enter the number of rows and columns of the matrices: ";
9 | cin >> rows >> cols;
10 |
11 | // Declare matrices
12 | int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];
13 |
14 | // Input elements for the first matrix
15 | cout << "Enter elements of the first matrix:" << endl;
16 | for (int i = 0; i < rows; i++) {
17 | for (int j = 0; j < cols; j++) {
18 | cin >> matrix1[i][j];
19 | }
20 | }
21 |
22 | // Input elements for the second matrix
23 | cout << "Enter elements of the second matrix:" << endl;
24 | for (int i = 0; i < rows; i++) {
25 | for (int j = 0; j < cols; j++) {
26 | cin >> matrix2[i][j];
27 | }
28 | }
29 |
30 | // Sum the matrices
31 | for (int i = 0; i < rows; i++) {
32 | for (int j = 0; j < cols; j++) {
33 | sum[i][j] = matrix1[i][j] + matrix2[i][j];
34 | }
35 | }
36 |
37 | // Display the sum matrix
38 | cout << "Sum of the matrices:" << endl;
39 | for (int i = 0; i < rows; i++) {
40 | for (int j = 0; j < cols; j++) {
41 | cout << sum[i][j] << " ";
42 | }
43 | cout << endl;
44 | }
45 |
46 | return 0;
47 | }
48 |
--------------------------------------------------------------------------------
/01-Java/Sorting Algorithms/MergeSort.java:
--------------------------------------------------------------------------------
1 | public class MergeSort {
2 | public static void conquer(int arr[], int si, int mid, int ei) {
3 | int merged[] = new int[ei-si+1];
4 | int idx1 = si;
5 | int idx2 = mid+1;
6 | int x = 0;
7 | while(idx1 <= mid && idx2 <= ei) {
8 | if(arr[idx1] <= arr[idx2]) {
9 | merged[x++] = arr[idx1++];
10 | } else {
11 | merged[x++] = arr[idx2++];
12 | }
13 | }
14 |
15 | while(idx1 <= mid) {
16 | merged[x++] = arr[idx1++];
17 | }
18 |
19 | while(idx2 <= ei) {
20 | merged[x++] = arr[idx2++];
21 | }
22 |
23 | for(int i=0, j=si; i= ei) {
30 | return;
31 | }
32 |
33 | int mid = si + (ei-si)/2;
34 | divide(arr, si, mid);
35 | divide(arr, mid+1, ei);
36 | conquer(arr, si, mid, ei);
37 | }
38 |
39 | public static void main(String args[]) {
40 | int arr[] = {6, 3, 9, 5, 2, 8};
41 | int n = arr.length;
42 | divide(arr, 0, n-1);
43 | //Print Array
44 | for(int i=0; i
2 | #include
3 | struct Node {
4 | int data ;
5 | struct Node *next;
6 | struct Node *prev;
7 | };
8 |
9 | struct Node *head=NULL;
10 | struct Node *temp;
11 |
12 | void Display(){
13 | temp=head;
14 | printf("[ ");
15 | while(temp!=NULL){
16 | printf("%d ",temp->data);
17 | temp=temp->next;
18 | }
19 | printf(" ]\n");
20 | }
21 | void CountNode(){
22 | temp=head;
23 | int count =0;
24 | while(temp!=NULL){
25 | count++;
26 | temp=temp->next;
27 | }
28 | printf("Number of Node in Linked List : %d",count);
29 | }
30 |
31 | void main()
32 | {
33 | int ch,Num,w;
34 | struct Node *Newnode;
35 | printf("First Create A Linked :\nEnter Number of Nodes You Want : ");
36 | scanf("%d",&Num);
37 | for (int i =1;i<=Num;i++){
38 | Newnode=(struct Node *)malloc(sizeof(struct Node));
39 | printf("Enter Data as a Number : ");
40 | scanf("%d",&Newnode->data);
41 | Newnode->next=NULL;
42 |
43 | if(head==NULL){
44 | head=Newnode;
45 | temp=Newnode;
46 | Newnode->prev=NULL;
47 | }
48 | else{
49 | temp->next=Newnode;
50 | Newnode->prev=temp;
51 | temp=Newnode;
52 | }
53 | }
54 | printf("Created Linked List Is : ");
55 | Display();
56 | CountNode();
57 | }
58 |
--------------------------------------------------------------------------------
/01-Java/Recursion/Recursive Sorting/MergeSort.java:
--------------------------------------------------------------------------------
1 | public class MergeSort {
2 | public static void conquer(int arr[], int si, int mid, int ei) {
3 | int merged[] = new int[ei-si+1];
4 | int idx1 = si;
5 | int idx2 = mid+1;
6 | int x = 0;
7 | while(idx1 <= mid && idx2 <= ei) {
8 | if(arr[idx1] <= arr[idx2]) {
9 | merged[x++] = arr[idx1++];
10 | } else {
11 | merged[x++] = arr[idx2++];
12 | }
13 | }
14 |
15 | while(idx1 <= mid) {
16 | merged[x++] = arr[idx1++];
17 | }
18 |
19 | while(idx2 <= ei) {
20 | merged[x++] = arr[idx2++];
21 | }
22 |
23 | for(int i=0, j=si; i= ei) {
30 | return;
31 | }
32 |
33 | int mid = si + (ei-si)/2;
34 | divide(arr, si, mid);
35 | divide(arr, mid+1, ei);
36 | conquer(arr, si, mid, ei);
37 | }
38 |
39 | public static void main(String args[]) {
40 | int arr[] = {6, 3, 9, 5, 2, 8};
41 | int n = arr.length;
42 | divide(arr, 0, n-1);
43 | //Print Array
44 | for(int i=0; i= ei) {
30 | return;
31 | }
32 |
33 | int mid = si + (ei-si)/2;
34 | divide(arr, si, mid);
35 | divide(arr, mid+1, ei);
36 | conquer(arr, si, mid, ei);
37 | }
38 |
39 | public static void main(String args[]) {
40 | int arr[] = {6, 3, 9, 5, 2, 8};
41 | int n = arr.length;
42 | divide(arr, 0, n-1);
43 | //Print Array
44 | for(int i=0; i= 0 && arr[prev] > curr) {
12 | arr[prev+1] = arr[prev];
13 | prev--;
14 | }
15 | arr[prev+1] = curr;
16 | }
17 | }
18 |
19 | public static void insertionSortDescending(int arr[]) {
20 | for(int i=1; i= 0 && arr[prev] < curr) {
25 | arr[prev+1] = arr[prev];
26 | prev--;
27 | }
28 | arr[prev+1] = curr;
29 | }
30 | }
31 |
32 | public static void printArr(int arr[]) {
33 | for(int i=0; i
2 | #include
3 | #define max 20
4 | char stack[max];
5 | int top = -1;
6 |
7 | void push(char x) {
8 | if (top == max) {
9 | printf("STACK OVERFLOW !!!!");
10 | } else {
11 | top++;
12 | stack[top] = x;
13 | }
14 | }
15 |
16 | char pop() {
17 | if (top < 0) {
18 | printf("STACK UNDERFLOW !!!!");
19 | return'\0' ;
20 | } else {
21 | char x=stack[top];
22 | top--;
23 | return x;
24 | }
25 | }
26 |
27 | int isPair(char c1 ,char c2){
28 |
29 | if (c1=='{' && c2=='}')
30 | return 1;
31 | else if (c1=='[' && c2==']')
32 | return 1;
33 | else if (c1=='(' && c2==')')
34 | return 1;
35 | else {
36 | return 0;
37 | }
38 | }
39 |
40 | int isBalanced(char *text){
41 | int i ;
42 | int length =strlen(text);
43 | for (i=0;i= ei) {
11 | return;
12 | }
13 |
14 | //pivot variable stores correct idx for pivot element
15 | int pIdx = partition(arr, si, ei);
16 | quickSort(arr, si, pIdx-1);
17 | quickSort(arr, pIdx+1, ei);
18 | }
19 |
20 | public static int partition(int arr[], int si, int ei) {
21 | int pivot = arr[ei];
22 | int i = si-1; // makes space for elements smaller than pivot
23 |
24 | for(int j=si; j= ei) {
11 | return;
12 | }
13 |
14 | //pivot variable stores correct idx for pivot element
15 | int pIdx = partition(arr, si, ei);
16 | quickSort(arr, si, pIdx-1);
17 | quickSort(arr, pIdx+1, ei);
18 | }
19 |
20 | public static int partition(int arr[], int si, int ei) {
21 | int pivot = arr[ei];
22 | int i = si-1; // makes space for elements smaller than pivot
23 |
24 | for(int j=si; j= ei) {
11 | return;
12 | }
13 |
14 | //pivot variable stores correct idx for pivot element
15 | int pIdx = partition(arr, si, ei);
16 | quickSort(arr, si, pIdx-1);
17 | quickSort(arr, pIdx+1, ei);
18 | }
19 |
20 | public static int partition(int arr[], int si, int ei) {
21 | int pivot = arr[ei];
22 | int i = si-1; // makes space for elements smaller than pivot
23 |
24 | for(int j=si; j arr[j]) {
11 | minPos = j;
12 | }
13 | }
14 |
15 | //swap
16 | int temp = arr[turn];
17 | arr[turn] = arr[minPos];
18 | arr[minPos] = temp;
19 | }
20 | }
21 |
22 | public static void selectionSortDescending(int arr[]) {
23 | for(int turn=0; turnnumber){
37 | System.out.println("Too high...");
38 | }
39 | return false;
40 | }
41 |
42 | }
43 | public class cwh_50_ex3sol {
44 | public static void main(String[] args) {
45 | Game g = new Game();
46 | boolean b= false;
47 | while(!b){
48 | g.takeUserInput();
49 | b = g.isCorrectNumber();
50 | }
51 |
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Hash Map/Top Leetcode Questions/GroupAnagrams.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | class Solution {
4 | public List> groupAnagrams(String[] strs) {
5 | // Map to store sorted string as key and list of anagrams as value
6 | Map> map = new HashMap<>();
7 |
8 | for (String str : strs) {
9 | // Convert the string to a character array, sort it, and then convert back to a string
10 | char[] charArray = str.toCharArray();
11 | Arrays.sort(charArray);
12 | String sortedStr = new String(charArray);
13 |
14 | // If the sorted string is not in the map, add it with an empty list
15 | if (!map.containsKey(sortedStr)) {
16 | map.put(sortedStr, new ArrayList<>());
17 | }
18 |
19 | // Add the original string to the list corresponding to the sorted string
20 | map.get(sortedStr).add(str);
21 | }
22 |
23 | // Collect all the lists from the map and return them as the result
24 | return new ArrayList<>(map.values());
25 | }
26 |
27 | public static void main(String[] args) {
28 | Solution sol = new Solution();
29 | String[] input = {"eat", "tea", "tan", "ate", "nat", "bat"};
30 | List> result = sol.groupAnagrams(input);
31 | System.out.println(result);
32 | // output : [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/01-Java/Data Structure/Stack/Applications/STACKasENCODER.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 | import java.util.Stack;
3 |
4 | public class DecimalToBinaryUsingStack {
5 |
6 | // Method to convert decimal to binary using a stack
7 | public static String convertToBinary(int decimal) {
8 | if (decimal == 0) {
9 | return "0";
10 | }
11 |
12 | Stack stack = new Stack<>();
13 |
14 | // Loop until the decimal number is greater than 0
15 | while (decimal > 0) {
16 | // Get the remainder when divided by 2 (either 0 or 1)
17 | int remainder = decimal % 2;
18 | // Push the remainder onto the stack
19 | stack.push(remainder);
20 | // Divide the number by 2
21 | decimal = decimal / 2;
22 | }
23 |
24 | // Construct the binary string from the stack
25 | StringBuilder binary = new StringBuilder();
26 | while (!stack.isEmpty()) {
27 | binary.append(stack.pop());
28 | }
29 |
30 | return binary.toString();
31 | }
32 |
33 | public static void main(String[] args) {
34 | Scanner scanner = new Scanner(System.in);
35 |
36 | System.out.print("Enter a decimal number: ");
37 | int decimal = scanner.nextInt();
38 |
39 | String binary = convertToBinary(decimal);
40 | System.out.println("Binary representation: " + binary);
41 |
42 | scanner.close();
43 | }
44 | }
45 |
--------------------------------------------------------------------------------