Skip to content

Commit deafbfe

Browse files
committed
#Modification 140
1 parent 7e8da61 commit deafbfe

15 files changed

+757
-117
lines changed

arrays/Array_Problem_17.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
package arrays;
22

3-
/* Problem Title :-> Best Time to buy and sell stocks */
3+
//? Problem Title :-> Best Time to buy and sell stocks [1]
4+
5+
// * You are allowed to buy and sell only once
6+
// ! You are not allowed to sell first and then buy,
7+
// ! (you must buy first and then sell)
8+
49
public class Array_Problem_17 {
510

611
public int maxProfit(int[] prices) {
12+
int mini = prices[0];
713
int maxProfit = 0;
14+
15+
int n = prices.size();
16+
17+
for(int i = 0; i < n; i++) {
18+
int cost = prices[i] - mini;
19+
maxProfit = Math.max(maxProfit, cost);
20+
mini = Math.min(mini, prices[i]);
21+
}
22+
823
return maxProfit;
924
}
25+
26+
//Driver Method
27+
public static void main(String[] args) {
28+
int[] prices = {2, 7, 4, -5, 11, 5, 20};
29+
System.out.println("Max profit is -> " + maxProfit(prices));
30+
}
1031
}

arrays/Array_Problem_26.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package arrays;
2-
/* Problem Title :-> Maximum profit by buying and selling a share atmost twice
3-
*/
2+
3+
//? Problem Title :-> Maximum profit by buying and selling a share atmost twice
4+
5+
// * You are allowed to buy and sell atmost twice
6+
47
public class Array_Problem_26 {
58
static int maxProfit(int[] price, int n) {
69

710
int[] profit = new int[n];
811

9-
for(int i =0; i < n; i++) {
12+
for(int i = 0; i < n; i++) {
1013
profit[i] = 0;
1114
}
1215

1316
int max_price = price[n-1];
1417

15-
for(int i = n -2; i >= 0; i--) {
18+
for(int i = n - 2; i >= 0; i--) {
1619
if(price[i] > max_price)
1720
max_price = price[i];
1821

@@ -27,7 +30,7 @@ static int maxProfit(int[] price, int n) {
2730
min_price = price[i];
2831

2932
profit[i]= Math.max(
30-
profit[i-1],
33+
profit[i - 1],
3134
profit[i] + (price[i] - min_price)
3235
);
3336
}

arrays/Array_Problem_28.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
package arrays;
2-
/* Problem Title :-> Find whether an arrays.array is a subset of another arrays.array
3-
*/
2+
// ? Problem Title :-> Find whether an array is a subset of another array
3+
44
public class Array_Problem_28 {
5+
6+
public static void main(String[] args) {
7+
8+
int arr1[] = { 11, 1, 13, 21, 3, 7 };
9+
int arr2[] = { 11, 3, 7, 1 };
10+
11+
int m = arr1.length;
12+
int n = arr2.length;
13+
14+
Set<Integer> s = new HashSet<Integer>();
15+
for (int i = 0; i < m; i++) {
16+
s.add(arr1[i]);
17+
}
18+
19+
int p = s.size();
20+
for (int i = 0; i < n; i++) {
21+
s.add(arr2[i]);
22+
}
23+
24+
if (s.size() == p) {
25+
System.out.println("arr2[] is subset of arr1[] " + "\n");
26+
}
27+
28+
else {
29+
System.out.println("arr2[] is not subset of arr1[] " + "\n");
30+
}
31+
}
532
}

arrays/Array_Problem_29.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ boolean find3Numbers(int[] A, int a_size, int sum) {
1616
}
1717
return false;
1818
}
19+
1920
public static void main(String[] args) {
2021
Array_Problem_29 triplet = new Array_Problem_29();
2122
int[] A = {1, 4, 45, 6, 10, 8};

linkedList/Problem_11.txt

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,97 @@ Output
120120
Complexity Analysis:
121121

122122
Time Complexity: O(n)
123+
124+
125+
package linkedList;
126+
// Pronblem Title => Intersection Point of two Linked Lists.
127+
public class Problem_11 {
128+
static Node head1, head2;
129+
130+
static class Node {
131+
132+
int data;
133+
Node next;
134+
135+
Node(int d) {
136+
data = d;
137+
next = null;
138+
}
139+
}
140+
141+
/*
142+
* function to get the intersection point of two linked lists head1 and head2
143+
*/
144+
int getNode() {
145+
int c1 = getCount(head1);
146+
int c2 = getCount(head2);
147+
int d;
148+
149+
if (c1 > c2) {
150+
d = c1 - c2;
151+
return _getIntesectionNode(d, head1, head2);
152+
} else {
153+
d = c2 - c1;
154+
return _getIntesectionNode(d, head2, head1);
155+
}
156+
}
157+
158+
/*
159+
* function to get the intersection point of two linked lists head1 and head2
160+
* where head1 has d more nodes than head2
161+
*/
162+
int _getIntesectionNode(int d, Node node1, Node node2) {
163+
int i;
164+
Node current1 = node1;
165+
Node current2 = node2;
166+
for (i = 0; i < d; i++) {
167+
if (current1 == null) {
168+
return -1;
169+
}
170+
current1 = current1.next;
171+
}
172+
while (current1 != null && current2 != null) {
173+
if (current1.data == current2.data) {
174+
return current1.data;
175+
}
176+
current1 = current2;
177+
current2 = current2.next;
178+
}
179+
180+
return -1;
181+
}
182+
183+
/*
184+
* Takes head pointer of the linked linkedList.list and returns the count of nodes in the
185+
* linkedList.list
186+
*/
187+
int getCount(Node node) {
188+
Node current = node;
189+
int count = 0;
190+
191+
while (current != null) {
192+
count++;
193+
current = current.next;
194+
}
195+
196+
return count;
197+
}
198+
199+
public static void main(String[] args) {
200+
Problem_11 list = new Problem_11();
201+
202+
// creating first linked linkedList.list
203+
list.head1 = new Node(3);
204+
list.head1.next = new Node(6);
205+
list.head1.next.next = new Node(9);
206+
list.head1.next.next.next = new Node(15);
207+
list.head1.next.next.next.next = new Node(30);
208+
209+
// creating second linked linkedList.list
210+
list.head2 = new Node(10);
211+
list.head2.next = new Node(15);
212+
list.head2.next.next = new Node(30);
213+
214+
System.out.println("The node of intersection is " + list.getNode());
215+
}
216+
}

linkedList/Problem_12.java

Lines changed: 47 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package linkedList;
2-
// Pronblem Title => Intersection Point of two Linked Lists.
2+
// Pronblem Title => Merge Sort for linked lists [Very Important].
33
public class Problem_12 {
44
static Node head1, head2;
55

@@ -14,79 +14,63 @@ static class Node {
1414
}
1515
}
1616

17-
/*
18-
* function to get the intersection point of two linked lists head1 and head2
19-
*/
20-
int getNode() {
21-
int c1 = getCount(head1);
22-
int c2 = getCount(head2);
23-
int d;
24-
25-
if (c1 > c2) {
26-
d = c1 - c2;
27-
return _getIntesectionNode(d, head1, head2);
28-
} else {
29-
d = c2 - c1;
30-
return _getIntesectionNode(d, head2, head1);
31-
}
32-
}
17+
static Node mergeSort(Node head) {
18+
if(head == null || head.next == null)
19+
return head;
3320

34-
/*
35-
* function to get the intersection point of two linked lists head1 and head2
36-
* where head1 has d more nodes than head2
37-
*/
38-
int _getIntesectionNode(int d, Node node1, Node node2) {
39-
int i;
40-
Node current1 = node1;
41-
Node current2 = node2;
42-
for (i = 0; i < d; i++) {
43-
if (current1 == null) {
44-
return -1;
45-
}
46-
current1 = current1.next;
47-
}
48-
while (current1 != null && current2 != null) {
49-
if (current1.data == current2.data) {
50-
return current1.data;
51-
}
52-
current1 = current2;
53-
current2 = current2.next;
54-
}
21+
Node middle = getMiddle(head);
22+
Node right = middle.next;
23+
middle.next = null;
24+
Node left = mergeSort(left);
25+
Node left1 = mergeSort(left);
26+
Node left2 = mergeSort(right);
27+
return merge(left1, left2);
28+
}
29+
30+
static Node getMiddle(Node head) {
31+
Node slow = head;
32+
Node fast = head.next;
5533

56-
return -1;
34+
while(fasty != null && fast.next != null) {
35+
slow = slow.next;
36+
fast = fast.next.next;
5737
}
5838

59-
/*
60-
* Takes head pointer of the linked linkedList.list and returns the count of nodes in the
61-
* linkedList.list
62-
*/
63-
int getCount(Node node) {
64-
Node current = node;
65-
int count = 0;
66-
67-
while (current != null) {
68-
count++;
69-
current = current.next;
70-
}
39+
return slow;
40+
}
41+
42+
static Node merge(Node head1, Node head2) {
43+
if(head1 == null)
44+
return head2;
7145

72-
return count;
46+
if(head2 == null)
47+
return head1;
48+
49+
if(head1.data < head2.data) {
50+
head1.next = merge(head1.next, head2);
51+
return head1;
52+
} else {
53+
head2.next = merge(head1, head2.next);
54+
return head2;
7355
}
56+
}
7457

7558
public static void main(String[] args) {
7659
Problem_12 list = new Problem_12();
7760

78-
// creating first linked linkedList.list
79-
list.head1 = new Node(3);
80-
list.head1.next = new Node(6);
81-
list.head1.next.next = new Node(9);
82-
list.head1.next.next.next = new Node(15);
83-
list.head1.next.next.next.next = new Node(30);
61+
list.head1 = new Node(1);
62+
list.head1.next = new Node(3);
63+
list.head1.next.next = new Node(5);
64+
list.head1.next.next.next = new Node(7);
65+
66+
list.head2 = new Node(2);
67+
list.head2.next = new Node(4);
68+
list.head2.next.next = new Node(6);
69+
list.head2.next.next.next = new Node(8);
8470

85-
// creating second linked linkedList.list
86-
list.head2 = new Node(10);
87-
list.head2.next = new Node(15);
88-
list.head2.next.next = new Node(30);
71+
Node result = mergeSort(head1);
72+
printList(result);
8973

90-
System.out.println("The node of intersection is " + list.getNode());
74+
9175
}
9276
}

0 commit comments

Comments
 (0)