Placement 19TH
Placement 19TH
JAMI JASWANTH
RA2211026010173
1)
import java.util.Scanner;
public class MinimumJumpsToEnd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input the array
System.out.print("Enter the array elements separated by space: ");
String[] input = sc.nextLine().split(" ");
int n = input.length;
// Convert input to an integer array
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
}
// Calculate the minimum jumps
int result = findMinimumJumps(arr);
// Output the result
System.out.println(result);
}
private static int findMinimumJumps(int[] arr) {
int n = arr.length;
// If the array has only one element or the first element is 0
if (n <= 1) return 0;
if (arr[0] == 0) return -1;
// Initialize variables
int jumps = 1; // At least one jump needed
int maxReach = arr[0]; // Maximum index reachable
int steps = arr[0]; // Steps left in the current range
for (int i = 1; i < n; i++) {
// If we have reached the end of the array
if (i == n - 1) return jumps;
// Update the maximum reach
maxReach = Math.max(maxReach, i + arr[i]);
// Decrease the steps left
steps--;
// If no steps are left
if (steps == 0) {
// Increment the jump count
jumps++;
// Check if the current index is beyond the maxReach
if (i >= maxReach) return -1;
// Update the steps to the number of steps to reach maxReach
steps = maxReach - i;
}
}
return -1; // If we never reach the end
}
}
2)
import java.util.Scanner;
3)
import java.util.Stack;
import java.util.Scanner;
public class FactorialUsingStack {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = scanner.nextInt();
if (n < 0) {
System.out.println("Factorial is not defined for negative numbers.");
return;
}
Stack<Integer> stack = new Stack<>();
int factorial = 1;
for (int i = 1; i <= n; i++) {
stack.push(i);
}
while (!stack.isEmpty()) {
factorial *= stack.pop();
}
System.out.println("FACTORIAL IS : " + factorial);
}
}
4)
import java.util.Scanner;
public class MazePath {
public static boolean findPath(int[][] maze, int n, int x, int y, int[][] path) {
if (x == n - 1 && y == n - 1 && maze[x][y] == 1) {
path[x][y] = 1;
return true;
}
if (x < 0 || x >= n || y < 0 || y >= n || maze[x][y] == 0) {
return false;
}
path[x][y] = 1;
if (findPath(maze, n, x + 1, y, path) || findPath(maze, n, x, y + 1, path)) {
return true;
}
path[x][y] = 0;
return false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the maze (N): ");
int n = scanner.nextInt();
int[][] maze = new int[n][n];
System.out.println("Enter the maze elements (0 or 1):");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
maze[i][j] = scanner.nextInt();
}
}
int[][] path = new int[n][n];
if (findPath(maze, n, 0, 0, path)) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(path[i][j] + " ");
}
System.out.println();
}
} else {
System.out.println("-1");
}
}
}
5)
import java.util.Scanner;
public class RodCutting {
public static int cutRod(int[] price, int n) {
int[] val = new int[n+1];
val[0] = 0;
for (int i = 1; i <= n; i++) {
int max_val = Integer.MIN_VALUE;
for (int j = 0; j < i; j++) {
max_val = Math.max(max_val, price[j] + val[i-j-1]);
}
val[i] = max_val;
}
return val[n];
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
6)
import java.util.Scanner;
public class JosephusProblem {
public static int josephus(int n, int k) {
if (n == 1) {
return 1;
}
return (josephus(n - 1, k) + k - 1) % n + 1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of persons (n): ");
int n = scanner.nextInt();
System.out.print("Enter the elimination count (k): ");
int k = scanner.nextInt();
int survivor = josephus(n, k);
System.out.println("The survivor is: " + survivor);
}
}
7)
import java.util.Scanner;
public class DivisorCount {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the array elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
System.out.print("Enter the range L and R: ");
int L = scanner.nextInt();
int R = scanner.nextInt();
int count = 0;
for (int i = 0; i < n; i++) {
boolean isDivisor = true;
for (int j = L; j <= R; j++) {
if (arr[i] % j != 0) {
isDivisor = false;
break;
}
}
if (isDivisor) {
count++;
}
}
System.out.println("Number of elements that divide all numbers in the range [" + L + ", " +
R + "]: " + count);
}
}
8)
import java.util.Scanner;
public class SudokuSolver {
public static boolean solveSudoku(int[][] grid) {
int row, col;
if (!findEmptyCell(grid, row, col)) {
return true; // Sudoku is solved
}
for (int num = 1; num <= 9; num++) {
if (isSafe(grid, row, col, num)) {
grid[row][col] = num;
if (solveSudoku(grid)) {
return true;
}
// If placing num doesn't lead to a solution, backtrack
grid[row][col] = 0;
}
}
return false;
}
private static boolean findEmptyCell(int[][] grid, int row, int col) {
for (row = 0; row < 9; row++) {
for (col = 0; col < 9; col++) {
if (grid[row][col] == 0) {
return true;
}
}
}
return false;
}
private static boolean isSafe(int[][] grid, int row, int col, int num) {
// Check if num is not present in the current row
for (int i = 0; i < 9; i++) {
if (grid[row][i] == num) {
return false;
}
}
// Check if num is not present in the current column
for (int i = 0; i < 9; i++) {
if (grid[i][col] == num) {
return false;
}
}
// Check if num is not present in the current 3x3 grid
int startRow = row - row % 3;
int startCol = col - col % 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (grid[i + startRow][j + startCol] == num) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[][] grid = new int[9][9];
System.out.println("Enter the 81 numbers of the Sudoku grid, row by row:");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
grid[i][j] = scanner.nextInt();
}
}
if (solveSudoku(grid)) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
} else {
System.out.println("No solution");
}
}
}
9)
import java.util.Scanner;
class Node {
char data;
Node next;
Node(char data) {
this.data = data;
this.next = null;
}
}
public class FibonacciLinkedList {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the Fibonacci sequence: ");
int n = scanner.nextInt();
int a = 1, b = 1, c;
for (int i = 3; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
Node head = new Node('A');
Node current = head;
char ch = 'B';
for (int i = 1; i < c; i++) {
current.next = new Node(ch);
current = current.next;
ch = (char) ((ch - 'A' + 1) % 26 + 'A');
}
a = 1;
b = 1;
current = head;
while (current != null) {
if (a == current.data - 'A' + 1) {
current = current.next;
a = b;
b = a + b;
continue;
}
System.out.print(current.data + " ");
current = current.next;
}
}
}