Google Code Jam 2021 ( Q. Round )

Problem 1: Reversort

#include <iostream>
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int check(int arr1[],int arr2[],int l){
    
    for(int i=0;i<l;i++)
    {
        if(arr1[i]==arr2[i])
        continue;
        else
        return 0;
    }
    return 1;
}
void insertionSort(int arr[], int n)
{
    int i, key, j;
    for (i = 1; i < n; i++)
    {
        key = arr[i];
        j = i - 1;
 
        /* Move elements of arr[0..i-1], that are
        greater than key, to one position ahead
        of their current position */
        while (j >= 0 && arr[j] > key)
        {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}
int main() {
	int t,n,ii,jj,sum,m;
	cin>>t;
	m=1;
	while(t--)
	{
	    sum=0;
	    cin>>n;
	    int arr[n];
	    int sarr[n];
	    for(int i=0;i<n;i++)
	    {
	    cin>>arr[i];
	    sarr[i]=arr[i];
	    }
	    insertionSort(sarr,n);
	    if(check(sarr,arr,n)==1)
	    sum=n-1;
	    else
	    {
	    for(int i=1;i<n;i++)
	    {
	        for(int j=i;j<=n;j++)
	        {
	            if(i==arr[j-1])
	            {
	                
	                jj=j;
	                ii=i;
                    if(j!=i)
                    reverse(arr+i-1,arr+j);
                    
	                break;
	            }
	        }
	            sum+=jj-ii+1;
	    }
	    }
	    cout<<"Case #"<<m<<": "<<sum<<"\n";
	    m++;
	
	}
	return 0;
}

Problem 2: Moons and Umbrellas

Code-1

#include <iostream>
using namespace std;
int main()
{
    int t, x, y, l,l1, sum, m;
    string s,s1="";
    cin >> t;
    m = 1;
    while (t--)
    {
        sum = 0; s1="";
        cin >> x >> y >> s;
        l = s.length();
        for(int i=0;i<l;i++)
        if(s.at(i)!=63)
        s1+=s.at(i);
        l1=s1.length();
        for(int i=0;i<l1-1;i++)
            {
                if((int)s1.at(i+1)>(int)s1.at(i))
                sum+=x;
                else if((int)s1.at(i+1)<(int)s1.at(i))
                sum+=y;
            }
            cout << "Case #" << m++ << ": " << sum << "\n";
        }
        return 0;
}

Code-2 (Explained in the video)

#include <iostream>
using namespace std;
string removeq(string str,int l) //to remove '?' 
{
    string snew="";
    for(int i=0;i<l;i++)
    {
        if(str.at(i)!=63)
        snew+=str.at(i);
    }
    return snew;
}
int main()
{
    int t, x, y, l,l1, n, n1, c, n2, sum, m, ii;
    string s,s1;
    char ch, ch1, ch2;
    cin >> t;
    m = 1;
    while (t--)
    {
        c = 1;
        sum = 0;
        cin >> x >> y >> s;

        l = s.length();
        s1=removeq(s,l);
        l1=s1.length();
        if (l1 == 1)
            sum = 0;
        else{
            for(int i=0;i<l1-1;i++)
            {
                ch=s1.at(i);
                ch1=s1.at(i+1);
                n=(int)ch;
                n1=(int)ch1;
                if(n==n1)
                continue;
                else if(n1>n)
                sum+=x;
                else if(n1<n)
                sum+=y;
            }
        }
        cout << "Case #" << m++ << ": " << sum << "\n";
    }
    return 0;
}

Published by Abhinav Awasthi

Entrepreneur | Coder | Harcourtian'24

Leave a comment

Design a site like this with WordPress.com
Get started