Skip to content

Commit be9fa37

Browse files
committed
Add solutions of Java DataStructures problems
1 parent fbb63b9 commit be9fa37

16 files changed

Lines changed: 521 additions & 0 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
7+
public class Solution {
8+
9+
public static boolean isPossible(int m,int[] a,int i){
10+
if(i<0||a[i]==1)
11+
return false;
12+
if(i==a.length-1 || i+m>a.length-1)
13+
return true;
14+
a[i]=1;
15+
return isPossible(m,a,i+1)||isPossible(m,a,i-1)||isPossible(m,a,i+m);
16+
}
17+
public static void main(String[] args) {
18+
Scanner sc = new Scanner(System.in);
19+
int t = sc.nextInt();
20+
while(t--!=0){
21+
int n=sc.nextInt();
22+
int m=sc.nextInt();
23+
int a[]=new int[n];
24+
for(int i=0;i<n;i++){
25+
a[i]=sc.nextInt();
26+
}
27+
28+
System.out.println(isPossible(m,a,0)?"YES":"NO");
29+
}
30+
}
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Declare array a here
2+
int a[]=new int[n];
3+
for(int i = 0 ; i < n; i++){
4+
int val = scan.nextInt();
5+
// Fill array a here
6+
a[i]=val;
7+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
7+
public class Solution {
8+
9+
public static void main(String[] args) {
10+
Scanner in = new Scanner(System.in);
11+
int a[][] = new int[6][6];
12+
for(int i=0; i < 6; i++){
13+
for(int j=0; j < 6; j++){
14+
a[i][j] = in.nextInt();
15+
}
16+
}
17+
int sum=Integer.MIN_VALUE;
18+
for(int i=0;i<=3;i++){
19+
for(int j=0;j<=3;j++){
20+
int s=a[i][j]+a[i][j+1]+a[i][j+2]+a[i+1][j+1]+a[i+2][j]+a[i+2][j+1]+a[i+2][j+2];
21+
if(s>sum)
22+
sum=s;
23+
}
24+
}
25+
System.out.println(sum);
26+
}
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
7+
public class Solution {
8+
9+
public static void main(String[] args) {
10+
Scanner sc=new Scanner(System.in);
11+
int t=sc.nextInt();
12+
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
13+
while(t-->0){
14+
ArrayList<Integer> b = new ArrayList<Integer>();
15+
int n= sc.nextInt();
16+
while(n-->0)
17+
b.add(sc.nextInt());
18+
a.add(b);
19+
}
20+
int q=sc.nextInt();
21+
while(q-->0){
22+
int x=sc.nextInt();
23+
int y=sc.nextInt();
24+
if(y>a.get(x-1).size())
25+
System.out.println("ERROR!");
26+
else
27+
System.out.println(a.get(x-1).get(y-1));
28+
}
29+
}
30+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
7+
public class Solution {
8+
9+
public static void main(String[] args) {
10+
Scanner sc=new Scanner(System.in);
11+
int n=sc.nextInt();
12+
BitSet b[] = new BitSet[2];
13+
b[0] = new BitSet(n);
14+
b[1] = new BitSet(n);
15+
int m=sc.nextInt();
16+
sc.nextLine();
17+
for(int i=0;i<m;i++){
18+
String s=sc.next();
19+
int n1=sc.nextInt();
20+
int n2=sc.nextInt();
21+
switch(s){
22+
case "AND":
23+
b[n1-1].and(b[n2-1]);
24+
break;
25+
case "OR":
26+
b[n1-1].or(b[n2-1]);
27+
break;
28+
case "XOR":
29+
b[n1-1].xor(b[n2-1]);
30+
break;
31+
case "FLIP":
32+
b[n1-1].flip(n2);
33+
break;
34+
case "SET":
35+
b[n1-1].set(n2);
36+
break;
37+
}
38+
System.out.println(b[0].cardinality()+" "+b[1].cardinality());
39+
}
40+
}
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Checker implements Comparator<Player>{
2+
public int compare(Player a, Player b){
3+
if(a.score==b.score)
4+
return a.name.compareTo(b.name);
5+
else
6+
return b.score-a.score;
7+
}
8+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.*;
2+
public class test {
3+
public static void main(String[] args) {
4+
Scanner in = new Scanner(System.in);
5+
Deque deque = new ArrayDeque<Integer>();
6+
int n = in.nextInt();
7+
int m = in.nextInt();
8+
int a[]=new int[10000001];
9+
int max=0,u=0;
10+
11+
for (int i = 0; i < n; i++) {
12+
int num = in.nextInt();
13+
if(deque.size()<m){
14+
deque.addLast(num);
15+
if(a[num]++==0)
16+
u++;
17+
}
18+
else{
19+
if(u>max)
20+
max=u;
21+
if(a[(int)deque.removeFirst()]--==1)
22+
u--;
23+
deque.addLast(num);
24+
if(a[num]++==0)
25+
u++;
26+
}
27+
}
28+
if(u>max)
29+
max=u;
30+
System.out.println(max);
31+
}
32+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Printer
2+
{
3+
public static<T> void printArray(T[] a){
4+
for(int i=0;i<a.length;i++){
5+
System.out.println(a[i]);
6+
}
7+
}
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
7+
public class Solution {
8+
9+
public static void main(String[] args) {
10+
Set<String> s = new HashSet<String>();
11+
Scanner sc=new Scanner(System.in);
12+
int t=sc.nextInt();
13+
sc.nextLine();
14+
while(t-->0){
15+
String str=sc.nextLine();
16+
s.add(str);
17+
System.out.println(s.size());
18+
}
19+
}
20+
}

‎Java/DataStructures/JavaList.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
7+
public class Solution {
8+
9+
public static void main(String[] args) {
10+
Scanner sc=new Scanner(System.in);
11+
int n=sc.nextInt();
12+
List<Integer> l= new ArrayList<Integer>();
13+
for(int i=0;i<n;i++){
14+
l.add(sc.nextInt());
15+
}
16+
int q=sc.nextInt();
17+
while(q-->0){
18+
sc.nextLine();
19+
String s=sc.nextLine();
20+
if(s.equals("Insert")){
21+
int x=sc.nextInt();
22+
int num=sc.nextInt();
23+
l.add(x,num);
24+
}
25+
else{
26+
int x=sc.nextInt();
27+
l.remove(x);
28+
}
29+
}
30+
for(int i=0;i<l.size();i++){
31+
System.out.print(l.get(i)+" ");
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)