Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions ArraysSolutions/Find All Duplicates in an Array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
## Solution of Find All Duplicates in an Array

### First Approach

#### Time Complexity - O (nlogn)

#### Space Complexity - O(1)

```java

class Solution {

public List<Integer> findDuplicates(int[] nums) {

List <Integer> list = new ArrayList <Integer>();

int start=0;

int ref;

Arrays.sort(nums);

for (int i = 0 ; i < nums.length-1 ; i++){

ref = nums[i] ^ nums[i+1];

if(ref == 0 ){

list.add(start,nums[i]);

start++;

}

}

return list;

}

}

```
### Second Approach

#### Time Complexity - O (n)

#### Space Complexity - O(1)

```java
class Solution {
public List<Integer> findDuplicates(int[] nums) {
List <Integer> list = new ArrayList <Integer>();
int n = nums.length;
int index;
for (int i = 0 ; i < n; i++){
int num = nums[i];
index = Math.abs(num) - 1 ;
if ( nums[index] < 0){
list.add(Math.abs(nums[i]));
}
else{
nums[index] = nums[index] * -1;
}
}
return list;
}
}

```

<div align="right">
Contributed By <a href="https://github.com/rath23"> Muneer Ahmad</a>
</div>
60 changes: 60 additions & 0 deletions ArraysSolutions/Intersection-of-two-arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
## Solution of Insertion of two Array
## Approach First
#### Time Complexity - O (n)
#### Space Complexity - O(n)

```java
class Solution {
public int [] intersection(int[] nums1, int[] nums2) {
HashSet <Integer> hs = new HashSet <Integer>();
HashSet <Integer> hs1 = new HashSet<Integer>();
for(int i = 0 ; i < nums1.length ;i ++){
hs1.add(nums1[i]);
}

for (int j = 0 ; j < nums2.length ; j++){
if(hs1.contains(nums2[j])){
hs.add(nums2[j]);
}
}

int arr[] = new int [hs.size()];
int l = 0;
for(Integer a : hs){
arr[l++]=a;
}

return arr;
}
}
```

### Second Approach
## Time complexity : O(n^2)
## Space complexity : O(n)

```java
class Solution {
public int [] intersection(int[] nums1, int[] nums2) {
HashSet <Integer> hs = new HashSet <Integer>();

for(int i = 0 ; i < nums1.length ;i ++){
for (int j = 0 ; j < nums2.length ; j++){
if(nums1[i]== nums2[j]){
hs.add(nums2[j]);
}
}
}
int arr[] = new int [hs.size()];
int l = 0;
for(Integer a : hs){
arr[l++]=a;
}

return arr;
}
}
```
<div align="right">
Contributed By <a href="https://github.com/rath23"> Muneer Ahmad</a>
</div>
21 changes: 21 additions & 0 deletions ArraysSolutions/Missing Number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

## Solution of Missing-Number
#### Time Complexity - O (n)
#### Space Complexity - O(1)

```java
class Solution {
public int missingNumber(int[] nums) {
int num = nums.length;
int sunOfNaturalNumbers = num*(num+1)/2;
int sumOfArrayNum = 0;
for(int i = 0; i<num ; i++){
sumOfArrayNum += nums[i];
}
return sunOfNaturalNumbers-sumOfArrayNum;
}
}
```
<div align="right">
Contributed By <a href="https://github.com/rath23"> Muneer Ahmad</a>
</div>
37 changes: 37 additions & 0 deletions ArraysSolutions/Move-zeroes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Solution of Move-zeroes
#### Time Complexity - O (n)
#### Space Complexity - O(1)

```java
class Solution {
public void moveZeroes(int[] nums) {
int start = 0;
int end = start+1;
while (end <= nums.length-1){
int temp;
if(nums[start] == 0 && nums[end]!=0){
temp = nums[start];
nums[start] = nums[end];
nums[end]= temp;
start++;
end++;
continue;
}

if(nums[start]!= 0 && nums[end]== 0 || nums[start]!=0 && nums[end]!= 0){
start++;
end++;
continue;
}

if (nums[start] == 0 && nums[end]==0){
end++;
}
}

}
}
```
<div align="right">
Contributed By <a href="https://github.com/rath23"> Muneer Ahmad</a>
</div>
21 changes: 21 additions & 0 deletions ArraysSolutions/Remove-duplicates-from-sorted-array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Solution of Remove duplicates from sorted array
#### Time Complexity - O (n)
#### Space Complexity - O(1)

```java
class Solution {
public int removeDuplicates(int[] nums) {
int result=1;
for (int i = 0 ; i < nums.length-1 ; i++){
if (nums[i] != nums[i+1]){
nums[result]=nums[i+1];
result++;
}
}
return result ;
}
}
```
<div align="right">
Contributed By <a href="https://github.com/rath23"> Muneer Ahmad</a>
</div>
22 changes: 22 additions & 0 deletions ArraysSolutions/Remove-element.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Solution of Remove Element
#### Time Complexity - O (n)
#### Space Complexity - O(n)

```java
class Solution {
public int removeElement(int[] nums, int val) {
int result=0;
int arr [] = nums;
for(int i = 0 ; i < arr.length ; i++){
if(arr[i]!= val){
nums[result] = arr[i];
result++;
}
}
return result;
}
}
```
<div align="right">
Contributed By <a href="https://github.com/rath23"> Muneer Ahmad</a>
</div>
57 changes: 57 additions & 0 deletions ArraysSolutions/Removing-minimum-and-maximum-from-array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## Solution of Removing-minimum-and-maximum-from-array
#### Time Complexity - O (n)
#### Space Complexity - O(1)

```java
class Solution {
public int minimumDeletions(int[] nums) {
int arrayLength = nums.length;
int max=nums[0];
int maxIndex = 0;
int min=nums[0];
int minIndex = 0;

for (int i = 0 ; i < nums.length; i++){
if (max<nums[i]){
max = nums[i] ;
maxIndex = i;
}
if (min>nums[i]){
min = nums[i] ;
minIndex=i;
}
}
int c1,c2,c3;
if(minIndex > maxIndex){
c1 = (maxIndex +1) + (arrayLength - minIndex);
c2 = minIndex + 1;
c3 = arrayLength - maxIndex;
}

else if(maxIndex > minIndex){
c1 = (minIndex +1) + (arrayLength - maxIndex);
c2 = maxIndex + 1;
c3 = arrayLength - minIndex;
}
else {
c1=c2=c3=maxIndex+1;
}

if ( c1 < c2 && c1 < c3 ){
return c1;
}
else if (c2 < c1 && c2 < c3){
return c2;
}
else if (c3 < c1 && c3 < c2){
return c3;
}
else {
return c2;
}
}
}
```
<div align="right">
Contributed By <a href="https://github.com/rath23"> Muneer Ahmad</a>
</div>
22 changes: 22 additions & 0 deletions ArraysSolutions/Reverse an Array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Solution of Reverse an Array
#### Time Complexity - O (n)
#### Space Complexity - O(1)

```java
class Solution {
public void reverseString(char[] s) {
int start = 0;
int end = s.length-1;
while ( start < end){
char temp = s[start];
s[start] = s[end];
s[end]= temp;
start++;
end--;
}
}
}
```
<div align="right">
Contributed By <a href="https://github.com/rath23"> Muneer Ahmad</a>
</div>
28 changes: 28 additions & 0 deletions ArraysSolutions/Rotate-Array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Solution of Rotate-Array
#### Time Complexity - O (n)
#### Space Complexity - O(1)

```java
class Solution {
public static void reverse(int [] a , int start , int end ){
while (start <end){
int temp = a[start];
a[start]=a[end];
a[end] = temp;
start++;
end--;
}
}

public void rotate(int[] nums, int k) {
int n = nums.length;
k = k%n;
reverse(nums,(n-k),(n-1));
reverse(nums,0,(n-k-1));
reverse(nums,0,(n-1));
}
}
```
<div align="right">
Contributed By <a href="https://github.com/rath23"> Muneer Ahmad</a>
</div>
24 changes: 24 additions & 0 deletions ArraysSolutions/Subarray-sum-equals-k.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Subarray Sum Equals K
#### Time Complexity - O (n^2)
#### Space Complexity - O(1)

```java
class Solution {
public int subarraySum(int[] nums, int k) {
int count = 0;
for(int i = 0 ; i < nums.length ; i++){
int sum = 0;
for (int j = i ; j < nums.length ; j++){
sum+=nums[j];
if (sum == k){
count++;
}
}
}
return count++;
}
}
```
<div align="right">
Contributed By <a href="https://github.com/rath23"> Muneer Ahmad</a>
</div>
Loading