i need generate arrays possible combinations, question i've found here: combinatorics: generate "states" - array combinations i'm doing simple work of optimal graph coloring , so, i'm trying generate possible color combinations (the array represents color each node). code working but, doing unnecessary work. in situation, [1, 1, 2] same thing of [2, 2, 1], don't need test if valid graph again . i can't think of anything, first know if there's simple code doing want to. for now, code this: void generatearray(int array[], int array_size, int idx){ int i; if(idx == array_size){ putchar('\n'); for(i = 0; < array_size; i++) printf("%i ", array[i]); } else for(i = 0; <= 3; i++){ array[idx] = i; generatearray(array, array_size, idx+1); } } and print: [0, 0, 0] [0, 0, 1] [0, 0, 2] [0, 0, 3] [0, 1, 0] [0, 1, 1] ... [3, 3, 0] [3, ...