Java - Data Structures
Java - Data Structures
Roll no: 92
import java.util.*;
○ SubArray
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
int count = 0;
long s = 0;
for(int i = 0; i < n; i++){
Name: Rashmi Sahani
Roll no: 92
s = 0;
for(int j = i; j < n; j++){
s +=a[j];
if(s<0) count++;
}
}
System.out.println(count);
}
}
○ 2D Array [ YT Link ]
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
Name: Rashmi Sahani
Roll no: 92
arr.add(arrRowItems);
}
bufferedReader.close();
int n = 6;
int max = Integer.MIN_VALUE;
for(int row = 0; row<n-2; row++){
for(int col = 0; col < n - 2; col++){
int sum = arr.get(row).get(col)
+arr.get(row).get(col+1) + arr.get(row).get(col+2) +
arr.get(row + 1).get(col + 1) + arr.get(row + 2).get(col)
+ arr.get(row + 2).get(col + 1) + arr.get(row +
2).get(col + 2);
if(sum>max){
max=sum;
Name: Rashmi Sahani
Roll no: 92
}
}
}
System.out.println(max);
}
}
class Result {
/*
Name: Rashmi Sahani
Roll no: 92
* Complete the 'getMax' function below.
*
* The function is expected to return an
INTEGER_ARRAY.
* The function accepts STRING_ARRAY operations as
parameter.
*/
int op = sc.nextInt();
switch (op){
case 1:
int item = sc.nextInt();
mainStack.push(item);
int maxSoFar = maxStack.peek();
if(maxSoFar<item){
maxStack.push(item);
}else {
maxStack.push(maxSoFar);
}
break;
case 2:
maxStack.pop();
mainStack.pop();
break;
case 3:
System.out.println(maxStack.peek());
break;
Name: Rashmi Sahani
Roll no: 92
}
}
sc.close();
int n =
Integer.parseInt(bufferedReader.readLine().trim());
bufferedWriter.write(
res.stream()
.map(Object::toString)
.collect(joining("\n"))
Name: Rashmi Sahani
Roll no: 92
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
class Solution {
public static void main(String[] argh) {
Scanner sc = new Scanner(System.in);
Stack<String> Symbol = new Stack<String>();
while (sc.hasNext()) {
String input = sc.next();
boolean ans = true;
for (String x : input.split("")) {
try {
if ((x.equals(")")) ||
(x.equals("}")) || (x.equals("]"))) {
Symbol.pop();
} else {
Symbol.add(input);
}
} catch (EmptyStackException e) {
ans = false;
break;
}
}
if ((ans == true) && (Symbol.size() == 0)) {
System.out.println("true");
Symbol.clear();
} else {
Name: Rashmi Sahani
Roll no: 92
System.out.println("false");
Symbol.clear();
}
}
}
}