Advertisement
apl-mhd

carPair

Aug 18th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include<cstdio>
  2.  
  3.  
  4. int solution(int A[], int N){
  5.  
  6.     int i,count = 0, sum = 0;
  7.    
  8.     for( i = 0; i < N; i++){
  9.  
  10.         if(A[i] == 0){
  11.  
  12.             count++;
  13.  
  14.         }
  15.  
  16.  
  17.  
  18.               sum = sum + ( count * A[i]);
  19.  
  20.  
  21.  
  22.     }
  23.  
  24.     if( sum >  1000000000)
  25.  
  26.         return -1;
  27.     else
  28.         return sum;
  29.  
  30.  
  31. }
  32.  
  33.  
  34. int main(){
  35.  
  36.  
  37.     int carPair[5] = {0,1,0,1,1};
  38.     int carPair2[8] = {0,1,0,1,1,0,1,1};
  39.     int carPair3[1] = {0};
  40.     int carPair4[1] = {1};
  41.  
  42.  
  43.  
  44.  
  45.    printf(" %d \n ", solution(carPair, 5) == 5);
  46.    printf(" %d \n ", solution(carPair2, 8) == 11);
  47.    printf(" %d \n ", solution(carPair3, 1) == 0);
  48.    printf(" %d \n ", solution(carPair4, 1) == 0);
  49.  
  50.  
  51.  
  52.  
  53.    
  54.  
  55.  
  56.  
  57.    return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement