Showing posts with label Ice Cream Parlor Hacker Rank Solution in C. Show all posts
Showing posts with label Ice Cream Parlor Hacker Rank Solution in C. Show all posts

Tuesday, 3 October 2017

Ice Cream Parlor Hacker Rank Solution in C, Electronics Shop Hacker Rank Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
 {
    int m,n,t,i,j,flag;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        int a[n];
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=0;i<n;i++)
            for(j=i+1;j<n;j++)
                if(a[i]+a[j] == m)
                   printf("%d\t%d\n",i+1,j+1);
    }
    return 0;
}

Here in the above solution, we need to find all possible pairs in a single array. But if we need to find all possible pairs that can be formed with the elements from two different arrays then the j loop must start from 0.
Here is the example of such a problem

Problem: Electronics Shop


Solution:

#include<stdio.h>
int main()
{
    unsigned long long int b,n,m,i,j,ans=0;
    scanf("%llu%llu%llu",&b,&n,&m);
    int k[n],u[m];
    for(i=0;i<n;i++)
        scanf("%llu",&k[i]);
    for(i=0;i<m;i++)
        scanf("%llu",&u[i]);
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
            if(k[i]+u[j] <= b && ans<k[i]+u[j])
                ans=k[i]+u[j];
    }
    if(ans == 0)
        printf("-1");
    else
        printf("%llu",ans);
}