Interview Question
Country: United States
// assume N<=9 (just for easier printing, not harder otherwise)
void rassclot()
{
static char ans[N+1] = {0};
static int i=0;
if(i==9) { puts(ans); return; }
for(int j=0; i<N; j++) {
ans[i]='0'+j;
i++, rassclot(), i--;
}
}
I think the best solution is to depict the problem as traversing n-ary tree with digits on its edges. When we reach the leaf we simply print accumulated value.
Here is code for my solution:
#include <cstdio>
int N;
void solution(int depth, int value)
{
if (depth == N)
printf("%d\n", value);
else {
for (int i=1; i<=N; ++i)
solution(depth+1, value*10+i);
}
}
int main()
{
scanf("%d", &N);
solution(0,0);
return 0;
}
Not sure how to cope with N higher than 9 but it depends on tasks constraints.
This will work please try this:-
public class Sequence {
static void printAllKLength(char set[], int k) {
int n = set.length;
printAllKLengthRec(set, "", n, k);
}
static void printAllKLengthRec(char set[], String prefix, int n, int k) {
if (k == 0) {
System.out.println(prefix);
return;
}
for (int i = 0; i < n; ++i) {
// Next character of input added
String newPrefix = prefix + set[i];
// k is decreased, because we have added a new character
printAllKLengthRec(set, newPrefix, n, k - 1);
}
}
public static void main(String []args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String str = new String();
for(int i=1; i<=n; i++)
{
str=str+i;
}
printAllKLength(str.toCharArray(),n);
}
}
public static void main(String args[]) {
String s = "6";
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(s);
int number = Integer.parseInt(s);
int i = 0;
int n = 0;
String trial = "";
for (int j = 0; j < number; j++) {
trial = trial + "1";
}
System.out.println("TRIAL:::"+trial);
String maxs= "";
for (int j = 0; j < number; j++) {
maxs = maxs + "9";
}
System.out.println("Max:::"+maxs);
if (number > 0) {
i = Integer.parseInt(trial);
n = Integer.parseInt(maxs);
}
for (; i <= n; i++) {
System.out.print(i);
System.out.println(",");
}
}
public static void print(int n) {
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i] = 1;
}
int k =1;
//get k=n^n for total rows
for (int j = 1; j <= n; j++) {
k = k * n;
}
for(int x = 1;x<=k;x++){
for(int i=0;i<n;i++){
System.out.print(a[i]);
}
a[a.length-1]++;
for(int z=a.length-1;z>0;z--){
if(a[z]>n){
a[z-1]++;
a[z]=a[z]%n==0?n:a[z]%n;
}
}
System.out.println();
}
}
package com.careercup.jenny;
import java.util.ArrayList;
public class Permutation {
public static void main(String[] args) {
int i =3;
ArrayList <StringBuffer> myList = getCombination(i, i, null);
for(StringBuffer sb : myList)
{
System.out.println(sb);
}
}
public static ArrayList <StringBuffer> getCombination (int n, int originalN, ArrayList <StringBuffer> myInputList)
{
ArrayList<StringBuffer> myOutput = new ArrayList <StringBuffer> ();
if (n == 0)
return myInputList;
if(myInputList == null || myInputList.size() == 0)
{
for(int i = 1; i <= originalN ; i ++)
{
StringBuffer sb = new StringBuffer();
sb.append(i);
myOutput.add(sb);
}
return getCombination(n-1, originalN, myOutput);
}
if(n > 0)
{
for(int i = 1; i <= originalN ; i ++)
{
for(StringBuffer inputSb: myInputList)
{
StringBuffer sb = new StringBuffer();
sb.append(i).append(inputSb);
myOutput.add(sb);
}
}
return getCombination(n-1,originalN, myOutput);
}
return null;
}
}
My solution is:
- blackball February 22, 2014