PLACEMENT SOLUTIONS
JAMI JASWANTH
RA2211026010173
1)
import [Link];
public class MinimumJumpsToEnd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Input the array
[Link]("Enter the array elements separated by space: ");
String[] input = [Link]().split(" ");
int n = [Link];
// Convert input to an integer array
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = [Link](input[i]);
}
// Calculate the minimum jumps
int result = findMinimumJumps(arr);
// Output the result
[Link](result);
}
private static int findMinimumJumps(int[] arr) {
int n = [Link];
// 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 = [Link](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 [Link];
public class LevenshteinDistance {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String X = [Link]();
[Link]("Enter the second string: ");
String Y = [Link]();
int m = [Link]();
int n = [Link]();
int[][] dp = new int[m + 1][n + 1];
// Fill the first row
for (int i = 0; i <= n; i++) {
dp[0][i] = i;
}
// Fill the first column
for (int j = 0; j <= m; j++) {
dp[j][0] = j;
}
// Fill the rest of the matrix
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if ([Link](i - 1) == [Link](j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + [Link]([Link](dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]);
}
}
}
[Link]("Levenshtein Distance: " + dp[m][n]);
}
}
3)
import [Link];
import [Link];
public class FactorialUsingStack {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
if (n < 0) {
[Link]("Factorial is not defined for negative numbers.");
return;
}
Stack<Integer> stack = new Stack<>();
int factorial = 1;
for (int i = 1; i <= n; i++) {
[Link](i);
}
while (![Link]()) {
factorial *= [Link]();
}
[Link]("FACTORIAL IS : " + factorial);
}
}
4)
import [Link];
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([Link]);
[Link]("Enter the size of the maze (N): ");
int n = [Link]();
int[][] maze = new int[n][n];
[Link]("Enter the maze elements (0 or 1):");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
maze[i][j] = [Link]();
}
}
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++) {
[Link](path[i][j] + " ");
}
[Link]();
}
} else {
[Link]("-1");
}
}
}
5)
import [Link];
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 = [Link](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([Link]);
[Link]("Enter the length of the rod: ");
int n = [Link]();
int[] price = new int[n];
[Link]("Enter the price for each length:");
for (int i = 0; i < n; i++) {
price[i] = [Link]();
}
int maxProfit = cutRod(price, n);
[Link]("Maximum Profit: " + maxProfit);
}
}
6)
import [Link];
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([Link]);
[Link]("Enter the number of persons (n): ");
int n = [Link]();
[Link]("Enter the elimination count (k): ");
int k = [Link]();
int survivor = josephus(n, k);
[Link]("The survivor is: " + survivor);
}
}
7)
import [Link];
public class DivisorCount {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the size of the array: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter the array elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
[Link]("Enter the range L and R: ");
int L = [Link]();
int R = [Link]();
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++;
}
}
[Link]("Number of elements that divide all numbers in the range [" + L + ", " +
R + "]: " + count);
}
}
8)
import [Link];
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([Link]);
int[][] grid = new int[9][9];
[Link]("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] = [Link]();
}
}
if (solveSudoku(grid)) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
[Link](grid[i][j] + " ");
}
[Link]();
}
} else {
[Link]("No solution");
}
}
}
9)
import [Link];
class Node {
char data;
Node next;
Node(char data) {
[Link] = data;
[Link] = null;
}
}
public class FibonacciLinkedList {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of elements in the Fibonacci sequence: ");
int n = [Link]();
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++) {
[Link] = new Node(ch);
current = [Link];
ch = (char) ((ch - 'A' + 1) % 26 + 'A');
}
a = 1;
b = 1;
current = head;
while (current != null) {
if (a == [Link] - 'A' + 1) {
current = [Link];
a = b;
b = a + b;
continue;
}
[Link]([Link] + " ");
current = [Link];
}
}
}