Skip to content

Instantly share code, notes, and snippets.

@oxalorg
Created February 25, 2016 15:41
Show Gist options
  • Save oxalorg/cc1cc6a63a0f4f77dae8 to your computer and use it in GitHub Desktop.
Save oxalorg/cc1cc6a63a0f4f77dae8 to your computer and use it in GitHub Desktop.
To find the smallest positive number (age) which can not be constructed out of the given count of numbers [0-9]
/* To find the smallest positive number
* which can not be constructed out of
* the given count of numbers [0-9]
*
* Date: 25th Feb, 2016
*/
#include<stdio.h>
#define N 10
#define MAX_COUNT 99999
int main(){
int n, count[N];
int i;
int zflag = 0, sflag = 0, snum = 0;
int min = 1;
for(i = 0; i < N; i++){
printf("Number of occurences of %d : ", i);
fflush(stdout);
scanf("%d", &count[i]);
}
if(count[0] == 0){
zflag = 1;
}
// Find the smallest count of a positive number (i.e not '0');
for(i = 1; i < N; i++){
// sflag and snum get the smallest number with atleast 1 digit
if(!sflag){
if(count[i] > 0){
sflag = 1;
snum = i;
}
}
if(count[i] < count[min]){
min = i;
}
}
printf("Smallest number = ");
if(count[0] != 0 && count[0] < count[min]){
// If number of '0's are less than other positive numbers
// Smallest number = [Smallest number with atleast 1 digit]+'0'*(count[0] + 1)
printf("%d", sflag);
for(i = 0; i < count[0] + 1; i++){
printf("0");
}
} else {
// otherwise
// Smallest number = [Digit with minimum count]*count[min] + last digit
for(i = 0; i < count[min]; i++){
printf("%d", min);
}
// Last digit
if(count[0] == 0){
printf("0");
} else {
printf("%d", min);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment