-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'abhishektripathi66:master' into FirstDuplicateFinder
- Loading branch information
Showing
16 changed files
with
317 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ The interview related questions and its solution from different coding websites | |
|
||
- 👋 Hi, I’m Abhishek Tripathi(@abhishektripathi66) | ||
- 💞️ I’m looking to collaborate on DSA | ||
- 📫 How to reach me (mail @ [email protected]) | ||
- 📫 How to reach me [mail]([email protected]) | ||
|
||
This is the structure of the Project where we would be able to find the required solutions and even add the new solutions at the same place. | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...reath First Search/BreathFirstSearch.java → ...adth First Search/BreadthFirstSearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Linear search is one of the simplest searching algorithms. | ||
It works by sequentially checking each element of a list (or array) until the desired element is found or the list is exhausted. | ||
#Algorithm: | ||
-> Start from the first element of the list. | ||
-> Compare the current element with the target element. | ||
-> If the element is found, return its index. | ||
-> If the element is not found, move to the next element and repeat the comparison. | ||
->If the end of the list is reached without finding the element, return a value indicating that the element is not in the list (commonly -1). | ||
#Time Complexity: | ||
Best Case: O(1) (if the element is the first one) | ||
Worst Case: O(n) (if the element is the last one or not found at all) | ||
#Space Complexity: O(1) (since no extra space is used apart from variables) | ||
#Pros: | ||
-> Simple and easy to implement. | ||
-> No need for sorting the array. | ||
#Cons: | ||
-> Inefficient for large datasets because it checks each element one by one. | ||
*/ | ||
|
||
class LinearSearch{ | ||
public static void main(String[] args){ | ||
int[] arr = {11,54,34,78,33,100,543,76,3,889,56}; | ||
int target = 33; | ||
boolean flag = false; | ||
for(int i=0; i<arr.length; i++){ | ||
if(arr[i] == target){ | ||
System.out.println("Element found at index: "+i); | ||
flag = true; | ||
} | ||
} | ||
if(flag == false){ | ||
System.out.println("Element not found"); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/Coding Questions/Leetcode/MedianoftwoSortedarrays.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/*4. Median of Two Sorted Arrays | ||
Hard | ||
Topics | ||
Companies | ||
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. | ||
The overall run time complexity should be O(log (m+n)). | ||
Example 1: | ||
Input: nums1 = [1,3], nums2 = [2] | ||
Output: 2.00000 | ||
Explanation: merged array = [1,2,3] and median is 2. | ||
Example 2: | ||
Input: nums1 = [1,2], nums2 = [3,4] | ||
Output: 2.50000 | ||
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. | ||
Constraints: | ||
nums1.length == m | ||
nums2.length == n | ||
0 <= m <= 1000*/ | ||
|
||
/*-Intuition | ||
The goal of the problem is to find the median of two sorted arrays. A naive approach would be to combine both arrays into one, sort the combined array, and then find the median. | ||
The intuition behind this solution is straightforward: since the input arrays are sorted, combining and sorting them ensures we have a unified sorted array from which the median | ||
can be easily calculated. | ||
-Approach | ||
*Combine both arrays into one by copying the elements of both nums1 and nums2 into a new array nums. | ||
*Sort the merged array nums. | ||
*If the length of the array is odd, the median is the middle element. If it's even, the median is the average of the two middle elements. | ||
Return the computed median value.*/ | ||
|
||
//Code- | ||
import java.util.*; | ||
class Solution { | ||
public double findMedianSortedArrays(int[] nums1, int[] nums2) { | ||
int nums[] = new int[nums1.length + nums2.length]; | ||
for(int i=0; i<nums1.length; i++){ | ||
nums[i] = nums1[i]; | ||
} | ||
for(int i=0; i<nums2.length; i++){ | ||
nums[i + nums1.length] = nums2[i]; | ||
} | ||
Arrays.sort(nums); | ||
double x = 0; | ||
if(nums.length%2==0){ | ||
double w = nums[nums.length/2 - 1]; | ||
double y = nums[nums.length/2]; | ||
x = (w+y)/2; | ||
} | ||
else{ | ||
x = nums[nums.length/2]; | ||
} | ||
return x; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main; | ||
|
||
import main.stack.Stack; | ||
|
||
public class Main { | ||
public static void main(String[] args) throws Exception { | ||
Stack s = new Stack(5); | ||
s.push(2); | ||
|
||
System.out.println(s.showStackContent()); | ||
|
||
s.push(3); | ||
|
||
System.out.println(s.showStackContent()); | ||
|
||
s.push(4); | ||
|
||
System.out.println(s.showStackContent()); | ||
|
||
s.push(5); | ||
|
||
System.out.println(s.showStackContent()); | ||
|
||
s.push(6); | ||
|
||
System.out.println(s.isFull()); | ||
|
||
// s.push(7); | ||
|
||
// s.pop(); | ||
s.pop(); | ||
s.pop(); | ||
s.pop(); | ||
s.pop(); | ||
s.pop(); | ||
|
||
System.out.println(s.peek()); | ||
|
||
System.out.println(s.showStackContent()); | ||
System.out.println(s.isEmpty()); | ||
|
||
// s.pop(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Data Structures/Stack/main/exceptions/EmptyStackException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main.exceptions; | ||
|
||
public class EmptyStackException extends Exception{ | ||
private String message; | ||
|
||
public EmptyStackException() { | ||
this.message = "Stack is empty!"; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/Data Structures/Stack/main/exceptions/StackOverflowException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main.exceptions; | ||
|
||
public class StackOverflowException extends Exception { | ||
private String message; | ||
|
||
public StackOverflowException() { | ||
message = "Stack overflow has occurred, max capacity is reached!"; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Data Structures/Stack/main/exceptions/StackUnderflowException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main.exceptions; | ||
|
||
public class StackUnderflowException extends Exception{ | ||
private String message; | ||
|
||
public StackUnderflowException() { | ||
this.message = "You can't pop from an empty stack!"; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main.stack; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import main.exceptions.EmptyStackException; | ||
import main.exceptions.StackOverflowException; | ||
import main.exceptions.StackUnderflowException; | ||
|
||
public class Stack { | ||
private List<Integer> stackContent; | ||
private int usedCapacity; | ||
private int maxCapacity; | ||
|
||
public Stack(int size) { | ||
this.stackContent = new LinkedList<Integer>(); | ||
this.usedCapacity = 0; | ||
this.maxCapacity = size; | ||
} | ||
|
||
public void push(Integer val) throws Exception { | ||
if (this.maxCapacity > this.usedCapacity) { | ||
this.stackContent.add(val); | ||
this.usedCapacity += 1; | ||
} else { | ||
throw new StackOverflowException(); | ||
} | ||
} | ||
|
||
public Integer pop() throws StackUnderflowException { | ||
Integer popped = -1; | ||
if (this.usedCapacity <= 0) { | ||
throw new StackUnderflowException(); | ||
} else { | ||
Integer elementForRemoval = this.stackContent.get(usedCapacity - 1); | ||
if (elementForRemoval != null) { | ||
popped = elementForRemoval; | ||
this.stackContent.remove(usedCapacity - 1); | ||
this.usedCapacity -= 1; | ||
} | ||
} | ||
|
||
return popped; | ||
} | ||
|
||
public boolean isFull() { | ||
System.out.println(usedCapacity + "vs" + maxCapacity); | ||
return this.usedCapacity == this.maxCapacity; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return this.usedCapacity == 0; | ||
} | ||
|
||
public String showStackContent() throws EmptyStackException { | ||
StringBuilder sb = new StringBuilder(); | ||
if (!this.isEmpty()) { | ||
sb.append("Bottom of the stack --> |"); | ||
for (int i = 0; i < this.usedCapacity; i++) { | ||
sb.append(stackContent.get(i).toString() + "|"); | ||
} | ||
sb.append(" <-- Top of the stack"); | ||
} else { | ||
throw new EmptyStackException(); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
public int peek() throws EmptyStackException { | ||
if (!this.isEmpty()) { | ||
return this.stackContent.get(this.usedCapacity - 1); | ||
} else { | ||
throw new EmptyStackException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- STACK --- | ||
- A Stack is a linear data structure that follows the "LIFO" prinicple, in other words Last In First Out. | ||
That means that the element that was first added to the stack will be the last element removed, and also | ||
that the last element added to the stack will be the first element removed. | ||
- Stacks can be easily and efficiently implemented using a Linked list data structure because of the time | ||
efficiency of removing from the end of the list and adding to it. | ||
- When initializing a stack we will specify the capacity it has. | ||
- Stack operations: | ||
- push(type val), stacks push operation will add an element that is passed as an argument to the method | ||
to the top of the stack, time efficiency of this operation is O(1)/constant (When an operation is done | ||
in constant time that means that the time needed to execute an operation isn't depending on the size of | ||
the stack itself.). If we tried to push an element to a stack that has reached full capacity we will | ||
get a StackOverflow Exception. | ||
- pop(), if the stack is not empty it will remove the element from the top and also return it to the | ||
caller of the method. If by any means a pop method is callled on a empty stack, a StackUnderflow Exception | ||
will be thrown. This method is also done in constant time! | ||
- peek(), another constant time stack operation that will return the element from the top of the | ||
stack but won't remove it as it's done by the pop operation. | ||
- isEmpty(), stack method that will check if the stack content is empty, and all of the stack capacity | ||
is available. If that is the case it will return a boolean value of true, in any other case it will | ||
return false. This method is implemented so that it's executed in constant time since we are tracking | ||
the usage of the capacity by modifying it after every pop operation where we will decrement the | ||
usedCapacity variable, and after every push operation where we will increment the previously mentioned | ||
variable. | ||
- isFull(), this method will check if the stacks maximum capacity has been reached, if that is the case | ||
a boolean value of true will be return, in any other case a false value will be returned. | ||
- Nice to know is that there is a special type of stack that is called a monotonic stack. A monotonic stack is | ||
a stack that is always organized in a specific order (increasingly or decreasingly). Below you will have a simple | ||
example of a monotonic stack. | ||
|
||
--- Increasing Monotonic Stack --- | ||
Bottom -> [1, 2, 3, 4, 5, 6, 8, 9, 100] <- Top | ||
|
||
--- Decreasing Monotonic Stack --- | ||
Bottom -> [100, 80, 76, 15, 3, 2, 1, 0] <- Top |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters