Skip to content

Commit ba0911d

Browse files
committed
#Modification 24
1 parent 7c19038 commit ba0911d

9 files changed

Lines changed: 286 additions & 8 deletions

arrays/Array_Problem_13.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
import java.util.*;
3+
4+
/* Problem Title :-> Kadane's Algo [V.V.V.V.V IMP] */
35
public class Array_Problem_13 {
6+
public static void main(String[] args) {
7+
@SuppressWarnings("resource")
8+
Scanner sc = new Scanner(System.in);
9+
int n = sc.nextInt();
10+
int[] a=new int[n];
11+
12+
for(int i=0;i<n;i++)
13+
a[i] = sc.nextInt();
14+
15+
int maxSum = 0;
16+
int currentSum = 0;
17+
18+
for(int i=0 ;i<n ;i++){
19+
currentSum += a[i];
20+
if(currentSum<0)
21+
currentSum=0;
22+
23+
maxSum = Math.max(maxSum, currentSum);
24+
}
25+
26+
System.out.println(maxSum);
27+
}
428
}

arrays/Array_Problem_14.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,58 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
import java.util.*;
3+
4+
/* Problem Title :-> Merge Intervals */
35
public class Array_Problem_14 {
6+
//The main function that takes a set of intervals, merges overlapping intervals & prints the result.
7+
public static void mergeIntervals(Interval[] a){
8+
//Test if the given set has at least one interval
9+
if(a.length <= 0)
10+
return;
11+
//Create an empty stack of intervals
12+
Stack<Interval> stack = new Stack<>();
13+
//sort the intervals in increasing order of start time
14+
Arrays.sort(a, new Comparator<>() {
15+
public int compare(Interval i1, Interval i2){
16+
return i1.start -i2.start;
17+
}
18+
});
19+
//push the first interval to stack
20+
stack.push(a[0]);
21+
//Start from the next interval and merge if necessary
22+
for(int i = 1; i < a.length; i++){
23+
//get interval from stack top
24+
Interval top = stack.peek();
25+
//if current interval is not overlapping with stack top,push it to the stack
26+
if(top.end < a[i].start)
27+
stack.push(a[i]);
28+
//Otherwise update the ending time of top if ending of current interval is more
29+
else if(top.end < a[i].end){
30+
top.end = a[i].end;
31+
stack.pop();
32+
stack.push(top);
33+
}
34+
}
35+
//Print contents of stack
36+
System.out.print("The Merged Intervals are: ");
37+
while(!stack.isEmpty()){
38+
Interval t = stack.pop();
39+
System.out. print("[" + t.start + "," + t.end + "] ");
40+
}
41+
}
42+
public static void main(String[] args) {
43+
Interval[] a = new Interval[4];
44+
a[0] = new Interval(6,8);
45+
a[1] = new Interval(1,9);
46+
a[2] = new Interval(2,4);
47+
a[3] = new Interval(4,7);
48+
mergeIntervals(a);
49+
}
50+
}
51+
52+
class Interval{
53+
int start, end;
54+
Interval(int start, int end){
55+
this.start = start;
56+
this.end = end;
57+
}
458
}

arrays/Array_Problem_15.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
/* Problem Title :-> Next Permutation */
33
public class Array_Problem_15 {
4+
5+
public static boolean nextPermutation(int[] numbs){
6+
int mark = -1;
7+
for(int i = numbs.length - 1; i > 0; i--){
8+
if(numbs[i] > numbs[i - 1]){
9+
mark = i - 1;
10+
break;
11+
}
12+
}
13+
if (mark == -1){
14+
reverse(numbs, 0, numbs.length-1);
15+
return false;
16+
}
17+
int idx = numbs.length - 1;
18+
for (int i = numbs.length-1; i >= mark + 1; i--){
19+
if(numbs[i] > numbs[mark]){
20+
idx = i;
21+
break;
22+
}
23+
}
24+
swap(numbs, mark, idx);
25+
reverse(numbs, mark + 1, numbs.length - 1);
26+
return true;
27+
}
28+
29+
private static void swap(int[] numbs, int i, int j){
30+
int t = numbs[i];
31+
numbs[i] = numbs[j];
32+
numbs[j] = t;
33+
}
34+
35+
private static void reverse(int[] numbs, int i, int j){
36+
while(i < j){
37+
swap(numbs, i, j);
38+
i++;
39+
j--;
40+
}
41+
}
42+
43+
public static void main(String[] args) {
44+
int[] numbs = new int[]{2, 3, 5, 7, 9, 4};
45+
System.out.print(nextPermutation(numbs));
46+
}
447
}

arrays/Array_Problem_16_i.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package arrays;
2+
/*
3+
* Problem Title :-> Count Inversion's in array By Method 1
4+
*/
5+
public class Array_Problem_16_i {
6+
static int[] a = new int[] {1, 20, 30, 6, 4, 5};
7+
8+
static int getInvCount(int n) {
9+
int inv_count = 0;
10+
for(int i = 0; i < n - 1; i++) {
11+
for(int j = i + 1; j < n; j++) {
12+
if(a[i] > a[j])
13+
inv_count++;
14+
}
15+
}
16+
return inv_count;
17+
}
18+
19+
// Driver method to test the above function
20+
public static void main(String[] args) {
21+
System.out.println("Number of inversions are " + getInvCount(a.length));
22+
}
23+
}

arrays/Array_Problem_16_ii.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package arrays;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
* Problem Title :-> Count Inversion's in array (By Method 2 : Using Enhance Merge Sort)
7+
*/
8+
public class Array_Problem_16_ii {
9+
10+
private static int mergeAndCount(int[] a, int l, int m, int r) {
11+
12+
// Left sub-array
13+
int[] left = Arrays.copyOfRange(a, l, m + 1);
14+
15+
// Right sub-array
16+
int[] right = Arrays.copyOfRange(a, m + 1, r + 1);
17+
18+
int i = 0, j = 0, k = 1, swaps = 0;
19+
20+
while(i < left.length && j < right.length) {
21+
if(left[i] <= right[j])
22+
a[k++] = left[i++];
23+
else {
24+
a[k++] = right[j++];
25+
swaps += (m + 1) - (l + i);
26+
}
27+
}
28+
29+
while(i < left.length)
30+
a[k++] = left[i++];
31+
32+
while(j < right.length)
33+
a[k++] = right[j++];
34+
35+
return swaps;
36+
}
37+
38+
private static int mergeSortAndCount(int[] a, int l, int r) {
39+
// keeps track of the inversion count at a particular node of the recursion tree
40+
int count = 0;
41+
42+
if(l < r) {
43+
int m = (l + r)/2;
44+
// Total inversion count = left sub-array count + right sub-array count + merge count
45+
// Left sub-array count
46+
count += mergeSortAndCount(a, l, m);
47+
// right sub-array count
48+
count += mergeSortAndCount(a, m + 1, r);
49+
// Merge count
50+
count += mergeAndCount(a, l, m, r);
51+
}
52+
return count;
53+
}
54+
55+
public static void main(String[] args) {
56+
int[] a = {1, 20, 6, 4, 5};
57+
System.out.println(mergeSortAndCount(a, 0, a.length - 1));
58+
}
59+
60+
}

arrays/Array_Problem_31.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
/*
3+
* Problem Title :-> Maximum profit by buying and selling a share at-most twice
4+
*/
35
public class Array_Problem_31 {
6+
7+
static int maxProfit(int price[], int n) {
8+
9+
int profit[] = new int[n];
10+
11+
for(int i =0; i < n; i++) {
12+
profit[i] = 0;
13+
}
14+
15+
int max_price = price[n - 1];
16+
17+
for(int i = n - 2; i >= 0; i--) {
18+
if(price[i] > max_price)
19+
max_price = price[i];
20+
21+
profit[i] = Math.max(profit[i + 1], max_price - price[i]);
22+
}
23+
24+
int min_price = price[0];
25+
26+
for(int i = 1; i < n; i++) {
27+
28+
if(price[i] < min_price)
29+
min_price = price[i];
30+
31+
profit[i] = Math.max(profit[i - 1], profit[i] + (price[i] - min_price));
32+
}
33+
int result = profit[n - 1];
34+
return result;
35+
}
36+
37+
public static void main(String args[]) {
38+
int price[] = {2, 30, 15, 10, 8, 25, 80};
39+
int n = price.length;
40+
System.out.println("Maximum Profit = " + maxProfit(price, n));
41+
}
42+
443
}

arrays/Array_Problem_33.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,41 @@
33
/* Problem Title :-> Three way partitioning of an array around a given value */
44
public class Array_Problem_33 {
55

6-
6+
// partition a[0..n-1] around [lowVal, highVal]
7+
public static void threeWayPartition(int[] a, int lowVal, int highVal) {
8+
int n = a.length;
9+
// Initialize ext available positions for smaller (than range) & greater element.
10+
int start = 0, end = n - 1;
11+
// Traverse elements from left
12+
for(int i = 0; i <= end;) {
13+
// If current element is smaller than range, put it on next available smaller position.
14+
if(a[i] < lowVal) {
15+
int temp = a[start];
16+
a[start] = a[i];
17+
a[i] = temp;
18+
start++;
19+
i++;
20+
}
21+
22+
// If current element is greater than range, put it on next available greater position.
23+
else if(a[i] > highVal) {
24+
int temp = a[end];
25+
a[end] = a[i];
26+
a[i] = temp;
27+
end--;
28+
}
29+
30+
else i++;
31+
}
32+
}
33+
34+
public static void main(String[] args) {
35+
int[] a = {1, 14, 5, 20, 4, 2, 54, 20, 87, 98, 3, 1, 32};
36+
threeWayPartition(a, 10, 20);
37+
System.out.println("Modified Array");
38+
for(int i = 0; i < a.length; i++) {
39+
System.out.println(a[i] + " ");
40+
}
41+
}
742

843
}

arrays/Array_Problem_34.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package arrays;
2-
import java.lang.*;
32

43
/* Problem Title :-> Minimum swaps required bring elements less equal K together */
54
public class Array_Problem_34 {

arrays/Array_Problem_36.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ else if(j == n) {
5353
}
5454

5555
/* Driver program to test above function */
56-
public static void main(String[] args) {
56+
@SuppressWarnings("unused")
57+
public static void main(String[] args) {
5758

5859
int[] a1 = {1, 12, 15, 26, 38};
5960
int[] a2 = {2, 13, 17, 30, 45};
6061

61-
int n1 = a1.length,n2 = a2.length;
62+
int n1 = a1.length, n2 = a2.length;
6263

6364
System.out.println("Median is " + getMedian(a1, a2, n1));
6465

0 commit comments

Comments
 (0)