Question: Mastercard, Recently asked On-Campus Question in IITs, November 2022
0
Entering edit mode

Question 1

Bob wants to purchase N pens and so has prepared a list of shops which sell the pens. Each shop charges a different price per pen and has limited quantity available in stock. Along with the price of pen, each shop charges fixed delivery fees irrespective of how many pens are bought. Write a program to help Bob get the N pens as cheaply as

possible.

Read the input from STDIN and print the output to STDOUT. Do not write arbitrary strings while reading the input or while printing, as these contribute to the standard output.


 

Input Format:

The first line of input has two integers: N, the number of pens Bob needs, and S, the number of shops, separated by single white space.

Next S lines have 3 integers, Q, P and D, each separated by single white spaces, where Q is the stock of

fee.

Output Format:

Single line of output has an integer, which is the minimum amount Bob has to spend to purchase N pen

 

Constraints:

i)   1 <= N <=10000

ii)  1 <= S <= 100

iii)  0 <= Q <= 10000

iv)  0 <= P <= 10000

v)   0 <= D <= 1000000

VI) N <= Q1+Q2+…+Qs

 

Sample Input 1:

10 2

5 5 50

1000 10 0

 

Sample Output 1:

100

 

Explanation 1:

No of pens Bob needs, N=10

No of shops, S=2

 

MasterCard1


Question 2

Consider the string str, split it into words based on the whitespaces they are having. Your task is to identify the number of pairs of consecutive characters of each word of str are in lexicographical order from left to right. Repete this process for all the words of the input string and then concatenate the counts of each word of the string in same order and display it as an output.

 

If there is no character in a word that comes lexicographically after its neighbor to the left, then its count becomes "0'.

 

Read the input from STDIN and write the output to STDOUT. You should not write arbitrary strings while reading the input standard output.

 

Constraints:

i. The given string only contains lowercase alphabets.

ii. The input contains only one string and it does not have any special characters in it other than white space.

 

Input Format:

A single line of input consists of a string str.

 

Output Format:

A single line of output consists of concatenated counts displayed as a result.

 

Sample Input1:

hello world

 

Sample Output1:

21

 

Explanation1:

From the given Sample Input1, the given string str is "hello world".

The string str is divided into the substrings “hello” and “world”. 

In “hello”, ‘e’ & ‘l’ and ‘l’ & ‘o’ are in lexicographical order. So, now the pair count is 2.

In the word “world”, only ‘o’ and ‘r’ are in lexical order, therefore pair count is 1. 

Concatenating both results in 21 so the output is 21.

 

Click here to Practice

MasterCard2

2
Entering edit mode

Question 2

Overview:

  • For every word in the string split on the basis of whitespace, find the pairs of the consecutive letter which are in lexicographically increasing order.
  • Do this for each word in the string, concatenate the result, and output the string.

Approach:

  • For each word, you need to check whether the alphabet to its right is lexicographically greater than the current alphabet.
  • We can process each word sequentially and keep adding the answer to a string.

Time Complexity:

  • O(N) where N is the length of the string.

PseudoCode:

    string s;
    while(cin&gt;&gt;s){
        int n=s.length();
        int count=0;
        for(int i=1;i &lt; n;i++){
            if(s[i] &gt; s[i-1]) count++;
        }
        cout &lt;&lt; count;
    }
    cout &lt;&lt; endl;
ADD COMMENTlink 23 months ago Harsh Priyadarshi 70
2
Entering edit mode

for question 1:

we can modify price => price+delivery fee

and then we will buy from that shop with minimum price  (as much as we can buy)  if stock is lesser than required than we will buy from next smaller price shop

ADD COMMENTlink 17 months ago Systumm • 200

Login before adding your answer.

Similar Posts
Loading Similar Posts