├── .DS_Store
├── .classpath
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── Notes
├── 2d Arrays.pdf
├── Adapters.pdf
├── Array Questions.pdf
├── Arrays.pdf
├── Basics.pdf
├── Bit Manipulation.pdf
├── Conditionals.pdf
├── Functions.pdf
├── Geometry.pdf
├── Hashmap questions.pdf
├── Hashmap.pdf
├── Interview Questions.pdf
├── Kadane_s Algo.pdf
├── LL Questions 1.pdf
├── LL Questions 2.pdf
├── LL Questions 3.pdf
├── Linked List.pdf
├── Loops.pdf
├── Maths.pdf
├── OOPS 1.pdf
├── OOPS 2.pdf
├── OOPS 3.pdf
├── OOPS 4.pdf
├── OOPS 5.pdf
├── Queue.pdf
├── Recursion 1.pdf
├── Recursion 2.pdf
├── Recursion 3.pdf
├── Recursion 4.pdf
├── Recursion 5.pdf
├── Recursion 6.pdf
├── Searching.pdf
├── Sorting 2.pdf
├── Sorting Questions.pdf
├── Sorting.pdf
├── Stack Questions.pdf
├── Stacks.pdf
├── Strings.pdf
├── Time And Space Complexity.pdf
└── Type Casting.pdf
├── bin
├── Adapters
│ ├── QueueUsing2StackEnqueEfficient.class
│ ├── QueueUsing2StacksDequeEfficient.class
│ └── Utility.class
├── Arrays
│ ├── ArrayListIntro.class
│ ├── Input.class
│ ├── Intro.class
│ ├── IntroTo2DArrays.class
│ ├── KadaneAlgo.class
│ ├── Questions.class
│ ├── Searching.class
│ ├── Sorting.class
│ ├── Sorting2.class
│ └── SortingQuestions.class
├── Bits
│ ├── BitwiseOperators.class
│ └── PlayingWithDataTypes.class
├── Doubts
│ └── Doubt2.class
├── Generics
│ ├── GenericArrayList.class
│ ├── GenricHashMap.class
│ ├── MyArrayList.class
│ └── Utility.class
├── HashMaps
│ ├── Intro.class
│ └── Questions.class
├── LinkedList
│ ├── LinkedList$Node.class
│ ├── LinkedList.class
│ └── Utility.class
├── Mathematics
│ ├── Basics.class
│ └── Geometry.class
├── OOPS
│ ├── A.class
│ ├── B.class
│ ├── Basics$Example.class
│ ├── Basics.class
│ ├── Child.class
│ ├── CollegeStudent.class
│ ├── ComparableAndComparator.class
│ ├── ExceptionHandling.class
│ ├── Inheritence.class
│ ├── OuterClass$NestedClass.class
│ ├── OuterClass.class
│ ├── Parent.class
│ ├── Polymorphism.class
│ ├── StaticExample.class
│ └── Student.class
├── OOPSImplementation
│ ├── AcessModifiers.class
│ ├── Bike.class
│ ├── Car.class
│ ├── Child.class
│ ├── Father.class
│ ├── Mother.class
│ ├── Shape.class
│ ├── Square.class
│ ├── Triangle.class
│ ├── Utility.class
│ └── Vehicle.class
├── Queue
│ ├── Basics.class
│ └── Questions.class
├── Recursion
│ ├── Basics.class
│ ├── Questions2.class
│ ├── Questions3.class
│ ├── Questions4.class
│ ├── Questions5.class
│ └── Recursion6.class
├── Stack
│ ├── Basics.class
│ └── Questions.class
├── Strings
│ ├── Intro.class
│ └── Questions.class
└── basics
│ ├── Conditionals.class
│ ├── Functions.class
│ ├── Loops.class
│ ├── Patterns.class
│ ├── TypeCasting.class
│ └── doubts.class
└── src
├── Adapters
├── QueueUsing2StackEnqueEfficient.java
├── QueueUsing2StacksDequeEfficient.java
└── Utility.java
├── Arrays
├── ArrayListIntro.java
├── Input.java
├── Intro.java
├── IntroTo2DArrays.java
├── KadaneAlgo.java
├── Questions.java
├── Searching.java
├── Sorting.java
├── Sorting2.java
└── SortingQuestions.java
├── Bits
├── BitwiseOperators.java
└── PlayingWithDataTypes.java
├── Doubts
└── Doubt2.java
├── Generics
├── GenericArrayList.java
├── GenricHashMap.java
├── MyArrayList.java
└── Utility.java
├── HashMaps
├── Intro.java
└── Questions.java
├── LinkedList
├── LinkedList.java
└── Utility.java
├── Mathematics
├── Basics.java
└── Geometry.java
├── OOPS
├── Basics.java
├── ComparableAndComparator.java
├── ExceptionHandling.java
├── Inheritence.java
├── Polymorphism.java
└── StaticExample.java
├── OOPSImplementation
├── AcessModifiers.java
├── Bike.java
├── Car.java
├── Child.java
├── Father.java
├── Mother.java
├── Shape.java
├── Square.java
├── Triangle.java
├── Utility.java
└── Vehicle.java
├── Queue
├── Basics.java
└── Questions.java
├── Recursion
├── Basics.java
├── Questions2.java
├── Questions3.java
├── Questions4.java
├── Questions5.java
└── Recursion6.java
├── Stack
├── Basics.java
└── Questions.java
├── Strings
├── Intro.java
└── Questions.java
└── basics
├── Conditionals.java
├── Functions.java
├── Loops.java
├── Patterns.java
├── TypeCasting.java
└── doubts.java
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/.DS_Store
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Newton-School-June-2022
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=16
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=16
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13 | org.eclipse.jdt.core.compiler.release=enabled
14 | org.eclipse.jdt.core.compiler.source=16
15 |
--------------------------------------------------------------------------------
/Notes/2d Arrays.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/2d Arrays.pdf
--------------------------------------------------------------------------------
/Notes/Adapters.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Adapters.pdf
--------------------------------------------------------------------------------
/Notes/Array Questions.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Array Questions.pdf
--------------------------------------------------------------------------------
/Notes/Arrays.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Arrays.pdf
--------------------------------------------------------------------------------
/Notes/Basics.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Basics.pdf
--------------------------------------------------------------------------------
/Notes/Bit Manipulation.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Bit Manipulation.pdf
--------------------------------------------------------------------------------
/Notes/Conditionals.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Conditionals.pdf
--------------------------------------------------------------------------------
/Notes/Functions.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Functions.pdf
--------------------------------------------------------------------------------
/Notes/Geometry.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Geometry.pdf
--------------------------------------------------------------------------------
/Notes/Hashmap questions.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Hashmap questions.pdf
--------------------------------------------------------------------------------
/Notes/Hashmap.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Hashmap.pdf
--------------------------------------------------------------------------------
/Notes/Interview Questions.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Interview Questions.pdf
--------------------------------------------------------------------------------
/Notes/Kadane_s Algo.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Kadane_s Algo.pdf
--------------------------------------------------------------------------------
/Notes/LL Questions 1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/LL Questions 1.pdf
--------------------------------------------------------------------------------
/Notes/LL Questions 2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/LL Questions 2.pdf
--------------------------------------------------------------------------------
/Notes/LL Questions 3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/LL Questions 3.pdf
--------------------------------------------------------------------------------
/Notes/Linked List.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Linked List.pdf
--------------------------------------------------------------------------------
/Notes/Loops.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Loops.pdf
--------------------------------------------------------------------------------
/Notes/Maths.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Maths.pdf
--------------------------------------------------------------------------------
/Notes/OOPS 1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/OOPS 1.pdf
--------------------------------------------------------------------------------
/Notes/OOPS 2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/OOPS 2.pdf
--------------------------------------------------------------------------------
/Notes/OOPS 3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/OOPS 3.pdf
--------------------------------------------------------------------------------
/Notes/OOPS 4.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/OOPS 4.pdf
--------------------------------------------------------------------------------
/Notes/OOPS 5.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/OOPS 5.pdf
--------------------------------------------------------------------------------
/Notes/Queue.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Queue.pdf
--------------------------------------------------------------------------------
/Notes/Recursion 1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Recursion 1.pdf
--------------------------------------------------------------------------------
/Notes/Recursion 2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Recursion 2.pdf
--------------------------------------------------------------------------------
/Notes/Recursion 3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Recursion 3.pdf
--------------------------------------------------------------------------------
/Notes/Recursion 4.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Recursion 4.pdf
--------------------------------------------------------------------------------
/Notes/Recursion 5.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Recursion 5.pdf
--------------------------------------------------------------------------------
/Notes/Recursion 6.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Recursion 6.pdf
--------------------------------------------------------------------------------
/Notes/Searching.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Searching.pdf
--------------------------------------------------------------------------------
/Notes/Sorting 2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Sorting 2.pdf
--------------------------------------------------------------------------------
/Notes/Sorting Questions.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Sorting Questions.pdf
--------------------------------------------------------------------------------
/Notes/Sorting.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Sorting.pdf
--------------------------------------------------------------------------------
/Notes/Stack Questions.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Stack Questions.pdf
--------------------------------------------------------------------------------
/Notes/Stacks.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Stacks.pdf
--------------------------------------------------------------------------------
/Notes/Strings.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Strings.pdf
--------------------------------------------------------------------------------
/Notes/Time And Space Complexity.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Time And Space Complexity.pdf
--------------------------------------------------------------------------------
/Notes/Type Casting.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/Notes/Type Casting.pdf
--------------------------------------------------------------------------------
/bin/Adapters/QueueUsing2StackEnqueEfficient.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Adapters/QueueUsing2StackEnqueEfficient.class
--------------------------------------------------------------------------------
/bin/Adapters/QueueUsing2StacksDequeEfficient.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Adapters/QueueUsing2StacksDequeEfficient.class
--------------------------------------------------------------------------------
/bin/Adapters/Utility.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Adapters/Utility.class
--------------------------------------------------------------------------------
/bin/Arrays/ArrayListIntro.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Arrays/ArrayListIntro.class
--------------------------------------------------------------------------------
/bin/Arrays/Input.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Arrays/Input.class
--------------------------------------------------------------------------------
/bin/Arrays/Intro.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Arrays/Intro.class
--------------------------------------------------------------------------------
/bin/Arrays/IntroTo2DArrays.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Arrays/IntroTo2DArrays.class
--------------------------------------------------------------------------------
/bin/Arrays/KadaneAlgo.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Arrays/KadaneAlgo.class
--------------------------------------------------------------------------------
/bin/Arrays/Questions.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Arrays/Questions.class
--------------------------------------------------------------------------------
/bin/Arrays/Searching.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Arrays/Searching.class
--------------------------------------------------------------------------------
/bin/Arrays/Sorting.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Arrays/Sorting.class
--------------------------------------------------------------------------------
/bin/Arrays/Sorting2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Arrays/Sorting2.class
--------------------------------------------------------------------------------
/bin/Arrays/SortingQuestions.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Arrays/SortingQuestions.class
--------------------------------------------------------------------------------
/bin/Bits/BitwiseOperators.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Bits/BitwiseOperators.class
--------------------------------------------------------------------------------
/bin/Bits/PlayingWithDataTypes.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Bits/PlayingWithDataTypes.class
--------------------------------------------------------------------------------
/bin/Doubts/Doubt2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Doubts/Doubt2.class
--------------------------------------------------------------------------------
/bin/Generics/GenericArrayList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Generics/GenericArrayList.class
--------------------------------------------------------------------------------
/bin/Generics/GenricHashMap.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Generics/GenricHashMap.class
--------------------------------------------------------------------------------
/bin/Generics/MyArrayList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Generics/MyArrayList.class
--------------------------------------------------------------------------------
/bin/Generics/Utility.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Generics/Utility.class
--------------------------------------------------------------------------------
/bin/HashMaps/Intro.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/HashMaps/Intro.class
--------------------------------------------------------------------------------
/bin/HashMaps/Questions.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/HashMaps/Questions.class
--------------------------------------------------------------------------------
/bin/LinkedList/LinkedList$Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/LinkedList/LinkedList$Node.class
--------------------------------------------------------------------------------
/bin/LinkedList/LinkedList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/LinkedList/LinkedList.class
--------------------------------------------------------------------------------
/bin/LinkedList/Utility.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/LinkedList/Utility.class
--------------------------------------------------------------------------------
/bin/Mathematics/Basics.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Mathematics/Basics.class
--------------------------------------------------------------------------------
/bin/Mathematics/Geometry.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Mathematics/Geometry.class
--------------------------------------------------------------------------------
/bin/OOPS/A.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/A.class
--------------------------------------------------------------------------------
/bin/OOPS/B.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/B.class
--------------------------------------------------------------------------------
/bin/OOPS/Basics$Example.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/Basics$Example.class
--------------------------------------------------------------------------------
/bin/OOPS/Basics.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/Basics.class
--------------------------------------------------------------------------------
/bin/OOPS/Child.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/Child.class
--------------------------------------------------------------------------------
/bin/OOPS/CollegeStudent.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/CollegeStudent.class
--------------------------------------------------------------------------------
/bin/OOPS/ComparableAndComparator.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/ComparableAndComparator.class
--------------------------------------------------------------------------------
/bin/OOPS/ExceptionHandling.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/ExceptionHandling.class
--------------------------------------------------------------------------------
/bin/OOPS/Inheritence.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/Inheritence.class
--------------------------------------------------------------------------------
/bin/OOPS/OuterClass$NestedClass.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/OuterClass$NestedClass.class
--------------------------------------------------------------------------------
/bin/OOPS/OuterClass.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/OuterClass.class
--------------------------------------------------------------------------------
/bin/OOPS/Parent.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/Parent.class
--------------------------------------------------------------------------------
/bin/OOPS/Polymorphism.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/Polymorphism.class
--------------------------------------------------------------------------------
/bin/OOPS/StaticExample.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/StaticExample.class
--------------------------------------------------------------------------------
/bin/OOPS/Student.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPS/Student.class
--------------------------------------------------------------------------------
/bin/OOPSImplementation/AcessModifiers.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPSImplementation/AcessModifiers.class
--------------------------------------------------------------------------------
/bin/OOPSImplementation/Bike.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPSImplementation/Bike.class
--------------------------------------------------------------------------------
/bin/OOPSImplementation/Car.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPSImplementation/Car.class
--------------------------------------------------------------------------------
/bin/OOPSImplementation/Child.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPSImplementation/Child.class
--------------------------------------------------------------------------------
/bin/OOPSImplementation/Father.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPSImplementation/Father.class
--------------------------------------------------------------------------------
/bin/OOPSImplementation/Mother.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPSImplementation/Mother.class
--------------------------------------------------------------------------------
/bin/OOPSImplementation/Shape.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPSImplementation/Shape.class
--------------------------------------------------------------------------------
/bin/OOPSImplementation/Square.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPSImplementation/Square.class
--------------------------------------------------------------------------------
/bin/OOPSImplementation/Triangle.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPSImplementation/Triangle.class
--------------------------------------------------------------------------------
/bin/OOPSImplementation/Utility.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPSImplementation/Utility.class
--------------------------------------------------------------------------------
/bin/OOPSImplementation/Vehicle.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/OOPSImplementation/Vehicle.class
--------------------------------------------------------------------------------
/bin/Queue/Basics.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Queue/Basics.class
--------------------------------------------------------------------------------
/bin/Queue/Questions.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Queue/Questions.class
--------------------------------------------------------------------------------
/bin/Recursion/Basics.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Recursion/Basics.class
--------------------------------------------------------------------------------
/bin/Recursion/Questions2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Recursion/Questions2.class
--------------------------------------------------------------------------------
/bin/Recursion/Questions3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Recursion/Questions3.class
--------------------------------------------------------------------------------
/bin/Recursion/Questions4.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Recursion/Questions4.class
--------------------------------------------------------------------------------
/bin/Recursion/Questions5.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Recursion/Questions5.class
--------------------------------------------------------------------------------
/bin/Recursion/Recursion6.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Recursion/Recursion6.class
--------------------------------------------------------------------------------
/bin/Stack/Basics.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Stack/Basics.class
--------------------------------------------------------------------------------
/bin/Stack/Questions.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Stack/Questions.class
--------------------------------------------------------------------------------
/bin/Strings/Intro.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Strings/Intro.class
--------------------------------------------------------------------------------
/bin/Strings/Questions.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/Strings/Questions.class
--------------------------------------------------------------------------------
/bin/basics/Conditionals.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/basics/Conditionals.class
--------------------------------------------------------------------------------
/bin/basics/Functions.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/basics/Functions.class
--------------------------------------------------------------------------------
/bin/basics/Loops.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/basics/Loops.class
--------------------------------------------------------------------------------
/bin/basics/Patterns.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/basics/Patterns.class
--------------------------------------------------------------------------------
/bin/basics/TypeCasting.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/basics/TypeCasting.class
--------------------------------------------------------------------------------
/bin/basics/doubts.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tanmayarya/NS-June2022-DSA/025cc33b89dbf5ba57de7d2430cb5d41cc88539a/bin/basics/doubts.class
--------------------------------------------------------------------------------
/src/Adapters/QueueUsing2StackEnqueEfficient.java:
--------------------------------------------------------------------------------
1 | package Adapters;
2 |
3 | import java.util.Stack;
4 |
5 | public class QueueUsing2StackEnqueEfficient {
6 |
7 | Stack main;
8 | Stack helper;
9 |
10 | public QueueUsing2StackEnqueEfficient() {
11 | main = new Stack<>();
12 | helper = new Stack<>();
13 | }
14 |
15 | // O(1)
16 | public void add(int val) {
17 | main.push(val);
18 | }
19 |
20 | public int remove() {
21 | if(main.isEmpty()) return -1;
22 | int valueToRemove;
23 |
24 | while(main.size() != 1) {
25 | helper.push(main.pop());
26 | }
27 | valueToRemove = main.pop();
28 |
29 | while(!helper.isEmpty()) {
30 | main.push(helper.pop());
31 | }
32 | return valueToRemove;
33 | }
34 |
35 | public int peek() {
36 | if(main.isEmpty()) return -1;
37 | int valueToReturn;
38 |
39 | while(main.size() != 1) {
40 | helper.push(main.pop());
41 | }
42 | valueToReturn = main.peek();
43 |
44 | while(!helper.isEmpty()) {
45 | main.push(helper.pop());
46 | }
47 | return valueToReturn;
48 | }
49 |
50 | public boolean isEmpty() {
51 | return main.isEmpty();
52 | }
53 |
54 | public int size() {
55 | return main.size();
56 | }
57 |
58 | public void display() {
59 | System.out.println(main);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/Adapters/QueueUsing2StacksDequeEfficient.java:
--------------------------------------------------------------------------------
1 | package Adapters;
2 |
3 | import java.util.Stack;
4 |
5 | public class QueueUsing2StacksDequeEfficient {
6 |
7 | Stack main;
8 | Stack helper;
9 |
10 | public QueueUsing2StacksDequeEfficient() {
11 | main = new Stack<>();
12 | helper = new Stack<>();
13 | }
14 |
15 | // O(n)
16 | public void add(int val) {
17 | while(!main.isEmpty()) {
18 | helper.push(main.pop());
19 | }
20 | main.push(val);
21 | while(!helper.isEmpty()) {
22 | main.push(helper.pop());
23 | }
24 | }
25 |
26 | // O(1)
27 | public int remove() {
28 | return main.pop();
29 | }
30 |
31 | public int peek() {
32 | return main.peek();
33 | }
34 |
35 | public boolean isEmpty() {
36 | return main.isEmpty();
37 | }
38 |
39 | public int size() {
40 | return main.size();
41 | }
42 |
43 | public void display() {
44 | System.out.println(main);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/Adapters/Utility.java:
--------------------------------------------------------------------------------
1 | package Adapters;
2 |
3 | public class Utility {
4 |
5 | public static void main(String[] args) {
6 | // QueueUsing2StackEnqueEfficient q = new QueueUsing2StackEnqueEfficient();
7 | QueueUsing2StacksDequeEfficient q = new QueueUsing2StacksDequeEfficient();
8 |
9 | q.add(1);
10 | q.add(3);
11 | q.add(2);
12 | q.add(4);
13 | q.display();
14 | System.out.println(q.peek());
15 | q.remove();
16 | q.display();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/Arrays/ArrayListIntro.java:
--------------------------------------------------------------------------------
1 | package Arrays;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class ArrayListIntro {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 |
10 |
11 | ArrayList list = new ArrayList();
12 | // add
13 | list.add(1);
14 | list.add(2);
15 | list.add(null);
16 | System.out.println(list);
17 | list.add(1, 10);
18 | System.out.println(list);
19 | //remove
20 | list.remove(0);
21 | System.out.println(list);
22 |
23 | list.set(2, 100);
24 | System.out.println(list);
25 |
26 | // get
27 | int elementAtidx1 = list.get(1);
28 |
29 |
30 | System.out.println(elementAtidx1);
31 |
32 | System.out.println(list.size());
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/Arrays/Input.java:
--------------------------------------------------------------------------------
1 | package Arrays;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Input {
6 |
7 | public static void main(String[] args) {
8 |
9 | Scanner scn = new Scanner(System.in);
10 | int n = scn.nextInt();
11 |
12 | int arr[] = new int[n];
13 |
14 | for(int i = 0; i < arr.length; i++) {
15 | arr[i] = scn.nextInt();
16 | }
17 |
18 |
19 | for(int i = 0; i < arr.length; i++) {
20 | System.out.println(arr[i]);
21 | }
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/Arrays/Intro.java:
--------------------------------------------------------------------------------
1 | package Arrays;
2 |
3 | public class Intro {
4 |
5 | public static void main(String[] args) {
6 |
7 | int marks[];
8 | marks = new int[5];
9 |
10 | marks[0] = 6;
11 | marks[1] = 8;
12 | marks[2] = 1;
13 | marks[3] = 4;
14 | marks[4] = 10;
15 |
16 |
17 | for(int i = 0; i < marks.length; i++) {
18 | System.out.println(marks[i]);
19 | }
20 |
21 |
22 | marks[1] = 30;
23 |
24 | System.out.println("value of ist index" + marks[1]);
25 | // System.out.println(marks.length);
26 |
27 | int arr[] = {6, 8, 1, 4, 10};
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/Arrays/IntroTo2DArrays.java:
--------------------------------------------------------------------------------
1 | package Arrays;
2 |
3 | public class IntroTo2DArrays {
4 |
5 | public static void display(int arr[][]) {
6 |
7 | // this loop will run for all rows
8 | for(int row = 0; row < arr.length; row++) {
9 |
10 | //print 1 row
11 | for(int col = 0; col < arr[row].length; col++) {
12 | System.out.print(arr[row][col] + " ");
13 | }
14 | // print empty line after each row
15 | System.out.println();
16 | }
17 | }
18 |
19 | public static void printZigZag(int arr[][]) {
20 | for(int row = 0; row < arr.length; row++) {
21 |
22 | if(row % 2 == 0) { // even row
23 | for(int col = 0; col < arr[row].length; col++)
24 | System.out.println(arr[row][col]);
25 | } else {
26 | // odd row
27 | for(int col = arr[row].length - 1; col >= 0; col--)
28 | System.out.println(arr[row][col]);
29 |
30 | }
31 | }
32 | }
33 |
34 | public static void printSpiral(int arr[][]) {
35 | int minr = 0, maxr = arr.length - 1, minc = 0, maxc = arr[0].length -1;
36 |
37 | int valuesLeftToPrint = arr.length * arr[0].length;
38 |
39 | while(valuesLeftToPrint > 0) {
40 |
41 | // print top wall
42 | for(int col = minc; col <= maxc; col++) {
43 | System.out.println(arr[minr][col]);
44 | valuesLeftToPrint--;
45 | }
46 | minr++;
47 |
48 | // print right wall
49 | for(int row = minr; row <= maxr; row++) {
50 | System.out.println(arr[row][maxc]);
51 | valuesLeftToPrint--;
52 | }
53 | maxc--;
54 |
55 | // print bottom wall
56 | for(int col = maxc; col >= minc; col--) {
57 | System.out.println(arr[maxr][col]);
58 | valuesLeftToPrint--;
59 | }
60 | maxr--;
61 |
62 | // print left wall
63 | for(int row = maxr; row >= minr; row--) {
64 | System.out.println(arr[row][minc]);
65 | valuesLeftToPrint--;
66 | }
67 | minc++;
68 | }
69 | }
70 |
71 | public static void main(String[] args) {
72 | int arr[][] = {
73 | {1, 2, 3},
74 | {4, 5, 6},
75 | {7, 8, 9},
76 | {10, 11, 12}
77 | };
78 |
79 | // display(arr);
80 | // printZigZag(arr);
81 | printSpiral(arr);
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/src/Arrays/KadaneAlgo.java:
--------------------------------------------------------------------------------
1 | package Arrays;
2 |
3 | public class KadaneAlgo {
4 |
5 | public static int kadane(int arr[]) {
6 | int meh = arr[0], max = arr[0];
7 |
8 | for(int i = 1; i < arr.length; i++) {
9 | if(meh < 0) {
10 | meh = arr[i];
11 | } else {
12 | meh = meh + arr[i];
13 | }
14 | if(max < meh) max = meh;
15 | }
16 | return max;
17 | }
18 |
19 | public static int negativeKadanes(int arr[]) {
20 | int minEndHere = arr[0], min = arr[0];
21 |
22 | for(int i = 1; i < arr.length; i++) {
23 | if(minEndHere < 0) {
24 | minEndHere = minEndHere + arr[i];
25 | } else minEndHere = arr[i];
26 |
27 | if(minEndHere < min) min = minEndHere;
28 | }
29 |
30 | return min;
31 | }
32 |
33 | public static int maxSumCircularSubarray(int arr[]) {
34 | int max = arr[0], maxeh = arr[0];
35 | int min = arr[0], mineh = arr[0];
36 | int sum = arr[0];
37 |
38 | for(int i = 1; i < arr.length; i++ ) {
39 |
40 | maxeh = Math.max(arr[i], maxeh + arr[i]);
41 | max = Math.max(maxeh, max);
42 |
43 | mineh = Math.min(arr[i], mineh + arr[i]);
44 | min = Math.min(min, mineh);
45 |
46 | sum += arr[i];
47 | }
48 | if(min == sum) return max;
49 |
50 | return Math.max(max, sum - min);
51 | }
52 |
53 | public static void main(String[] args) {
54 | System.out.println(maxSumCircularSubarray(new int[] {7,-14,13,-18,11,3}));
55 | }
56 |
57 | }
--------------------------------------------------------------------------------
/src/Arrays/Questions.java:
--------------------------------------------------------------------------------
1 | package Arrays;
2 |
3 | import java.lang.reflect.Array;
4 | import java.util.Arrays;
5 |
6 | public class Questions {
7 |
8 | public static int sum(int arr[]) {
9 | int sum = 0;
10 | for(int i = 0; i < arr.length; i++) {
11 | sum += arr[i]; // sum = sum + arr[i]
12 | }
13 | return sum;
14 | }
15 |
16 | public static int max(int arr[]) {
17 | int max = Integer.MIN_VALUE;
18 | for(int i = 0; i < arr.length; i++) {
19 | if(max < arr[i]) {
20 | max = arr[i];
21 | }
22 | }
23 | return max;
24 | }
25 |
26 | public static int span(int arr[]) {
27 | int max = Integer.MIN_VALUE;
28 | int min = Integer.MAX_VALUE;
29 |
30 | for(int i = 0; i < arr.length; i++) {
31 | if(arr[i] > max) max = arr[i];
32 | if(arr[i] < min) min = arr[i];
33 | }
34 | return max - min;
35 | }
36 |
37 | public static boolean isElementRepeated(int arr[], int n) {
38 |
39 | int count = 0;
40 | for(int i = 0; i < arr.length; i++) {
41 | if(arr[i] == n) count++;
42 | if(count > 1) return true;
43 | }
44 | return false;
45 | }
46 |
47 | public static void reverse(int arr[]) {
48 | int left = 0;
49 | int right = arr.length - 1;
50 |
51 | while(left < right) {
52 |
53 | // swap
54 | int temp = arr[left];
55 | arr[left] = arr[right];
56 | arr[right] = temp;
57 |
58 | left++;
59 | right--;
60 | }
61 | }
62 |
63 | public static int[] inverse(int arr[]) {
64 | int res[] = new int[arr.length];
65 |
66 | for(int i = 0; i < arr.length; i++)
67 | res[arr[i]] = i;
68 |
69 | return res;
70 | }
71 |
72 | public static void display(int arr[]) {
73 | for(int i = 0; i < arr.length; i++)
74 | {
75 | System.out.print(arr[i] + " ");
76 | }
77 | System.out.println();
78 | }
79 |
80 |
81 |
82 | public static void targetSumPair(int arr[], int target) {
83 |
84 | // find all pairs
85 | for(int sp = 0; sp < arr.length - 1; sp++) {
86 |
87 | // move end point
88 | for(int ep = sp + 1; ep < arr.length; ep++) {
89 |
90 | if(arr[sp] + arr[ep] == target)
91 | System.out.println(arr[sp] + ", " + arr[ep]);
92 | }
93 | }
94 | }
95 |
96 |
97 | public static void targetSumPairOptimised(int arr[], int target) {
98 |
99 | Arrays.sort(arr);
100 |
101 | int left = 0, right = arr.length - 1;
102 |
103 | while(left < right) {
104 | if(arr[left] + arr[right] < target) {
105 | left++;
106 | } else if(arr[left] + arr[right] > target) {
107 | right--;
108 | } else { // case when target is equal to pair sum
109 | System.out.println(arr[left] + ", " + arr[right]);
110 | right--;
111 | }
112 | }
113 |
114 | }
115 |
116 |
117 | public static void main(String[] args) {
118 | int arr[] = {1, 2, 3, 4, 5, 4};
119 | // System.out.println(sum(arr));
120 | // System.out.println(max(arr));
121 | // System.out.println(span(arr));
122 | // System.out.println(isElementRepeated(arr, 4));
123 | //
124 | // display(arr);
125 | // reverse(arr);
126 | // display(arr);
127 |
128 | int arr2[] = {4, 1, 0, 2, 3};
129 | int inv[] = inverse(arr2);
130 | // display(arr2);
131 | // display(inv);
132 | // targetSumPair(arr2, 4);
133 |
134 | targetSumPairOptimised(arr2, 4);
135 | }
136 |
137 | }
138 |
--------------------------------------------------------------------------------
/src/Arrays/Searching.java:
--------------------------------------------------------------------------------
1 | package Arrays;
2 |
3 | public class Searching {
4 |
5 |
6 | public static int binarySearch(int arr[], int val) {
7 |
8 | int left = 0, right = arr.length - 1;
9 |
10 | while(left <= right) {
11 | int mid = (left + right) / 2;
12 |
13 | if(arr[mid] == val) {
14 | return mid;
15 | } else if(arr[mid] > val) {
16 | right = mid - 1;
17 | } else { // arr[mid] < val
18 | left = mid + 1;
19 | }
20 | }
21 | return -1;
22 |
23 | }
24 |
25 |
26 | public static int findFirstOccurence(int arr[], int val) {
27 | int possibleAnswer = -1;
28 |
29 | int left = 0, right = arr.length - 1;
30 |
31 | while(left <= right) {
32 | int mid = (left + right) / 2;
33 |
34 | if(arr[mid] == val) {
35 | possibleAnswer = mid;
36 | right = mid - 1;
37 | } else if(arr[mid] > val) {
38 | right = mid - 1;
39 | } else { // arr[mid] < val
40 | left = mid + 1;
41 | }
42 | }
43 | return possibleAnswer;
44 | }
45 |
46 | public static int findLastOccurence(int arr[], int val) {
47 | int possibleAnswer = -1;
48 |
49 | int left = 0, right = arr.length - 1;
50 |
51 | while(left <= right) {
52 | int mid = (left + right) / 2;
53 |
54 | if(arr[mid] == val) {
55 | possibleAnswer = mid;
56 | left = mid + 1;
57 | } else if(arr[mid] > val) {
58 | right = mid - 1;
59 | } else { // arr[mid] < val
60 | left = mid + 1;
61 | }
62 | }
63 | return possibleAnswer;
64 | }
65 |
66 | public static void main(String[] args) {
67 | int arr[] = {10,20,20,20,50,50,50,50,90, 90};
68 | // System.out.println(binarySearch(arr, 102));
69 | System.out.println(findLastOccurence(arr, 20));
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/Arrays/Sorting.java:
--------------------------------------------------------------------------------
1 | package Arrays;
2 |
3 | import javax.print.attribute.standard.JobOriginatingUserName;
4 |
5 | public class Sorting {
6 |
7 | public static void swap(int arr[], int i, int j) {
8 | int temp = arr[i];
9 | arr[i] = arr[j];
10 | arr[j] = temp;
11 | }
12 |
13 | public static void bubbleSort(int arr[]) {
14 | for(int pass = 1; pass < arr.length; pass++) {
15 | for(int i = 1; i <= arr.length - pass; i++) {
16 | if(arr[i-1] > arr[i]) {
17 | swap(arr, i, i-1);
18 | }
19 | }
20 | }
21 | }
22 |
23 | public static void selectionSort(int arr[]) {
24 |
25 | for(int pass = 1; pass < arr.length; pass++) {
26 | int pos = pass - 1;
27 | int minIdx = pos;
28 | // find minIdx
29 | for(int i = pos + 1; i < arr.length; i++) {
30 | if(arr[i] < arr[minIdx]) {
31 | minIdx = i;
32 | }
33 | }
34 | swap(arr, minIdx, pos);
35 | }
36 | }
37 |
38 | public static void insertionSort(int arr[]) {
39 |
40 | for(int unsortedStartIdx = 1; unsortedStartIdx < arr.length; unsortedStartIdx++) {
41 |
42 | int key = arr[unsortedStartIdx];
43 | int j = unsortedStartIdx - 1;
44 |
45 | // right shift
46 | while(j >= 0 && arr[j] > key) {
47 | arr[j+1] = arr[j];
48 | j--;
49 | }
50 | arr[j+1] = key;
51 | }
52 | }
53 |
54 | public static void display(int arr[]) {
55 | for(int i = 0; i < arr.length; i++)
56 | System.out.print(arr[i] + " ");
57 | System.out.println();
58 | }
59 |
60 | public static void main(String[] args) {
61 |
62 | int arr[] = {6, 4, 1, 3, 8, 5};
63 | display(arr);
64 | insertionSort(arr);
65 | display(arr);
66 |
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/src/Arrays/Sorting2.java:
--------------------------------------------------------------------------------
1 | package Arrays;
2 |
3 | public class Sorting2 {
4 |
5 | public static int[] merge(int a[], int b[]) {
6 | int res[] = new int[a.length + b.length];
7 |
8 | int i = 0, j = 0, k = 0;
9 |
10 | while (i < a.length && j < b.length) {
11 | if(a[i] <= b[j]) {
12 | res[k] = a[i];
13 | i++;
14 | k++;
15 | } else {
16 | res[k] = b[j];
17 | j++;
18 | k++;
19 | }
20 | }
21 |
22 | while(i < a.length) {
23 | res[k] = a[i];
24 | i++;
25 | k++;
26 | }
27 |
28 | while(j < b.length) {
29 | res[k] = b[j];
30 | j++;
31 | k++;
32 | }
33 |
34 | return res;
35 | }
36 |
37 |
38 |
39 | public static int[] mergeSort(int arr[], int start, int end) {
40 | // base case
41 | if(start == end) {
42 | int res[] = new int[1];
43 | res[0] = arr[start];
44 | return res;
45 | }
46 |
47 | int mid = (start + end) / 2;
48 |
49 | int firstHalf[] = mergeSort(arr, start, mid);
50 | int secondHalf[] = mergeSort(arr, mid + 1, end);
51 |
52 |
53 | int res[] = merge(firstHalf, secondHalf);
54 | return res;
55 | }
56 |
57 |
58 | public static void swap (int arr[], int i, int j) {
59 | int temp = arr[i];
60 | arr[i] = arr[j];
61 | arr[j] = temp;
62 | }
63 |
64 |
65 | public static void partition3Way(int arr[], int pivot) {
66 | int left = 0, mid = 0, right = arr.length - 1;
67 |
68 | while(mid <= right) {
69 | if(arr[mid] < pivot) {
70 | // case 1
71 | swap(arr, mid, left);
72 | mid++;
73 | left++;
74 |
75 | }else if(arr[mid] == pivot) {
76 | // case 2
77 | mid++;
78 | }else {
79 | swap(arr, mid, right);
80 | right--;
81 | }
82 | }
83 |
84 | }
85 |
86 | public static int partition(int arr[], int pivot, int start, int end) {
87 | int i = start, j = start;
88 | while(i <= end) {
89 | if(arr[i] <= pivot) {
90 | // region 1
91 | swap(arr, i, j);
92 | i++;
93 | j++;
94 | }else {
95 | // region 2
96 | i++;
97 | }
98 | }
99 | return j - 1;
100 | }
101 |
102 | public static void quickSort(int arr[], int start, int end) {
103 |
104 | if(start >= end) return;
105 |
106 | int pivot = arr[end];
107 | int pi = partition(arr, pivot, start, end);
108 | quickSort(arr, start, pi - 1);
109 | quickSort(arr, pi + 1, end);
110 | }
111 |
112 |
113 | public static void display(int arr[]) {
114 | for(int i = 0; i < arr.length; i++)
115 | System.out.print(arr[i] + " ");
116 | System.out.println();
117 | }
118 | public static void main(String[] args) {
119 | // TODO Auto-generated method stub
120 |
121 | int a[] = {1,3, 7, 11, 13};
122 | int b[] = {1, 3, 4, 6, 10, 14, 15, 17};
123 |
124 | int c[] = merge(a, b);
125 | // display(c);
126 |
127 | int arr[] = {1,5,2,9,3,1,7,4};
128 | // display(mergeSort(arr, 0, arr.length - 1));
129 | display(arr);
130 |
131 | System.out.println(partition(arr, 4, 0, arr.length - 1));
132 | display(arr);
133 |
134 | }
135 |
136 | }
137 |
--------------------------------------------------------------------------------
/src/Arrays/SortingQuestions.java:
--------------------------------------------------------------------------------
1 | package Arrays;
2 |
3 | import java.util.Arrays;
4 |
5 | public class SortingQuestions {
6 |
7 |
8 | public static int findCeilIndex(int arr[], int val) {
9 |
10 | int ans = -1, left = 0, right = arr.length - 1;
11 |
12 | while(left <= right) {
13 | int mid = (right + left)/ 2;
14 | if(arr[mid] >= val) {
15 | ans = mid; // potential ans
16 | right = mid - 1;
17 | }
18 | else {
19 | left = mid + 1;
20 | }
21 | }
22 |
23 | return ans;
24 | }
25 |
26 | public static int countTriplets(int arr[], int threshold) {
27 | int count = 0;
28 |
29 | Arrays.sort(arr);
30 |
31 | for(int i = 2; i < arr.length; i++) {
32 | int lowerBound = arr[i] - threshold;
33 | int lowerBoundIdx = findCeilIndex(arr, lowerBound);
34 |
35 | int totalElementsToConcider = i - lowerBoundIdx;
36 |
37 | //calculating nC2
38 | count += (totalElementsToConcider * (totalElementsToConcider - 1))/2;
39 |
40 | }
41 | return count;
42 | }
43 |
44 | public static void main(String[] args) {
45 | System.out.println(countTriplets(new int[] {5, 1, 3, 2, 7, 9}, 3));
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/Bits/BitwiseOperators.java:
--------------------------------------------------------------------------------
1 | package Bits;
2 |
3 | public class BitwiseOperators {
4 |
5 | public static int turnIthBitOn(int n, int i) {
6 | int mask = (1 << (i-1));
7 | int ans = (n | mask);
8 | return ans;
9 | }
10 |
11 | public static int turnIthBitOff(int n, int i) {
12 | int mask = (~(1 << (i-1)));
13 | return n & mask;
14 | }
15 |
16 | public static int toogleIthBit(int n, int i) {
17 | int mask = (1 << (i-1));
18 | return (n ^ mask);
19 | }
20 |
21 |
22 | public static boolean checkIthBit(int n, int i) {
23 | int mask = (1 << (i-1));
24 | return ((n & mask) > 0) ? true : false;
25 | }
26 |
27 | public static void CheckOddEven(int n) {
28 | if((n & 1) == 0) System.out.println("even");
29 | else System.out.println("odd");
30 | }
31 |
32 | public static void main(String[] args) {
33 | System.out.println(turnIthBitOn(13, 2));
34 | System.out.println(turnIthBitOff(15, 2));
35 | System.out.println(toogleIthBit(15, 4));
36 | System.out.println(checkIthBit(15, 5));
37 | CheckOddEven(5);
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/Bits/PlayingWithDataTypes.java:
--------------------------------------------------------------------------------
1 | package Bits;
2 |
3 | public class PlayingWithDataTypes {
4 |
5 | public static void main(String[] args) {
6 |
7 | byte a = (byte)156;
8 | System.out.println(a);
9 |
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/src/Doubts/Doubt2.java:
--------------------------------------------------------------------------------
1 | package Doubts;
2 | import java.util.Arrays;
3 | public class Doubt2 {
4 |
5 | public static int gcd(int a, int b) {
6 | int dividend = a, divisor = b;
7 |
8 | while(dividend % divisor > 0) {
9 | int rem = dividend % divisor;
10 | dividend = divisor;
11 | divisor = rem;
12 | }
13 | return divisor;
14 | }
15 |
16 | public static void cb(int arr[]) {
17 |
18 | int res[] = new int[arr.length];
19 | Arrays.fill(res, -2);
20 |
21 | for(int i = 0; i < arr.length; i++) {
22 |
23 | for (int j = arr.length - 1; j >= i; j--) {
24 | if(gcd(arr[i], arr[j]) == 1) {
25 | res[j] = i;
26 | if(res[i] != -2) {
27 | if(i - res[i] > j - i)
28 | res[i] = j;
29 | }
30 |
31 | }
32 | }
33 | }
34 |
35 | for(int i = 0; i < res.length; i++)
36 | System.out.print(res[i] + 1 + " ");
37 |
38 | }
39 |
40 | public static void closestBuddy(int arr[]) {
41 | for(int i = 0; i = 0; j--) {
53 | if(gcd(arr[i], arr[j]) == 1) {
54 | lidx = j;
55 | break;
56 | }
57 | }
58 |
59 | // right direction
60 | for(int j = i + 1; j < arr.length; j++) {
61 | if(gcd(arr[i], arr[j]) == 1) {
62 | ridx = j;
63 | break;
64 | }
65 | }
66 | int ans = -1;
67 | if(lidx == -1 && ridx == -1) {
68 | // no solution
69 | ans = -1;
70 | } else if(lidx == -1) {
71 | ans = ridx + 1;
72 | } else if(ridx == -1) {
73 | ans = lidx + 1;
74 | } else { // both are present
75 | if(i - lidx <= ridx - i) {
76 | ans = lidx + 1;
77 | } else {
78 | ans = ridx + 1;
79 | }
80 | }
81 | System.out.print(ans + " ");
82 | }
83 | }
84 |
85 | public static void main(String[] args) {
86 | // TODO Auto-generated method stub
87 | cb(new int[] {3, 21, 7, 7, 21});
88 |
89 | }
90 |
91 | }
92 |
--------------------------------------------------------------------------------
/src/Generics/GenericArrayList.java:
--------------------------------------------------------------------------------
1 | package Generics;
2 |
3 | public class GenericArrayList {
4 | private Object data[];
5 | private int size;
6 | private int CAPACITY = 10;
7 |
8 | public GenericArrayList() {
9 | this.data = new Object[CAPACITY];
10 | this.size = 0;
11 | }
12 |
13 | public void add(T value) {
14 | if(this.size == this.CAPACITY) {
15 | this.resize();
16 | }
17 | this.data[this.size++] = value;
18 | }
19 |
20 | public T remove() {
21 | if(this.size == 0) return null;
22 | this.size--;
23 | return (T)this.data[this.size];
24 | }
25 |
26 | public T get(int index) {
27 | return (T)this.data[index];
28 | }
29 |
30 | public void set(int index, T value) {
31 | this.data[index] = value;
32 | }
33 |
34 | private void resize() {
35 | Object temp[] = new Object[this.CAPACITY * 2];
36 | for(int i = 0; i < this.data.length; i++) {
37 | temp[i] = this.data[i];
38 | }
39 | this.data = temp;
40 | this.CAPACITY *= 2;
41 | }
42 |
43 | @Override
44 | public String toString() {
45 | String res = "[ ";
46 | for(int i = 0; i < this.size; i++) {
47 | res += data[i] + " ";
48 | }
49 | res += ']';
50 | return res;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/Generics/GenricHashMap.java:
--------------------------------------------------------------------------------
1 | package Generics;
2 |
3 | public class GenricHashMap {
4 | K key;
5 | V value;
6 | }
7 |
--------------------------------------------------------------------------------
/src/Generics/MyArrayList.java:
--------------------------------------------------------------------------------
1 | package Generics;
2 |
3 | // add, remove, get, set, size
4 | public class MyArrayList {
5 | private int data[];
6 | private int size;
7 | private int CAPACITY = 10;
8 |
9 | public MyArrayList() {
10 | this.data = new int[CAPACITY];
11 | this.size = 0;
12 | }
13 |
14 | public void add(int value) {
15 | if(this.size == this.CAPACITY) {
16 | this.resize();
17 | }
18 | this.data[this.size++] = value;
19 | }
20 |
21 | public int remove() {
22 | if(this.size == 0) return -1;
23 | this.size--;
24 | return this.data[this.size];
25 | }
26 |
27 | public int get(int index) {
28 | return this.data[index];
29 | }
30 |
31 | public void set(int index, int value) {
32 | this.data[index] = value;
33 | }
34 |
35 | private void resize() {
36 | int temp[] = new int[this.CAPACITY * 2];
37 | for(int i = 0; i < this.data.length; i++) {
38 | temp[i] = this.data[i];
39 | }
40 | this.data = temp;
41 | this.CAPACITY *= 2;
42 | }
43 |
44 | @Override
45 | public String toString() {
46 | String res = "[ ";
47 | for(int i = 0; i < this.size; i++) {
48 | res += data[i] + " ";
49 | }
50 | res += ']';
51 | return res;
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/src/Generics/Utility.java:
--------------------------------------------------------------------------------
1 | package Generics;
2 |
3 | public class Utility {
4 |
5 | public static void main(String[] args) {
6 | // MyArrayList al = new MyArrayList();
7 | // al.add(10);
8 | // al.add(20);
9 | // System.out.println(al);
10 | // al.add(30);
11 | // al.set(1, 50);
12 | // System.out.println(al);
13 | // System.out.println(al.remove());
14 | //
15 | // System.out.println(al);
16 |
17 | GenericArrayList list = new GenericArrayList<>();
18 | list.add("Hi");
19 | list.add("Hello");
20 | list.add("Welcome");
21 | // list.add(20);
22 | System.out.println(list);
23 |
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/HashMaps/Intro.java:
--------------------------------------------------------------------------------
1 | package HashMaps;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map.Entry;
5 | import java.util.Map;
6 |
7 | public class Intro {
8 |
9 | public static void main(String[] args) {
10 |
11 | HashMap map = new HashMap();
12 | HashMap map1 = new HashMap();
13 |
14 | System.out.println(map1);
15 | // add
16 | map.put("Tanmay", 90);
17 | map.put("Nikhil", 90);
18 | map.put("gaurav", 85);
19 | map.put(null, null);
20 |
21 | System.out.println(map);
22 |
23 | // set or update
24 | map.put("Tanmay", 50);
25 | System.out.println(map);
26 |
27 | // remove
28 | map.remove("Nikhil");
29 | System.out.println(map);
30 |
31 |
32 | // get
33 | int data = map.get("Tanmay");
34 | System.out.println(data);
35 |
36 | int data2 = map.getOrDefault("Shiv", 0);
37 | System.out.println("data2 is " + data2);
38 |
39 |
40 | System.out.println(map.containsKey("Tanmay "));
41 |
42 | // size
43 | System.out.println(map.size());
44 |
45 |
46 | String arr[] = {"abc", "def"};
47 |
48 | // for(int i = 0; i < arr.length; i++) {
49 | // String element = arr[i];
50 | //
51 | // System.out.println(element);
52 | // }
53 |
54 | // for(String element : arr) {
55 | // System.out.println(element);
56 | // }
57 |
58 | for(Entry entry: map.entrySet()) {
59 | System.out.println(entry.getKey() + " -> " + entry.getValue());
60 | }
61 |
62 | for(String k : map.keySet()) {
63 | System.out.println(k);
64 | }
65 |
66 | map.forEach((k, v) -> {
67 | System.out.println(k + "<->" + v);
68 | });
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/src/HashMaps/Questions.java:
--------------------------------------------------------------------------------
1 | package HashMaps;
2 |
3 | import java.util.HashMap;
4 | import java.util.HashSet;
5 | import java.util.Map.Entry;
6 |
7 | public class Questions {
8 |
9 | public static void charWithMaxFreq(char arr[]) {
10 | char ans = ' ';
11 | int max = 0;
12 | // make freq map
13 | HashMap fmap = new HashMap<>();
14 |
15 | for(char ch : arr) {
16 | if(fmap.containsKey(ch)) {
17 | int initialFreq = fmap.get(ch);
18 | fmap.put(ch, initialFreq + 1);
19 | } else {
20 | fmap.put(ch, 1);
21 | }
22 | }
23 |
24 |
25 | for(Entry entry : fmap.entrySet()) {
26 | if(entry.getValue() > max) {
27 | max = entry.getValue();
28 | ans = entry.getKey();
29 | }
30 | }
31 |
32 | System.out.println(ans + " -> " + max);;
33 | }
34 |
35 |
36 | public static void printCommonElements(int arr1[], int arr2[]) {
37 |
38 | HashSet set = new HashSet<>();
39 | for(int i = 0; i < arr1.length; i++) {
40 | int elem = arr1[i];
41 | set.add(arr1[i]);
42 | }
43 |
44 | for(int elem : arr2) {
45 | if(set.contains(elem)) {
46 | System.out.println(elem);
47 | }
48 | }
49 | }
50 |
51 |
52 | public static int longestSubarrayWithSumK(int arr[], int k) {
53 |
54 | int ps = 0;
55 | HashMap map = new HashMap<>();
56 | map.put(0, -1);
57 | int maxLength = 0;
58 |
59 | for(int i = 0; i < arr.length; i++) {
60 | ps = ps + arr[i];
61 | if(map.containsKey(ps) == false) {
62 | map.put(ps, i);
63 | }
64 |
65 | if(map.containsKey(ps - k) == true) {
66 | // posible solution
67 | int idxPsMK = map.get(ps - k);
68 | if(i - idxPsMK > maxLength)
69 | maxLength = i - idxPsMK;
70 | }
71 | }
72 |
73 | return maxLength;
74 | }
75 |
76 | public static int countSubarraysWithEqual0and1(int arr[]) {
77 | HashMap map = new HashMap<>();
78 | map.put(0, 0);
79 | int count = 0, ps = 0;
80 |
81 | for(int i = 0; i < arr.length; i++) {
82 | if(arr[i] == 0) arr[i] = -1;
83 | }
84 |
85 | for(int i = 0; i < arr.length; i++) {
86 | // calculate prefix sum
87 | ps = ps + arr[i];
88 |
89 | // check if map contains ps
90 | if(map.containsKey(ps)) {
91 | int initalCount = map.get(ps);
92 | count += initalCount;
93 | map.put(ps, initalCount + 1);
94 | }else {
95 | map.put(ps, 1); // first time
96 | }
97 | }
98 |
99 | return count;
100 | }
101 |
102 | public static boolean isVovel(char ch) {
103 | return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
104 | }
105 |
106 | public static int shortestSubStringWithAllVovels(String s) {
107 |
108 | HashMap map = new HashMap();
109 | int start = 0, end = 0, minLength = Integer.MAX_VALUE;
110 |
111 | while(end < s.length()) {
112 |
113 | // acquire
114 | while(end < s.length() && map.size() < 5) {
115 | char ch = s.charAt(end);
116 | if(isVovel(ch) == true) {
117 | // add in the hashmap
118 | int initialCount = map.getOrDefault(ch, 0);
119 | map.put(ch, initialCount + 1);
120 | }
121 | end++;
122 | }
123 |
124 | // check for possible ans
125 | if(map.size() == 5) { // we have 1 valid ans
126 | // e - s
127 | if(end - start < minLength) minLength = end - start;
128 | }
129 |
130 | // Release for optimisation
131 | while(start < s.length() && map.size() == 5) {
132 | char ch = s.charAt(start);
133 | if(isVovel(ch)) {
134 | int initialCount = map.get(ch);
135 | if(initialCount == 1) {
136 | map.remove(ch);
137 |
138 | // check for valid ans
139 | if(end - start < minLength) minLength = end - start;
140 | }
141 | else map.put(ch, initialCount - 1);
142 | }
143 | start++;
144 | }
145 | }
146 | return minLength;
147 |
148 | }
149 |
150 | public static void main(String[] args) {
151 | // charWithMaxFreq(new char[] {'a', 'b', 'c','a', 'b', 'b'});
152 |
153 | int arr1[] = {10,1,1,1,12,2,2,4,5,6,8,8,2};
154 |
155 | int arr2[] = {1, 2, 3, 4, 4};
156 |
157 | // printCommonElements(arr1, arr2);
158 |
159 | System.out.println(countSubarraysWithEqual0and1(new int[] {0, 0 ,1, 0, 1, 0, 1 }));
160 |
161 |
162 |
163 | }
164 |
165 | }
166 |
--------------------------------------------------------------------------------
/src/LinkedList/LinkedList.java:
--------------------------------------------------------------------------------
1 | package LinkedList;
2 |
3 | public class LinkedList {
4 |
5 | private static class Node {
6 | int data;
7 | Node next;
8 |
9 | public Node() {}
10 |
11 | Node(int data) {
12 | this.data = data;
13 | }
14 |
15 | Node(int data, Node next) {
16 | this.data = data;
17 | this.next = next;
18 | }
19 | }
20 |
21 | private Node head;
22 | private Node tail;
23 | private int size;
24 |
25 | private Node left;
26 |
27 |
28 | public void addFirst(int data) {
29 | Node n = new Node(data);
30 | n.next = this.head;
31 | this.head = n;
32 | if(this.size == 0) {
33 | this.tail = head;
34 | }
35 | this.size++;
36 | }
37 |
38 | public void addLast(int data) {
39 | if(this.size == 0) {
40 | this.addFirst(data);
41 | return;
42 | }
43 | Node n = new Node(data);
44 | this.tail.next = n;
45 | this.tail = n;
46 | this.size++;
47 | }
48 |
49 | public void add(int data, int idx) {
50 | if(this.size == 0 || idx == 0) {
51 | this.addFirst(data);
52 | return;
53 | }
54 | if(idx == this.size) {
55 | this.addLast(data);
56 | return;
57 | }
58 | Node n = new Node(data);
59 | Node temp = this.getNode(idx - 1);
60 | n.next = temp.next;
61 | temp.next = n;
62 | this.size++;
63 | }
64 |
65 | public int removeFirst() {
66 | if(this.size == 0) return -1;
67 | int itemGettingRemoved = this.head.data;
68 | if(this.size == 1) {
69 | this.tail = null;
70 | }
71 | this.head = this.head.next;
72 | this.size--;
73 | return itemGettingRemoved;
74 | }
75 |
76 | public int removeLast() {
77 | if(this.size <= 1) return this.removeFirst();
78 |
79 | int itemGettingRemoved = this.tail.data;
80 | Node secondLastNode = this.getNode(this.size - 2);
81 | secondLastNode.next = null;
82 | this.tail = secondLastNode;
83 | this.size--;
84 | return itemGettingRemoved;
85 | }
86 |
87 | public int remove(int idx) {
88 | if(this.size <= 1) return this.removeFirst();
89 | else if(idx == this.size - 1) return this.removeLast();
90 |
91 | Node temp = this.getNode(idx - 1);
92 | int itemGettingRemoved = temp.next.data;
93 | temp.next = temp.next.next;
94 | this.size--;
95 | return itemGettingRemoved;
96 | }
97 |
98 | public int get(int idx) {
99 | Node targetNode = this.getNode(idx);
100 | if(targetNode == null) return -1;
101 | else return targetNode.data;
102 | }
103 |
104 | public void set(int idx, int data) {
105 | Node targetNode = this.getNode(idx);
106 | if(targetNode != null) {
107 | targetNode.data = data;
108 | }
109 | }
110 |
111 | private Node getNode(int idx) {
112 | Node temp = this.head;
113 | int count = 0;
114 | while(temp != null && count < idx) {
115 | count++;
116 | temp = temp.next;
117 | }
118 |
119 | return temp;
120 | }
121 |
122 | public void display() {
123 | Node temp = head;
124 | while(temp != null) {
125 | System.out.print(temp.data + " -> ");
126 | temp = temp.next;
127 | }
128 | System.out.println("null");
129 | }
130 |
131 | @Override
132 | public String toString() {
133 | Node temp = head;
134 | String res = "";
135 | while(temp != null) {
136 | res += temp.data + " -> ";
137 | temp = temp.next;
138 | }
139 | res += "null";
140 | return res;
141 | }
142 |
143 | public void swap(Node one, Node two) {
144 | int temp = one.data;
145 | one.data = two.data;
146 | two.data = temp;
147 | }
148 |
149 | // tc => O(n^2)
150 | public void reverseDI() {
151 | int left = 0, right = this.size - 1;
152 |
153 | while(left < right) {
154 | Node leftNode = getNode(left);
155 | Node rightNode = getNode(right);
156 |
157 | // swap
158 | this.swap(leftNode, rightNode);
159 | left++;
160 | right--;
161 | }
162 | }
163 |
164 | public void swapHeadTail() {
165 | Node temp = this.head;
166 | this.head = this.tail;
167 | this.tail = temp;
168 | }
169 |
170 | // tc => O(n)
171 | public void reversePI() {
172 | Node curr = this.head;
173 | Node prev = null;
174 |
175 | while(curr != null) {
176 | Node next = curr.next;
177 | curr.next = prev;
178 | prev = curr;
179 | curr = next;
180 | }
181 |
182 | this.swapHeadTail();
183 | }
184 |
185 | // tc => O(n)
186 | public void reversePR() {
187 | this.reversePR(this.head);
188 | this.swapHeadTail();
189 | }
190 |
191 | // tc => O(n)
192 | private void reversePR(Node curr) {
193 |
194 | if(curr.next == null) return;
195 |
196 | reversePR(curr.next);
197 | // curr.next.next = curr;
198 | Node nextNode = curr.next;
199 | nextNode.next = curr;
200 | curr.next = null;
201 |
202 | }
203 |
204 | public void reverseDR() {
205 | left = this.head;
206 | reverseDR(this.head, 0);
207 | }
208 |
209 | private void reverseDR(Node right, int ridx) {
210 | if(right == null) return;
211 |
212 | reverseDR(right.next, ridx + 1);
213 | if(ridx >= this.size / 2) {
214 | swap(left, right);
215 | left = left.next;
216 | }
217 | }
218 |
219 | public void removeDuplicate() {
220 | Node temp = this.head;
221 |
222 | while(temp.next != null) {
223 | if(temp.data == temp.next.data) {
224 | temp.next = temp.next.next;
225 | } else {
226 | temp = temp.next;
227 | }
228 | }
229 | }
230 |
231 |
232 | public static LinkedList merge2SortedLL(LinkedList l1, LinkedList l2) {
233 | Node one = l1.head;
234 | Node two = l2.head;
235 |
236 | LinkedList res = new LinkedList();
237 | res.size = l1.size + l2.size;
238 | // first node to be added in res
239 | if(one.data < two.data) {
240 | res.head = one;
241 | res.tail = one;
242 | one = one.next;
243 | } else {
244 | res.head = two;
245 | res.tail = two;
246 | two = two.next;
247 | }
248 |
249 | while(one != null && two != null) {
250 | if(one.data < two.data) {
251 | res.tail.next = one;
252 | res.tail = one;
253 | one = one.next;
254 | } else {
255 | res.tail.next = two;
256 | res.tail = two;
257 | two = two.next;
258 | }
259 | res.tail.next = null;
260 | }
261 |
262 | if (one == null) {
263 | res.tail.next = two;
264 | } else if(two == null) {
265 | res.tail.next = one;
266 | }
267 |
268 | return res;
269 | }
270 |
271 | private void addLast(Node n) {
272 | if(this.head == null) {
273 | this.head = n;
274 | }else {
275 | this.tail.next = n;
276 | }
277 | this.tail = n;
278 | this.size++;
279 | }
280 |
281 | public void oddEven() {
282 | LinkedList odd = new LinkedList();
283 | LinkedList even = new LinkedList();
284 |
285 | Node curr = this.head;
286 | while(curr != null) {
287 | Node next = curr.next;
288 | curr.next = null;
289 | if(curr.data % 2 == 1) { // odd
290 | odd.addLast(curr);
291 | } else {
292 | even.addLast(curr);
293 | }
294 | curr = next;
295 | }
296 |
297 | odd.tail.next = even.head;
298 | this.head = odd.head;
299 | this.tail = even.tail;
300 | }
301 |
302 | public static Node reverse(Node node) {
303 | return node;
304 | }
305 |
306 |
307 | public static Node add(Node one, Node two) {
308 | reverse(one);
309 | reverse(two);
310 | int carry = 0;
311 | Node prev = null;
312 | Node res = null;
313 | while(!(one == null && two == null && carry == 0)) {
314 | Node curr = new Node();
315 | int sum = (one == null ? 0 : one.data)
316 | + (two == null ? 0 : two.data)
317 | + carry;
318 | carry = sum / 10;
319 | curr.data = sum % 10;
320 |
321 | if(one != null) one = one.next;
322 | if(two != null) two = two.next;
323 |
324 | if(prev != null) {
325 | prev.next = curr;
326 | } else {
327 | res = curr;
328 | }
329 | prev = curr;
330 | }
331 |
332 | return reverse(res);
333 | }
334 |
335 |
336 |
337 |
338 |
339 | }
340 |
--------------------------------------------------------------------------------
/src/LinkedList/Utility.java:
--------------------------------------------------------------------------------
1 | package LinkedList;
2 | public class Utility {
3 |
4 |
5 |
6 | public static void main(String[] args) {
7 |
8 | LinkedList ll = new LinkedList();
9 | // ll.display();
10 | // ll.addFirst(10);
11 | // ll.addFirst(20);
12 | // ll.addFirst(100);
13 | // ll.addLast(50);
14 | // ll.display();
15 | // ll.add(40, 2);
16 | // System.out.println(ll.get(1));
17 | // ll.set(1, 200);
18 | // System.out.println(ll);
19 | // System.out.println(ll.removeFirst());
20 | // System.out.println(ll.removeFirst());
21 | // System.out.println(ll.removeFirst());
22 | // ll.display();
23 | // System.out.println(ll.removeLast());
24 | // ll.display();
25 | // ll.reverseDI();
26 | // ll.reversePI();
27 | // ll.reversePR();
28 | // ll.reverseDR();
29 | // ll.display();
30 | ll.addLast(1);
31 | ll.addLast(2);
32 | ll.addLast(9);
33 | ll.addLast(4);
34 | ll.addLast(7);
35 | ll.display();
36 |
37 | // LinkedList l2 = new LinkedList();
38 | // l2.addLast(0);
39 | // l2.addLast(2);
40 | // l2.addLast(3);
41 | // l2.addLast(5);
42 | // l2.display();
43 | // ll.removeDuplicate();
44 | // ll.display();
45 | // LinkedList res = LinkedList.merge2SortedLL(ll, l2);
46 | // res.display();
47 |
48 | ll.oddEven();
49 | ll.display();
50 |
51 |
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/Mathematics/Basics.java:
--------------------------------------------------------------------------------
1 | package Mathematics;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class Basics {
7 |
8 | public static int gcd(int a, int b) {
9 | int min = a > b ? b : a;
10 | for(int i = min; i >= 1; i--) {
11 | if(a % i == 0 && b % i == 0)
12 | return i;
13 | }
14 | return 1;
15 | }
16 |
17 | public static int gcdOptimised(int a, int b) {
18 | int dividend = a, divisor = b;
19 |
20 | while(dividend % divisor > 0) {
21 | int rem = dividend % divisor;
22 | dividend = divisor;
23 | divisor = rem;
24 | }
25 | return divisor;
26 | }
27 |
28 | public static int lcm(int a, int b) {
29 | int gcd = gcdOptimised(a, b);
30 | return (a*b)/gcd;
31 | }
32 |
33 | public static boolean isPrime(int n) {
34 | for(int i = 2; i * i <= n; i++) {
35 | if(n % i == 0) return false;
36 | }
37 | return true;
38 | }
39 |
40 | public static void printPrime(int n) {
41 | for(int num = 2; num <= n; num++) {
42 | if(isPrime(num)) System.out.println(num);
43 | }
44 | }
45 |
46 | public static boolean[] sieveOfEratosthenes(int n) {
47 | boolean sieve[] = new boolean[n + 1];
48 | Arrays.fill(sieve, true);
49 |
50 | sieve[0] = false;
51 | sieve[1] = false;
52 |
53 | for(int i = 2; i * i <= n; i++) {
54 | // work only if true
55 | if(sieve[i]) { // work only if true
56 |
57 | for(int j = i * 2; j <= n; j += i) {
58 | sieve[j] = false;
59 | }
60 | }
61 | }
62 | return sieve;
63 | }
64 |
65 |
66 | public static void displaySieve(boolean sieve[]) {
67 | for(int i = 0; i < sieve.length; i++) {
68 | if(sieve[i]) System.out.println(i);
69 | }
70 | }
71 |
72 | public static void main(String[] args) {
73 |
74 | Scanner scn = new Scanner(System.in);
75 | int a = scn.nextInt();
76 | // int b = scn.nextInt();
77 |
78 | // System.out.println(lcm(a, b));
79 | // System.out.println(isPrime(a));
80 | // printPrime(a);
81 | displaySieve(sieveOfEratosthenes(a));
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/src/Mathematics/Geometry.java:
--------------------------------------------------------------------------------
1 | package Mathematics;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Geometry {
6 |
7 | public static boolean isRightAngledTriangle(int ax, int ay, int bx, int by, int cx, int cy) {
8 | // p, q and r are sides of our triangle
9 |
10 | // square of side ab
11 | int ab = ((ax - bx) * (ax - bx)) + ((ay - by) * (ay - by));
12 |
13 | int ac = ((ax - cx) * (ax - cx)) + ((ay - cy) * (ay - cy));
14 |
15 | int bc = ((bx - cx) * (bx - cx)) + ((by - cy) * (by - cy));
16 |
17 | // PYTHAGORAS THEOREM
18 | if(ab == ac + bc || ac == ab + bc || bc == ab + ac) return true;
19 | else return false;
20 | }
21 |
22 | public static void trianglesLove(int ax, int ay, int bx, int by, int cx, int cy) {
23 | if(isRightAngledTriangle(ax, ay, bx, by, cx, cy)) {
24 | System.out.println("Right");
25 | return;
26 | }
27 |
28 | int dirx[] = {0, 0, 1, -1};
29 | int diry[] = {1, -1, 0, 0};
30 |
31 | for(int i = 0; i < 4; i++) {
32 | // check with 1st point
33 | int newax = ax + dirx[i];
34 | int neway = ay + diry[i];
35 | if(isRightAngledTriangle(newax, neway, bx, by, cx, cy)) {
36 | System.out.println("Special");
37 | return;
38 | }
39 |
40 | // check with second point
41 |
42 | if(isRightAngledTriangle(ax, ay, bx + dirx[i], by + diry[i], cx, cy)) {
43 | System.out.println("Special");
44 | return;
45 | }
46 |
47 |
48 | // check with third point
49 | if(isRightAngledTriangle(ax, ay, bx, by, cx + dirx[i], cy + diry[i])) {
50 | System.out.println("Special");
51 | return;
52 | }
53 |
54 | }
55 | System.out.println("Simple");
56 | }
57 |
58 | public static void main(String[] args) {
59 | // TODO Auto-generated method stub
60 |
61 | Scanner scn = new Scanner(System.in);
62 | int ax = scn.nextInt();
63 | int ay = scn.nextInt();
64 | int bx = scn.nextInt();
65 | int by = scn.nextInt();
66 | int cx = scn.nextInt();
67 | int cy = scn.nextInt();
68 |
69 |
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/src/OOPS/Basics.java:
--------------------------------------------------------------------------------
1 | package OOPS;
2 | import Recursion.Questions2;
3 |
4 | class Student implements Comparable {
5 | String name;
6 | int rollno;
7 | static int count;
8 |
9 | static {
10 | System.out.println("inside static block");
11 | count = 0;
12 | }
13 |
14 |
15 | Student() {
16 | System.out.println("Running constructor");
17 | this.name = "";
18 | this.rollno = 0;
19 | count++;
20 | System.out.println(count);
21 | }
22 |
23 | public void nonStaticMethod() {
24 | printCount();
25 | this.printInfo();
26 | }
27 |
28 | public void printInfo() {
29 | System.out.println("----------------------------");
30 | System.out.println("Name is " + this.name);
31 | System.out.println("Rollno is " + this.rollno);
32 | System.out.println("total count is " + count);
33 | System.out.println("----------------------------");
34 | printCount();
35 | }
36 |
37 | public static void printCount() {
38 | System.out.println(count);
39 | // printInfo();
40 | }
41 |
42 | @Override
43 | public int compareTo(Student o) {
44 | // return o.rollno - this.rollno;
45 | if(this.rollno == o.rollno) return 0;
46 | else if(this.rollno > o.rollno) return 1;
47 | else return -1;
48 | }
49 | }
50 |
51 |
52 | public class Basics {
53 |
54 | class Example {
55 |
56 | }
57 |
58 | public static void main(String[] args) {
59 | Student s1 = new Student();
60 | Student s2 = new Student();
61 | // s1.count = 10;
62 | // System.out.println(s2.count);
63 | // Student.count = 30;
64 | s1.name = "Tanmay";
65 | s1.rollno = 20;
66 | s1.printInfo();
67 | s2.printInfo();
68 | System.out.println();
69 | Student s3 = new Student();
70 | s1.name = "Rajat";
71 | s1.printInfo();
72 | s1.printCount();
73 | Student.printCount();
74 | // Student.printInfo(); does not exist
75 |
76 | System.out.println(Questions2.fact(5));
77 | }
78 |
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/src/OOPS/ComparableAndComparator.java:
--------------------------------------------------------------------------------
1 | package OOPS;
2 |
3 | import java.util.Arrays;
4 |
5 |
6 | class CollegeStudent {
7 | int marks;
8 | int attendance;
9 |
10 | public void printInfo() {
11 | System.out.println(marks + ", " + this.attendance);
12 | }
13 | }
14 |
15 | public class ComparableAndComparator {
16 |
17 | public static void main(String[] args) {
18 | // Student students[] = new Student[5];
19 | // for(int i = 0; i < students.length; i++) {
20 | // students[i] = new Student();
21 | // students[i].rollno = i + 1;
22 | // }
23 | // Arrays.sort(students);
24 | // for(int i = 0; i < students.length; i++) {
25 | // students[i].printInfo();
26 | // }
27 |
28 | CollegeStudent students[] = new CollegeStudent[5];
29 | for(int i = 0; i < students.length; i++) {
30 | students[i] = new CollegeStudent();
31 | students[i].marks = (int)(Math.random() * 100);
32 | students[i].attendance = (int)(Math.random() * 100);
33 | }
34 |
35 |
36 | // Arrays.sort(students,
37 | // (a, b) -> { return a.marks - b.marks; });
38 |
39 | Arrays.sort(students, (a, b) -> {
40 | if(a.marks < b.marks) return 1;
41 | else if(a.marks > b.marks) return -1;
42 | else return 0;
43 | });
44 |
45 |
46 | for(int i = 0; i < students.length; i++) {
47 | students[i].printInfo();
48 | }
49 |
50 |
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/src/OOPS/ExceptionHandling.java:
--------------------------------------------------------------------------------
1 | package OOPS;
2 |
3 | import java.util.Stack;
4 |
5 | public class ExceptionHandling {
6 | public static void divide(int a, int b) {
7 | try {
8 | if(b == 0) {
9 | throw new NullPointerException("b can not be 0");
10 | }
11 |
12 | int arr[] = new int[0];
13 | arr[0] = 1;
14 | Stack st = null;
15 | st.push(1);
16 | System.out.println(a/b);
17 | System.out.println("After the exception");
18 | }
19 | // catch (ArithmeticException e) {
20 | //
21 | // System.out.println("arithmatic excception is thrown");
22 | // System.out.println(e.getMessage());
23 | // }
24 | // catch(NullPointerException e) {
25 | // System.out.println("null pointer excception is thrown");
26 | // System.out.println(e);
27 | // }
28 | catch (Exception e) {
29 | System.out.println("generic exception");
30 | // System.out.println(e);
31 | // e.printStackTrace();
32 | }
33 | finally {
34 | System.out.println("Inside finally block");
35 | }
36 |
37 | System.out.println("after try catch");
38 |
39 | }
40 | public static void fun() throws Exception {
41 |
42 | // divide(0, 0);
43 | demo();
44 | }
45 |
46 | public static void demo() throws Exception {
47 | // try {
48 | // throw new Exception();
49 | // } catch(Exception e) {
50 | //
51 | // }
52 | Exception e = new Exception();
53 | throw e;
54 |
55 | }
56 |
57 | public static void main(String[] args){
58 | // fun();
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/OOPS/Inheritence.java:
--------------------------------------------------------------------------------
1 | package OOPS;
2 |
3 | class Parent {
4 | int a;
5 |
6 | Parent() {
7 | System.out.println("Parent default constructor");
8 | }
9 |
10 | Parent (int a) {
11 | System.out.println("Child parameterized constructor");
12 | this.a = a;
13 | }
14 |
15 | void printA() {
16 | System.out.println(this.a);
17 | }
18 | }
19 |
20 | class Child extends Parent{
21 | int b;
22 |
23 |
24 | Child() {
25 | this(0);
26 | System.out.println("Child default constructor");
27 | }
28 |
29 | Child(int b) {
30 | System.out.println("Child parameterized constructor");
31 | this.b = b;
32 | }
33 |
34 | Child(int a, int b) {
35 | super(a);
36 | this.b = b;
37 | }
38 |
39 | void printB() {
40 | System.out.println(this.b);
41 | }
42 |
43 | void printAll() {
44 | System.out.println(super.a);
45 | System.out.println(this.b);
46 | super.printA();
47 | }
48 |
49 |
50 | }
51 |
52 | public class Inheritence {
53 |
54 | public static void main(String[] args) {
55 |
56 | Child c = new Child();
57 | // Child c2 = new Child(1);
58 | c.printA();
59 | c.printB();
60 | // Object
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/OOPS/Polymorphism.java:
--------------------------------------------------------------------------------
1 | package OOPS;
2 |
3 | import OOPSImplementation.AcessModifiers;
4 |
5 | class A {
6 | final int a;
7 |
8 | A() {
9 | a = 10;
10 | }
11 |
12 | void printA() {
13 | System.out.println(this.a);
14 | // a = 20;
15 | }
16 | void print() {
17 | System.out.println("Print of A class");
18 | }
19 | }
20 |
21 | class B extends A{
22 | int b;
23 |
24 | void printB() {
25 | System.out.println(this.b);
26 | }
27 |
28 | @Override
29 | void print() {
30 | // change the definition
31 | System.out.println("Print of B class");
32 | }
33 | }
34 |
35 |
36 | public class Polymorphism extends AcessModifiers{
37 |
38 | // function overloading
39 | static void sum(int a, int b) {
40 | System.out.println(a + b);
41 | }
42 |
43 | static void sum(int a, int b, int c) {
44 | System.out.println(a + b + c);
45 | }
46 |
47 | static void sum(double a, double b) {
48 | System.out.println(a + b);
49 | }
50 |
51 | void test() {
52 | this.a = 20;
53 | // this.printA() not accessible
54 | }
55 |
56 | public static void main(String[] args) {
57 | // sum(1,2);
58 | // sum(1,2,3);
59 | // sum(1.2, 2);
60 |
61 | A obj1 = new A();
62 | obj1.print();
63 |
64 | B obj2 = new B();
65 | obj2.print();
66 |
67 | A obj3 = new B();
68 | obj3.print();
69 | obj3.printA();
70 | // obj3.printB();
71 | // not allowed
72 | // B obj4 = new A();
73 | Parent p = null;
74 |
75 | // AcessModifiers acc = new AcessModifiers();
76 | // acc.a = 20;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/OOPS/StaticExample.java:
--------------------------------------------------------------------------------
1 | package OOPS;
2 |
3 | class OuterClass {
4 |
5 | int nonsvar = 10;
6 | static int statvar = 20;
7 |
8 | static class NestedClass {
9 | public void fun() {
10 | statvar = 30;
11 | // nonsvar = 40; can not access non static data member inside static class
12 | }
13 | }
14 | }
15 |
16 | public class StaticExample {
17 | public static void main(String[] args) {
18 |
19 | OuterClass obj = new OuterClass();
20 | // NestedClass a = obj.new NestedClass();
21 | OuterClass.NestedClass a = new OuterClass.NestedClass();
22 |
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/OOPSImplementation/AcessModifiers.java:
--------------------------------------------------------------------------------
1 | package OOPSImplementation;
2 |
3 | public class AcessModifiers {
4 | protected int a;
5 | private int test;
6 |
7 | // getter
8 | public int getTest() {
9 | return this.test;
10 | }
11 | // setter
12 | // public void setTest(int test) {
13 | // this.test = test;
14 | // }
15 |
16 |
17 | void printA() {
18 | System.out.println(this.a);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/OOPSImplementation/Bike.java:
--------------------------------------------------------------------------------
1 | package OOPSImplementation;
2 |
3 | public class Bike implements Vehicle {
4 |
5 | @Override
6 | public void start() {
7 | System.out.println("Bike engine started");
8 | }
9 |
10 | @Override
11 | public void stop() {
12 | System.out.println("Bike engine stoped");
13 | }
14 |
15 | @Override
16 | public void accelerate(int speed) {
17 | System.out.println("Bike accelerated by " + speed);
18 | }
19 |
20 | @Override
21 | public void brake() {
22 | System.out.println("Bike brakes");
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/OOPSImplementation/Car.java:
--------------------------------------------------------------------------------
1 | package OOPSImplementation;
2 |
3 | public class Car implements Vehicle {
4 |
5 | @Override
6 | public void start() {
7 | System.out.println("Car engine started");
8 | }
9 |
10 | @Override
11 | public void stop() {
12 | System.out.println("Car engine stoped");
13 | }
14 |
15 | @Override
16 | public void accelerate(int speed) {
17 | System.out.println("car accelerated by " + speed);
18 | }
19 |
20 | @Override
21 | public void brake() {
22 | System.out.println("Car brakes");
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/OOPSImplementation/Child.java:
--------------------------------------------------------------------------------
1 | package OOPSImplementation;
2 |
3 | // multiple inheritance
4 | public class Child implements Father, Mother {
5 |
6 | @Override
7 | public void getTraits() {
8 | System.out.println("General traits");
9 |
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/src/OOPSImplementation/Father.java:
--------------------------------------------------------------------------------
1 | package OOPSImplementation;
2 |
3 | public interface Father {
4 |
5 | void getTraits();
6 | }
7 |
--------------------------------------------------------------------------------
/src/OOPSImplementation/Mother.java:
--------------------------------------------------------------------------------
1 | package OOPSImplementation;
2 |
3 | public interface Mother {
4 | void getTraits();
5 | }
6 |
--------------------------------------------------------------------------------
/src/OOPSImplementation/Shape.java:
--------------------------------------------------------------------------------
1 | package OOPSImplementation;
2 |
3 | public abstract class Shape {
4 |
5 | private int side;
6 |
7 | public Shape(int side) {
8 | this.setSide(side);
9 | }
10 |
11 | public abstract void area();
12 |
13 | public int getSide() {
14 | return side;
15 | }
16 |
17 | public void setSide(int side) {
18 | this.side = side;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/OOPSImplementation/Square.java:
--------------------------------------------------------------------------------
1 | package OOPSImplementation;
2 |
3 | public class Square extends Shape {
4 |
5 | Square() {
6 | super(4);
7 | }
8 |
9 | @Override
10 | public void area() {
11 | System.out.println("Area of square is side * side");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/OOPSImplementation/Triangle.java:
--------------------------------------------------------------------------------
1 | package OOPSImplementation;
2 |
3 | public class Triangle extends Shape {
4 |
5 | public Triangle() {
6 | super(3);
7 | }
8 |
9 | @Override
10 | public void area() {
11 | System.out.println("Area of triangle is 0.5 * base * height");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/OOPSImplementation/Utility.java:
--------------------------------------------------------------------------------
1 | package OOPSImplementation;
2 |
3 | public class Utility {
4 | static int a;
5 | public static void main(String[] args) {
6 |
7 | AcessModifiers obj = new AcessModifiers();
8 | obj.a = 20;
9 | // obj.b = 20;
10 | obj.printA();
11 |
12 | StringBuilder sb = new StringBuilder("Tanmay");
13 | sb.toString();
14 |
15 | Shape triangle = new Triangle();
16 | triangle.area();
17 |
18 | Square square = new Square();
19 | square.area();
20 |
21 | Vehicle car = new Car();
22 | car.start();
23 | car.accelerate(20);
24 | car.stop();
25 |
26 | Vehicle bike = new Bike();
27 |
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/OOPSImplementation/Vehicle.java:
--------------------------------------------------------------------------------
1 | package OOPSImplementation;
2 |
3 | public interface Vehicle {
4 |
5 | int MAX_SPEED = 200;
6 |
7 | void start();
8 | void stop();
9 | void accelerate(int speed);
10 | void brake();
11 | }
12 |
--------------------------------------------------------------------------------
/src/Queue/Basics.java:
--------------------------------------------------------------------------------
1 | package Queue;
2 |
3 | import java.util.*;
4 |
5 | public class Basics {
6 |
7 | public static void main(String[] args) {
8 | // Queue q = new Queue<>();
9 | Queue q = new LinkedList<>();
10 | Queue q2 = new ArrayDeque<>();
11 |
12 | q.add(1);
13 | q.add(2);
14 | q.add(3);
15 | q.add(4);
16 |
17 | System.out.println(q);
18 |
19 | q.remove();
20 | System.out.println(q);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/Queue/Questions.java:
--------------------------------------------------------------------------------
1 | package Queue;
2 |
3 | import java.util.ArrayDeque;
4 | import java.util.Deque;
5 |
6 | public class Questions {
7 |
8 | public static void maxOfkSizedWindow(int arr[], int k) {
9 |
10 | int start = 0, end = 0;
11 | Deque q = new ArrayDeque<>();
12 |
13 | while(end < arr.length) {
14 |
15 | // remove smaller elements from end
16 | while(!q.isEmpty() && q.peekLast() < arr[end])
17 | q.removeLast();
18 |
19 | // add the element at end in the deque
20 | q.addLast(arr[end]);
21 |
22 | if(end - start + 1 < k) {
23 | end++;
24 | } else {
25 | System.out.println(q.peekFirst());
26 |
27 | if(q.peekFirst() == arr[start])
28 | q.removeFirst();
29 |
30 | start++;
31 | end++;
32 | }
33 |
34 |
35 | }
36 | }
37 |
38 | public static void main(String[] args) {
39 | maxOfkSizedWindow(new int[] {1, 3, -1, -3, 5, 3, 6, 7}, 3);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/Recursion/Basics.java:
--------------------------------------------------------------------------------
1 | package Recursion;
2 |
3 | public class Basics {
4 |
5 | public static void printDecreasing(int n) {
6 |
7 | if(n == 0) return;
8 |
9 | System.out.println(n);
10 | printDecreasing(n - 1);
11 |
12 | }
13 |
14 | public static void printDI(int n) {
15 | if(n == 0) return;
16 |
17 | System.out.println(n);
18 | printDI(n - 1);
19 | System.out.println(n);
20 | }
21 |
22 | public static void main(String[] args) {
23 | // printDecreasing(10);
24 | printDI(5);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Recursion/Questions2.java:
--------------------------------------------------------------------------------
1 | package Recursion;
2 |
3 | public class Questions2 {
4 |
5 | public static int fact(int n) {
6 | if(n == 1) return 1;
7 |
8 | int factnm1 = fact(n-1);
9 | return n * factnm1;
10 | }
11 |
12 | public static int power(int a, int b) {
13 | if(b == 0) return 1;
14 |
15 | int p = power(a, b - 1);
16 | return a * p;
17 | }
18 |
19 | public static int powerOptimised(int a, int b) {
20 |
21 | if(b == 1) return a;
22 |
23 | int powerHalf = powerOptimised(a, b/2);
24 |
25 | if(b % 2 == 1) // odd
26 | return powerHalf * powerHalf * a;
27 | else return powerHalf * powerHalf;
28 | }
29 |
30 | public static int fibonacci(int n) {
31 |
32 | if(n == 0 || n == 1) return n;
33 |
34 | int fibnm1 = fibonacci(n - 1);
35 | int fibnm2 = fibonacci(n - 2);
36 |
37 | return fibnm1 + fibnm2;
38 |
39 | }
40 |
41 |
42 |
43 | public static void main(String[] args) {
44 |
45 | // System.out.println(fact(6));
46 | // System.out.println(power(10, 9));
47 | // System.out.println(powerOptimised(10, 9));
48 | System.out.println(fibonacci(50));
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/src/Recursion/Questions3.java:
--------------------------------------------------------------------------------
1 | package Recursion;
2 |
3 | public class Questions3 {
4 |
5 | public static void printReverse(int arr[], int idx) {
6 | if(idx == arr.length) return;
7 |
8 | printReverse(arr, idx + 1);
9 | System.out.println(arr[idx]);
10 | }
11 |
12 |
13 | public static int max(int arr[], int idx) {
14 | if(idx == arr.length - 1)
15 | return arr[idx];
16 |
17 | int maxAhead = max(arr, idx + 1);
18 | return Math.max(maxAhead, arr[idx]);
19 | }
20 |
21 | public static int lastIndex(int arr[], int val, int idx) {
22 | if(idx == arr.length) return -1;
23 |
24 | int ansFromArrayStartingAtNextIdx = lastIndex(arr, val, idx + 1);
25 |
26 | if(ansFromArrayStartingAtNextIdx == -1) { // val was not present
27 | if(arr[idx] == val) return idx; // found val at idx
28 | else return -1; // didn't find any ans
29 | } else { // val is present
30 | return ansFromArrayStartingAtNextIdx;
31 | }
32 | }
33 |
34 |
35 | public static void main(String[] args) {
36 |
37 | int arr[] = {1,1,1,2,3,4,5,6,7,8};
38 | // printReverse(arr, 0);
39 | // System.out.println(max(arr, 0));
40 | System.out.println(lastIndex(arr, 1, 0));
41 |
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/Recursion/Questions4.java:
--------------------------------------------------------------------------------
1 | package Recursion;
2 |
3 | public class Questions4 {
4 |
5 | public static int firstIndex(int arr[], int val, int idx) {
6 |
7 | if(idx == arr.length) return -1;
8 |
9 | if(arr[idx] == val) return idx;
10 | else return firstIndex(arr, val, idx + 1);
11 | }
12 |
13 | public static int countMazePath(int maze[][], int row, int col) {
14 |
15 | // negative base case
16 | if(row >= maze.length ||col >= maze[0].length)
17 | return 0;
18 |
19 | // positive base case
20 | if(row == maze.length - 1 && col == maze[0].length - 1)
21 | return 1;
22 |
23 | int countFromRightBlock = countMazePath(maze, row, col + 1);
24 | int countFromBottomBlock = countMazePath(maze, row + 1, col);
25 |
26 | return countFromRightBlock + countFromBottomBlock;
27 |
28 | }
29 |
30 | public static void printMazePath(int maze[][], String psf, int row, int col)
31 | {
32 | // negative base case
33 | if(row >= maze.length ||col >= maze[0].length)
34 | return;
35 |
36 | // positive base case
37 | if(row == maze.length - 1 && col == maze[0].length - 1) {
38 | System.out.println(psf);
39 | return;
40 | }
41 |
42 | printMazePath(maze, psf + "R", row, col + 1);
43 | printMazePath(maze, psf + "D", row + 1, col);
44 | }
45 |
46 | public static void main(String[] args) {
47 | int arr[] = {1,1,1,2,3,4,5,6,7,8};
48 | // System.out.println(firstIndex(arr, 2, 0));
49 | int maze[][] = new int[3][3];
50 |
51 | // System.out.println(countMazePath(maze, 0, 0));
52 | printMazePath(maze, "", 0, 0);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/Recursion/Questions5.java:
--------------------------------------------------------------------------------
1 | package Recursion;
2 |
3 | public class Questions5 {
4 |
5 | public static int countStairPaths(int n) {
6 |
7 | // positive base case
8 | if(n == 0) return 1;
9 |
10 | // negative base case
11 | if(n < 0) return 0;
12 |
13 | int noOfWaysFromnm1 = countStairPaths(n - 1);
14 | int noOfWaysFromnm2 = countStairPaths(n - 2);
15 | int noOfWaysFromnm3 = countStairPaths(n - 3);
16 |
17 | return noOfWaysFromnm1 + noOfWaysFromnm2 + noOfWaysFromnm3;
18 | }
19 |
20 | public static void printStairPaths(int n, String psf) {
21 | if(n == 0){
22 | System.out.println(psf);
23 | return;
24 | }
25 | if(n < 0) return;
26 |
27 | printStairPaths(n - 1, psf + 1);
28 | printStairPaths(n - 2, psf + 2);
29 | printStairPaths(n - 3, psf + 3);
30 | }
31 |
32 | public static void toh(int n, char source, char destination, char helper) {
33 |
34 | if(n == 0) return;
35 |
36 | toh(n - 1, source, helper, destination);
37 | System.out.println("Move disk " + n + " from " + source + " to " + destination);
38 | toh(n - 1, helper, destination, source);
39 | }
40 |
41 |
42 | public static void main(String[] args) {
43 | // System.out.println(countStairPaths(8));
44 | // printStairPaths(8, "");
45 | toh(3, 'A', 'C', 'B');
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/Recursion/Recursion6.java:
--------------------------------------------------------------------------------
1 | package Recursion;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class Recursion6 {
6 |
7 | public static ArrayList getAllSubSequence(String s) {
8 |
9 | // base case
10 | if(s.isEmpty()) {
11 | ArrayList result = new ArrayList<>();
12 | result.add("");
13 | return result;
14 | }
15 |
16 | char firstChar = s.charAt(0);
17 | String rem = s.substring(1);
18 |
19 | ArrayList recResult = getAllSubSequence(rem);
20 | ArrayList myResult = new ArrayList();
21 |
22 | // add all the elements of recResult with empty string
23 | for(int i = 0; i < recResult.size(); i++) {
24 | String element = recResult.get(i);
25 | myResult.add("" + element);
26 | }
27 |
28 | // add all the elements of recResult with first char
29 | for(int i = 0; i < recResult.size(); i++) {
30 | String element = recResult.get(i);
31 | myResult.add(firstChar + element);
32 | }
33 |
34 | return myResult;
35 |
36 | }
37 |
38 | public static ArrayList getStairPaths(int n) {
39 | // positive basecase
40 | if(n == 0) {
41 | ArrayList result = new ArrayList<>();
42 | result.add("");
43 | return result;
44 | }
45 |
46 | //negative basecase
47 | if(n < 0) {
48 | return new ArrayList();
49 | }
50 |
51 | ArrayList list1stepdown = getStairPaths(n- 1);
52 | ArrayList list2stepdown = getStairPaths(n- 2);
53 | ArrayList list3stepdown = getStairPaths(n- 3);
54 |
55 | ArrayList myResult = new ArrayList();
56 |
57 | for(int i = 0; i < list1stepdown.size(); i++) {
58 | String path = list1stepdown.get(i);
59 | myResult.add(1 + path);
60 | }
61 |
62 | for(int i = 0; i < list2stepdown.size(); i++) {
63 | String path = list2stepdown.get(i);
64 | myResult.add(2 + path);
65 | }
66 |
67 | for(int i = 0; i < list3stepdown.size(); i++) {
68 | String path = list3stepdown.get(i);
69 | myResult.add(3 + path);
70 | }
71 |
72 | return myResult;
73 |
74 | }
75 |
76 | public static void main(String[] args) {
77 | // System.out.println(getAllSubSequence("abc"));
78 | System.out.println(getStairPaths(4));
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/src/Stack/Basics.java:
--------------------------------------------------------------------------------
1 | package Stack;
2 |
3 | import java.util.*;
4 |
5 | public class Basics {
6 |
7 | public static int evaluatePostfix(String exp) {
8 | Stack st = new Stack<>();
9 | String elements[] = exp.split(" ");
10 | for(String element: elements) {
11 | switch (element.charAt(0)) {
12 | case '+': {
13 | int oprnd2 = st.pop();
14 | int oprnd1 = st.pop();
15 | st.push(oprnd1 + oprnd2);
16 | break;
17 | }
18 | case '-': {
19 | int oprnd2 = st.pop();
20 | int oprnd1 = st.pop();
21 | st.push(oprnd1 - oprnd2);
22 | break;
23 | }
24 | case '/': {
25 | int oprnd2 = st.pop();
26 | int oprnd1 = st.pop();
27 | st.push(oprnd1 / oprnd2);
28 | break;
29 | }
30 | case '*': {
31 | int oprnd2 = st.pop();
32 | int oprnd1 = st.pop();
33 | st.push(oprnd1 * oprnd2);
34 | break;
35 | }
36 |
37 | default:
38 | st.push(Integer.parseInt(element));
39 | }
40 | }
41 | return st.peek();
42 | }
43 |
44 | public static boolean hasDuplicateBrackets(String exp) {
45 |
46 | Stack st = new Stack<>();
47 |
48 | for(int i = 0; i < exp.length(); i++) {
49 | char currentChar = exp.charAt(i);
50 |
51 | if(currentChar == ')') {
52 | if(st.peek() == '(') return true;
53 | else {
54 | while(st.peek() != '(')
55 | st.pop();
56 |
57 | // remove one opening bracket
58 | st.pop();
59 | }
60 |
61 | } else st.push(currentChar);
62 | }
63 |
64 | return false;
65 | }
66 |
67 |
68 | public static boolean hasBalancedBrackets(String exp) {
69 | Stack st = new Stack<>();
70 |
71 | for(int i = 0; i < exp.length(); i++) {
72 | char currentChar = exp.charAt(i);
73 |
74 | if(currentChar == '(' || currentChar == '[' || currentChar == '{') {
75 | st.push(currentChar);
76 | } else if(currentChar == ')') {
77 | if(isComplimentry(st, '(') == false) return false;
78 | } else if(currentChar == ']') {
79 | if(isComplimentry(st, '[') == false) return false;
80 | } else if(currentChar == '}') {
81 | if(isComplimentry(st, '{') == false) return false;
82 | }
83 | }
84 | if(!st.isEmpty()) return false;
85 | return true;
86 | }
87 |
88 | public static boolean isComplimentry(Stack st, char bracket) {
89 | if(st.isEmpty() || st.peek() != bracket) return false;
90 | st.pop();
91 | return true;
92 | }
93 |
94 |
95 |
96 |
97 | public static void main(String[] args) {
98 | // Stack st = new Stack<>();
99 | //
100 | // st.push(12);
101 | // st.push(1);
102 | // st.push(2);
103 | // st.push(3);
104 | //
105 | // System.out.println(st);
106 | //
107 | // System.out.println(st.pop());
108 | // System.out.println(st);
109 | // System.out.println(st.peek());
110 | // System.out.println(st.size());
111 | // System.out.println(st.isEmpty());
112 | // st.clear();
113 | // System.out.println(st.size());
114 |
115 | // System.out.println(hasDuplicateBrackets("((a+b)) = c"));
116 | // System.out.println(hasBalancedBrackets("{(a+b)+[c + (d+e)]}"));
117 | // System.out.println(evaluatePostfix("4 13 5 / +"));
118 |
119 | Scanner scn = new Scanner(System.in);
120 | int n = scn.nextInt();
121 | String elements[] = new String[n];
122 | for(int i = 0; i < n; i++)
123 | elements[i] = scn.next();
124 | Stack st = new Stack<>();
125 |
126 | for(String element: elements) {
127 | System.out.println(element + " * ");
128 | switch (element.charAt(0)) {
129 | case '+': {
130 | int oprnd2 = st.pop();
131 | int oprnd1 = st.pop();
132 | st.push(oprnd1 + oprnd2);
133 | break;
134 | }
135 | case '-': {
136 | int oprnd2 = st.pop();
137 | int oprnd1 = st.pop();
138 | st.push(oprnd1 - oprnd2);
139 | break;
140 | }
141 | case '/': {
142 | int oprnd2 = st.pop();
143 | int oprnd1 = st.pop();
144 | st.push(oprnd1 / oprnd2);
145 | break;
146 | }
147 | case '*': {
148 | int oprnd2 = st.pop();
149 | int oprnd1 = st.pop();
150 | st.push(oprnd1 * oprnd2);
151 | break;
152 | }
153 |
154 | default:
155 | st.push(Integer.parseInt(element));
156 | }
157 | }
158 | System.out.println(st.peek());
159 |
160 | }
161 |
162 | }
163 |
--------------------------------------------------------------------------------
/src/Stack/Questions.java:
--------------------------------------------------------------------------------
1 | package Stack;
2 |
3 | import java.util.*;
4 |
5 | public class Questions {
6 |
7 | public static int[] nextGreaterElement(int arr[]) {
8 |
9 | int res[] = new int[arr.length];
10 | Stack st = new Stack();
11 |
12 | res[res.length - 1] = -1;
13 | st.push(arr[arr.length - 1]);
14 |
15 | for(int i = arr.length - 2; i >= 0; i--) {
16 | while(!st.isEmpty() && st.peek() < arr[i]) {
17 | st.pop();
18 | }
19 | if(st.isEmpty()) res[i] = -1;
20 | else res[i] = st.peek();
21 |
22 | st.push(arr[i]);
23 | }
24 | return res;
25 | }
26 |
27 | public static int largestAreaHistogram(int arr[]) {
28 |
29 | int maxArea = Integer.MIN_VALUE;
30 | Stack st = new Stack<>();
31 | st.push(-1);
32 |
33 | for(int i = 0; i < arr.length; i++) {
34 | while(st.peek() != -1 && arr[st.peek()] >= arr[i]) {
35 | int idxPoped = st.pop();
36 | int area = (i - st.peek() - 1) * arr[idxPoped];
37 | maxArea = Math.max(maxArea, area);
38 | }
39 | st.push(i);
40 | }
41 |
42 | while(st.peek() != -1) {
43 | int idxPoped = st.pop();
44 | int area = (arr.length - st.peek() - 1) * arr[idxPoped];
45 | maxArea = Math.max(maxArea, area);
46 | }
47 |
48 | return maxArea;
49 | }
50 |
51 | public static void main(String[] args) {
52 | int arr[] = {2,1,5,3,4,6,1,3};
53 | System.out.println(largestAreaHistogram(arr));
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/Strings/Intro.java:
--------------------------------------------------------------------------------
1 | package Strings;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Intro {
6 | public static void main(String[] args) {
7 | Scanner scn = new Scanner(System.in);
8 | // String s1 = scn.next();
9 | // String s2 = scn.next();
10 | // String s3 = scn.nextLine();
11 |
12 |
13 | // System.out.println(s1);
14 | // System.out.println(s2);
15 | // System.out.println(s3);
16 | //
17 | // System.out.println(s3.charAt(5));
18 | //
19 | // System.out.println(s3.substring(1, 8));
20 | // System.out.println(s3.substring(1,1));
21 | // System.out.println(s3.substring(1));
22 | // System.out.println(s3.substring(1, s3.length()));
23 | // System.out.println(s3.substring(3, s3.length() + 1));
24 |
25 | System.out.println("Hello" + " " + "World" + 9);
26 | System.out.println(10 + 20 + "Hello" + 10 + 20);
27 |
28 | String s = "Hey How are you";
29 |
30 | char arr[] = s.toCharArray();
31 | for(int i = 0; i < arr.length; i++)
32 | System.out.println(arr[i]);
33 |
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/Strings/Questions.java:
--------------------------------------------------------------------------------
1 | package Strings;
2 |
3 | public class Questions {
4 |
5 | public static void printAllSubstrings(String s) {
6 | for(int start = 0; start < s.length(); start++) {
7 |
8 | for(int end = start + 1; end <= s.length(); end++) {
9 | // print substring
10 | System.out.println(s.substring(start, end));
11 | }
12 | }
13 | }
14 |
15 | public static String toUpperCase(String s) {
16 |
17 | String res = "";
18 | for(int i = 0; i < s.length(); i++) {
19 | char ch = s.charAt(i);
20 | if(ch >= 'a' && ch <= 'z') {
21 | char upperCasechar = (char)(ch - ('a'- 'A'));
22 | res += upperCasechar;
23 | } else {
24 | res += ch;
25 | }
26 | }
27 | return res;
28 | }
29 |
30 | public static void main(String[] args) {
31 | String s = "Hello World";
32 | // printAllSubstrings(s);
33 |
34 | String arr[] = s.split("o");
35 | System.out.println(arr[0]);
36 | System.out.println(arr[1]);
37 | System.out.println(arr[2]);
38 | // System.out.println(toUpperCase(s));
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/basics/Conditionals.java:
--------------------------------------------------------------------------------
1 | package basics;
2 |
3 | public class Conditionals {
4 |
5 | public static void main(String[] args) {
6 |
7 | int a = 25;
8 |
9 | // if(a > 20) {
10 | // System.out.println("a is smaller than 20");
11 | // }
12 | // else {
13 | // System.out.println("a is greater than 20");
14 | // }
15 |
16 | // if(a > 0) {
17 | // System.out.println("a is positive");
18 | // } else if(a < 0){
19 | // System.out.println("a is negative");
20 | // } else {
21 | // System.out.println("a is 0");
22 | // }
23 |
24 | // nesting if
25 | // if(a > 20) {
26 | // System.out.println("inside 1st if block");
27 | // if (a > 30) {
28 | // System.out.println("inside 2nd if block");
29 | // }
30 | // else {
31 | //
32 | // }
33 | // } else {
34 | // if(a < 20) {
35 | //
36 | // }
37 | // }
38 |
39 | // if(a > 18) {
40 | // if(a > 60) {
41 | // System.out.println("Senior citizen");
42 | // } else {
43 | // System.out.println("Adult");
44 | // }
45 | // }else {
46 | // System.out.println("child");
47 | // }
48 |
49 | int day = 1;
50 |
51 | switch (day) {
52 | case 4: {
53 | System.out.println("Thursday");
54 | break;
55 | }
56 | case 5: {
57 | System.out.println("Friday");
58 | break;
59 | }
60 | case 6: {
61 | System.out.println("Saturday");
62 | break;
63 | }
64 | case 7: {
65 | System.out.println("Sunday");
66 | break;
67 | }
68 | case 1: {
69 | System.out.println("Monday");
70 | break;
71 | }
72 | case 2: {
73 | System.out.println("Tuesday");
74 | break;
75 | }
76 | case 3: {
77 | System.out.println("Wednesday");
78 | break;
79 | }
80 |
81 | default: {
82 | System.out.println("invalid day");
83 | }
84 | }
85 |
86 |
87 | if(day == 1) {
88 | System.out.println("Monday");
89 | } else if (day == 2) {
90 | System.out.println("Tuesday");
91 | }
92 | else if (day == 3) {
93 | System.out.println("Wednesday");
94 | }
95 | else if (day == 4) {
96 | System.out.println("Thursday");
97 | }
98 | else if (day == 5) {
99 | System.out.println("Friday");
100 | }
101 | else if (day == 6) {
102 | System.out.println("Saturday");
103 | }
104 | else if (day == 7) {
105 | System.out.println("Sunday");
106 | }
107 | else {
108 | System.out.println("invalid");
109 | }
110 |
111 |
112 |
113 | String s = a >= 18 ? "adult" : "child";
114 |
115 | System.out.println(s);
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 | }
135 |
136 | }
137 |
--------------------------------------------------------------------------------
/src/basics/Functions.java:
--------------------------------------------------------------------------------
1 | package basics;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Functions {
6 |
7 | public static int factorial(int n) {
8 | int nfact = 1;
9 |
10 | for(int i = 1; i <= n; i++) {
11 | nfact *= i;
12 | }
13 |
14 | return nfact;
15 | }
16 |
17 | public static int ncr(int n, int r) {
18 | int nfact = factorial(n);
19 | int rfact = factorial(r);
20 | int nmrfact = factorial(n-r);
21 |
22 | int ncr = nfact / (rfact * nmrfact);
23 | return ncr;
24 | }
25 |
26 | public static void main(String[] args) {
27 |
28 | Scanner scn = new Scanner(System.in);
29 | int n = scn.nextInt();
30 | int r = scn.nextInt();
31 |
32 |
33 | // int nfact = 1;
34 | //
35 | // for(int i = 1; i <= n; i++) {
36 | // nfact *= i;
37 | // }
38 | //
39 | // int rfact = 1;
40 | //
41 | // for(int i = 1; i <= r; i++) {
42 | // rfact *= i;
43 | // }
44 | //
45 | // int nmrfact = 1;
46 | //
47 | // for(int i = 1; i <= n-r; i++) {
48 | // nmrfact *= i;
49 | // }
50 | //
51 | // int ncr = nfact / (rfact * nmrfact);
52 |
53 | System.out.println(ncr(n, r));
54 |
55 |
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/src/basics/Loops.java:
--------------------------------------------------------------------------------
1 | package basics;
2 |
3 | public class Loops {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 |
8 | // for(int i = 1; i <= 10; i++) {
9 | //
10 | // if(i == 5) {
11 | // break;
12 | // }
13 | // System.out.println(i);
14 | // }
15 |
16 | // int i = 1;
17 | // while (i <= 10) {
18 | //
19 | // System.out.println(i);
20 | // i++;
21 | //
22 | // }
23 | int i = 1;
24 | do {
25 | System.out.println(i);
26 | i++;
27 | } while(i < 5);
28 |
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/basics/Patterns.java:
--------------------------------------------------------------------------------
1 | package basics;
2 |
3 | import java.util.*;
4 |
5 | public class Patterns {
6 | public static void main(String[] args) {
7 |
8 |
9 | Scanner scn = new Scanner(System.in);
10 | int n = scn.nextInt();
11 |
12 | for(int row = 1; row <= n; row++) {
13 | // print each row
14 | for(int i = 1; i <= row; i++) {
15 | System.out.print("*");
16 | }
17 |
18 | // enter new line
19 | System.out.println();
20 |
21 | }
22 |
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/basics/TypeCasting.java:
--------------------------------------------------------------------------------
1 | package basics;
2 |
3 | public class TypeCasting {
4 |
5 | public static void main(String[] args) {
6 | int a = 65;
7 | // type casting
8 | byte b = (byte)a;
9 |
10 | // type conversion
11 | long c = a;
12 |
13 | char ch = (char)a;
14 | System.out.println(ch);
15 |
16 | String s = "";
17 |
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/basics/doubts.java:
--------------------------------------------------------------------------------
1 | package basics;
2 |
3 | public class doubts {
4 |
5 | public static int reverse(int n) {
6 | int ans = 0;
7 | while(n > 0) {
8 | int rem = n % 10;
9 | ans = ans * 10 + rem;
10 | n /= 10;
11 | }
12 |
13 |
14 | return ans;
15 | }
16 |
17 | public static void main(String[] args) {
18 |
19 | reverse(122);
20 | System.out.println(reverse(12345678));
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------