Linkedin Interview Question
<pre lang="" line="1" title="CodeMonkey35705" class="run-this">class Problem {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = { 1, 2, 3, 4, 5, 6, 7};
Problem.printArr(nums, 5);
}
private static void printArr(int[] nums, int numCols) {
int numRows = nums.length / numCols;
int numLastRow = nums.length % numCols;
if (numLastRow > 0) {
numRows++;
}
int[][] matrix = new int[numRows][numCols];
int arrIndex = 0;
for (int i = 0; i < numCols; i++) {
for (int j = 0; j < numRows; j++) {
// If we're on the bottom row and over the number
// of items to be put on it just skip this iteration
if ((j == numRows - 1) && i >= numLastRow) {
continue;
}
matrix[j][i] = nums[arrIndex];
arrIndex++;
if (arrIndex >= nums.length)
break;
}
if (arrIndex >= nums.length)
break;
}
arrIndex = 0;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (matrix[i][j] > 0)
System.out.print(String.format("%3d", matrix[i][j]));
if (arrIndex >= nums.length)
break;
}
System.out.println();
if (arrIndex >= nums.length)
break;
}
}
}
</pre>
Realize that, the difference between two column lengths is AT MAX 1.
If..
len = sizeof(input)
Num of columns = col.
Then,
All columns would have a minimum of (floor)len/col elements.
The first (len%col) columns would have 1 extra element
For len = 7
num of col = 3
All columns have min of (floor)7/3 = 2 elements
The first (7%3) rows have 1 extra element.
That is 3 for the first col. 2 for the rest of cols
public static int[][] represent(int[] a, int cols) {
int size = a.length;
int elementsPerCol = size / cols;
int remain = size % cols;
int[][] output;
if (remain <= 0)
output = new int[elementsPerCol][cols];
else
output = new int[elementsPerCol + 1][cols];
int k = 0;
for (int i = 0; i < output[0].length; i++) {
for (int j = 0; j < output.length - 1; j++) {
if (k >= a.length)
return output;
output[j][i] = a[k];
k++;
}
if (remain > 0) {
output[output.length - 1][i] = a[k];
remain--;
k++;
}
}
return output;
}
O(n), O(1)
void dispArr(int arr[], int n, int c) {
int nr = ((n%c) == 0) ? n/c : n/c + 1;
int nlast = (n % c == 0) ? c : n%c;
for (int r = 0; r < nr; r++) {
int nc = (r < nr-1) ? c : (n%c == 0) ? c : n%c;
int inc = nr, ind = r;
for (int i = 0; i < nc; i++) {
cout << arr[ind] << " ";
inc = (i < nlast) ? inc : inc - 1;
ind += inc;
}
cout << endl;
}
}
void display(int *arr, int size, int cols){
if(arr == NULL || cols == 0 || size == 0) return;
int q = size/cols;
int r = size - q*cols;
int i, j, offset[cols], rowsV[cols], idx;
offset[0] = 0;
for(j = 1; j < cols; j ++){
offset[j] = offset[j-1] + q;
offset[j] += (j-1)<r ? 1 : 0;
}
idx = 0;
for(i = 0; i < q+1; i ++){
for(j = 0; j < cols; j ++){
if(idx++ < size) cout << arr[offset[j]+i] << " ";
}
cout << endl;
}
}
Do we need actually have to create a matrix? How about this?
public void printMatrix(int[] a, int col){
int len = a.length, count =0;
for(int i=0;i<len;i++){
if(count == col){
System.out.println();count=0;
}
if(count < col){
System.out.print(a[i] + " "); count++;
}
}
}
int nRow = len / nColumn;
int res = len % nColumn;
if( res != 0 ) nRow++;
for( int i = 0; i < nRow; i++ ) {
for( int j = 0; j < nColumn; j++ ) {
int index = 0;
if( j > res )
index = i*nColumn + res*nRow + (j-res)*(nRow-1);
else
index = i*nColumn + j*nRow;
cout << arr[index] << " ";
}
cout << endl;
}
sorry it is wrong.
int nRow = len / nColumn + 1;
int res = len % nColumn;
for( int i = 0; i < nRow; i++ ) {
for( int j = 0; j < nColumn; j++ ) {
if( i*nColumn+j >= len ) break;
int index = 0;
if( j > res )
index = i + res*nRow + (j-res)*(nRow-1);
else
index = i + j*nRow;
cout << arr[index] << " ";
}
cout << endl;
}
void printTable(final int [] array, final int columns)
{
final int length = array.length;
final int rows = length / columns;
final int additional = length % columns;
for(int i = 0; i < rows; i++)
{
int k = i;
for(int j = 0; j < columns; j++)
{
System.out.print(array[k] + ((j != columns - 1) ? " " : " \n"));
k += ((j < additional) ? (rows + 1): rows);
}
}
int k = rows;
for(int j = 0; j < additional; j++)
{
System.out.print(array[k] + ((j != additional - 1) ? " " : " \n"));
k += (rows + 1);
}
}
public class ArrayDisp {
public static void main(String arg[]){
int[] arr = {1,2,3,4,5,6,7,8,9,10};
ArrayDisp.display(arr,2);
}
static void display(int arr[], int cols){
for(int i=0; i< arr.length; i++){
System.out.print(arr[i]);
if(cols>0){
if((i+1)%cols == 0){
System.out.print("\n");
}
}
}
}
}
With column input, we can know row = len(array) / column
If devision is clean with no residue, then the row value is perfect.
Otherwise, there will be residue < column,
j
row1
i row2 i-j
row3
res1 res2 res3
if (residue==0)
i-j = array[row*j+i]
else
if (j<residue)
i-j = array[(row+1)*j+i]
else
i-j = array[(row+1)*residue+ row*(j-residue)+i]
/*
* Display an integer array of [1, 2, 3, 4, 5, 6, 7] in the following format
1 4 6
2 5 7
3
The method signature takes in an array of integers and the number of columns.
In the above example, noOfCols = 3. The columns should contain equal number
of elements as much as possible.
*/
public class App {
public static void main(String[] args) {
int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 };
int columnsplit = 5;
int i = 0;
int j = 0;
int printrow = 0;
for (i = 0; i < arr.length / columnsplit; i++) {
int rowmod = arr.length % columnsplit;
printrow = i;
System.out.print(arr[printrow] + " ");
for (j = 1; j < columnsplit; j++) {
printrow = printrow + arr.length / columnsplit;
if (rowmod > 0) {
printrow++;
rowmod--;
}
System.out.print(arr[printrow] + " ");
}
System.out.println();
}
int rowmod = arr.length % columnsplit;
printrow = arr.length / columnsplit;
while (rowmod > 0) {
System.out.print(arr[printrow] + " ");
printrow = printrow + arr.length / columnsplit + 1;
rowmod--;
}
}
}
#include<stdio.h>
#include<stdlib.h>
void print_hori_equally(int *arr, int no_cols, int N){
int i, j;
int min_rows = N/no_cols;
int extra_cols = N%no_cols;
int add=1;
if(extra_cols == 0)
add=0;
for(i=0;i<min_rows+add;i++)
{
for(j=0;j<no_cols;j++)
{
if(i == (min_rows+add-1) && j == extra_cols)
return;
if(j<=extra_cols)
printf("%d ", arr[i+j*(min_rows+add)]);
else
printf("%d ", arr[i+extra_cols*(min_rows+add) + (j-extra_cols)*(min_rows)]);
}
printf("\n");
}
}
int main(){
int i, N=100;
int no_of_cols = 6;
int * arr = (int *)malloc(sizeof(int)*N);
for(i = 0; i<N; i++)
arr[i] = i+1;
print_hori_equally(arr, no_of_cols, N);
getchar();
return 0;
}
public static void dispNum(int[] ia, int col) {
int extra = ia.length % col;
int row = ia.length / col;
if ( extra > 0)
row++;
System.out.println(row);
StringBuilder sb[] = new StringBuilder[row];
for(int i = 0 ; i < sb.length; i++ ){
sb[i] = new StringBuilder();
}
int i = 0;
for(int e = 0 ; e < extra; e++){
for(int j = 0 ; j < row; j++)
sb[i % row].append(ia[i++]);
}
for(; i < ia.length;){
sb[(i - extra * row) % (row - 1)].append(ia[i++]);
}
for(StringBuilder sba: sb)
System.out.println(sba.toString());
}
public static void display( int [] arr, int cols){
int baseRows = arr.length / cols;
int extras = arr.length % cols;
//print the complete rows
for(int i = 1; i<=baseRows; i++){
int j = extras;
for(int k=i-1; k<arr.length;k=k+baseRows){
System.out.print(arr[k]+"\t");
if(j>0){
j--;
k++;
}
}
System.out.println();
}
//print the incomplete row
int i = baseRows;
while(extras!=0){
System.out.print(arr[i]+"\t");
i=i+baseRows+1;
extras--;
}
}
{
public class ArrayDisp {
public static void main(String arg[]){
int[] arr = {1,2,3,4,5,6,7,8,9,10};
display(arr,3);
}
static void display(int a[], int cols)
{
int rows = a.length/cols;
int x[][]=new int[rows][cols];
for (int i=0;i<cols;i++)
{
for (int j=0;j<rows;j++)
{
x[i][j]=a[j*cols+i];
}
}
for (int i=0;i<cols;i++)
{ for (int j=0;j<rows;j++)
{System.out.print(x[i][j]);
}
System.out.println();
}
}
}
sorry , please ignore previous code : this is correct and works for all inputs ... O(n^2)
public class ArrayDisp {
public static void main(String arg[]){
int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12};
display(arr,3);
}
static void display(int a[], int cols)
{
int rows = a.length/cols;
int x[][]=new int[rows][cols];
for (int i=0;i<rows;i++)
{
for (int j=0;j<cols;j++)
{
x[i][j]=a[j*rows+i];
}
}
for (int i=0;i<rows;i++)
{ for (int j=0;j<cols;j++)
{System.out.print(x[i][j]+ " ");
}
System.out.println();
}
}
}
At a given position (i,j) the number to print is the vertical count of the numbers before it.
Random r = new Random();
int size = r.nextInt(100)+1, k = size/10;
int m=size/k, n = size%k;
System.out.printf("%d, %d\n", size, k);
for (int j=0; j<=m; j++) {
for (int i=0; i<k; i++) {
int d = i<n? i*(m+1)+j : n*(m+1)+(i-n)*m+j;
d=d+1;
if (j*k+i<=size) System.out.printf("%d\t",d);
}
System.out.println();
}
The two expressions can be further reduced.
i*(m+1)+j = i*m+i+j
n*(m+1)+(i-n)*m+j = i*m+n+j
so
int d = i*m+j+ (i<n? i:n);
In its simplest format:
Random r = new Random();
int size = r.nextInt(100)+1, k = size/10;
// int size=7, k=3;
int m=size/k, n = size%k;
System.out.printf("%d, %d\n", size, k);
for (int j=0; j<=m; j++) {
for (int i=0; i<k; i++) {
int d= 1+i*m+j+ (i<=n? i:n);
if (j*k+i<size) System.out.printf("%d\t",d);
}
System.out.println();
}
}
public static void main(String[] args) {
int arr[]={1,2,3,4,5,6,7};
int columns = 3;
printArr(arr,columns);
}
private static void printArr(int[] arr, int columns) {
for(int i=0;i<columns;i++){
for(int j=i;j<arr.length;j+=columns){
System.out.print(arr[j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
int arr[]={1,2,3,4,5,6,7};
int columns = 3;
printArr(arr,columns);
}
private static void printArr(int[] arr, int columns) {
for(int i=0;i<columns;i++){
for(int j=i;j<arr.length;j+=columns){
System.out.print(arr[j]+" ");
}
System.out.println();
}
}
def printArrayMatrix(a, numCols):
if len(a) == 0:
print "Array with len 0"
return
lenA = len(a)
numRows = int(lenA/numCols)
numbersOnLastRow = lenA%numCols
print "numRows: ", numRows, "numbersOnlastRow: ", numbersOnLastRow
if numbersOnLastRow:
numRows += 1
matrix = [[0 for i in range(numCols)] for j in range(numRows)]
numLast = 0
index = 0
for currentCol in range(numCols):
for currentRow in range(numRows):
print "numRows: ", numRows
matrix[currentRow][currentCol] = a[index]
index += 1
if index > lenA -1:
break
numLast += 1
if numLast == numbersOnLastRow:
numRows -= 1
for row in matrix:
for col in row:
if col:
print col,
print ''
printArrayMatrix([1,2,3,4,5,6,7],3)
void printNumbers(int A[], int n, int col)
{
if(col <=0 )
{
cout<<"col error"<<endl;
return;
}
for(int i=0; i<col; i++)
{
for(int j=i; j<n; j+=col)
cout<<A[j]<<"\t";
cout<<endl;
}
}
x=(Size of the array)%noOfCols
- Vinod May 09, 2011First x columns alone put (Size of the array)/noOfCols + 1 elements, remaining columns put (Size of the array)/noOfCols elements