Interview Question
StudentsCountry: United States
#include <stdio.h>
#include <math.h>
#define BASE 10
int main()
{
int x,y,z,j,t;
long long num, n;
int i, lastDigit;
int freq[BASE];
printf("Enter frequncy of x y and z");
scanf("%d %d %d", &x,&y,&z);
t=x+y+z;
for(j=1;j<(6*pow(10,t));j++)
{
/* Input number from user */
// printf("Enter any number: ");
//scanf("%lld", &num);
/* Initialize frequency array with 0 */
for(i=0; i<BASE; i++)
{
freq[i] = -10;
// printf("%d",freq[i]);
}
/* Copy the value of 'num' to 'n' */
num = j;
n = num;
/* Run till 'n' is not equal to zero */
while(n != 0)
{
/* Get last digit */
lastDigit = n % 10;
/* Remove last digit */
n /= 10;
/* Increment frequency array */
if(lastDigit==4 || lastDigit==5 || lastDigit==6)
{
if(freq[lastDigit]==-10)
freq[lastDigit] = -1;
freq[lastDigit]++;
}
else
{
num=-1;
break;
}
}
if(freq[4] ==(x-1) && freq[5]==-10 && freq[6] == -10 && num>0)
printf(" %d\n",num);
if(freq[5] ==(y-1) && freq[4]==-10 && freq[6] == -10 && num>0)
printf(" %d\n",num);
if(freq[6] ==(z-1) && freq[5]==-10 && freq[4] == -10 && num>0)
printf(" %d\n",num);
if(freq[4] ==(x-1) && freq[5]==(y-1) && freq[6]==-10 && num>0)
printf(" %d\n",num);
if(freq[5] ==(y-1) && freq[6]==(z-1) && freq[4]==-10 && num>0)
printf(" %d\n",num);
if(freq[6] ==(z-1) && freq[4]==(x-1) && freq[5]==-10 && num>0)
printf(" %d\n",num);
if(freq[4]==(x-1) && freq[5]==(y-1) && freq[6]==(z-1) && num>0)
printf(" %d\n",num);
}
return 0;
}
- Create an array with x,y,z determines the occurences of 4,5,6 in the array. For example: x=1,y=1,z=1 => numArray = [4,5,6];
- Generate all the subsets of that array. ([4],[5],[6],[4,5],...)
- Generate all the permutations of each subsets ([4,5],[5,4],...)
- Convert each permutations array into a number ([4,5] = 45,...)
static void FindSolutions(int x, int y, int z)
{
Dictionary<int, int> numsDict = new Dictionary<int, int>
{
{4,x},
{5,y},
{6,z}
};
int maxDigit = x + y + z;
int[] allNums = ConvertToArray(numsDict,maxDigit);
// Get all the subsets of the array
List<int> solutions = new List<int>();
for (int i = 0; i < (1 << allNums.Length); i++)
{
List<int> comb = new List<int>();
for (int j = 0; j < allNums.Length; j++)
{
if ((i & (1 << j)) > 0)
{
comb.Add(allNums[j]);
}
}
if (comb.Count > 0)
{
Permute(solutions, comb.ToArray(), 0, comb.Count - 1);
}
}
for (int i = 0; i<solutions.Count; i++)
Console.WriteLine(solutions[i]);
}
//Get all the permutations of an array and turn it into a number
static void Permute(List<int> solutions, int[] numArray, int l, int r)
{
int a;
if (l == r)
{
int num = numArray.ToNumber();
if (!solutions.Contains(num))
solutions.Add(num);
}
else {
for (a = l; a <= r; a++)
{
Swap(ref numArray[l], ref numArray[a]);
Permute(solutions, numArray, l+1, r);
Swap(ref numArray[l], ref numArray[a]);
}
}
}
static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
static int ToNumber(this int[] numArray)
{
double returnNum = 0;
int arrayLength = numArray.Length;
for (int i = 0; i < arrayLength; i++)
{
returnNum = returnNum + (numArray[i]*Math.Pow(10, arrayLength -i -1));
}
return (int)returnNum;
}
static int[] ConvertToArray(Dictionary<int, int> numsDict, int arrayLength)
{
int[] allNums = new int[arrayLength];
int index = 0;
foreach (var kvp in numsDict)
{
int n = kvp.Value;
for (int i = 0; i < n; i++)
{
allNums[index] = kvp.Key;
index++;
}
}
return allNums;
}
Then on Main, just call FindSolutions(1,1,1);
- Create an array with x,y,z determines the occurences of 4,5,6 in the array. (x=1, y=1, z=1 => numArray = [4,5,6]
- Generate all the subsets of that array ([4], [5], [6], [4,5],..)
- Generate all the permutations for each subsets ([4,5],[5,4],...)
- Turn each permutations array into a number ([4,5] = 45,...)
static void FindSolutions(int x, int y, int z)
{
Dictionary<int, int> numsDict = new Dictionary<int, int>
{
{4,x},
{5,y},
{6,z}
};
int maxDigit = x + y + z;
int[] allNums = ConvertToArray(numsDict,maxDigit);
// Get all the subsets of the array
List<int> solutions = new List<int>();
for (int i = 0; i < (1 << allNums.Length); i++)
{
List<int> comb = new List<int>();
for (int j = 0; j < allNums.Length; j++)
{
if ((i & (1 << j)) > 0)
{
comb.Add(allNums[j]);
}
}
if (comb.Count > 0)
{
Permute(solutions, comb.ToArray(), 0, comb.Count - 1);
}
}
for (int i = 0; i<solutions.Count; i++)
Console.WriteLine(solutions[i]);
}
//Get all the permutations of an array and turn it into a number
static void Permute(List<int> solutions, int[] numArray, int l, int r)
{
int a;
if (l == r)
{
int num = numArray.ToNumber();
if (!solutions.Contains(num))
solutions.Add(num);
}
else {
for (a = l; a <= r; a++)
{
Swap(ref numArray[l], ref numArray[a]);
Permute(solutions, numArray, l+1, r);
Swap(ref numArray[l], ref numArray[a]);
}
}
}
static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
static int ToNumber(this int[] numArray)
{
double returnNum = 0;
int arrayLength = numArray.Length;
for (int i = 0; i < arrayLength; i++)
{
returnNum = returnNum + (numArray[i]*Math.Pow(10, arrayLength -i -1));
}
return (int)returnNum;
}
static int[] ConvertToArray(Dictionary<int, int> numsDict, int arrayLength)
{
int[] allNums = new int[arrayLength];
int index = 0;
foreach (var kvp in numsDict)
{
int n = kvp.Value;
for (int i = 0; i < n; i++)
{
allNums[index] = kvp.Key;
index++;
}
}
return allNums;
}
Then on Main, just call FindSolutions(1,1,1);
- Create an array with x,y,z determines the occurences of 4,5,6 in the array. (x=1, y=1, z=1 => numArray = [4,5,6]
- Generate all the subsets of that array ([4], [5], [6], [4,5],..)
- Generate all the permutations for each subsets ([4,5],[5,4],...)
- Turn each permutations array into a number ([4,5] = 45,...)
static void FindSolutions(int x, int y, int z)
{
Dictionary<int, int> numsDict = new Dictionary<int, int>
{
{4,x},
{5,y},
{6,z}
};
int maxDigit = x + y + z;
int[] allNums = ConvertToArray(numsDict,maxDigit);
// Get all the subsets of the array
List<int> solutions = new List<int>();
for (int i = 0; i < (1 << allNums.Length); i++)
{
List<int> comb = new List<int>();
for (int j = 0; j < allNums.Length; j++)
{
if ((i & (1 << j)) > 0)
{
comb.Add(allNums[j]);
}
}
if (comb.Count > 0)
{
Permute(solutions, comb.ToArray(), 0, comb.Count - 1);
}
}
for (int i = 0; i<solutions.Count; i++)
Console.WriteLine(solutions[i]);
}
//Get all the permutations of an array and turn it into a number
static void Permute(List<int> solutions, int[] numArray, int l, int r)
{
int a;
if (l == r)
{
int num = numArray.ToNumber();
if (!solutions.Contains(num))
solutions.Add(num);
}
else {
for (a = l; a <= r; a++)
{
Swap(ref numArray[l], ref numArray[a]);
Permute(solutions, numArray, l+1, r);
Swap(ref numArray[l], ref numArray[a]);
}
}
}
static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
static int ToNumber(this int[] numArray)
{
double returnNum = 0;
int arrayLength = numArray.Length;
for (int i = 0; i < arrayLength; i++)
{
returnNum = returnNum + (numArray[i]*Math.Pow(10, arrayLength -i -1));
}
return (int)returnNum;
}
static int[] ConvertToArray(Dictionary<int, int> numsDict, int arrayLength)
{
int[] allNums = new int[arrayLength];
int index = 0;
foreach (var kvp in numsDict)
{
int n = kvp.Value;
for (int i = 0; i < n; i++)
{
allNums[index] = kvp.Key;
index++;
}
}
return allNums;
}
Then on Main, just call FindSolutions(1,1,1);
static void FindSolutions(int x, int y, int z)
{
Dictionary<int, int> numsDict = new Dictionary<int, int>
{
{4,x},
{5,y},
{6,z}
};
int maxDigit = x + y + z;
int[] allNums = ConvertToArray(numsDict,maxDigit);
// Get all the subsets of the array
List<int> solutions = new List<int>();
for (int i = 0; i < (1 << allNums.Length); i++)
{
List<int> comb = new List<int>();
for (int j = 0; j < allNums.Length; j++)
{
if ((i & (1 << j)) > 0)
{
comb.Add(allNums[j]);
}
}
if (comb.Count > 0)
{
Permute(solutions, comb.ToArray(), 0, comb.Count - 1);
}
}
for (int i = 0; i<solutions.Count; i++)
Console.WriteLine(solutions[i]);
}
//Get all the permutations of an array and turn it into a number
static void Permute(List<int> solutions, int[] numArray, int l, int r)
{
int a;
if (l == r)
{
int num = numArray.ToNumber();
if (!solutions.Contains(num))
solutions.Add(num);
}
else {
for (a = l; a <= r; a++)
{
Swap(ref numArray[l], ref numArray[a]);
Permute(solutions, numArray, l+1, r);
Swap(ref numArray[l], ref numArray[a]);
}
}
}
static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
static int ToNumber(this int[] numArray)
{
double returnNum = 0;
int arrayLength = numArray.Length;
for (int i = 0; i < arrayLength; i++)
{
returnNum = returnNum + (numArray[i]*Math.Pow(10, arrayLength -i -1));
}
return (int)returnNum;
}
static int[] ConvertToArray(Dictionary<int, int> numsDict, int arrayLength)
{
int[] allNums = new int[arrayLength];
int index = 0;
foreach (var kvp in numsDict)
{
int n = kvp.Value;
for (int i = 0; i < n; i++)
{
allNums[index] = kvp.Key;
index++;
}
}
return allNums;
}
Then on Main, just call FindSolutions(1,1,1);
/******************************************************************************
- Anonymous July 26, 2019Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#include <math.h>
#define BASE 10
int main()
{
int x,y,z,j,t;
long long num, n;
int i, lastDigit;
int freq[BASE];
printf("Enter frequncy of x y and z");
scanf("%d %d %d", &x,&y,&z);
t=x+y+z;
for(j=1;j<(6*pow(10,t));j++)
{
/* Input number from user */
// printf("Enter any number: ");
//scanf("%lld", &num);
/* Initialize frequency array with 0 */
for(i=0; i<BASE; i++)
{
freq[i] = -10;
// printf("%d",freq[i]);
}
/* Copy the value of 'num' to 'n' */
num = j;
n = num;
/* Run till 'n' is not equal to zero */
while(n != 0)
{
/* Get last digit */
lastDigit = n % 10;
/* Remove last digit */
n /= 10;
/* Increment frequency array */
if(lastDigit==4 || lastDigit==5 || lastDigit==6)
{
if(freq[lastDigit]==-10)
freq[lastDigit] = -1;
freq[lastDigit]++;
}
else
{
num=-1;
break;
}
}
if(freq[4] ==(x-1) && freq[5]==-10 && freq[6] == -10 && num>0)
printf(" %d\n",num);
if(freq[5] ==(y-1) && freq[4]==-10 && freq[6] == -10 && num>0)
printf(" %d\n",num);
if(freq[6] ==(z-1) && freq[5]==-10 && freq[4] == -10 && num>0)
printf(" %d\n",num);
if(freq[4] ==(x-1) && freq[5]==(y-1) && freq[6]==-10 && num>0)
printf(" %d\n",num);
if(freq[5] ==(y-1) && freq[6]==(z-1) && freq[4]==-10 && num>0)
printf(" %d\n",num);
if(freq[6] ==(z-1) && freq[4]==(x-1) && freq[5]==-10 && num>0)
printf(" %d\n",num);
if(freq[4]==(x-1) && freq[5]==(y-1) && freq[6]==(z-1) && num>0)
printf(" %d\n",num);
}
return 0;
}