You are on page 1of 7

NAME OF THE STUDENT : SRIHARSHITHA DEEPALA

REG NO : 19BCD7246
SLOT : L27 + L28 + L31 + L32
ASSIGNMENT NUMBER : 05

1.Rabbit to Holes Problem


There are N rabbit and N holes that are placed in a straight line. Each hole
can accommodate only 1 mouse.
The positions of rabbit are denoted by array A and the position of holes are
denoted by array B.
A mouse can stay at his position, move one step right from x to x + 1, or move one
step left from x to x − 1. Any of these moves consumes 1 minute.
Assign rabbit to holes so that the time when the last mouse gets inside a hole is
minimized.
Problem Constraints
1 <= N <= 105
-109 <= A[i], B[i] <= 109
Input Format
First argument is an integer array A.
Second argument is an integer array B.
Output Format
Return an integer denoting the minimum time when the last mouse gets inside the
holes.
Example Input
Input 1:
A = [-4, 2, 3]
B = [0, -2, 4]
Input 2:
A = [-2]
B = [-6]
Example Output
Output 1:
2
Output 2:
4
CODE

import java.util.*;
class RabbitToHole{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
int t = scan.nextInt(); //no.of testcases
while(t-->0){
int n = scan.nextInt(); //no.of rabbits and holes
int A[] = new int[n]; //no.of rabbits
int B[] = new int[n]; //no.of holes
for(int i=0;i<n;i++){
A[i]=scan.nextInt();
}
for(int i=0;i<n;i++){
B[i]=scan.nextInt();
}
System.out.println(assign(A,B));
}
scan.close();
}
static int assign(int A[], int B[]){
int n = A.length;
int max_val=0;
Arrays.sort(A);
Arrays.sort(B);
for(int i=0;i<n;i++){
if(max_val<Math.abs(A[i]-B[i])){
max_val=Math.abs(A[i]-B[i]);
}
}
return Math.abs(max_val);
}
}
OUTPUT

2. Given a singly linked list


L: L0 → L1 → … → Ln-1 → Ln,
reorder it to:
L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …
You must do this in-place without altering the nodes’ values.
For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

CODE

import java.util.*;
class SwapLL{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();//no.of test cases
while(t-->0){
LinkedList<Integer> ll = new LinkedList<Integer>();
LinkedList<Integer> ll2 = new LinkedList<Integer>();
LinkedList<Integer> ll3 = new LinkedList<Integer>();
int n = scan.nextInt(); // no.of nodes in linkedlist
for(int i=0;i<n;i++){
ll.add(scan.nextInt());
}
for(int i:ll){
ll3.add(i);
}
for(int i=0;i<n;i++){
ll2.add(ll.get(n-i-1));
}

System.out.print("Before Swapping :");


for(int i:ll){
System.out.print(i+" ");
}
System.out.println();
for(int i=0;i<n;i++){
if(i%2!=0){
ll.set(i,ll2.get(i/2));
}
else{
ll.set(i,ll3.get(i/2));
}
}

System.out.print("After Swapping :");


for(int i:ll){
System.out.print(i+" ");
}
}
scan.close();
}
}
OUTPUT

You might also like