Interview Question
Software Trainees Applications DevelopersCountry: India
Interview Type: Written Test
Now my solution look kind of stupid.. the way you did
public static void printInPattern(int n){
if(n<=0){
System.out.println("NO NO Mr");
return;
}
int length = 2*n-1;
int printArr[][] = new int[length][length];
for(int i=0;i<n;i++)
fillArr(printArr, i);
for(int i=0;i<length;i++){
for(int j=0;j<length;j++)
System.out.print(""+printArr[i][j]+" ");
System.out.print("\n");
}
}
public static void fillArr(int[][] printArr, int i){
int N = printArr.length;
int k=i;
int val = (N+1)/2 -k;
//int i=0;
int j=0;
//start at daigonal a[i][i]
// from j=i to N-i-1
for(j=i;j<N-k;j++){
printArr[i][j]= val;
}
j--;
i++;
for(;i<N-k;i++){
printArr[i][j]= val;
}
i--;
j--;
for(;j>=k;j--)
printArr[i][j]= val;
j++;
i--;
for(;i>k;i--)
printArr[i][j]= val;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int N = 9;
for (int i = 0; i < 2 * N - 1; i++) {
int k = i < N ? i : 2 * (N - 1) - i;
for (int j = 0; j < k; j++)
cout << N - j;
for (int j = 0; j < 2 * (N - k) - 1; j++)
cout << N - k;
for (int j = k - 1; j >= 0; j--)
cout << N - j;
cout << endl;
}
return 0;
}
/**** output for N = 9
99999999999999999
98888888888888889
98777777777777789
98766666666666789
98765555555556789
98765444444456789
98765433333456789
98765432223456789
98765432123456789
98765432223456789
98765433333456789
98765444444456789
98765555555556789
98766666666666789
98777777777777789
98888888888888889
99999999999999999
***/
A simple and brute force approach by storing values to print in a 2D matrix .Though we can optimize it.
public static void printPattern(int x)
{
int len=x*2-1;
int leftCorner=0,rightCorner=len,upCorner=0,downCorner=len;
int a[][]=new int[len][len];
while(true)
{
if(leftCorner>rightCorner)
break;
else
for(int i=leftCorner;i<rightCorner;i++)
a[upCorner][i]=x;
upCorner++;// up row is visited
if(upCorner>downCorner)
break;
else
for(int i=upCorner;i<downCorner;i++)
a[i][rightCorner-1]=x;
rightCorner--;//right colum is visited
if(leftCorner>rightCorner)
break;
else
for(int i=rightCorner;i>=leftCorner;i--)
a[downCorner-1][i]=x;
downCorner--;//down row is visited
if(upCorner>downCorner)
break;
else
for(int i=downCorner;i>=upCorner;i--)
a[i][leftCorner]=x;
leftCorner++;//left column is visited
x--;//save next pattern
}
/*
* Print pattern
*/
for(int i=0;i<len;i++)
{
for(int j=0;j<len;j++)
{
System.out.print(a[i][j]);
}
System.out.println("");
}
}
Java code.
public static void draw(int N) {
int N = Integer.parseInt(args[0]);
if (N > 0) {
int m = 2 * (N - 1) + 1;
int mid = N - 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
int d = Math.max(Math.abs(i -mid), Math.abs(j - mid)) + 1;
System.out.print(d);
}
System.out.print("\n");
}
}
}
Here is the C++ code.
Basically, you need the print the maximum of the x-distance or y-distance to the center position
#include <iostream>
using namespace std;
int abs(int i) {
return (i < 0 ? -i : i);
}
void print(int n) {
for (int i = 0; i<2*n-1; ++i) {
for (int j = 0; j < 2*n-1; ++j)
printf("%d", (abs(i-n+1) > abs(j-n+1) ? abs(i-n+1) : abs(j-n+1))+1);
printf("\n");
}
}
int main() {
print(7);
return 0;
}
Here is my little solution:
package other;
public class PrintSomeNumbers {
private static int[] array = null;
private static int number = -1;
private static int direction = 1;
public static void main(String[] args){
printThem(9);
}
public static void printThem(int n){
if(n < 1)
return;
if(n == 1){
System.out.println(1);
}
number = n;
int size = (n*2) - 1;
array = new int[size];
for(int i =0; i < array.length; i++){
array[i] = number;
}
int index = 0;
printThemRec(index);
}
public static void printThemRec(int ind){
int rightIndex = (array.length - 1) - ind;
int index = ind;
//Case when the left crosses right
//which is time to reverse.
if(index > rightIndex){
direction = -1;
}
//for going backwards
if(ind < 0)
return;
//current number to print
int curNum = number - index;
//print within the range
while(index <= rightIndex){
//System.out.println(curNum);
array[index] = curNum;
index++;
}
//omits the two extra printings before the middle one.
if(ind != number -1)
printIt();
printThemRec(ind+(1*direction));
}
private static void printIt(){
for(int i : array){
System.out.print("|"+i);
}
System.out.println("|");
}
}
There will be always a center point that has value of 1 (Let's call it Layer 1).
Then, surround it with number 2 in all directions (Let's call it Layer 2)
Then, surround all with number 3 in all directions (Let's call it Layer 3)
and so on...
Basically, n will represent how many Layers are there...
Java code... Without nested loops...
private static void print2(int n) {
int length = n * 2 - 1;
// long trim = Long.parseLong(Long.toBinaryString((long) (Math.pow(2, length) - 1)), 10);
// long highTrim = (long) Math.pow(10, length - 1);
long trim = 1, lowTrim = 1, highTrim = 1;
for (int x = 0; x < length - 1; x++) {
trim = trim * 10 + 1;
highTrim = highTrim * 10;
}
// trim = 1111111
// highTrim = 1000000
long number = trim * n;
for (int i = 0; i < length; i++) {
System.out.println(number);
// remove 1 from left, 1 from right and subtract 1 in between ;)
trim = trim - highTrim - lowTrim;
number = number - trim;
lowTrim = lowTrim * 10;
highTrim = highTrim / 10;
}
}
Print lowTrim, highTirm and trim inside the loop to better understand the logic.
Python version:
#generate first line
line=str(n)*(2*n-1)
#array for the unique numbers/lines
rows=[]
# generate the unique rows
for i in range(len(line)/2+1):
line=line[0:i]+str(n-i)*(2*(n-i)-1)+line[2*n-1-i:2*n-1]
rows.append(line)
#print the matrix
for i in range(len(rows)):
print rows[i]
for i in range(len(rows)-1):
print rows[len(rows)-2-i]
#include "stdlib.h"
#include "stdio.h"
int main(int argc, char* argv[]) {
int i, j, n, k;
unsigned int abs_i, abs_j;
if(argc != 2) {
return 0;
}
n = atoi(argv[1]);
for(i = -(n-1); i < n; i++) {
for(j = -(n-1); j < n; j++) {
abs_i = (i < 0) ? (-i) : (i) ;
abs_j = (j < 0) ? (-j) : (j) ;
k = (abs_i > abs_j) ? (abs_i) : (abs_j);
printf("%d ",k+1);
}
printf("\n");
}
return 0;
}
public class testStrings {
public static void main(String[] args)
{
/*input N = 4, print below pattern
4444444
4333334
4322234
4321234
4322234
4333334
4444444 */
int max = 4;
int n = 4;
List<String> list = new ArrayList<String>();
while(n > 0)
{
String s = "";
for (int i = (n * 2) - 1; i > 0; i--)
{
s += n;
}
if (list.size() == 0)
{
list.add(s);
}
else
{
String source = list.get(list.size() - 1);
char sourceArr[] = source.toCharArray();
char destArr[] = s.toCharArray();
int j = 0;
int pos = max - n;
int len = sourceArr.length;
for (int i = pos; i < (len - pos); i++)
{
sourceArr[i] = destArr[j];
j++;
}
source = "";
for (int k = 0; k < len; k++)
{
source += sourceArr[k];
}
list.add(source);
}
n--;
}
List<String> finalList = new ArrayList<String>();
finalList.addAll(list);
for (int i = list.size() - 2; i >= 0; i--)
{
finalList.add(list.get(i));
}
list = null;
for (String s: finalList)
{
System.out.println(s);
}
}
}
#include <stdio.h>
int main(){
int i, j, k, n, flag = 0;
int number_to_print, number_to_repeat, next_number_to_repeat;
printf("Please enter the number\n");
scanf("%d", &n);
number_to_print = n;
for(i = 0; i < n; i++){
//--next_number_to_repeat;
number_to_print = n;
for(j =0 ; j< (2*n-1); j++){
printf("%d ", number_to_print);
if( j < i )
--number_to_print;
else if( j > ( (n*2)- i -3 ) )
++number_to_print;
}
printf("\n");
}
#if 1
for(i = n-2; i >= 0; i--){
//--next_number_to_repeat;
number_to_print = n;
for(j =0 ; j < (2*n-1); j++){
printf("%d ", number_to_print);
if( j < i )
--number_to_print;
else if( j > ( (n*2)- i -3 ) )
++number_to_print;
}
printf("\n");
}
#endif
return 0;
}
I solved it essentially looking at the closest side and calculating the proper value to se.
public static void printPattern (int n)
{
int width = n*2 - 1;
int height = n*2 - 1;
StringBuilder pattern = new StringBuilder();
System.out.println("Pattern of "+n);
for (int row=0; row<height; row++)
{
for (int col=0; col<width; col++)
{
int left = col;
int right = width-col-1;
int top = row;
int bottom = height-row-1;
int lr = Math.min(left, right);
int tb = Math.min(top, bottom);
int closest = Math.min(lr, tb);
pattern.append(""+(n-closest));
}
pattern.append("\n");
}
System.out.println(pattern.toString());
}
public class CombinationPrint {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("Enter the no.");
int n = sc.nextInt();
int arr[] =new int[(2*n)-1];
int j=0;
int k=(2*n)-1;
int l=2;
for(int i = 0; i < ((2 * n) - 1); i++) {
if((n-i)>0){
for(j=0;j<k;j++){
arr[j+i]=n-i;
}
k=k-2;
}else{
if(k==-1){
k=3;
}
for(j=0;j<k;j++){
arr[j+i-l]=i+2-n;
}
k=k+2;
l=l+2;
}
print(arr);
j=0;
System.out.println("");
}
}
private static void print(int[] arr) {
for(int x=0;x<arr.length;x++){
System.out.print(arr[x]);
}
}
}
public class CombinationPrint {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("Enter the no.");
int n = sc.nextInt();
int arr[] =new int[(2*n)-1];
int j=0;
int k=(2*n)-1;
int l=2;
for(int i = 0; i < ((2 * n) - 1); i++) {
if((n-i)>0){
for(j=0;j<k;j++){
arr[j+i]=n-i;
}
k=k-2;
}else{
if(k==-1){
k=3;
}
for(j=0;j<k;j++){
arr[j+i-l]=i+2-n;
}
k=k+2;
l=l+2;
}
print(arr);
j=0;
System.out.println("");
}
}
private static void print(int[] arr) {
for(int x=0;x<arr.length;x++){
System.out.print(arr[x]);
}
}
}
package com.practice;
public class Matrix {
public static void main(String[] args) {
Matrix m = new Matrix();
int[][] a = m.fillValues(3);
for (int[] is : a) {
for (int i : is) {
System.out.print(i + " ");
}
System.out.println();
}
}
private int[][] fillValues(int n){
int first = 0;
int last = 2*n - 1;
int i = first;
int j = first;
int[][] a = new int[last][last];
while(n>0){
i = first;
j = first;
for (; i < last; i++) {
a[i][j] = n;
}
i--;
for (; j < last; j++) {
a[i][j] = n;
}
j--;
for (; i > first; i--) {
a[i][j] = n;
}
for (; j > first; j--) {
a[i][j] = n;
}
n--;
first++;
last--;
}
return a;
}
}
public class Matrix {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int value = s.nextInt();
int actuallength = value * 2;
int matrixLength = actuallength - 1;
int[][] a = new int[matrixLength][matrixLength];
for (int i = 0; i < actuallength / 2; i++) {
for (int j = i; j < (matrixLength - i); j++) {
a[i][j] = value;
a[j][i] = value;
a[matrixLength - i - 1][j] = value;
a[j][matrixLength - i - 1] = value;
}
value--;
}
for (int i = 0; i < matrixLength; i++) {
for (int j = 0; j < matrixLength; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
import java.io.*;
import java.util.*;
public class Trial {
public static void main(String [] args)
{
int n, row, col,i,j;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
row = 2*n - 1;
col = row;
int [][] matrix = new int [row][col];
for(i=0; i<row; i++)
{
for( j=i; j<col-i; j++)
{
matrix[i][j] = n;
}
for(j=i+1; j<col-i; j++)
{
matrix[j][col-i-1] = n;
}
for(j=i; j<col-i-1; j++)
{
matrix[col-i-1][j] = n;
}
for(j=i+1; j<col-i-1; j++)
{
matrix[j][i] = n;
}
n--;
}
for(i=0; i<row; i++)
{
for(j=0;j<col; j++)
System.out.print(matrix[i][j] +" ");
System.out.println();
}
}
}
public class PrintNums {
public static void main(String[] args) {
// TODO Auto-generated method stub
int N = 9, k = 0;
int [][] matrix = new int [2*N-1][2*N-1];
int counter = 0, counterLast = N-2;
for(int i = 0; i<matrix.length; i++){
for(int j = 0; j<matrix[0].length ; j++){
if(i < (matrix.length + 1)/2){
if( i == 0){
matrix[i][j] = N;
}
else if(j < k || j > (matrix[0].length-1) - k){
matrix[i][j] = matrix[i-1][j];
}
else{
matrix[i][j] = N - k;
}
}
else{
matrix[i][j] = matrix[counterLast][j];
}
}
k++;
if(i >= (matrix.length + 1)/2){
counterLast--;
}
}
for(int i = 0; i<matrix.length; i++){
System.out.println();
for(int j = 0; j<matrix.length; j++){
System.out.print(matrix[i][j] + " ");
}
}
}
}
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,row,col,x,n,tmp,temp;
printf("Enter the number:");
scanf("%d",&n);
row=n-1;
col=n+row;
x=1-row;
temp=col-1;
int a[col][col];
for(tmp=0;tmp<col;tmp++)
{
for(i=0;i<col;i++)
{
for(j=0;j<col;j++)
{
if(i==tmp || j==tmp || i==temp || j==temp)
a[i][j]=x;
}
}
temp--;
x++;
}
for(i=0;i<col;i++)
{
for(j=0;j<col;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
getch();
}
#include<stdio.h>
void main()
{
int i,j,n=0,k=0,l=0;
printf("\n Enter n \n");
scanf("%d",&n);
printf("\n\n");
for(i=0;i<(2*n)-1;i++)
{
for(j=0;j<(2*n)-1;j++)
{
if(j>=n)
k=(2*n)-j-2;
else
k=j;
if(i>=n)
l=(2*n)-i-2;
else
l=i;
if((j==0) || (j==(2*n)-2) || (i==0) || (i==(2*n)-2))
printf("%d",n);
else if(k==l)
printf("%d",n-k);
else if(l<k)
printf("%d",n-l);
else
printf("%d",n-k);
}
printf("\n");
}
}
#include<stdio.h>
int main()
{ int a[7],j,z,k,m,o,i,p,l;
m=4;
i=0;
z=6;
p=4;
for(j=0;j<=6;j++)
{if(j<=3)
{for(k=j;k<=z;k++)
{ a[k]=m;
}
for(o=0;o<=6;o++)
{printf("%d",a[o]);}
m--;
z=z-1;
}
else
{
for(l=p;l<=j;l++)
{a[p-1]=a[p];}
for(o=0;o<=6;o++)
{printf("%d",a[o]);}
}
printf("\n");
p=p-1;
}
return 0;
}
#include<stdio.h>
int main()
{ int a[7],j,z,k,m,o,i,p,l;
m=4;
i=0;
z=6;
p=4;
for(j=0;j<=6;j++)
{if(j<=3)
{for(k=j;k<=z;k++)
{ a[k]=m;
}
for(o=0;o<=6;o++)
{printf("%d",a[o]);}
m--;
z=z-1;
}
else
{
for(l=p;l<=j;l++)
{a[p-1]=a[p];}
for(o=0;o<=6;o++)
{printf("%d",a[o]);}
}
printf("\n");
p=p-1;
}
return 0;
}
#include<stdio.h>
int main()
{ int a[7],j,z,k,m,o,i,p,l;
m=4;
i=0;
z=6;
p=4;
for(j=0;j<=6;j++)
{if(j<=3)
{for(k=j;k<=z;k++)
{ a[k]=m;
}
for(o=0;o<=6;o++)
{printf("%d",a[o]);}
m--;
z=z-1;
}
else
{
for(l=p;l<=j;l++)
{a[p-1]=a[p];}
for(o=0;o<=6;o++)
{printf("%d",a[o]);}
}
printf("\n");
p=p-1;
}
return 0;
}
public class Beta {
public static void main(String args[])
{
int i=0,j=0,N=9,k=0,n=2*N-1;
int a[][]=new int[17][17];
for(k=0;k<=n/2;k++)
{
for(i=k;i<n-k;i++)
{
for(j=k;j<n-k;j++)
{
a[i][j]=N;
}
}
N=N-1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.print("\n");
}
}
}
public class Beta {
public static void main(String args[])
{
int i=0,j=0,N=9,k=0,n=2*N-1;
int a[][]=new int[17][17];
for(k=0;k<=n/2;k++)
{
for(i=k;i<n-k;i++)
{
for(j=k;j<n-k;j++)
{
a[i][j]=N;
}
}
N=N-1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.print("\n");
}
}
}
public class Beta {
public static void main(String args[])
{
int i=0,j=0,N=9,k=0,n=2*N-1;
int a[][]=new int[17][17];
for(k=0;k<=n/2;k++)
{
for(i=k;i<n-k;i++)
{
for(j=k;j<n-k;j++)
{
a[i][j]=N;
}
}
N=N-1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.print("\n");
}
}
}
The value in position (i,j) is 1 plus the maximum of the horizontal distance from the center and the vertical distance from the center. In Java a function to print this would look like:
- Guy March 08, 2014