algo

Just a bunch of algorithm implementations
Log | Files | Refs | README

pc.c (410B)


      1 #include <stdio.h>
      2 /* codility lessons - passing cars */
      3 
      4 int solution(int A[], int N);
      5 
      6 int
      7 main(void)
      8 {
      9 	int a[] = {0, 1, 0, 0, 1, 1, 1, 0, 1};
     10 
     11 	printf("%d\n", solution(a, 9));
     12 
     13 	return 0;
     14 }
     15 
     16 int
     17 solution(int A[], int N)
     18 {
     19 	int pc;
     20 
     21 	pc = 0;
     22 	
     23 	for (int i = 0; i < N; i++)
     24 		for(int j = i+1; j < N; j++)
     25 			if (A[i] == 0 && A[j] == 1) {
     26 				pc++;
     27 				if (pc > 1000000000)
     28 					return -1;
     29 			}
     30 	return pc;
     31 }