-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLongestSubarray.java
More file actions
145 lines (120 loc) · 3.93 KB
/
LongestSubarray.java
File metadata and controls
145 lines (120 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//O(n^3)
import java.util.*;
public class Subarray {
public static int getLongestSubarray(int []a, long k) {
int n = a.length; // size of the array.
int len = 0;
for (int i = 0; i < n; i++) { // starting index
for (int j = i; j < n; j++) { // ending index
// add all the elements of
// subarray = a[i...j]:
long s = 0;
for (int K = i; K <= j; K++) {
s += a[K];
}
if (s == k)
len = Math.max(len, j - i + 1);
}
}
return len;
}
public static void main(String[] args) {
int[] a = {2, 3, 5, 1, 9};
long k = 10;
int len = getLongestSubarray(a, k);
System.out.println("The length of the longest subarray is: " + len);
}
}
//O(n^2)
import java.util.*;
public class Subarray {
public static int getLongestSubarray(int []a, long k) {
int n = a.length; // size of the array.
int len = 0;
for (int i = 0; i < n; i++) { // starting index
long s = 0; // Sum variable
for (int j = i; j < n; j++) { // ending index
// add the current element to
// the subarray a[i...j-1]:
s += a[j];
if (s == k)
len = Math.max(len, j - i + 1);
}
}
return len;
}
public static void main(String[] args) {
int[] a = {2, 3, 5, 1, 9};
long k = 10;
int len = getLongestSubarray(a, k);
System.out.println("The length of the longest subarray is: " + len);
}
}
//O(n) or O(n*logn)
import java.util.*;
public class Subarray {
public static int getLongestSubarray(int []a, long k) {
int n = a.length; // size of the array.
Map<Long, Integer> preSumMap = new HashMap<>();
long sum = 0;
int maxLen = 0;
for (int i = 0; i < n; i++) {
//calculate the prefix sum till index i:
sum += a[i];
// if the sum = k, update the maxLen:
if (sum == k) {
maxLen = Math.max(maxLen, i + 1);
}
// calculate the sum of remaining part i.e. x-k:
long rem = sum - k;
//Calculate the length and update maxLen:
if (preSumMap.containsKey(rem)) {
int len = i - preSumMap.get(rem);
maxLen = Math.max(maxLen, len);
}
//Finally, update the map checking the conditions:
if (!preSumMap.containsKey(sum)) {
preSumMap.put(sum, i);
}
}
return maxLen;
}
public static void main(String[] args) {
int[] a = {2, 3, 5, 1, 9};
long k = 10;
int len = getLongestSubarray(a, k);
System.out.println("The length of the longest subarray is: " + len);
}
}
//O(2*N)
import java.util.*;
public class Main {
public static int getLongestSubarray(int []a, long k) {
int n = a.length; // size of the array.
int left = 0, right = 0; // 2 pointers
long sum = a[0];
int maxLen = 0;
while (right < n) {
// if sum > k, reduce the subarray from left
// until sum becomes less or equal to k:
while (left <= right && sum > k) {
sum -= a[left];
left++;
}
// if sum = k, update the maxLen i.e. answer:
if (sum == k) {
maxLen = Math.max(maxLen, right - left + 1);
}
// Move forward thw right pointer:
right++;
if (right < n) sum += a[right];
}
return maxLen;
}
public static void main(String[] args) {
int[] a = {2, 3, 5, 1, 9};
long k = 10;
int len = getLongestSubarray(a, k);
System.out.println("The length of the longest subarray is: " + len);
}
}