Skip to content

Commit

Permalink
Merge branch 'abhishektripathi66:master' into FirstDuplicateFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
arraymahdi authored Feb 13, 2025
2 parents 99cfb0b + 2728bc6 commit 525f87c
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 19 deletions.
14 changes: 7 additions & 7 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ A clear and concise description of what you expected to happen.
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]
- OS: e.g. iOS
- Browser: e.g. chrome, safari
- Version: e.g. 1.1

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]
- Device: e.g. iPhone6
- OS: e.g. iOS8.1
- Browser: e.g. stock browser, safari
- Version: e.g. 22

**Additional context**
Add any other context about the problem here.
4 changes: 2 additions & 2 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ This code of conduct outlines our expectations for all those who participate in

We invite all those who participate in Dsa to help us create safe and positive experiences for everyone.

## 2. Open [Source/Culture/Tech] Citizenship
## 2. Open Source/Culture/Tech Citizenship

A supplemental goal of this Code of Conduct is to increase open [source/culture/tech] citizenship by encouraging participants to recognize and strengthen the relationships between our actions and their effects on our community.
A supplemental goal of this Code of Conduct is to increase open source/culture/tech citizenship by encouraging participants to recognize and strengthen the relationships between our actions and their effects on our community.

Communities mirror the societies in which they exist and positive action is essential to counteract the many forms of inequality and abuses of power that exist in society.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import java.util.*;
import java.util.Queue;

public class BreathFirstSearch {
public class BreadthFirstSearch {
// Graph adjacency list
private List<List<Integer>> adjList;

Expand Down
2 changes: 1 addition & 1 deletion src/Algorithms/FibonacciSearch/FibonacciSearch.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package FibonacciSearch;
// package FibonacciSearch;

public class FibonacciSearch {
public static int fibonacciSearch(int[] arr, int x){
Expand Down
Binary file added src/Algorithms/Linear Search/LinearSearch.class
Binary file not shown.
43 changes: 43 additions & 0 deletions src/Algorithms/Linear Search/LinearSearch.java
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 src/Coding Questions/Leetcode/MedianoftwoSortedarrays.java
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;
}
}

44 changes: 44 additions & 0 deletions src/Data Structures/Stack/main/Main.java
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 src/Data Structures/Stack/main/exceptions/EmptyStackException.java
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;
}

}
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;
}
}
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;
}

}
77 changes: 77 additions & 0 deletions src/Data Structures/Stack/main/stack/Stack.java
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();
}
}
}
35 changes: 35 additions & 0 deletions src/Data Structures/Stack/stack.txt
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
14 changes: 7 additions & 7 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
DSA repo
DSA repo\n

This repo is used for adding the DSA related stuffs
This repo is used for adding the DSA related stuffs\n

all the data structure related items are present in the data structure folder
all the algorithm related items are present in the Algorithm folder
The interview related questions and its solution from different coding websites are in the Coding Questions.
all the data structure related items are present in the data structure folder\n
all the algorithm related items are present in the Algorithm folder\n
The interview related questions and its solution from different coding websites are in the Coding Questions.\n

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.
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.\n

-Src
- Algorithm
Expand All @@ -24,4 +24,4 @@ This is the structure of the Project where we would be able to find the required
- Binary Tree
- Doubly Linked List
- Queue
- Singly Linked List
- Singly Linked List

0 comments on commit 525f87c

Please sign in to comment.