You are given N round clocks.
Every clock has M hands, and these hands can point to positions 1, 2, 3, ..., P (yes, these represent numbers around each face). The clocks are represented by the matrix A consisting of N rows and M columns of integers. The first row represents the hands of the first clock, and so on.
For example, you are given matrix A consisting of five rows and two columns, and P = 4:
A[0][0] = 1 A[0][1] = 2 A[1][0] = 2 A[1][1] = 4 A[2][0] = 4 A[2][1] = 3 A[3][0] = 2 A[3][1] = 3 A[4][0] = 1 A[4][1] = 3You can rotate the clocks to obtain several clocks that look identical. For example, if you rotate the third, fourth and fifth clocks you can obtain the following clocks:
After rotation, the first, third and fourth clocks look the same, and the second clock looks the same as the fifth one. That means there are four pairs of clocks that look the same: (1, 3), (1, 4), (2, 5) and (3, 4).
Write a function:
int solution(int **A, int N, int M, int P);
that, given a zero-indexed matrix A consisting of N rows and M columns of integers and integer P, returns the maximal number of pairs of clocks that can look the same after rotation.
For example, given the following array A and P = 4:
A[0][0] = 1 A[0][1] = 2 A[1][0] = 2 A[1][1] = 4 A[2][0] = 4 A[2][1] = 3 A[3][0] = 2 A[3][1] = 3 A[4][0] = 1 A[4][1] = 3the function should return 4, as explained above.
Write an efficient algorithm for the following assumptions:
- N and M are integers within the range [1..500];
- P is an integer within the range [1..1,000,000,000];
- each element of matrix A is an integer within the range [1..P];
- the elements of each row of matrix A are all distinct.
int compare_ints (int a, int b) { return a - b ; }
int compare_clocks_M;
int compare_clocks (const int * a, const int * b)
{
int i;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + offset) % positions;
}
qsort (clock, hands, sizeof(int), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
In file included from user.c:23: func.c: In function 'solution': func.c:48: error: 'offset' undeclared (first use in this function) func.c:48: error: (Each undeclared identifier is reported only once func.c:48: error: for each function it appears in.) func.c:50: error: 'hands' undeclared (first use in this function) func.c:50: warning: passing argument 4 of 'qsort' from incompatible pointer type /usr/include/stdlib.h:756: note: expected '__compar_fn_t' but argument is of type 'int (*)(int, int)' func.c:55: warning: passing argument 4 of 'qsort' from incompatible pointer type /usr/include/stdlib.h:756: note: expected '__compar_fn_t' but argument is of type 'int (*)(const int *, const int *)' func.c:61: warning: passing argument 1 of 'compare_clocks' from incompatible pointer type func.c:4: note: expected 'const int *' but argument is of type 'int **' func.c:61: warning: passing argument 2 of 'compare_clocks' from incompatible pointer type func.c:4: note: expected 'const int *' but argument is of type 'int **'
int compare_ints (int a, int b) { return a - b ; }
int compare_clocks_M;
int compare_clocks (const int * a, const int * b)
{
int i;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, hands, sizeof(int), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
In file included from user.c:23: func.c: In function 'solution': func.c:50: error: 'hands' undeclared (first use in this function) func.c:50: error: (Each undeclared identifier is reported only once func.c:50: error: for each function it appears in.) func.c:50: warning: passing argument 4 of 'qsort' from incompatible pointer type /usr/include/stdlib.h:756: note: expected '__compar_fn_t' but argument is of type 'int (*)(int, int)' func.c:55: warning: passing argument 4 of 'qsort' from incompatible pointer type /usr/include/stdlib.h:756: note: expected '__compar_fn_t' but argument is of type 'int (*)(const int *, const int *)' func.c:61: warning: passing argument 1 of 'compare_clocks' from incompatible pointer type func.c:4: note: expected 'const int *' but argument is of type 'int **' func.c:61: warning: passing argument 2 of 'compare_clocks' from incompatible pointer type func.c:4: note: expected 'const int *' but argument is of type 'int **'
int compare_ints (int a, int b) { return a - b ; }
int compare_clocks_M;
int compare_clocks (const int * a, const int * b)
{
int i;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, num_hands, sizeof(int), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
#include <stdlib.h>
static int compare_ints (const void * pa, const void * pb) { return *(int*)pa - *(int *)pb ; }
static int compare_clocks_M;
static int compare_clocks (const void * pa, const void * pb)
{
int i;
const int * a = *(const int *)pa;
const int * b = *(const int *)pb;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, num_hands, sizeof(*clock), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
#include <stdlib.h>
static int compare_ints (const void * pa, const void * pb) { return *(int*)pa - *(int *)pb ; }
static int compare_clocks_M;
static int compare_clocks (const void * pa, const void * pb)
{
int i;
const int * a = *((const int *)pa);
const int * b = *(const int *)pb;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, num_hands, sizeof(*clock), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
#include <stdlib.h>
static int compare_ints (const void * pa, const void * pb) { return *(int*)pa - *(int *)pb ; }
static int compare_clocks_M;
static int compare_clocks (const void * pa, const void * pb)
{
int i;
const int * a = *((const int *)pa);
const int * b = *(const int *)pb;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, num_hands, sizeof(*clock), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, num_hands * sizeof(**clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
Segmentation Fault [MONITOR] syscall tgkill was blocked! [MONITOR] syscall tgkill was blocked!
#include <stdlib.h>
static int compare_ints (const void * pa, const void * pb) { return *(int*)pa - *(int *)pb ; }
static int compare_clocks_M;
static int compare_clocks (const void * pa, const void * pb)
{
int i;
const int * a = *((const int *)pa);
const int * b = *(const int *)pb;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, num_hands, sizeof(*clock), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
#include <stdlib.h>
static int compare_ints (const void * pa, const void * pb) { return *(int*)pa - *(int *)pb ; }
static int compare_clocks_M;
static int compare_clocks (const void * pa, const void * pb)
{
int i;
const int * a = *((const int *)pa);
const int * b = *(const int *)pb;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, num_hands, sizeof(*clock), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (!compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
#include <stdlib.h>
static int compare_ints (const void * pa, const void * pb) { return *(int*)pa - *(int *)pb ; }
static int compare_clocks_M;
static int compare_clocks (const void * pa, const void * pb)
{
int i;
const int * a = *(const int **)pa;
const int * b = *(const int **)pb;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, num_hands, sizeof(*clock), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (!compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
#include <stdlib.h>
static int compare_ints (const void * pa, const void * pb) { return *(int*)pa - *(int *)pb ; }
static int compare_clocks_M;
static int compare_clocks (const void * pa, const void * pb)
{
int i;
const int * a = *(const int **)pa;
const int * b = *(const int **)pb;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min=-1;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, num_hands, sizeof(*clock), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (!compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
#include <stdlib.h>
static int compare_ints (const void * pa, const void * pb) { return *(int*)pa - *(int *)pb ; }
static int compare_clocks_M;
static int compare_clocks (const void * pa, const void * pb)
{
int i;
const int * a = *(const int **)pa;
const int * b = *(const int **)pb;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min=-1;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, num_hands, sizeof(*clock), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (!compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
#include <stdlib.h>
static int compare_ints (const void * pa, const void * pb) { return *(int*)pa - *(int *)pb ; }
static int compare_clocks_M;
static int compare_clocks (const void * pa, const void * pb)
{
int i;
const int * a = *(const int **)pa;
const int * b = *(const int **)pb;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min=-1;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, num_hands, sizeof(*clock), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (!compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}
The following issues have been detected: timeout errors.
large random tests, number of clocks = 500 (different = 2)
running time: >0.61 sec., time limit: 0.56 sec.
all clocks looks the same
running time: >0.61 sec., time limit: 0.44 sec.