Advertisement
apl-mhd

BinaryGap

Aug 3rd, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int solution(int N);
  4.  
  5.  
  6. int main(){
  7.  
  8.  
  9.     printf(" %d ", solution(10));
  10.  
  11.  
  12.  
  13.     return 0;
  14. }
  15.  
  16.  
  17. int solution(int N){
  18.  
  19.  
  20.  
  21.  
  22.  
  23.     int number = 1041, count = 0, i, j, temp, gapIndex = 0, gapCount = 0;
  24.  
  25.    int  binaryConvertArray[100], binaryGap[100];
  26.  
  27.    /*decimal to binary convert section*/
  28.  
  29.     while(number !=0 ){
  30.  
  31.         binaryConvertArray[count] = number % 2;
  32.  
  33.         number /=2;
  34.         count++;
  35.  
  36.  
  37.     }
  38.  
  39.  
  40.     /* binary array reverse section*/
  41.  
  42.     j = count -1, i = 0;
  43.  
  44.     while(i < j){
  45.  
  46.         temp = binaryConvertArray[i];
  47.  
  48.         binaryConvertArray[i] = binaryConvertArray[j];
  49.         binaryConvertArray[j] = temp;
  50.         i++;
  51.         j--;
  52.  
  53.  
  54.     }
  55.  
  56.  
  57.     for(i = 0; i < count; i++){  /*print binary to decimal number */
  58.  
  59.      printf(" %d ", binaryConvertArray[i]);
  60.  
  61.     }
  62.  
  63.     printf("\n");
  64.  
  65.  
  66.     /*searching gap indexr*/
  67.  
  68.     for(i = 1; i < count; i++){
  69.  
  70.         if(binaryConvertArray[i] == 0){
  71.  
  72.              gapCount++;
  73.  
  74.  
  75.         }
  76.  
  77.         if(binaryConvertArray[i] == 1){
  78.  
  79.            // printf("gapcount =  %d \n", gapCount);
  80.  
  81.             binaryGap[gapIndex] = gapCount;
  82.             gapCount = 0;
  83.             gapIndex++;
  84.  
  85.         }
  86.  
  87.     }
  88.  
  89.  
  90.     for(i = 0; i < gapIndex; i++){  /*print binary to decimal number */
  91.  
  92.      printf("a %d \n", binaryGap[i]);
  93.  
  94.     }
  95.  
  96.  
  97.  
  98.     if(gapIndex > 1){
  99.  
  100.  
  101.         if(binaryGap[0] > binaryGap[1]){
  102.  
  103.             return binaryGap[0];
  104.         }
  105.  
  106.         else{
  107.  
  108.           return  binaryGap[1];
  109.         }
  110.     }
  111.  
  112.     else if(binaryGap[0] == 0){
  113.  
  114.         return 0;
  115.     }
  116.  
  117.     else{
  118.  
  119.         return binaryGap[0];
  120.     }
  121.  
  122.  
  123.  
  124. //    if(binaryGap[0] == 0){
  125.  
  126.  
  127. //        return 0;
  128. //    }
  129.  
  130.  
  131. //     if (binaryGap[0] > binaryGap[1]) {
  132.  
  133. //         return binaryGap[0];
  134. //    }
  135.  
  136. //     else{
  137.  
  138. //         return []
  139. //     }
  140.  
  141.  
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement