Q.
import java.io.*;
import java.util.*;
public class Solution {
public static void sort(int[] arr){
int n = arr.length;
for(int i = n/2-1; i>= 0; i--){
heapify(arr, n, i);
}
for(int i = n-1; i>0; i--){
swap(arr, 0, i);
heapify(arr, i, 0);
}
}
private static void heapify(int[] arr, int n, int i){
int largest = i;
int left = 2*i+1;
int right= 2*i+2;
if(left<n && arr[left]>arr[largest])
largest = left;
if(right<n && arr[right]>arr[largest])
largest = right;
if(largest != i){
swap(arr, i, largest);
heapify(arr, n, largest);
}
}
private static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
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();
}
sort(arr);
for(int i : arr){
System.out.print(i+" ");
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT.
Your class should be named Solution. */
}
}
===================================================================================
===
Q.3
import java.io.*;
import java.util.*;
class ListNode{
int val;
ListNode next;
ListNode(int val){
this.val = val;
this.next = null;
}
}
public class Solution {
public static ListNode findMid(ListNode head){
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public static boolean findElement(ListNode head, int tar){
ListNode curr = head;
while(curr != null){
if(curr.val == tar){
return true;
}
curr = curr.next;
}
return false;
}
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);
ListNode t = new ListNode(0);
ListNode curr = t;
int n = sc.nextInt();
for(int i = 0; i<n; i++){
int num = sc.nextInt();
curr.next = new ListNode(num);
curr = curr.next;
}
LinkedNode head = t.next;
curr = head;
while(curr !=null){}
System.out.print(curr.val +" ");
curr = curr.next;
}
System.out.println();
ListNode mid = findMid(head);
if(mid != null){
System.out.println(mid.val);
}
int tar = sc.nextInt();
boolean found = findElement(head, tar);
if(found){
System.out.println("Found");
}else{
System.out.println("Not Found");
}
}