Posts

Showing posts from September, 2020

CodeChef::VOTERS

 #include <bits/stdc++.h> using namespace std ; #define boost ios::sync_with_stdio(0);cin.tie(0) #define ll long long int main(){ boost ; ll n1,n2,n3,n ; cin >> n1 >> n2 >> n3 ; n = n1+n2+n3 ; ll arr[n]; for(ll i = 0; i < n ; i++){     cin >> arr[i] ; } vector <ll> v ; ll count{1},first_elmt; sort(arr,arr+n); first_elmt = arr[0]; for(ll i =1;i<n;i++){     if(first_elmt == arr[i]){         count ++ ;     }     else{         if(count >= 2)         {             v.push_back(first_elmt);         }         first_elmt = arr[i];         count = 1;     } } cout << v.size() << '\n' ; for(ll i = 0;i<v.size();i++){     cout << v[i] << '\n'; } return 0 ; }

CodeChef::CSUB

  #include   <bits/stdc++.h> using  namespace   std  ; #define   boost   ios :: sync_with_stdio ( 0 ); cin . tie ( 0 ) #define   ll  long long  int   main  (){      boost  ;      ll   t  ;      cin   >>   t  ;      while  ( t --)     {        ll   n  ;        cin   >>   n  ;        string   seq  ;        cin   >>   seq  ;        ll   count { 0 };        for  ( int   i  =  0 ;  i  <  seq . size ();  i ++){            if  ( seq [ i ]  ==  '1' )  count  ++ ;       }        cout   <<  ( count *( count + 1 ))/ 2   <<   ' \n ' ;                  }           return   0  ; }

CodeChef::Bowlers(Incorrect)

  #include   <bits/stdc++.h> #define   boost   ios :: sync_with_stdio ( 0 ); cin . tie ( 0 ) using  namespace   std  ; int   main  (){      boost  ;      long   long   int   t  ;      cin   >>   t  ;      while  ( t --)     {          long   long   int   n , k , l  ;          cin   >>   n   >>   k   >>   l  ;          long   long   int   temp  =  k  ;          if ( k  *  l  <  n )  cout   <<  - 1   <<   ' \n ' ;          else         {              long   long   int   arr [ n ];              for  ( long   long   int   i  =  0 ;  i  <  n ;  i ++)             {                                   if ( k  ==  0 )  k  =  temp  ;                                   arr [ i ] =  k  ;                   k  -- ;                                   }              for  ( long   long   int   i  =  0 ;  i  <  n ;  i ++)             {                  cout   <<   arr [ i ]  <<   " "  ;             }                  cout   &l

Check if Array is Sorted?

  #include   <bits/stdc++.h> using  namespace   std  ; bool   sorted ( int   arr [], int   n ){     //Base case      if ( n == 0  ||  n == 1 )  return   true  ;      if ( arr [ 0 ] >  arr [ 1 ])  return   false  ;          //Recursive case      bool   smallarraysorted  =  sorted ( arr + 1  ,  n - 1 );     // Calculation      if ( smallarraysorted )  return   true  ;      else   return   false  ; } int   main (){      int   arr [] = { 1 , 2 , 3 , 5 , 4 } ;      int   n  =  5  ;      cout   <<   sorted ( arr , n ) ;      return   0 ; }

Geometric Sum

  #include   <bits/stdc++.h> using  namespace   std  ; float   Gsum ( int   k ){     //base case      if ( k == 0 )  return   1  ;     //Recursive case      double   smallAns  =  Gsum ( k - 1 );     //Calculation      return   smallAns  +  1.0 / pow ( 2 , k ); } int   main (){      cout   <<   Gsum ( 3 );      return   0  ; }

Count Zeros

  #include   <bits/stdc++.h> using  namespace   std  ; int   countZeros ( int   n ){     //base case      if ( n == 0 )  return   0 ;     //recursive case      int   smallAns  =  countZeros ( n / 10 ) ;     //calculation      int   last_digit  =  n % 10  ;      if ( last_digit  ==  0 )  return   1 + smallAns  ;      else     {          return   smallAns  ;     }      } int   main () {      cout   <<   countZeros ( 10320 ) ;      return   0  ;  }

Multiply two numbers

  #include   <bits/stdc++.h> using  namespace   std  ; int   multiply ( int   m , int   n ){     //base case      if ( n == 0 )  return   0 ;     //Recursive case      int   smallAns  =  multiply ( m , n - 1 ) ;     //Calculation      return   smallAns  +  m  ; } int   main  (){      cout   <<   multiply ( 5 , 3 )  <<   ' \n ' ;      return   0  ; }

Codechef::SC_01

  #include <bits/stdc++.h> using namespace std ; #define boost ios::sync_with_stdio( false ) ; cin.tie( 0 ) long long int subArrayExists ( long long int arr[] , long long int m) { unordered_set < int > sumSet ; // Traverse through array and store prefix sums long long int sum = 0 ; long long int count = 0 ; for ( long long int i = 0 ; i < m ; i++) { sum += arr[i] ; // If prefix sum is 0 or it is already present if (sum == 0 || sumSet.find(sum) != sumSet.end()) count ++ ; sumSet.insert(sum) ; } return count ; } int main (){ long long int n ; cin >> n ; long long int arr[n] ; for ( long long int i = 0 ; i < n ; ++i) { cin >> arr[i] ; } long long int m = sizeof (arr)/ sizeof (arr[ 0 ]) ; long long int count = subArrayExists(arr , m) ; cout << count << ' \n ' ; return 0 ; }

CodeChef::SC_02

  #include <bits/stdc++.h> using namespace std ; #define boost ios::sync_with_stdio( false ) ; cin.tie( 0 ) int main (){ int t ; cin >> t ; while (t--){ int n ; cin >> n ; int boys[n] ; int girls[n] ; for ( int i = 0 ; i < n ; ++i) { cin >> boys[i] ; } for ( int i = 0 ; i < n ; ++i) { cin >> girls[i] ; } sort(boys , boys+n) ; sort(girls , girls+n) ; bool checker{ false } ; for ( int i = 1 ; i < n ; ++i) { if (boys[ 0 ] > girls[ 0 ] and boys[i] > girls[i]){ checker = true ; } else if (girls[ 0 ] > boys[ 0 ] and girls[i] > boys[i]){ checker = true ; } else { checker = false ; break; } } if (checker){ cout << "YES" << ' \n

ZCO-2014::Variation

  #include <bits/stdc++.h> using namespace std ; #define boost ios::sync_with_stdio( false ) ; cin.tie( 0 ) int main (){ int n , k ; cin >> n >> k ; int arr[n] ; for ( int i = 0 ; i < n ; ++i) { cin >> arr[i] ; } int count{ 0 } ; int i = 0 ; int j = 0 ; sort(arr , arr + n) ; while (j < n){ if (arr[j] - arr[i] >= k){ count += n-j ; i++ ; } else { j++ ; } } cout << count << ' \n ' ; return 0 ; }

ZCO-2013::Video Game

// CODE : ZCO14001 #include <bits/stdc++.h> using namespace std ; #define boost ios::sync_with_stdio( false ) ; cin.tie( 0 ) int main () { boost ; long long int width , maxHeight ; cin >> width >> maxHeight ; long long int stack[width] ; for ( long long int i = 0 ; i < width ; i++) { cin >> stack[i] ; } long long int pointer{ 0 } ; bool hasBox{ false } ; while ( true ) { int input ; cin >> input ; if (input == 1 and pointer > 0 ) { pointer -- ; } else if (input == 2 and pointer < width - 1 ) { pointer ++ ; } else if (input == 3 and hasBox == false and stack[pointer] != 0 ) { stack[pointer] -- ; hasBox = true; } else if (input == 4 and hasBox == true and stack[pointer] != maxHeight) { stack[pointer] ++ ; hasBox = false; } else if (input == 0 ) { break; } }

Sum of digits

  #include <bits/stdc++.h> using namespace std ; int sum ( int n){ //Base case if (n== 0 ) return 0 ; //Recursive case int smallAns = sum(n/ 10 ) ; //Calculation int last_digit = n% 10 ; return smallAns + last_digit ; } int main (){ int n ; cin >> n ; cout << sum(n) << ' \n ' ; return 0 ; }

Number of Digits

  #include <bits/stdc++.h> using namespace std ; int count ( int n){ //Base case if (n== 0 ) return 0 ; //Recursive case int smallAns = count(n/ 10 ) ; //Calculation return smallAns + 1 ; } int main (){ int n ; cin >> n ; cout << count(n) << ' \n ' ; }

House Area Calculator

  #include <bits/stdc++.h> using namespace std ; float converter ( float n){ int intpart = ( int )n ; float decpart = (n - intpart)* 10 ; decpart = decpart/ 12 ; float sum = intpart + decpart ; return ( float )sum ; } int main (){ cout << "__________House Area Calculator_________ " << ' \n ' ; cout << "Note :- Only give values in Feet & Inches" << ' \n ' ; cout << "Some Examples on how to enter values" << ' \n ' ; cout << "1) 5.6 --Here 5 is in Feet's and 6 is in inches" << ' \n ' ; float kitchen_length , feet_kitchen_length ; float kitchen_width , feet_kitchen_width ; float hall_length , feet_hall_length ; float hall_width , feet_hall_width ; float bedroom1_length , feet_bedroom1_length ; float bedroom1_width , feet_bedroom1_width ; float bedroom2_length , feet_bedroom2_length ; float

Print first n Numbers?

  #include <bits/stdc++.h> using namespace std ; void print ( int n){ //Base case if (n== 0 ) return ; //Mandatory //Recursive case print(n- 1 ) ; // 1 2 3 4 ...n-1 //Calculation cout << n << ' \n ' ; return ; //Optional } void print2 ( int n){ //Base case if (n== 0 ) return ; //Mandatory cout << n << ' \n ' ; //Recursive case print2(n- 1 ) ; ///n-1....3 2 1 return ; //Optional } int main (){ int n ; cin >> n ; print2(n) ; return 0 ; }

Power

  #include <bits/stdc++.h> using namespace std ; int pow ( int x , int n){ //Base case if (n== 0 ) return 1 ; //Recursive case int smallAns = pow( x , n- 1 ) ; //Calculation return x*smallAns ; } int main (){ int x ; cin >> x ; int n ; cin >> n ; cout << pow(x , n) ; }

Fibonacci Numbers

  #include <bits/stdc++.h> using namespace std ; int fib ( int n){ //Base case if (n== 0 ) return 0 ; if (n== 1 ) return 1 ; //Recursive case int smallOutput1 = fib(n- 1 ) ; int smallOutput2 = fib(n- 2 ) ; //Calculation return smallOutput1 + smallOutput2 ; } int main (){ int n ; cin >> n ; cout << fib(n) << ' \n ' ; return 0 ; }

How Recursion Works?

  #include <bits/stdc++.h> using namespace std ; int fact ( int n){ //1st step Base Case if (n== 0 ){ return 1 ; } int smallans = fact(n- 1 ) ; // 2nd step Assumption Recursive Case int ans = n*smallans ; //3rd step Calculation return ans ; } int main (){ int n ; cin >> n ; cout << fact(n) << ' \n ' ; return 0 ; }

Introduction to Recursion

  #include <bits/stdc++.h> using namespace std ; int fact ( int n){ if (n < 0 ) return - 1 ; if (n== 0 ) return 1 ; int small_ans = fact(n- 1 ) ; return n*small_ans ; } int main (){ int n ; cin >> n ; int ans = fact(n) ; cout << ans << ' \n ' ; return 0 ; }

CodeChef::KOL15A

  #include <bits/stdc++.h> using namespace std ; int main (){ int t ; cin >> t ; while (t--){ string s ; cin >> s ; int sum{ 0 } ; for ( int i = 0 ; s [ i ] ; ++i) { if ( '0' <= s [ i ] && s [ i ] <= '9' ){ sum = sum + s [ i ] - '0' ; } } cout << sum << ' \n ' ; } return 0 ; }

CodeChef::NUM239

  #include <bits/stdc++.h> using namespace std ; int main (){ int t ; cin >> t ; while (t--){ int left ; int right ; cin >> left >> right ; int full_array[right - left + 1 ] ; for ( int i = 0 ; i < right - left + 1 ; ++i) { full_array[i] = i + left ; } int count { 0 } ; for ( int i = 0 ; i < right - left + 1 ; ++i) { if (full_array[i] % 10 == 2 or full_array[i] % 10 == 3 or full_array[i] % 10 == 9 ){ count ++ ; } } cout << count << ' \n ' ; } return 0 ; }

CodeChef::JOHNY

  #include <bits/stdc++.h> using namespace std ; int main (){ int t ; cin >> t ; while (t--){ int n ; cin >> n ; long int a[n] ; for ( int i = 0 ; i < n ; ++i) { cin >> a[i] ; } int k ; cin >> k ; int smallest{ 0 } ; for ( int i = 0 ; i < n ; ++i) { if (a[i] < a[k- 1 ]){ smallest ++ ; } } cout << smallest + 1 << ' \n ' ; } return 0 ; }