0% found this document useful (0 votes)
15 views

Java - Data Structures

The document contains details about an assignment on data structures for a pre-placement training in Java. It includes questions from easy, medium, and hard levels related to arrays, stacks, and more. The questions cover concepts like 1D arrays, subarrays, 2D arrays, maximum elements in a stack, and validating parentheses in expressions using a stack.

Uploaded by

s1062230092
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java - Data Structures

The document contains details about an assignment on data structures for a pre-placement training in Java. It includes questions from easy, medium, and hard levels related to arrays, stacks, and more. The questions cover concepts like 1D arrays, subarrays, 2D arrays, maximum elements in a stack, and validating parentheses in expressions using a stack.

Uploaded by

s1062230092
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Name: Rashmi Sahani

Roll no: 92

Pre-Placement Training - Java


Assignment 4 - Data Structures

● Easy Level questions (Attempt any 2) -


○ 1D Array [ YT Link ]

import java.util.*;

public class Solution {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


int n = scan.nextInt();
int[] a = new int[n];
for(int i = 0; i < n;i++){
a[i] = scan.nextInt();
}
scan.close();

// Prints each sequential element in array a


for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
Name: Rashmi Sahani
Roll no: 92

○ SubArray
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {


/* Enter your code here. Read input from STDIN.
Print output to STDOUT. Your class should be named
Solution. */
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = sc.nextInt();
}

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);

}
}

● Medium Level Questions (Attempt any 2) -

○ 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

public class Solution {


public static void main(String[] args) throws
IOException {
BufferedReader bufferedReader = new
BufferedReader(new InputStreamReader(System.in));

List<List<Integer>> arr = new ArrayList<>();

for (int i = 0; i < 6; i++) {


String[] arrRowTempItems =
bufferedReader.readLine().replaceAll("\\s+$", "").split("
");

List<Integer> arrRowItems = new


ArrayList<>();

for (int j = 0; j < 6; j++) {


int arrItem =
Integer.parseInt(arrRowTempItems[j]);
arrRowItems.add(arrItem);
}

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);
}
}

○ Stack - Maximum Elements [YT Link]


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

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.
*/

public static List<Integer> getMax(List<String>


operations) {
// Write your code here

Stack<Integer> mainStack = new Stack<>();


Stack<Integer> maxStack = new Stack<>();

for(int i = 0 ; i < N; i++){

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();

public class Solution {


public static void main(String[] args) throws
IOException {
BufferedReader bufferedReader = new
BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new
BufferedWriter(new
FileWriter(System.getenv("OUTPUT_PATH")));

int n =
Integer.parseInt(bufferedReader.readLine().trim());

List<String> ops = IntStream.range(0,


n).mapToObj(i -> {
try {
return bufferedReader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.collect(toList());

List<Integer> res = Result.getMax(ops);

bufferedWriter.write(
res.stream()
.map(Object::toString)
.collect(joining("\n"))
Name: Rashmi Sahani
Roll no: 92
+ "\n"
);

bufferedReader.close();
bufferedWriter.close();
}
}

● Hard Level Questions (Attempt any 1) -


○ Stack [ YT Link ]
import java.util.*;

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();
}
}
}
}

You might also like