Posts

Showing posts from August, 2020

ACM ICPC Team

 Question :- https://www.hackerrank.com/challenges/acm-icpc-team/problem Solution in C language :- #include< stdio.h > #include< string.h > int   main (){      int   n , m ;      scanf ( "%d%d" ,& n ,& m );      char   str [ n ][ m ];      int   i , j ;      for ( i = 0 ; i < n ; i ++){          scanf ( "%s" , str [ i ]);      }      int   c = 0 , k , cm = 0 , z = 1 ;      for ( i = 0 ; i < n ; i ++){          for ( j = i + 1 ; j < n ; j ++){              c = 0 ;              for ( k = 0 ; k < m ; k ++){                  if ( str [ i ][ k ]== '1'   ||   str [ j ][ k ]== '1' )                      c ++;              }              if ( cm < c ){                  cm = c ;                  z = 1 ;              }              else   if ( cm == c )                  z ++;          }      }      printf ( "%d\n%d" , cm , z );      //printf("\n%c\n",str[1][2]);      return   0 ; }

Jumping On The Clouds

 Question :- https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem Solution in C language :- #include< stdio.h > main () {      int   n , i , sum = 0 ;      scanf ( "%d" ,& n );      int   a [ n ];      for ( i = 0 ; i < n ; i ++)      scanf ( "%d" ,& a [ i ]);      for ( i = 0 ; i < n ;)      {          if ( a [ i + 1 ]== 0 && a [ i + 2 ]== 1 )          {              i = i + 1 ;              sum ++;          }          else          {              sum ++;              i = i + 2 ;          }            if ( i == n - 1 )          sum --;         }      printf ( "%d" , sum ); }

Repeated String

 Question :- https://www.hackerrank.com/challenges/repeated-string/problem Solution in C language :- #include< stdio.h > #include< string.h > int   main (){      long   long   int   n ;      char   s [ 100 ];      scanf ( "%s %lld" , s ,& n );      int   len = strlen ( s );      long   long   int   i , a = 0 ;      for ( i = 0 ; s [ i ]!= ' \ 0 ' ; i ++){          if ( s [ i ]== 'a' )              a ++;      }      long   long   int   t = n / len , temp = n % len ;      t = t * a ;      if ( temp != 0 ){          for ( i = 0 ; i < temp ; i ++){              if ( s [ i ]== 'a' )                  t ++;          }      }      printf ( "%lld" , t );      return   0 ; }

Cut The Sticks

 Question :- https://www.hackerrank.com/challenges/cut-the-sticks/problem Solution in C language :- #include< stdio.h > int   main (){      int   n ;      scanf ( "%d" ,& n );      int   a [ n ], i ;      for ( i = 0 ; i < n ; i ++){          scanf ( "%d" ,& a [ i ]);      }      int   temp = a [ 0 ], j , t = n , c = 0 , d = 0 ;      printf ( "%d\n" , n );      for ( j = 0 ; j < t ; j ++){          temp = 1000 ;          for ( i = 0 ; i < n ; i ++){              if ( a [ i ]> 0 ){                  if ( temp > a [ i ])                      temp = a [ i ];              }          }          for ( i = 0 ; i < n ; i ++){              a [ i ]= a [ i ]- temp ;              if ( a [ i ]> 0 )                  d ++;              if ( a [ i ]== 0 )                  c ++;          }          t = t - c + 1 ;          c = 0 ;          if ( d > 0 )              printf ( "%d\n" , d );         d = 0 ;      }      return   0

Library Fine

 Question :- https://www.hackerrank.com/challenges/library-fine/problem Solution in C language :- #include< stdio.h > int   main (){      int   d1 , d2 , m1 , m2 , y1 , y2 ;      scanf ( "%d%d%d%d%d%d" ,& d1 ,& m1 ,& y1 ,& d2 ,& m2 ,& y2 );      if ( d1 != d2   ||   m1 != m2   ||   y1 != y2 )      { if ( y1 >= y2 ){          if ( m1 > m2   &&   y1 == y2 ){              printf ( "%d" , 500 *( m1 - m2 ));          }          if ( m1 < m2   &&   y1 == y2 ){              printf ( "0" );          }          else   if ( d1 > d2   &&   m1 == m2   &&   y1 == y2 ){              printf ( "%d" , 15 *( d1 - d2 ));          }          else   if ( d1 < d2   &&   m1 == m2   &&   y1 == y2 ){              printf ( "0" );          }          else   if ( y1 > y2 ){              printf ( "10000" );          }      }      else   if ( y1 < y2 ){      

Sherlock and Squares

 Question :- https://www.hackerrank.com/challenges/sherlock-and-squares/problem Solution in C language :- #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > int   main (){ int   a , b ; scanf ( "%d" ); while ( scanf ( "%d%d" ,& a ,& b )> 0 ){      printf ( "%d\n" ,( int )( floor ( sqrt ( b ))- ceil ( sqrt ( a ))+ 1 ));} return   0 ;}

Append and Delete

 Question :- https://www.hackerrank.com/challenges/append-and-delete/problem Solution in C language :- #include< stdio.h > #include< string.h > int   main (){      char   a [ 100 ], b [ 100 ];      int   k ;      scanf ( "%s" , a );      scanf ( "%s" , b );      scanf ( "%d" ,& k );      int   la = strlen ( a ), lb = strlen ( b ), i , j , h , c = 0 , temp = 0 ; //    printf("%d %d\n",la,lb);      if ( la > lb ){          j = lb ;          h = la ;      }      else {          j = la ;          h = lb ;      }      for ( i = 0 ; i < j ; i ++){          if ( a [ i ]== b [ i ])              c ++;          else              break ;      }      if ( la + lb + 1 < k ){          printf ( "Yes" );          temp ++;      } //    printf("%d\n",c);      if ( la == lb   &&   la == c ){          if ( 2 * la == k - 1 )              printf ( "Yes" );          else   if ( k != 20   &&   2 * la

Find Digits

 Question :- https://www.hackerrank.com/challenges/find-digits/problem Solution in C language :- #include< stdio.h > int   main (){      int   n ;      scanf ( "%d" ,& n );      int   a [ n ], i ;      for ( i = 0 ; i < n ; i ++){          scanf ( "%d" ,& a [ i ]);      }      int   t , temp , c = 0 ;      for ( i = 0 ; i < n ; i ++){          t = a [ i ];          do {          temp = t % 10 ;          if ( temp == 0 ){              c = c ;          }          else {          if ( a [ i ]% temp == 0 ){              c ++;          }          }          t = t / 10 ;          } while ( t != 0 );          printf ( "%d\n" , c );          c = 0 ;      }           return   0 ; }

Extra Long Factorial

 Question :- https://www.hackerrank.com/challenges/extra-long-factorials/problem Solution in C++ language :- #include< iostream > using   namespace   std ; int   main (){      int   n ;      cin >> n ;      int   arr [ 100000 ]={ 0 };      int   leng = 1 , x = 0 , num = 0 , i ;      arr [ 0 ]= 1 ;      for ( i = 2 ; i <= n ; i ++){          x = 0 ;          num = 0 ;          while ( x < leng ){              arr [ x ]= arr [ x ]* i ;              arr [ x ]= arr [ x ]+ num ;              num = arr [ x ]/ 10 ;              arr [ x ]= arr [ x ]% 10 ;              x ++;          }          while ( num != 0 ){              arr [ leng ]= num % 10 ;              num = num / 10 ;              leng ++;          }      }      for ( i = leng - 1 ; i >= 0 ; i --){          cout << arr [ i ];      }      return   0 ; }

Circular Array Rotation

 Question :- https://www.hackerrank.com/challenges/circular-array-rotation/problem Solution in C language :- #include   < stdio.h >   /* function to print an array */ void   printArray ( int   arr [],   int   size );   /*Fuction to get gcd of a and b*/ int   gcd ( int   a ,   int   b );   /*Function to left rotate arr[] of siz n by d*/ void   leftRotate ( int   arr [],   int   d ,   int   n )   {        int   i ,   j ,   k ,   temp ;        int   g_c_d   =   gcd ( d ,   n );        for   ( i   =   0 ;   i   <   g_c_d ;   i ++)   {            /* move i-th values of blocks */          temp   =   arr [ i ];            j   =   i ;            while   ( 1 )   {                k   =   j   +   d ;                if   ( k   >=   n )                    k   =   k   -   n ;                if   ( k   ==   i )                    break ;                arr [ j ]   =   arr [ k ];                j   =   k ;            }            arr [ j ]   =   temp ;        }   }   /*UTILITY FUNCTIONS*/

Save The Prisoner!

 Question :- https://www.hackerrank.com/challenges/save-the-prisoner/problem Solution in C language :- #include< stdio.h > int   main (){      long   long   int   t , n , m , s , sum ;      scanf ( "%lld" ,& t );      for ( int   i = 0 ; i < t ; i ++){          scanf ( "%lld%lld%lld" ,& n ,& m ,& s );          long   long   int   temp = m % n ;          if (( temp + s - 1 )% n == 0 ){              printf ( "%lld\n" , n );          }          else {              printf ( "%lld\n" ,(( temp + s - 1 )% n ));          }         }      return   0 ; }