Jabongg
BAN USERHeres the java implementation of the spiral matrix print.
//package com.learning.java;
/**
* Created by ejangpa on 1/13/2017.
*/
public class PrintArraySpirally {
public static void main(String[] args) {
int n = 4;
int m = 4;
int[][] number = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
System.out.println("Initial Array");
printArray(number);
System.out.println("Spiral Array");
printSpirallyMyCodeSchool(number, n, m);
}
/**
* printSpirally prints array in spiral manner
* @param number
*/
static void printSpirallyMyCodeSchool(int[][] number, int n, int m) {
int T = 0;
int R = n - 1;
int B = m - 1;
int L = 0;
int dir = 0;
while (L <= R && T <= B) {
if(dir == 0) {
for (int k = L; k <= R; k++) {
System.out.println(number[T][k]);
}
T++;
} else if (dir == 1) {
for(int k = T; k <= B; k++) {
System.out.println(number[k][R]);
}
R--;
} else if (dir == 2) {
for (int k = R; k >= L; k--) {
System.out.println(number[B][k]);
}
B--;
} else if (dir == 3) {
for (int k = B; k >= T; k--) {
System.out.println(number[k][L]);
}
L++;
}
dir = (dir + 1) % 4;
}
}
/**
* printArray() prints array Elements
*/
static void printArray(int[][] number) {
for (int i = 0; i < number.length; i++) {
for(int j = 0; j < number.length; j++) {
System.out.print(number[i][j] + "\t");
}
System.out.println();
}
}
}
Here's my JavaCode .
import java.util.Scanner;
/**
* Created by ejangpa on 1/12/2017.
*/
public class SumOfMulitplicationExceptNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arrayA = new int[n];
for(int i = 0; i < n; i++) {
arrayA[i] = scanner.nextInt();
}
int[] arrayB = new int[n];
int multiplicationValue = 1;
for(int i = 0; i < n; i++) {
multiplicationValue = multiplicationValue * arrayA[i];
}
if(multiplicationValue > 0) {
sumOfProduct(arrayA, arrayB, multiplicationValue);
} else {
return;
}
}
static void sumOfProduct(int[] arrayA, int[] arrayB, int multiplicationValue) {
for(int i = 0; i < arrayA.length; i++) {
arrayB[i] = multiplicationValue / arrayA[i];
}
printArray(arrayB);
}
static void printArray(int[] arrayB) {
for(int i = 0; i < arrayB.length; i++) {
System.out.print(arrayB[i] + " ");
}
System.out.println();
}
}
Here's my solution with extra space.
package com.learning.java.library;
/**
* Created by ejangpa on 1/12/2017.
*/
public class ArrayRotationLeftBy90Degree {
public static void main(String[] args) {
int[][] arrayA = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] arrayB = new int[3][3];
System.out.println("Initial Array :");
printArray(arrayA);
System.out.println();
System.out.println("Left Rotated Array");
rotateArrayAntiClockwise(arrayA, arrayB);
int[][] arrayC = new int[3][3];
System.out.println("Array Rotated Again :");
rotateArrayAntiClockwise(arrayB, arrayC);
System.out.println("Right Rotated Array");
rotateArrayClockwise(arrayA, arrayB);
}
static void rotateArrayAntiClockwise(int[][] arrayA, int[][] arrayB) {
for(int i = 0; i < arrayA.length; i++) {
for(int j = 0 ; j <arrayA.length; j++) {
int k = i;
int l = (arrayA.length - 1) - j;
arrayB[l][k] = arrayA[i][j];
}
}
printArray(arrayB);
}
static void rotateArrayClockwise(int[][] arrayA, int[][] arrayB) {
for(int i = 0; i < arrayA.length; i++) {
for(int j = 0 ; j <arrayA.length; j++) {
int k = (arrayA.length - 1 ) - i;
int l = j;
arrayB[l][k] = arrayA[i][j];
}
}
printArray(arrayB);
}
static void printArray(int[][] arrayB) {
for(int i = 0; i < arrayB.length; i++) {
for(int j = 0; j < arrayB.length; j++) {
System.out.print(arrayB[i][j] + " ");
}
System.out.println();
}
}
}
Here is my Java Solution using LinkedHashMap...
- Jabongg January 25, 2017