mirror of https://github.com/evanferrao/dsa
add array questions
> thats some crazy pascals triangle code
This commit is contained in:
parent
433e4880e3
commit
633d0fc287
|
|
@ -0,0 +1,100 @@
|
|||
// write a code in java to print the pascals triangle for a given number of rows using a function
|
||||
import java.util.Scanner;
|
||||
|
||||
class PascalsTriangle{
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Enter the number of rows: ");
|
||||
int rows = sc.nextInt();
|
||||
System.out.println("Pascal's Triangle: ");
|
||||
// for(int i=0; i<rows; i++){
|
||||
// for(int j=0; j<=i; j++){
|
||||
// System.out.print(pascal(i, j) + " ");
|
||||
// }
|
||||
// System.out.println();
|
||||
// }
|
||||
//printPascalsTriangle(rows);
|
||||
// int[][] memo = new int[rows][rows];
|
||||
// for(int i=0; i<rows; i++){
|
||||
// for(int j=0; j<=i; j++){
|
||||
// System.out.print(pascalMemo(i, j, memo) + " ");
|
||||
// }
|
||||
// System.out.println();
|
||||
// }
|
||||
pascalDPSpaceOptimized(rows);
|
||||
sc.close();
|
||||
}
|
||||
|
||||
public static void printPascalsTriangle(int rows){
|
||||
for(int i=0; i<rows; i++){
|
||||
int number = 1;
|
||||
for(int j=0; j<=i; j++){
|
||||
System.out.print(number + " ");
|
||||
// proof using the formula nCr = n! / (r! * (n-r)!) [binomial coefficient]
|
||||
number = number * (i-j) / (j+1);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
// write another function to print pasclas triangle using recursion
|
||||
public static int pascal(int i, int j){
|
||||
if(j == 0 || j == i){
|
||||
return 1;
|
||||
}
|
||||
return pascal(i-1, j-1) + pascal(i-1, j);
|
||||
}
|
||||
|
||||
// write another function to print pascals triangle using memoization
|
||||
public static int pascalMemo(int i, int j, int[][] memo){
|
||||
if(j == 0 || j == i){
|
||||
return 1;
|
||||
}
|
||||
if(memo[i][j] != 0){
|
||||
return memo[i][j];
|
||||
}
|
||||
memo[i][j] = pascalMemo(i-1, j-1, memo) + pascalMemo(i-1, j, memo);
|
||||
return memo[i][j];
|
||||
}
|
||||
|
||||
// write another function to print pascals triangle using dynamic programming
|
||||
public static void pascalDP(int rows){
|
||||
int[][] dp = new int[rows][rows];
|
||||
for(int i=0; i<rows; i++){
|
||||
for(int j=0; j<=i; j++){
|
||||
if(j == 0 || j == i){
|
||||
dp[i][j] = 1;
|
||||
}else{
|
||||
dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i=0; i<rows; i++){
|
||||
for(int j=0; j<=i; j++){
|
||||
System.out.print(dp[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
// write another function to print pascals triangle using dynamic programming with space optimization
|
||||
public static void pascalDPSpaceOptimized(int rows){
|
||||
int[] dp = new int[rows];
|
||||
for(int i=0; i<rows; i++){
|
||||
for(int j=i; j>=0; j--){
|
||||
if(j == 0 || j == i){
|
||||
dp[j] = 1;
|
||||
}else{
|
||||
dp[j] = dp[j-1] + dp[j];
|
||||
}
|
||||
}
|
||||
|
||||
// does not save the entire triangle, only the current row
|
||||
// its indented in the loop to print the triangle
|
||||
for(int j=0; j<=i; j++){
|
||||
System.out.print(dp[j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
// wrie a code in java to print the rotation matrix using the functions calculateRotation and calculateTranspose
|
||||
import java.util.Scanner;
|
||||
class RotationMatrix{
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.print("Enter the number of rows and columns: ");
|
||||
int rows = sc.nextInt();
|
||||
int columns = sc.nextInt();
|
||||
int[][] matrix = new int[rows][columns];
|
||||
System.out.println("Enter the elements of the matrix: ");
|
||||
for(int i = 0; i < rows; i++){
|
||||
for(int j = 0; j < columns; j++){
|
||||
matrix[i][j] = sc.nextInt();
|
||||
}
|
||||
}
|
||||
System.out.println("Original Matrix: ");
|
||||
printMatrix(matrix);
|
||||
System.out.println("Transpose Matrix: ");
|
||||
printMatrix(calculateTranspose(matrix));
|
||||
System.out.println("Rotation Matrix: ");
|
||||
printMatrix(calculateRotation(matrix));
|
||||
sc.close();
|
||||
}
|
||||
public static int[][] calculateTranspose(int[][] matrix){
|
||||
int rows = matrix.length;
|
||||
int columns = matrix[0].length;
|
||||
int[][] transpose = new int[columns][rows];
|
||||
for(int i = 0; i < rows; i++){
|
||||
for(int j = 0; j < columns; j++){
|
||||
transpose[j][i] = matrix[i][j];
|
||||
}
|
||||
}
|
||||
return transpose;
|
||||
}
|
||||
public static int[][] calculateRotation(int[][] matrix){
|
||||
int rows = matrix.length;
|
||||
int columns = matrix[0].length;
|
||||
int[][] rotation = new int[columns][rows];
|
||||
for(int i = 0; i < rows; i++){
|
||||
for(int j = 0; j < columns; j++){
|
||||
rotation[j][rows - i - 1] = matrix[i][j];
|
||||
}
|
||||
}
|
||||
return rotation;
|
||||
}
|
||||
public static void printMatrix(int[][] matrix){
|
||||
for(int[] row : matrix){
|
||||
for(int element : row){
|
||||
System.out.print(element + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
// calculate rotation from transpose
|
||||
|
||||
public static void reverseArray(int[] arr){
|
||||
int start = 0;
|
||||
int end = arr.length - 1;
|
||||
while(start < end){
|
||||
int temp = arr[start];
|
||||
arr[start] = arr[end];
|
||||
arr[end] = temp;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
}
|
||||
public static int[][] calculateRotationFromTranspose(int[][] matrix){
|
||||
int transpose[][] = calculateTranspose(matrix);
|
||||
for (int i = 0; i < transpose.length; i++) {
|
||||
reverseArray(transpose[i]);
|
||||
}
|
||||
|
||||
return transpose;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
// write a program to generate a n * n matrix in spiral order given user input n and the elements of the matrix
|
||||
|
||||
import java.util.Scanner;
|
||||
class SpiralMatrixGeneration{
|
||||
|
||||
public static void main(String[] args){
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Enter the number of rows and columns of the matrix");
|
||||
int rows = sc.nextInt();
|
||||
int cols = sc.nextInt();
|
||||
int length = rows*cols;
|
||||
int[][] matrix = new int[rows][cols];
|
||||
int lengthArray[] = new int[length];
|
||||
System.out.println("Enter the elements of the matrix");
|
||||
for(int i=0;i<length;i++){
|
||||
lengthArray[i] = sc.nextInt();
|
||||
}
|
||||
System.out.println("The elements of the matrix in spiral order are:");
|
||||
spiralOrder(matrix,rows, cols, lengthArray);
|
||||
for(int i=0;i<rows;i++){
|
||||
for(int j=0;j<cols;j++){
|
||||
System.out.print(matrix[i][j]+" ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
public static void spiralOrder(int[][] matrix, int rows, int cols, int lengthArray[]){
|
||||
int top = 0;
|
||||
int bottom = rows-1;
|
||||
int left = 0;
|
||||
int right = cols-1;
|
||||
int length_index = 0;
|
||||
while(top<=bottom && left<=right){
|
||||
// this loop does not need to check for the condition top<=bottom and left<=right as
|
||||
for(int i=left;i<=right;i++){
|
||||
|
||||
matrix[top][i] = lengthArray[length_index++];
|
||||
}
|
||||
top++;
|
||||
// this loop does not need to check for the condition top<=bottom and left<=right as it is already checked in the while loop
|
||||
for(int i=top;i<=bottom;i++){
|
||||
matrix[i][right] = lengthArray[length_index++];
|
||||
}
|
||||
right--;
|
||||
if(top<=bottom){
|
||||
for(int i=right;i>=left;i--){
|
||||
matrix[bottom][i] = lengthArray[length_index++];
|
||||
}
|
||||
bottom--;
|
||||
}
|
||||
if(left<=right){
|
||||
for(int i=bottom;i>=top;i--){
|
||||
matrix[i][left] = lengthArray[length_index++];
|
||||
}
|
||||
left++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
// write a code in java to print the elements in a matrix in a spiral order using functions
|
||||
|
||||
import java.util.Scanner;
|
||||
class SpiralMatrixTraversal{
|
||||
public static void main(String[] args){
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Enter the number of rows and columns of the matrix");
|
||||
int rows = sc.nextInt();
|
||||
int cols = sc.nextInt();
|
||||
int[][] matrix = new int[rows][cols];
|
||||
System.out.println("Enter the elements of the matrix");
|
||||
for(int i=0;i<rows;i++){
|
||||
for(int j=0;j<cols;j++){
|
||||
matrix[i][j] = sc.nextInt();
|
||||
}
|
||||
}
|
||||
System.out.println("The elements of the matrix in spiral order are:");
|
||||
spiralOrder(matrix,rows,cols);
|
||||
sc.close();
|
||||
}
|
||||
public static void spiralOrder(int[][] matrix, int rows, int cols){
|
||||
int top = 0;
|
||||
int bottom = rows-1;
|
||||
int left = 0;
|
||||
int right = cols-1;
|
||||
while(top<=bottom && left<=right){
|
||||
// this loop does not need to check for the condition top<=bottom and left<=right as
|
||||
for(int i=left;i<=right;i++){
|
||||
System.out.print(matrix[top][i]+" ");
|
||||
}
|
||||
top++;
|
||||
// this loop does not need to check for the condition top<=bottom and left<=right as it is already checked in the while loop
|
||||
for(int i=top;i<=bottom;i++){
|
||||
System.out.print(matrix[i][right]+" ");
|
||||
}
|
||||
right--;
|
||||
if(top<=bottom){
|
||||
for(int i=right;i>=left;i--){
|
||||
System.out.print(matrix[bottom][i]+" ");
|
||||
}
|
||||
bottom--;
|
||||
}
|
||||
if(left<=right){
|
||||
for(int i=bottom;i>=top;i--){
|
||||
System.out.print(matrix[i][left]+" ");
|
||||
}
|
||||
left++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -45,7 +45,7 @@ calss TransposeMatrix {
|
|||
return result;
|
||||
}
|
||||
|
||||
public static int[][] transposeOriginalMatrix(int[][] matrix) {
|
||||
public static void transposeOriginalMatrix(int[][] matrix) {
|
||||
int rows = matrix.length;
|
||||
int cols = matrix[0].length;
|
||||
for (int i = 0; i < rows; i++) {
|
||||
|
|
@ -55,6 +55,5 @@ calss TransposeMatrix {
|
|||
matrix[j][i] = temp;
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue