How can I get the index of a given combination? - c

I have a sequence of arrays of numbers with 5 elements each, from 0 to 8, and I have to order than using that combination, I mean:
i=0, {0,0,0,0,0}
i=1, {0,0,0,0,1}
i=2, {0,0,0,0,2}
i=3, {0,0,0,0,3}
i=4, {0,0,0,0,4}
i=5, {0,0,0,0,5}
i=6, {0,0,0,0,6}
i=7, {0,0,0,0,7}
i=8, {0,0,0,0,8}
i=9, {0,0,0,1,1}
...
i=1285, {7,8,8,8,8}
i=1286, {8,8,8,8,8}
so if I give {0,0,1,1,2} to the function it's returns 7.
I thought about using the Combinatorial number system
but I'm missing something that I don't know what it is, the code below just doesn't work
#include <stdio.h>
#include <stdlib.h>
#define size 1287
int combination[9][5] = {
{0, 0, 0, 0, 0},
{1, 1, 1, 1, 1},
{2, 3, 4, 5, 6},
{3, 3, 6, 15, 21},
{4, 10, 20, 35, 56},
{5, 15, 35, 70, 126},
{6, 21, 56, 126, 252},
{7, 28, 84, 210, 462},
{8, 36, 120, 330, 792}
};
int getKey(int array[]){
int key=0;
int tempArray[9] = {0};
for(int i=0;i<5;i++){
tempArray[array[i]]++;
}
int j=0;
for(int i=0;i<9;i++){
if(tempArray[i]!=0){
while(tempArray[i]!=0){
array[j++] = i;
key += combination[i][5-j];
tempArray[i]--;
}
}
}
return key;
}
int main(){
int it[5];
for(it[0] = 0 ;it[0]<9;it[0]++){
for(it[1]=it[0];it[1]<9;it[1]++){
for(it[2]=it[1];it[2]<9;it[2]++){
for(it[3]=it[2];it[3]<9;it[3]++){
for(it[4]=it[3];it[4]<9;it[4]++){
printf("{%d %d %d %d %d} = %d\n",it[0],it[1],it[2],it[3],it[4],getKey(it));
}
}
}
}
}
return 0;
}
Obs: I'm using counting sort to keep the lexicographic order, in theory I will receive unsorted arrays.

I will give you the Answer I wrote for the previous version of this question which you posted yesterday and deleted. (And that's bad form, by the way.)
Let's call the binomial coefficient C(n, k) = n!/(k!(n-k)!)
The number of unordered strings of m letters drawn from an alphabet of s symbols is C(m+s-1, s-1). Let's call that D(m, s). In this case, D(5, 9) = C(5+9-1, 9-1) = C(13, 8) = 1287
Let's sort each string, then number them:
aaaaa 1
aaaab 2
aaaac 3
...
aaaai 9
aaabb 10
aaabc 11
...
If a string contains 5 a's, its number is D(5, 1) = C(5+1-1, 1-1) = C(5, 0) = 1.
If a string contains 4 a's, its number is 1 plus a number determined by the non-a letter, which goes up to D(1,8) = C(1+8-1,8-1) = C(8, 7) = 8. So they go up to 1+8=9.
If a string contains 3 a's, its number is 9 plus a number determined by the non-a letters, which goes up to D(2,8) = C(9,7) = 36, so 9+36=45.
If a string contains 2 a's, its number is in [46,165].
If a string contains 1 a, its number is in [166, 495].
If a string contains no a, its number is in [496, 1287].
So how about the string "aabgg"? It's number is (45)+(8)+(7)+(6)+(5)+(4)+(1)=76
No collisions, and the calculation of the index is O(sm(s+m)), which is not too bad for m=5 and s=9.
EDIT: to clarify, let's define
E(j, m, s) = D(0,s-1)+D(1,s-1)+...+D(m-j-1,s-1)
Suppose a string of m letters drawn from an alphabet of s symbols contains j of the first letter of the alphabet. There are E(j,m,s) strings in the catalogue before the first such string. For instance, before the first string that begins with exactly two a's ("aabbb"), there are E(2, 5, 9)=45 strings.
To get to "aabbb" we must count out 45 strings.
To get from "aabbb" to the next string that contains exactly one b ("aabcc"), we must count out E(1, 3, 8) = 8 strings.
From there to the next string that contains no c ("aabdd"), E(0, 2, 7) = 7 strings.
No d ("aabee"): E(0, 2, 6) = 6
No e ("aabff"): E(0, 2, 5) = 5
No f ("aabgg"): E(0, 2, 4) = 4
And we must count "aabgg" itself: 1

well, I think this look like oct number 1...8
0 = {00000}
1 = {00001}
2 = {00002}
8 = {00010}
if this what you mean
I think this algorithm is the best
int get_key(int a[5]){
int idx, rval=0;
for(i=4; i<=0; i--)
rval += powl(a[i], i);
return rval;
}

Related

Need Help Finding Segmentation fault in C Program

I'm implementing the Data Encryption Standard in C for a personal learning project and I have a seg fault that has been driving me up the wall for the past 3 days. I understand this isn't the place for "fix my code for me" type questions, but I need a second pair of eyes to look over this:
/*we must define our own modulo, as the C modulo returns unexpected results:*/
#define MOD(x, n) ((x % n + n) % n)
/*example: the 12th bit should be in the second byte so return 1 (the first byte being 0)*/
#define GET_BYTE_NUM(bit_index) (bit_index/8)
/*example: a bit index of 12 means this bit is the 4th bit of the second byte so return 4*/
#define GET_BIT_NUM(bit_index) MOD(bit_index, 8)
typedef unsigned char byte;
/*each row represents a byte, at the bit to be place in the position
* for example for the first row (first byte) we will place bits 31, 0, 1, 2, 3, 4 in
* in bit positions 0-6, respectively. The last two bits will be left blank. Since this is supposed to be a crypto implementation, static prevents this value from being accessed outside the file.*/
const static byte e_box[8][6] = { {31, 0, 1, 2, 3, 4}, {3, 4, 5, 6, 7, 8}, {7, 8, 9, 10, 11, 12}, {11, 12, 13, 14, 15, 16}, {12, 16, 17, 18, 19, 20}, {19, 20, 21, 22, 23, 24},
{23, 24, 25, 26, 27, 28}, {27, 28, 29, 30, 31, 0} }
void e(byte **four_byte_block)
{
int i, n, l = 0, four_bit_num, four_byte_num;
/*create the new byte_block and initialize all values to 0, we will have 4 spaces of bytes, so 32 bits in total*/
byte *new_byte_block = (byte*)calloc(4, sizeof(byte));
byte bit;
for(i = 0 i < 8; i++)
{
for(n = 0; n < 6; n++)
{
/*get the byte number of the bit at l*/
four_byte_num = GET_BYTE_NUM(e_box[i][n]);
/*find what index the bit at l is in its byte*/
half_bit_num = GET_BIT_NUM(e_box[i][n]);
bit = *four_byte_block[half_byte_num]; /*SEG FAULT!*/
}
}
/*finally, set four_byte_block equal to new_byte_block*/
/*four_byte_block = NULL;
* four_byte_block = new_byte_block;*/
}
I have narrowed the problem down to the line marked /SEG FAULT!/ but I can't see what the issue is. When I print the half_byte_num, I get a number that is within bounds of half_block, and when I print the values of half_block, I can confirm that those values exist.
I believe I may be doing something wrong with the pointers ie by passing **four_byte_block, (a pointer to a pointer) and it's manipulation could be causing the seg fault.
Have you tried this:
bit = (*four_byte_block)[half_byte_num];
Instead of this:
bit = *four_byte_block[half_byte_num];
Those are not the same, as an example, take the following code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char **lines = malloc(sizeof(char *) * 8);
for (int i = 0; i < 8l; i++)
lines[i] = strdup("hey");
printf("%c\n", *lines[1]);
printf("%c\n",(*lines)[1]);
return 0;
}
The former will output h.
The latter will output e.
This is because of the operators precedence, [] will be evalued before *, thus if you want to go to the nth index of *foo, you need to type (*foo)[n].

How can I reduce the execution time in this code?

Problem
Consider the sequence D of the last decimal digits of the first N Fibonacci numbers, i.e. D = (F0%10,F1%10,…,FN−1%10).
Now, you should perform the following process:
Let D=(D1,D2,…,Dl)
If l=1, the process ends.
Create a new sequence
E=(D2,D4,…,D2⌊l/2⌋)
In other words, E is the sequence created by removing all odd-indexed elements from D
Change D to E
When this process terminates, the sequence D
contains only one number. You have to find this number.
Input
The first line of the input contains a single integer T
denoting the number of test cases.
The description of T test cases follows.
The first and only line of each test case contains a single integer N
Output
For each test case, print a single line containing one integer ― the last remaining number.
Code
#include <stdio.h>
#include <stdlib.h>
int test(int *arr, int n);
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int *arr;
arr = (int *)malloc((n + 1) * sizeof(int));
arr[1] = 0;
arr[2] = 1;
for (int i = 3; i <= n; i++) {
arr[i] = arr[i - 1] + arr[i - 2];
}
/*
for(int k=1;k<=n;k++){
printf("%d ",arr[k] );
}
printf("\n");
*/
printf("%d\n", (test(arr, n)) % 10);
}
}
int test(int *arr, int n) {
if (n == 1) {
return arr[1];
} else {
for (int i = 1; i <= (n / 2); i++) {
arr[i] = arr[2 * i];
}
return test(arr, n / 2);
}
}
Using the algorithm from https://math.stackexchange.com/questions/681674/recursively-deleting-every-second-element-in-a-list,
Find the largest integer A, such that 2^A < N.
Find Fibonnaci(2^A - 1) % 10
Adding to Bill Lynch's answer, which is itself based on this other answer by happymath:
You will always end up getting 2n − 1 where n is maximum integer such that 2n < K
I'd like to point out another useful mathematical property.
In number theory, the nth Pisano period, written π(n), is the period with which the sequence of Fibonacci numbers taken modulo n repeats.
(https://en.wikipedia.org/wiki/Pisano_period)
Here we need to consider the case where n = 10, π(10) = 60 and the last decimal digits correspond to the OEIS sequence A003893:
0, 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, 7, 7, 4, 1, 5, 6, 1, 7, 8, 5, 3, 8, 1, 9, 0, 9, 9, 8, 7, 5, 2, 7, 9, 6, 5, 1, 6, 7, 3, 0, 3, 3, 6, 9, 5, 4, 9, 3, 2, 5, 7, 2, 9, 1
So that there's no need to calculate the actual Fibonacci number, nor to generate all the sequence up to N.

Every possible path from 2D array? (Cross Product)

Let's suppose I have this array
int diml = 3;
int dims = 3;
int time [diml][dims] ={
(10, 3, 5),
( 4, 7, 2),
( 2, 8, 1)
};
How can I get every combination like:
(10, 3, 5)
(10, 3, 2)
(10, 3, 1)
(10, 7, 5)
(10, 7, 2)
(10, 7, 1)
...
(2, 8, 5)
(2, 8, 2)
(2, 8, 1)
*Is this possible without saving all the combinations in a new array, but just a 1D local array that can store the current combination on every cycle?
*I'd prefer cycles over recursion. And at the end of each cycle I need the pattern (like 10, 3, 2) so I can elaborate it.
*The dimensions of the 2D array are MxN (3x3 is just an example).
*A solution with binary trees is accepted (but I want to save the indexes too).
I should do this in C. I have found similar solutions in StackOverflow but they work by column and they save the data in a 2D array, but that's not what I need.
Thanks in advance! (:
For this example codes, The first one was built so it would be easier to understand example 2. Example 1 was built for only 3x3 matrixes. Example 2 was built so that it can accommodate a matrix with 8 columns at maximum. I didn't use malloc or return an array. It will print back all the possible combinations for you. It doesn't deal with returning the data but it wouldn't be hard to incorporate that into the code.
For the method of calculation all the possible combination, I would use a 3x3 matrix as an example.
In a 3x3 matrix, there are 3 rows and 3 columns. I treated each column as a set of number that I can pick and the rows as the possible of numbers that I can pick from. So in that example, I can pick 012 for my first, second, and third set of number.
So to get all the possible combinations, I have 3 arrays, [0] [1] [2]. They all start at 0. I first save the possible combination of 0 0 0. Then I increase array 2 by 1. Then I would get 0 0 1. I then save that combination. I will keep on doing that and one the [2] array == 2. I turn that to 0 and add a 1 to the array to the left of it. So it become 0 1 0. When I reach a loop where my values of my arrays are 0 2 2, the loop after that, I will get 1 0 0. I will keep on doing that until all the value turn to zero then I am done.
For how the data is store, I store them continually in an array. To read back the value properly. Say for example in a 2x5 matrix. Each combination will have 5 numbers. Thus, the first combination is the first five indexes, the next combination, is the next five numbers after that one.
To calculate how much array length you would need before calculating the combinations, you can just base the calculation on rows and columns. Think of this like the lottery. If there are 3 columns, it like you can pick 3 numbers. Each column have 3 rows, so that mean for each time you pick a number there are 3 possible numbers to pick from. For you to hit a jackpot the chances are 1:3 x 1:3 x 1:3 or 1:27 because there are 27 possibilities if picking 3 numbers like this (123 123 123) and matching them in the correct order. Thus, for a 3x3 matrix, it is 3x3x3, 4x4 = 4x4x4x4, 1x3 = 1, 3x1 = 3, 2x5 = 2x2x2x2x2 = 32.
Thus, the amount of possible combinations is "the amount of rows" to the power of "the amount of columns".
The size is the amount of possible combinations multiply by the amount of numbers per combination. Of which would be "possibilities multiply column count = array size needed.
Example 1:
#include <stdio.h>
void printMatrixCombo(int row, int col, int matrix[row][col]);
int main(){
const int m1 = 3;
const int m2 = 3;
int matrix[m1][m2] = {
{10, 3, 5},
{4, 7, 2},
{2, 8, 1}
};
printMatrixCombo(m1, m2, matrix);
return 0;
}
// Only use this for a 3x3
void printMatrixCombo(int row, int col, int matrix[row][col]){
int oi = 0;
int output[81] = {0};
for (int group1 = 0; group1 < 3; group1++){
for (int group2 = 0; group2 < 3; group2++ ){
for (int group3 = 0; group3 < 3; group3++ ){
output[oi++] = matrix[group1][0];
output[oi++] = matrix[group2][1];
output[oi++] = matrix[group3][2];
}
}
}
printf("There were %d combination in the matrix of %d x %d\n", oi / col, row, col );
for ( int i = 0; i < oi ; ){
printf("(");
for ( int j = 0; j < col; j++ ){
printf("%d", output[i+j]);
if ( j != col - 1 ) printf(", ");
}
printf(")\n");
i = i + col;
}
}
Example 2:
#include <stdio.h>
void printMatrixCombo(int row, int col, int matrix[row][col]);
int main(){
const int row = 4;
const int col = 4;
/*// 3x3
int matrix[row][col] = {
{10, 3, 5},
{4, 7, 2},
{2, 8, 1}
};//*/
// 4 x 4
int matrix[row][col] = {
{10, 3, 5, 7},
{4, 7, 2, 3},
{2, 8, 1, 9},
{9, 4, 8, 11}
};//*/
/*// 5 x 5
int matrix[row][col] = {
{10, 3, 5, 7, 25},
{4, 7, 2, 87, 42},
{2, 8, 1, 85, 39},
{9, 4, 8, 94, 57},
{10, 3, 5, 7, 93},
};//*/
/*// 2 x 2
int matrix[row][col] = {
{10, 3},
{4, 7},
};//*/
/*// 1 x 1
int matrix[row][col] = {
{10},
};//*/
/* // 3 x 1
int matrix[row][col] = {
{10},
{4},
{1}
}; //*/
/*// 1 x 3
int matrix[row][col] = {
{10, 4, 1},
};// */
printMatrixCombo(row, col, matrix);
return 0;
}
void printMatrixCombo(int row, int col, int matrix[row][col]){
int oi = 0;
int allZ = 0;
// This is the maximum for a 5x5
// Change to fit usage case
int output[15625] = {0};
int colCount[8] = {0};
int lastCol = col - 1;
int lastRow = row - 1;
while (1){
for ( int i = 0; i < col; i++ )
output[oi++] = matrix[colCount[i]][i];
if ( colCount[lastCol] == lastRow ){
colCount[lastCol] = 0;
for (int i = lastCol - 1; i > -1; i--){
if ( colCount[i] == lastRow ){
colCount[i] = 0;
} else {
colCount[i]++;
break;
}
}
} else {
colCount[lastCol]++;
}
allZ = 1;
for ( int i = 0; i < col; i++ ){
if ( colCount[i] != 0 ){
allZ = 0;
break;
}
}
if (allZ == 1) break;
}
printf("There were %d combination in the matrix of %d x %d\n", oi / col, row, col );
printf("Array's length(indexes) is %d\n", oi );
for ( int i = 0; i < oi ; ){
printf("(");
for ( int j = 0; j < col; j++ ){
printf("%d", output[i+j]);
if ( j != col - 1 ) printf(", ");
}
printf(")\n");
i = i + col;
}
}

C - delete element from array and reorganise

Given this array:
int a[] = {5, 8, 5, 6, 9, 5};
Would it be possible to remove all ints which equals 5 and move the rest the front of the array?
So that after the removal the array would look like this:
int a[] = {8, 6, 9, 0, 0, 0}
I don't know if by removing a element it becomes a 0 or a NULL?
Thanks!
You can do it with two iterations over the array, first iteration two to turn the element you want to delete, second iteration to separate zeros from non-zeros.
int a[] = {5, 8, 5, 6, 9, 5};
int n = 6;
for(int i = 0 ; i < n ; i++ ) {
if(a[i] == 5 ) {
a[i] = 0;
}
}
int* zero = a;
int* nonZero = a;
int j = 0;
while(j < n) {
while(*zero != 0) {
zero++;
}
while(*nonZero == 0) {
nonZero++;
j++;
}
if(zero < nonZero) {
*zero = *nonZero;
*nonZero = 0;
}
j++;
}
Your array is statically allocated, so always has the same size and deleted elements have the 0 value (according how you define the deleted values).
This link can help you and explains about how to delete element from array.
It is been awhile that i have programmed in C but it is posibble.
This is just a pseudo code, but you just need to change it to way of C programming.
int a[] = {5, 8, 5, 6, 9, 5};
int b[] = {5, 8, 5, 6, 9, 5}; // copy of array a to hold temp
for(int i = 0; i < Size of array; i++ ){
for(int j = i; j < Size of array; j++ ){
if(b[j] != 5){
a[i] = b[j];
a[j] = b[i];
break;
}
}
}
It will be like (▼: Target to swap, F: Finished, X: Not a targe to swap, N: Not processed):
▼, ▼, N, N, N, N
5, 8, 5, 6, 9, 5
F, ▼, X, ▼, N, N
8, 5, 5, 6, 9, 5
F, F, ▼, X, ▼, N
8, 6, 5, 5, 9, 5
Result:
8, 6, 9, 5, 5, 5
And remove 5s, it is quite different depends what you mean. If you do not change size of array then they can be 0 or undefined(null). So I think it differs by how you program the function that returns array.
your array is not dynamic so you just can't reduce its size after its been allocated.setting the value zero might solve the problem in your case.

Replace number by sum of other numbers in a list without subtraction

I was asked:
Replace each number in a list by sum of remaining elements, the list is not sorted.
So suppose if we have a list of numbers like {2, 7, 1, 3, 8}, now we are to replace each element with sum of rest of elements. The output should be:
{(7 + 1 + 3 + 8), (2 + 1 + 3 + 8), (2 + 7 + 3 + 8), (2 + 7 + 1 + 8), (2 + 7 + 1 + 3)}
== {19, 14, 20, 18, 13}
I answered an obvious solution:
First evaluate sum of all numbers then subtract each element from sum.
So for above list sum is 2 + 7 + 1 + 3 + 8 = 21, then for output do like:
{sum - 2, sum - 7, sum - 1, sum - 3, sum - 8}
{21 - 2, 21 - 7, 21 - 1, 21 - 3, 21 - 8}
== {19, 14, 20, 18, 13}
It needs only two iterations of list.
Then Interviewer asked me: Now do it without subtraction? and I couldn't answer :(
Is other solution possible? Can some share any other trick? A better trick is possible?
Lets extra memory space can be used (I asked after a few minutes of try, even then I couldn't answer).
One possibility would be to compute prefix and suffix sums of your array and then combine the appropriate entries. This would still be O(n) but needs more memory space so I think your original method is better.
In other words, from {2, 7, 1, 3, 8} compute {2, 2+7, 2+7+1, 2+7+1+3, 2+7+1+3+8} and {2+7+1+3+8, 7+1+3+8, 1+3+8, 3+8, 8} and then add the appropriate entries.
The solution is to sum everything but the element. Then you don't have to subtract after the fact. You just skip adding the element at the current index.
Alternatively, you could get a subset of the list that excludes the element at the current index, then just sum the subset together. Pretty much the same thing as my first suggestion with more implementation detail.
C++ implementation. O(n) and done by keeping sums of all elements before and after a certain index.
#include <iostream>
int main() {
int a[] = {2,7,1,3,8};
int prefix[5]; // Sum of all values before current index
int suffix[5]; // Sum of all values after current index
prefix[0] = 0;
suffix[4] = 0;
for(int i = 1; i < 5; i++) {
prefix[i] = prefix[i-1] + a[i-1];
suffix[4 - i] = suffix[4 - i + 1] + a[4 - i + 1];
}
// Print result
for (int i = 0; i < 5; i++) {
std::cout << prefix[i] + suffix[i] << " ";
}
std::cout << std::endl;
}
I can't think anything better than yours.
But how about this :
Create a (n-1)xn matrix:
[ 2, 7, 1, 3, 8 ]
| 7, 1, 3, 8, 2 | rotate by 1
| 1, 3, 8, 2, 7 | by 2
| 3, 8, 2, 7, 1 | by 3
| 8, 2, 7, 1, 3 | by 4
Then Sum up the columns
C++'s std::rotate_copy can be used to create matrix
std::vector<int> v1 {2, 7, 1, 3, 8 };
std::vector<int> v2 (v1.size());
int i,j;
std::vector< std::vector<int> > mat;
for (int i=1; i<v1.size();++i){
std::rotate_copy(v1.begin(),v1.begin()+i,v1.end(),v2.begin());
mat.push_back(v2);
}
for(j=0;j<v1.size();++j)
for(i=0;i<v1.size()-2;++i)
v2[j]+=mat[i][j];
for(i=0;i<v2.size();++i)
std::cout<<v2[i]<<" ";
#include <iostream.h>
#include <stdio.h>
int main() {
int a[] = {2,7,1,3,8};
int sum[5]={0};
for(int j = 0; j < 5; j++){
for(int i = 1; i < 5; i++) {
sum[j]=sum[j]+a[(j+i+5)%5];
}
printf("%d ", sum[j]); }
}
Instead of subtracting the element you can add the element multiplied by -1. Multiplication and addition are allowed operations, I guess.

Resources