Skip to content

Commit 21ea598

Browse files
committed
Added solution to find largest sum subsequence and find smallest k
1 parent 056f952 commit 21ea598

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.eprogrammerz.examples.algorithm.crackingCoding;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.assertArrayEquals;
9+
10+
/**
11+
* You are given an array of integers (both positive and negative). Find the contiguous sequence with the largest sum. Return the sum.
12+
*
13+
* EXAMPLE
14+
* lnput:2, -8, 3, -2, 4, -10 Output:5 (i.e•, {3, -2, 4})
15+
*/
16+
public class ContinuousSequence {
17+
/**
18+
* [2, -8, 3, -2, 4, -10]
19+
* sum arr: [2, -6, -3, -5, -1, -11]
20+
* 2
21+
* 3
22+
* 4
23+
*/
24+
/**
25+
* O(n^2)
26+
*
27+
* @param arr
28+
* @return
29+
*/
30+
public int[] largestSumSeq(int[] arr) {
31+
int sum = Integer.MIN_VALUE;
32+
33+
List<Integer> sequence = new ArrayList<>();
34+
35+
for (int i = 0; i < arr.length; i++) {
36+
int tempSum = 0;
37+
List<Integer> tempSeq = new ArrayList<>();
38+
for (int j = i; j < arr.length; j++) {
39+
tempSum += arr[j];
40+
tempSeq.add(arr[j]);
41+
if (tempSum > sum) {
42+
sum = tempSum;
43+
sequence.clear();
44+
sequence.addAll(tempSeq);
45+
}
46+
}
47+
}
48+
49+
int[] result = new int[sequence.size()];
50+
for (int i = 0; i < result.length; i++) {
51+
result[i] = sequence.get(i);
52+
}
53+
return result;
54+
}
55+
56+
@Test
57+
public void testLargestSumSeq() {
58+
int[] arr = new int[]{2, -8, 3, -2, 4, -10};
59+
int[] actual = largestSumSeq(arr);
60+
int[] expected = new int[]{3, -2, 4};
61+
assertArrayEquals(expected, actual);
62+
}
63+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.eprogrammerz.examples.algorithm.crackingCoding;
2+
3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
/**
10+
* Design an algorithm to find the smallest K numbers in an array.
11+
*/
12+
public class SmallestK {
13+
public int[] findKSmallest(int[] arr, int k) {
14+
// use max heap so that max of current will be on root
15+
PriorityQueue<Integer> pq = new PriorityQueue<>((n1, n2) -> n2 - n1);
16+
17+
for (int i = 0; i < k; i++) { // O(k logk)
18+
pq.add(arr[i]);
19+
}
20+
21+
for (int i = k; i < arr.length; i++) { // O(n logk)
22+
if (!pq.isEmpty() && pq.peek() > arr[i]) {
23+
pq.poll();
24+
pq.add(arr[i]);
25+
}
26+
}
27+
28+
int[] result = new int[k];
29+
30+
int idx = 0;
31+
for (int n : pq) {
32+
result[idx++] = n;
33+
}
34+
35+
return result;
36+
}
37+
38+
@Test
39+
public void testFindSmallestK() {
40+
int[] input = new int[]{5, 1, 4, 3, 10, 6, 2};
41+
int[] expected = new int[]{3, 1, 2};
42+
int[] actual = findKSmallest(input, 3);
43+
assertArrayEquals(expected, actual);
44+
}
45+
}

0 commit comments

Comments
 (0)