Recent Post

Electronics Shop.c-HackerRank

Problem-Monica wants to buy a keyboard and a USB drive from her favorite electronics store. The store has several models of each. Monica wants to spend as much as possible for the 2 items, given her budget.

Given the price lists for the store's keyboards and USB drives, and Monica's budget, find and print the amount of money Monica will spend. If she doesn't have enough money to both a keyboard and a USB drive, print -1 instead. She will buy only the two required items.


Solution-

Code for the function provided:-

int getMoneySpent(int n, int* keyboards, int m, int* drives, int b) 

{

    int sum,max=-1;

    for(int i=0;i<n;i++)

    {

        for(int j=0;j<m;j++)

        {

            sum=keyboards[i]+drives[j];

            if(sum>max&&sum<=b)

            max=sum;

        }

    }

    return(max);

}



Link to the problem:-

https://www.hackerrank.com/challenges/electronics-shop/problem

Comments

Popular posts from this blog

Caesar Cipher.c-HackerRank

Bon Appétit.c-HackerRank