Skip to content

Commit

Permalink
index files added
Browse files Browse the repository at this point in the history
  • Loading branch information
Sahil-14 committed Feb 24, 2023
1 parent 9186233 commit eb415c4
Show file tree
Hide file tree
Showing 171 changed files with 2,994 additions and 472 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file added Array/.DS_Store
Binary file not shown.
4 changes: 3 additions & 1 deletion Array/ArrayProbs.java/aread.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
24.find sum query in specific range(h)
25.longest subarray with sum divisible by k (***)
26.next greater
27.next smaller
27.next smaller
28.29.array rearrangements
30.Minimum number of swaps required to sort an array
31 changes: 31 additions & 0 deletions Array/ArrayProbs.java/array_prob10.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import java.util.*;

//stock busy sell to maximum profit
// Solution structure
class Interval {
int buy, sell;
}

public class array_prob10 {
// at most 1 time
public static int maxProfit(int prices[]) {
int minPrice = Integer.MAX_VALUE;
int maxProfit = 0;
Expand Down Expand Up @@ -74,6 +77,34 @@ void stockBuySell(int price[], int n) {

}

void stockBuySellR(int price[], int n) {
if (n == 1)
return;

int count = 0;
ArrayList<Interval> list = new ArrayList<>();
int i = 0;
while (i < n - 1) {
while ((i < n - 1) && price[i] > price[i + 1]) {
i++;
}

if (i == n - 1) {
break;
}
Interval s = new Interval();
s.buy = i++;
while ((i < n) && price[i] > price[i - 1]) {
i++;
}

s.sell = i - 1;
list.add(s);
count++;

}
}

public static void main(String[] args) {
int arr[] = { 100, 180, 260, 310, 40, 535, 695 };
System.out.println(maxProfit(arr));
Expand Down
43 changes: 43 additions & 0 deletions Array/ArrayProbs.java/array_prob12.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,49 @@
//trappping rainwater

public class array_prob12 {

/**
* Time Complexity: O(N)
* Auxiliary Space: O(N)
*
*/

/**
* Brute Approach
* Traverse every array element and find the highest bars on the left and right
* sides. Take the smaller of two heights. The difference between the smaller
* height and the height of the current element is the amount of water that can
* be stored in this array element.
*/

public static int maxWater(int[] arr, int n) {

// To store the maximum water
// that can be stored
int res = 0;

// For every element of the array
// except first and last element
for (int i = 1; i < n - 1; i++) {

// Find maximum element on its left
int left = arr[i];
for (int j = 0; j < i; j++) {
left = Math.max(left, arr[j]);
}

// Find maximum element on its right
int right = arr[i];
for (int j = i + 1; j < n; j++) {
right = Math.max(right, arr[j]);
}

// Update maximum water value
res += Math.min(left, right) - arr[i];
}
return res;
}

public static int trapingRainwater(int arr[]) {
Stack<Integer> s = new Stack<>();
int ans = 0;
Expand Down
1 change: 1 addition & 0 deletions Array/ArrayProbs.java/array_prob16.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void productArray2(int arr[], int n) {
public static void main(String[] args) {
array_prob16 pa = new array_prob16();
int arr[] = { 10, 3, 5, 6, 2 };

int n = arr.length;
System.out.println("The product array is : ");
pa.productArray(arr, n);
Expand Down
97 changes: 31 additions & 66 deletions Array/ArrayProbs.java/array_prob17.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,76 +61,41 @@ public static List<List<Integer>> findTriplets(int[] nums, int sum) {
return pair;
}

public static void findTriplets2(int a[], int n, int sum) {
int i;

// Sort the input array
Arrays.sort(a);

// For handling the cases when no such
// triplets exits.
boolean flag = false;

// Iterate over the array from start to n-2.
for (i = 0; i < n - 2; i++) {
if (i == 0 || a[i] > a[i - 1]) {
// Index of the first element in
// remaining range.
int start = i + 1;

// Index of the last element
int end = n - 1;

// Setting our new target
int target = sum - a[i];

while (start < end) {
// Checking if current element
// is same as previous
if (start > i + 1
&& a[start] == a[start - 1]) {
start++;
continue;
}

// Checking if current element is
// same as previous
if (end < n - 1
&& a[end] == a[end + 1]) {
end--;
continue;
}
// target 0
public static List<List<Integer>> threeSum(int[] nums) {

// If we found the triplets then print it
// and set the flag
if (target == a[start] + a[end]) {
System.out.print("[" + a[i]
+ "," + a[start]
+ "," + a[end] + "] ");
flag = true;
start++;
end--;
}

// If target is greater then
// increment the start index
else if (target > (a[start] + a[end])) {
start++;
}

// If target is smaller than
// decrement the end index
else {
end--;
}
}
List<List<Integer>> ans = new ArrayList<List<Integer>>();
Arrays.sort(nums);
int n = nums.length;

for (int i = 0; i < n; i++) {
int p = i + 1, q = n - 1;
while (p < q) {
if (nums[p] + nums[q] == -nums[i]) { // System.out.println(p+" "+q);
List<Integer> t = new ArrayList<Integer>();
t.add(nums[i]);
t.add(nums[p]);
t.add(nums[q]);

ans.add(t);

while (p + 1 < q && nums[p + 1] == nums[p])
p++;
while (q - 1 > p && nums[q - 1] == nums[q])
q--;

p++;
q--;
} else if (nums[p] + nums[q] < -nums[i])
p++;
else
q--;
}
}

// If no such triplets found
if (flag == false) {
System.out.print("No Such Triplets Exist");
while (i + 1 < n && nums[i + 1] == nums[i])
i++;
}
return ans;
}

// Driver code
Expand Down
3 changes: 3 additions & 0 deletions Array/ArrayProbs.java/array_prob18.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public int compare(Interval i1, Interval i2) {
}
});




int index = 0; //
// i = 1
for (int i = 1; i < arr.length; i++) {
Expand Down
21 changes: 21 additions & 0 deletions Array/ArrayProbs.java/array_prob26.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
import java.util.Stack;

public class array_prob26 {

/*
* prints element and NGE pair for
* all elements of arr[] of size n
*
* O(n2)
*/
static void printNG1(int arr[], int n) {
int next, i, j;
for (i = 0; i < n; i++) {
next = -1;
for (j = i + 1; j < n; j++) {
if (arr[i] < arr[j]) {
next = arr[j];
break;
}
}
System.out.println(arr[i] + " -- " + next);
}
}

public static void nextGreater(int arr[], int n) {
int i = 0;
Stack<Integer> s = new Stack<>();
Expand Down
2 changes: 1 addition & 1 deletion Array/ArrayProbs.java/array_prob27.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void nextSmaller(int arr[], int n) {
next = -1;
// System.out.println(element + "-->" + next);
map.put(element, next);
}


for (i = 0; i < n; i++) {
System.out.println(arr[i] + " --> " + map.get(arr[i]));
Expand Down
Binary file added Array/ArrayProbs.java/array_prob28.class
Binary file not shown.
Loading

0 comments on commit eb415c4

Please sign in to comment.