Yooo 2
Yooo 2
Yooo 2
class node {
public int data;
public node next;
public node prev;
}
class Main {
static node head = null, tail = null; // Renamed 'temp' to 'tail'
boolean swapped;
node i, j = null;
do {
swapped = false;
i = head;
while (i.next != j) {
if (i.data > i.next.data) {
swap(i, i.next);
swapped = true;
}
i = i.next;
}
j = i;
} while (swapped);
}
2Segregate
import java.io.*;
import java.util.*;
// Set the next of the last node of the even list to null
if (oddend != null) {
oddend.next = null;
}
}
public void display()
{
Node temp = head;
while(temp!=null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
}
public static void main(String[] args) {
Solution list = new Solution();
Scanner s = new Scanner(System.in);
int x = s.nextInt();//1
// while(x!=-1)
// {
// list.insert(x);//1
// x = s.nextInt();//2
// }
for(int i=0;i<x;i++){
int m=s.nextInt();
list.insert(m);
}
list.segOddEven();
list.display();
}
}
3-LOOP DETECTION
import java.util.*;
class Node {
int num;
Node next;
Node(int val) {
num = val;
next = null;
}
}
class Main {
static Node insertNode(Node head, int val) {
Node newNode = new Node(val);
if(head == null) {
head = newNode;
return head;
}
Node temp = head;
while(temp.next != null)
temp = temp.next;
temp.next = newNode;
return head;
}
5-Minimum Stack
import java.util.*;
class Mystack {
Stack<Integer> s;
Stack<Integer> a;
Mystack() {
s = new Stack<Integer>();
a = new Stack<Integer>();
}
void getMin() {
if (a.isEmpty())
System.out.println("Stack is Empty");
else
System.out.println("Minimum element: " + a.peek());
}
void peek() {
if (s.isEmpty()) {
System.out.println("Stack is Empty");
return;
}
Integer t = s.peek();
System.out.println("Top most element: " + t);
}
void pop() {
if (s.isEmpty()) {
System.out.println("Stack is Empty");
return;
}
Integer t = s.pop();
System.out.println("Removed element: " + t);
if (t.equals(a.peek()))
a.pop();
}
void push(int x) {
if (s.isEmpty()) {
s.push(x);
a.push(x);
System.out.println("Number Inserted: " + x);
return;
} else {
s.push(x);
System.out.println("Number Inserted: " + x);
}
if (x <= a.peek())
a.push(x);
}
}
for(int i=0;i<mat.length;i++) {
if(i == x) continue;
if(mat[i][x] == 0) {
return -1;
}
}
return x;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int matrix[][]=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
matrix[i][j]=sc.nextInt();
}
}
int res = celebritySolution(matrix);
if(res == -1) {
System.out.println("There is no celebrity in the party");
} else {
System.out.println(res + " is the celebrity in the party");
}
}
}
7-Stock Span problem
import java.util.*;
public class Main {
static void calculate(int arr[], int n, int S[])
{
Stack<Integer> st = new Stack<>();
st.push(0);
S[0] = 1;
for (int i = 1; i < n; i++)
{
while (!st.isEmpty() && arr[st.peek()] <= arr[i])
st.pop();
S[i] = (st.isEmpty()) ? (i + 1) : (i - st.peek());
st.push(i);
}
}
static void printArray(int arr[])
{
System.out.print(Arrays.toString(arr));
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int [n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int S[] = new int[n];
calculate(arr, n, S);
printArray(S);
}
}
node.next = head;
head.prev = node;
head = node;
}
else if (temp == null) {
parent.next = node;
node.prev = parent;
}
else {
parent.next = node;
node.prev = parent;
node.next = temp;
temp.prev = node;
} }
private static int peek() {
if (head != null) {
return head.data;
}
return -1;
}
private static int pop() {
if (head != null) {
int curr = head.data;
head = head.next;
if (head != null) {
head.prev = null;
} return curr;
}
return -1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
int data=sc.nextInt();
int pri=sc.nextInt();
push(data, pri);
}
System.out.println(peek());
System.out.println(pop());
System.out.println(pop());
System.out.println(peek());
}
}
9-Stack permutations
import java.util.*;
class Main {
static Boolean check(int ip[], int op[], int n)
{
Stack<Integer> s = new Stack<Integer>();
int j = 0;
for (int i = 0; i < n; i++) {
s.push(ip[i]);
while (!s.isEmpty() && s.peek() == op[j]) {
s.pop();
j++;
}
}
if (s.isEmpty()) {
return true; }
return false;
}
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
int n=sc.nextInt();
int input[]=new int[n];
int output[]=new int[n];
for(int i=0;i<n;i++)
{
input[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
output[i]=sc.nextInt();
}
if (check(input, output, n))
System.out.println("Yes");
else
System.out.println("Not Possible");
}
}
public static void merge(int[] arr, int low, int mid, int high) {
int n1 = mid - low + 1;
int n2 = high - mid;
int[] left = new int[n1];
int[] right = new int[n2];
for (int i = 0; i < n1; i++) {
left[i] = arr[low + i];
}
for (int j = 0; j < n2; j++) {
right[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = low;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = left[i];
i++;
k++;
}
while (j < n2) {
arr[k] = right[j];
j++;
k++;
}
}
public static void main(String[] args) {
int[] arr = { 7, 2, 1, 6, 8, 5 };
mergeSort(arr, 0, arr.length - 1);
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}
12-TOWER OF HANOI
import java.util.Stack;
class Solution {
front = fr;
rear = rr;
return res;
}
// Driver code
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++){
int m=sc.nextInt();
int a=sc.nextInt();
push(front,rear,m,a);
}
System.out.println(pop(front, rear));
System.out.println(peek(front));
}
}