0% found this document useful (0 votes)
9 views13 pages

21BCS9185 Himanshu Bhardwaj It Day1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

Day-1

Name: Himanshu Bhardwaj UID: 21BCS9185

AIM: To solve Code Chef Problems

Q1-> Important Pages on CodeChef Problem

Code:

#include <bits/stdc++.h>

using namespace std;

int main() {

int a ,b;

cin>>a>>b;

if(a==1 && b==1) cout<<"https://discuss.codechef.com";

else if(a==1 && b==0) cout<<"https://www.codechef.com/contests";

else cout<<"https://www.codechef.com/practice";

OUTPUT:
Q2: Fire and Ice Problem

Code:
#include <bits/stdc++.h>

using namespace std;

string func(string &s1,string &s2){

string ans;

int carry = 0;

int x = s1.size()-1;

int y = s2.size()-1;

while(x>=0 || y>=0 || carry>0){

if(x>=0){

carry+=s1[x]-'0';

x--;

if(y>=0){

carry+=s2[y]-'0';

y--;
}

ans.push_back(carry%10+'0');

carry/=10;

reverse(ans.begin(),ans.end());

return ans;

int main() {

// your code goes here

vector<string>f(1001);

f[1]=f[2]="1";

for(int i=3;i<=1000;i++){

f[i]=func(f[i-1],f[i-2]);

int t;

cin>>t;

while(t--){

int n,m,ans=0;

cin>>n>>m;

for(auto &i:f[n]){

ans=(ans*10)+(i-'0');

ans%=m;

ans=(ans*2)%m;

cout<<ans<<'\n';

return 0;

OUTPUT:
Q3: WildCard Matching Problem

Code:
bool solve(string s,string p,int i,int j ,vector<vector<int>> &dp){

if(i<0 && j<0) return true;

if(i<0 && j>=0) {

for(int hold = 0;hold<=j;hold++){

if(p[hold]!='*') return false;

return true;

if(i>=0 && j<0) {

return false;

}
if(p[j]=='?' || s[i]==p[j]){

return dp[i][j] = solve(s,p,i-1,j-1,dp);

else if(p[j]=='*'){

return dp[i][j] = solve(s,p,i,j-1,dp) || solve(s,p,i-1,j,dp);

return dp[i][j]=false;

bool isMatch(string s, string p) {

int n= s.size();

vector<vector<bool>> dp(n+1,vector<bool> (m+1,0));

dp[0][0] = true;

for(int i=1;i<=n;i++){

dp[i][0]=false;

for(int j=1;j<=m;j++){

bool flag = true;

for(int k = 1;k<=j;k++){

if(p[k-1]!='*'){

flag = false;

break;

dp[0][j] = flag;

for(int i=1;i<=n;i++){

for(int j =1;j<=m;j++){

if(p[j-1]=='?' || s[i-1]==p[j-1]){

dp[i][j] = dp[i-1][j-1];

else if(p[j-1]=='*'){
dp[i][j] = dp[i][j-1] || dp[i-1][j];

else

dp[i][j]=false;

return dp[n][m];

OUTPUT:

Q4: Helping Machine Problem

CODE:

#include <iostream>

using namespace std;

void solve(){
int nd,xd;

cin>>nd;

int ad[3]={0};

for(int i=0; i<nd; i++){

cin>>xd;

if(xd==1 || xd==2){

ad[0]++;

}else if(xd==3 || xd==4){

ad[1]++;

}else{

ad[2]++;

cout<<"TYPE 1 - "<<ad[0]<<" TYPE 2 - "<<ad[1]<<" TYPE 3 - "<<ad[2]<<"\n";

int main() {

int t;

cin>>t;

while(t--){

solve();

return 0;

OUTPUT:
Q5: Sum of digits Problem

CODE:

#include <iostream>

using namespace std;

int sumOfDigits(int num) {

int sum = 0;

while(num > 0) {

sum += num % 10;

num /= 10;}

return sum;

int main() {

int T;

cin >> T;

while (T--) {

int num;

cin >> num;

int sum = sumOfDigits(num);

cout << sum << endl;


}

return 0;

OUTPUT:

Q6: Correct Sentence Problem

CODE:

#include <bits/stdc++.h>

#define ll long long int

using namespace std;

int main() {

int t;

cin>>t;

while(t--)

bool ans=true;

int k;
cin>>k;

while(k--){

bool upper=false,lower=false;

for(int i=0;i<s.length();i++){

if(s[i]>='N' && s[i]<='Z')

upper=true;

else if(s[i]>='a' && s[i]<='m')

lower=true;

else

ans=false;

if(upper && lower)

ans=false;

if(ans)

cout<<"YES\n";

else

cout<<"NO\n";

return 0;

OUTPUT:

Q7: Basic Calculator Problem


CODE:

class Solution {

public:

int calculate(string s) {

stack <int> nums, ops;

int num = 0;

int rst = 0;

int sign = 1;

for (char c : s) {

if (isdigit(c)) {

num = num * 10 + c - '0';

else {

rst += sign * num;

num = 0;

if (c == '+') sign = 1;

if (c == '-') sign = -1;

if (c == '(') {

nums.push(rst);

ops.push(sign);

rst = 0;

sign = 1;

if (c == ')' && ops.size()) {

rst = ops.top() * rst + nums.top();

ops.pop(); nums.pop();

rst += sign * num;


return rst;

};

OUTPUT:

Q8: Count Good Numbers Problem

CODE:

#define mod 1000000007

class Solution {

private:

long long power(long long x, long long n){

if(n == 0){
return 1;

long long ans = power(x, n/2);

ans *= ans;

ans %= mod;

if(n%2==1){

ans *= x;

ans %= mod;

return ans;

public:

int countGoodNumbers(long long n) {

long long numberOfOddPlaces = n/2;

long long numberOfEvenPlaces = n/2 + n%2;

return (power(5, numberOfEvenPlaces) * power(4, numberOfOddPlaces))%mod;

};

OUTPUT:

You might also like