Day 22 of 100 days of code

ยท

1 min read

On the 22nd day, I took on three practice problems on CodeChef. Here they are:

Airline Restriction

#include <bits/stdc++.h>
using namespace std;

bool solution(int a,int b,int c,int d,int e){
  return ((a+b)<=d && c<=e);
}

int main(){
  int t;
  cin>>t;
  while(t--){
    int a,b,c,d,e;
    cin>>a>>b>>c>>d>>e;
    if(solution(a,b,c,d,e)||solution(a,c,b,d,e)||solution(b,c,a,d,e)){
      cout<<"YES"<<endl;
    }else{
      cout<<"NO"<<endl;
    }
  }
}

Zero String

#include <bits/stdc++.h>
using namespace std;

int solution(string s){
  int count_0 = count(s.begin(),s.end(),'0');
  int count_1 = count(s.begin(),s.end(),'1');
  if(count_0>=count_1){
    return count_1;
  }
  return 1+count_0;
}

int main(){
  int t;
  cin>>t;
  while(t--){
    int n;
    cin>>n;
    string s;
    cin>>s;
    int ans = solution(s);
    cout<<ans<<endl;
  }
}

Chef and subarray

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int solution(int arr[],int n){
  int cnt = n;
  for(int i =0;i<n-1;i++){
    ll sum = arr[i];
    ll p = arr[i];
    for(int j =i+1;j<n;j++){
      sum += arr[j];
      p *= arr[j];
      if(sum==p){
        cnt++;
      }

    }
  }
  return cnt;
}

int main(){
  int t;
  cin>>t;
  while(t--){
    int n;
    cin>>n;
    int arr[n];
    for(int i =0;i<n;i++){
      cin>>arr[i];
    }
    int ans =solution(arr,n);
    cout<<ans<<endl;
  }
}

Tomorrow, I plan to delve into sliding window algorithms and related problems. Join me in tomorrow's blog for more insights.

ย