public static void noRepeat(int n) { int[] ar; for(int i=1;i1) { flag = 1; } temp = temp/10; } if(flag==0) { System.out.println(i); } } }

Your answer is correct, but it is slow. Here you are an algorithm that does same thinkg as you do, but also one more algorithm - using permutations. Instead of outputting to System.out I simply calculate how many numbers are there. Run and see how slow is your method for n = 8 (where n is the number of digits). import java.io.*; import java.util.*; public class NumbersNoRepeatDigits{ public class StopWatch { private long startTime = 0; private long stopTime = 0; private boolean running = false; public void start() { this.startTime = System.currentTimeMillis(); this.running = true; } public void stop() { this.stopTime = System.currentTimeMillis(); this.running = false; } //elaspsed time in milliseconds public long getElapsedTime() { long elapsed; if (running) { elapsed = (System.currentTimeMillis() - startTime); } else { elapsed = (stopTime - startTime); } return elapsed; } //elaspsed time in seconds public long getElapsedTimeSecs() { long elapsed; if (running) { elapsed = ((System.currentTimeMillis() - startTime) / 1000); } else { elapsed = ((stopTime - startTime) / 1000); } return elapsed; } } // mode complicated solution public static void permuteArray(int[] a){ permuteArrayCore(a, a.length - 1); } public static void permuteArrayCore(int[] a, int i){ if (i == -1) { if (a[0] > 0) printArray(a); return; } permuteArrayCore(a, i-1); // when ith element is on place; for (int j=i-1; j>=0; j--) { //swap i and j swap(a, i, j); permuteArrayCore(a, i-1); swap(a, i, j); } } private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void selectMAmong10(int m) { boolean[] take = new boolean[10]; selectMAmongNCore(m, 0, take, 0); } public static void selectMAmongNCore(int m, int next, boolean[] take, int countTaken) { if (countTaken == m){ int[] x = new int[m]; int xi = 0; for (int i = 0; i = m - countTaken) selectMAmongNCore(m, next + 1, take, countTaken); } // simpliest solution public static boolean checkDistinct(int i) { boolean[] digits = new boolean[10]; while (i > 0) { int rem = i % 10; if (digits[rem]) return false; digits[rem] = true; i = i / 10; } return true; } public static int totalCount = 0; public static void printArray(int[] a){ totalCount++; /* for (int i = 0; i < a.length; ++i) System.out.format("%d ", a[i]); System.out.println(); */ } public static void main(String[] args){ int n = 8; NumbersNoRepeatDigits t = new NumbersNoRepeatDigits(); NumbersNoRepeatDigits.StopWatch st = t.new StopWatch(); st.start(); for (int i = 1; i <= n; ++i) selectMAmong10(i); System.out.println(totalCount); System.out.println(st.getElapsedTime()); totalCount = 0; System.out.println("-----------"); int max = (int)Math.pow(10, n); for (int i = 1; i < max ; ++i) { if (checkDistinct(i)) // System.out.println(i); printArray(null); } System.out.println(totalCount); st.stop(); System.out.println(st.getElapsedTime()); } }

looks really simple (if I understood it right). Here is the SHORT java version. static boolean hasDupDigit(int i){ boolean[] digits = new boolean[10]; while(i!=0){ int r = i%10; if(digits[r])return true; digits[r] = true; i/=10; } return false; } static void printNums(){ for (int i=1;i<=1000000;i++) if(!hasDupDigit(i)) System.out.println(i); }

public static void main(String[] args) { for(int i=1;i<1000000;i++){ if(!hasDup(i)) System.out.println(i); } } private static boolean hasDup(int i) { String num=Integer.toString(i); boolean ret=false; char[] carr=num.toCharArray(); for(int k=0;k

Permutations anyone? #include using namespace std; bool used[10]; // init to false int count = 0; int num = 1; void backtrack (int pos) { if (pos == 6) { count++; cout << num << endl; } else { for (int i = 0; i <= 9; ++i) { if (not used[i]) { used[i] = true; num = num*10+i; backtrack(pos+1); num /= 10; used[i] = false; } } } } int main () { backtrack (0); }

Very Concise Python Solution.!!! def recur(start,nums): if start*10>=1000000: return for i in nums: val=(start*10)+i print val recur(val,nums[0:i]+nums[i+1:]) nums=[0,1,2,3,4,5,6,7,8,9] recur(0,nums)