Skip to content

Commit 0084059

Browse files
committed
Solutions to Java Data Structures section
1 parent db3cae4 commit 0084059

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
Scanner scan = new Scanner(System.in);
7+
int n = scan.nextInt();
8+
9+
int[] a = new int[n];
10+
11+
for (int i = 0; i < n; i++) {
12+
a[i] = scan.nextInt();
13+
}
14+
scan.close();
15+
16+
// Prints each sequential element in array a
17+
for (int i = 0; i < a.length; i++) {
18+
System.out.println(a[i]);
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
int[][] a = new int[6][6];
8+
int max = Integer.MIN_VALUE;
9+
10+
for (int i = 0; i < 6; i++) {
11+
for (int j = 0; j < 6; j++) {
12+
a[i][j] = sc.nextInt();
13+
if (i > 1 && j > 1) {
14+
int sum = a[i][j]
15+
+ a[i][j - 1]
16+
+ a[i][j - 2]
17+
+ a[i - 1][j - 1]
18+
+ a[i - 2][j]
19+
+ a[i - 2][j - 1]
20+
+ a[i - 2][j - 2];
21+
22+
if (sum > max)
23+
max = sum;
24+
}
25+
}
26+
}
27+
28+
sc.close();
29+
System.out.println(max);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
Scanner in = new Scanner(System.in);
7+
int n = in.nextInt();
8+
ArrayList<ArrayList<Integer>> rows = new ArrayList<>();
9+
10+
for (int i = 0; i < n; i++) {
11+
int m = in.nextInt();
12+
ArrayList<Integer> row = new ArrayList<>();
13+
14+
for (int j = 0; j < m; j++) {
15+
row.add(in.nextInt());
16+
}
17+
rows.add(row);
18+
}
19+
n = in.nextInt();
20+
for (int i = 0; i < n; i++) {
21+
int x = in.nextInt();
22+
int y = in.nextInt();
23+
try {
24+
System.out.println(rows.get(x - 1).get(y - 1));
25+
} catch (Exception e) {
26+
System.out.println("ERROR!");
27+
}
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
int n = sc.nextInt();
8+
int[] a = new int[n];
9+
10+
for (int i = 0; i < n; i++) {
11+
a[i] = sc.nextInt();
12+
}
13+
sc.close();
14+
15+
int negativeSubarrays = 0;
16+
for (int i = 0; i < n; i++) {
17+
int sum = 0;
18+
for (int j = i; j < n; j++) {
19+
sum += a[j];
20+
if (sum < 0) {
21+
negativeSubarrays++;
22+
}
23+
}
24+
}
25+
System.out.println(negativeSubarrays);
26+
}
27+
}

0 commit comments

Comments
 (0)