Monday, 1 July 2019

Restaurant Hacker Rank Solution in C, Filling Jars Hacker Rank Solution in C, Connecting Towns Hacker Rank Solution in C,

                                    

Restaurant


GCD of length and breadth of the bread will divide both the length and breadth giving squares of bread pieces of size GCD x GCD and remaining part of bread can only be cut into 1x1 pieces.
The number of square pieces of maximum size = length *breadth/GCDxGCD

#include <stdio.h>
int gcd(int a, int b)
{
    if(a == b)
        return a;
    if(a == 0)
        return b;
    return gcd(b%a,a);
}
int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int l,b,ans;
        scanf("%d%d",&l,&b);

        if(l<b)
            ans=gcd(l,b);
        else
            ans=gcd(b,l);

        printf("%d\n",(l*b)/(ans*ans));
    }
    return 0;

}
Connecting Towns
https://www.hackerrank.com/challenges/connecting-towns/submissions/code/112096613

int connectingTowns(int n, int* routes) {

     int i,ans=1;
     for(i=0;i<n-1;i++) // if n cities are there then edges between them will be n-1
        ans = ((ans%1234567)*(routes[i]%1234567))%1234567;
    return ans;

}
Filling Jars


#include <stdio.h> int main() { long long int n,m; scanf("%ld%ld",&n,&m); unsigned long long int sum=0; while(m--) { long int a,b,k; scanf("%ld%ld%ld",&a,&b,&k); sum = sum+(b-a+1)*k; } printf("%lld",sum/n); return 0; }

No comments:

Post a Comment