SDOT Training Codes
SDOT Training Codes
//Creation of Linked List & 3 LINKKED LIST OPERATION (at BEGINNING , at END and at
MIDDLE)
___________________________________________________________________________________
__________________________________________________________________________
//MERGE TWO INDIVIDUALLY SORTED LINKEDLIST to form a SINGLE SORTED LL
//Examples
/*
3
1 5 8
6
0 1 2 6 9 10
0->1->1->2->5->6->8->9->10->NULL
*/
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
LinkedList LL1=new LinkedList();
LinkedList LL2=new LinkedList();
int s1=sc.nextInt();
for(int i=0;i<s1;i++){
LL1.insertAtEnd(sc.nextInt());
}
int s2=sc.nextInt();
for(int i=0;i<s1;i++){
LL2.insertAtEnd(sc.nextInt());
}
merger listm=new merger();
Node sortedListNode=listm.merge(LL1.head,LL2.head);
listm.printMerge(sortedListNode);
}
static class merger{
Node merge(Node h1,Node h2){
Node dummy=new Node(0);
Node tail=dummy;
while(h1!=null & h2!=null){
if(h1.data<=h2.data){
tail.next=h1;
h1=h1.next;
}
else
{
tail.next=h2;
h2=h2.next;
}
tail=tail.next;
}
while(h1!=null){
tail.next=h1;
h1=h1.next;
tail=tail.next;
}
while(h2!=null){
tail.next=h2;
h2=h2.next;
tail=tail.next;
}
return dummy.next;
}
void printMerge(Node head)
{
Node temp = head;
while (temp != null) {
System.out.print(temp.data + "->");
temp = temp.next;
}
System.out.print("NULL");
}
}
static class LinkedList{
Node head=null;
Node tail=null;
void insertAtBeg(int data){
Node newNode=new Node(data);
if(head==null){
head=newNode;
}
else{
newNode.next=head;
head=newNode;
}
}
void insertAtEnd(int data){
Node newNode=new Node(data);
if(head==null){
head=newNode;
tail=newNode;
}
else{
tail.next=newNode;
tail=newNode;
}
}
void insertAtMid(int pos ,int data){
Node newNode=new Node(data);
if(pos==0){
insertAtBeg(data);
return;
}
Node temp=head;
for(int i=0;i<pos-1;i++){
temp=temp.next;
}
newNode.next=temp.next;
temp.next=newNode;
}
void print(){
Node temp=head;
while(temp!=null){
System.out.print(temp.data+"->");
temp=temp.next;
}
}
}
static class Node{
int data;
Node next=null;
Node(int data){
this.data=data;
}
}
}
___________________________________________________________________________________
___________________________________________________________________________
// Linked List Reversal
import java.util.*;
class Main{
import java.util.*;
class Main{
while(secondHalf!=null){
if(firstHalf.data!=secondHalf.data){
return false;}
firstHalf=firstHalf.next;
secondHalf=secondHalf.next;
}
return true;
}
void print(Node temp){
//Node temp=head;
while(temp!=null){
System.out.print(temp.data+"->");
temp=temp.next;
}
System.out.println("\n");
}
}
while (!pq.isEmpty()) {
// Extract the min from the priority queue
ListNode node = pq.poll();
class ListNode {
int val;
ListNode next;
___________________________________________________________________________________
____________________________________________________
//21/02/2024 WEDNESDAY 08:15:47
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
int top=-1;
for(int i=0;i<n;i++){
String ip=sc.next();
if(ip.equals("push")){
top++;
arr[top]=sc.nextInt();
}
else if(ip.equals("pop")){
if (top==-1)
System.out.println("Stack underflow");
else{
arr[top]=0;
top--;
}
}
else if(ip.equals("print")){
for(int j=0;j<=top;j++){
System.out.println(arr[j]);
}
}
}
}
}
___________________________________________________________________________________
_________________________________________________________________________________
// STACK IMPLEMENTATION USING LINKED LIST AND IT'S ALLIED COMMANDS (Using Built-in
Header Function LIST)
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
List<Integer> list=new LinkedList<>();
int top=-1;
for(;;){
String ip=sc.next();
if (ip.equals("stop")){
break;
}
if(ip.equals("push")){
list.add(sc.nextInt());
}
else if(ip.equals("pop")){
if (list.isEmpty())
System.out.println("\t\t -1"); //Stack Underflow
else{
int rem=list.remove(list.size()-1);
System.out.println("Popped Element: "+rem);
}
}
else if(ip.equals("print")){
if (list.isEmpty())
System.out.println("\t\t NULL"); //List Empty
else{
for(int j=0;j<list.size();j++){
System.out.println(list.get(j));
}
} }
else if(ip.equals("peak")){
System.out.println("Peak Element: "+list.get(list.size()-1));
}
}
}
}
___________________________________________________________________________________
_________________________________________________________________________________
//STACK IMPLEMENTATION USING LINKED LIST W/O 'Collections.io'
/* INPUT
push 1
push 2
push 3
pop
print
stop
OUTPUT
popped element 3
elements in stack
1
2
*/
import java.util.*;
int rem=list.remove();
System.out.println("popped element "+rem);
}
else if(ip.equals("print"))
{
System.out.println("elements in stack");
list.print();
}
else if(ip.equals("stop"))
{
break;
}
}
}
static class LinkedList
{
Node head=null;
Node tail=null;
void push(int data)
{
Node newNode=new Node(data);
if(head==null)
{
head=newNode;
tail=newNode;
}
else
{
tail.next=newNode;
tail=newNode;
}
}
int remove()
{
Node temp=head;
while(temp.next.next!=null)
temp=temp.next;
int remEle=tail.data;
temp.next=null;
tail=temp;
return remEle;
}
void print()
{
Node temp=head;
while(temp!=null)
{
System.out.println(temp.data+" ");
temp=temp.next;
}
}
}
static class Node{
int data;
Node next=null;
Node(int data)
{
this.data=data;
}
}
}
___________________________________________________________________________________
__________________________________________________________________________________
//VALID Paranthesis using Stack
import java.util.Stack;
import java.util.*;
class Main
{
public static void main (String[]args)
{
Scanner sc=new Scanner(System.in);
String s = sc.next();
Stack < Integer > st = new Stack <> ();
int max = 0;
st.push (-1);
for (int i = 0; i < s.length (); i++)
{
char c = s.charAt (i);
if (c == '(')
{
st.push (i);
}
else
{
st.pop ();
if (st.isEmpty ())
{
st.push (i);
}
else
{
int len = i - st.peek ();
max = Math.max (max, len);
}
}
}
System.out.println ("Length of the longest valid parentheses substring: " +
max);
}
}
___________________________________________________________________________________
________________________________________________________________________________
//ODD & Even LinkedList (Ethnus)
import java.util.*;
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
class OddEvenLinkedList {
public ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
infixToPostfix(s);
}
static int prec(char c)
{
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
static char associativity(char c)
{
if (c == '^')
return 'R';
return 'L';
}
static void infixToPostfix(String s)
{
StringBuilder result = new StringBuilder();
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <=
'9'))
{
result.append(c);
}
else if (c == '(')
{
stack.push(c);
}
else if (c == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
{
result.append(stack.pop());
}
stack.pop();
}
else
{
while (!stack.isEmpty() && (prec(s.charAt(i)) < prec(stack.peek()) ||
prec(s.charAt(i)) == prec(stack.peek()) && associativity(s.charAt(i)) == 'L'))
{
result.append(stack.pop());
}
stack.push(c);
}
}
while (!stack.isEmpty())
{
result.append(stack.pop());
}
System.out.println(result);
}
}
___________________________________________________________________________________
_________________________________________________________________________________
INFIX to POSTFIX (method 2)
import java.util.Scanner;
import java.util.Stack;
import java.util.*;
Node(int data) {
this.data = data;
}
}
}
/*
-----------------------------------------------------------------------------------
--------------------------------
import java.util.*;
Node(int data) {
this.data = data;
}
}
}
*/
___________________________________________________________________________________
__________________________________________________________________________________
//Print Node which do not have Siblings in Binary Tree
// Sibling Node
import java.util.*;
Node(int data) {
this.data = data;
}
}
}
/* Google code
import java.util.*;
class Node
{
int data;
Node left, right;
Node(int item)
{
data = item;
left = right = null;
}
}
class Main
{
Node root;
void printSingles(Node node)
{
if (node == null)
return;
if (node.left != null && node.right != null)
{
printSingles(node.left);
printSingles(node.right);
}
import java.util.*;
public class Main
{
public static int evaluatePostfix (String expression)
{
Stack < Integer > stack = new Stack <> ();
for ( int i=0;i<expression.length();i++)
{
char c=expression.charAt(i);
if (Character.isDigit (c))
{
stack.push (Character.getNumericValue(c));
}
else
{
int operand2 = stack.pop ();
int operand1 = stack.pop ();
switch (c)
{
case '+':
stack.push (operand1 + operand2);
break;
case '-':
stack.push(operand1 - operand2);
break;
case '*':
stack.push(operand1 * operand2);
break;
case '/':
stack.push(operand1 / operand2);
break;
}
}
}
return stack.pop();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x=scanner.nextInt();
class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine(); // Read the postfix expression
int result = epx(str);
System.out.println("Result: " + result);
}
//BACKTRACKING N-QUEENS
import java.util.*;
class Main {
static int sol[][];
class Main{
static int sol[][];
if (sol[row][i]==1){
return false;
}
}
//Upper Diagonal Line Checking x
for(int i=row,j=col;i>=0 && j>=0;i--,j--){// \
if(sol[i][j]==1){// \
return false;// \
}// Q
}
//Lower DiagonalCheck
for(int i=row,j=col;i<n&&j>=0;i++,j--){
if(sol[i][j]==1){
return false;
}
}
return true;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(); //No. of Rows = No.of columns (CHESS BOARD)
if (n<4){
System.out.println("No possible ways to place "+n+" Queens in a
"+n+"x"+n+" matrix");
return;
}
sol=new int[n][n];
nqueen(n,0); // 0 implies the the first column of the matrix
}
}
*/
___________________________________________________________________________________
_______________________________________________________________________
/* Output:
1*2*3*4*17*18*19*20
--5*6*7*14*15*16
----8*9*12*13
------10*11
*/
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int no=1;
int value=n*n+1;
for(int i=n;i>0;i--)
{
for(int h=0;h<n-i;h++)
System.out.print("--");
for (int j=0;j<i;j++)
System.out.print(no+++"*");
for(int k=0;k<i-1;k++)
System.out.print(value+++"*");
System.out.print(value);
value=value-2*(i-1);
System.out.println("");
}
}
}
___________________________________________________________________________________
_______
//SPIRAL PATTERN 1 2 3
//
4 5 6
// 7 8 9
//OUTPUT 1 2 3 6 9 8 7 4 5
import java.util.*;
class Main{
public static void main(String args[]){
int n=5,val=0,rem=0;
for(int i=n;i>0;i--){
for(int j=i;j>rem;j--){
val++;
System.out.print(val+" ");
}
for(int k=i;k>rem+1;k--){
val+=n;
System.out.print(val+" ");
}
for(int l=i;l>rem+1;l--){
val--;
System.out.print(val+" ");
}
for(int m=i-1;m>rem+1;m--){
val-=n;
System.out.print(val+" ");
}
rem++;
}
}
}
------------------------------------------------------------
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[][] arr=new int[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
arr[i][j]=sc.nextInt();
int round=(int)Math.ceil(n/2.0);
for(int i=0;i<round;i++){
for(int j=i;j<=n-i-1;j++)
System.out.print(arr[i][j]+" ");
for(int j=i+1;j<=n-i-1;j++)
System.out.print(arr[j][n-i-1]+" ");
for(int j=n-i-2;j>=i;j--)
System.out.print(arr[n-i-1][j]+" ");
for(int j=n-i-2;j>i;j--)
System.out.print(arr[j][i]+" "); //arr[changing][constant]
}
}
}
__________________________________________________________________________________
//SUDOKU BACKTRACKING
import java.io.*;
import java.util.*;
if(sudokoSolver(grid,0,0))
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
System.out.print(grid[i][j]+" ");
System.out.println();
}
}
else
System.out.println("No Solution exists");
if(grid[row][col]!=0)
{
if(sudokoSolver(grid,nrow,ncol))
return true;
}
else
{
for(int i=1;i<=9;i++)
{
if(isSafe(grid,row,col,i))
{
grid[row][col]=i;
if(sudokoSolver(grid,nrow,ncol))
{
return true;
}
else
{
grid[row][col]=0;
}
}
}
}
return false;
}
//input
9
9
3 0 6 5 0 8 4 0 0
5 2 0 0 0 0 0 0 0
0 8 7 0 0 0 0 3 1
0 0 3 0 1 0 0 8 0
9 0 0 8 6 3 0 0 5
0 5 0 0 9 0 6 0 0
1 3 0 0 0 0 2 5 0
0 0 0 0 0 0 0 7 4
0 0 5 2 0 6 3 0 0
output:
3 1 6 5 7 8 4 9 2
5 2 9 1 3 4 7 6 8
4 8 7 6 2 9 5 3 1
2 6 3 4 1 5 9 8 7
9 7 4 8 6 3 1 2 5
8 5 1 7 9 2 6 4 3
1 3 8 9 4 7 2 5 6
6 9 2 3 5 1 8 7 4
7 4 5 2 8 6 3 1 9
*/
_______________________________________________
//prefix suffix (Code is incorrect I think)
//koi bkl hi hoga jo cut krega
import java.io.*;
import java.util.*;
______________________________________________________----
Trapped Rainwater 2d