0% found this document useful (0 votes)
22 views11 pages

Mock Test 3

Uploaded by

sruthisruhi54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views11 pages

Mock Test 3

Uploaded by

sruthisruhi54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

2024_Infosys_MRECW_Power_Programmer_Test 3

Test Summary
 No. of Sections: 2
 No. of Questions: 3
 Total Duration: 90 min
Section 1 - Coding
Section Summary
 No. of Questions: 2
 Duration: 60 min
Additional Instructions:
None
Q1.
Problem Statement:

In recreational mathematics, a Niven number in a given number base, is an integer


that is divisible by the sum of its digits when written in that base. For example, in
base 10, 18 is a Niven number since 18 is divisible by 1+8 = 9. Also, 12001 in base 3 is
also a Niven number since the sum of the digits is 4 (which is 11 in base 3) divides
12001 (12001 = 1021 x 11).

Given a base b, any number n < b is trivially a Niven number. We will ignore this case.

Given a base b, and a positive integer T, find the lowest number L such that L, L+1,
..., L+T-1 are all Niven numbers but neither L-1 nor L+T are Niven numbers.
Input Format
The input consists of two integers b and t separated by a space.
 b: The base to which the numbers will be converted.
 t: The number of consecutive numbers in the given base which satisfy the condition.
Output Format
The output displays a single integer L such that L, L+1, ..., L+T-1 are all Niven
numbers but neither L-1 nor L+T are Niven numbers.

Refer to the sample outptut for the formatting specifications.


Constraints
2 ≤ b ≤ 10
1<T<7
Sample Input Sample Output
10 4
510
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q2.
Problem Statement:
Given a stair case, find total number of ways to reach the nth stair from bottom of the
stair when a person is only allowed to climb either 1 or 2 stairs at a time.

Assume he is standing on 0th step.


Input Format
The input consists of a single integer N, representing the total number of steps to be
climbed.
Output Format
The output is a single integer M, representing total number of ways to reach the n th
stair.

Refer to the sample output for the formatting specifications.


Constraints
1≤ N ≤ 50
Sample Input Sample Output
45
1836311903
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Section 2 - Additional Coding
Section Summary
 No. of Questions: 1
 Duration: 30 min
Additional Instructions:
None
Q1.
Problem Statement

You are given an integer array prices where prices[i] is the price of a given stock on
the ith day, and an integer k.

Find the maximum profit you can achieve. You may complete at most k transactions:
i.e. you may buy at most k times and sell at most k times.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell
the stock before you buy again).

Example 1

Input:
2
3
241

Output:
2

Explanation:
Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.

Example 2

Input:
2
6
326503

Output:
7

Explanation:
Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day
5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Input Format
The first line contains an integer k representing the maximum number of transactions
allowed.
The second line contains an integer n representing the number of days.
The third line contains n space-separated integers representing the prices of the stock
on each day.
Output Format
The output displays an integer representing the maximum profit achievable with at
most k transactions.

Refer to the sample output for the formatting specifications.


Constraints
1 ≤ k ≤ 10
2 ≤ n ≤ 10
0 ≤ price[i] ≤100
Sample Input Sample Output
2
3
2 4 1
2
Sample Input Sample Output
2
6
3 2 6 5 0 3
7
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Answer Key & Solution
Section 1 - Coding

Q1Test Case

Input
5 5
Output
44

Weightage – 10

Input
5 3
Output
200

Weightage – 15

Input
7 5
Output
66

Weightage – 10

Input
8 4
Output
11020

Weightage – 25

Input
8 3
Output
4224
Weightage – 15

Input
10 6
Output
12751220

Weightage – 25

Sample Input
10 4
Sample Output
510

Solution
#include<stdio.h>
#include<string.h>

int convert_to_base(int no, int base)


{
int store[100], index = 0;

while(no > 0)
{
int quot = no / base;
int rem = no % base;
store[index++] = rem;

no = quot;
}

int converted_no = 0, i;
for(i = index - 1; i >= 0; i--)
{
converted_no = converted_no * 10 + store[i];
}

return converted_no;
}

int compute_sum_of_digits(int n)
{
int sod = 0;
while(n > 0)
{
sod = sod + n % 10;
n = n / 10;
}
return sod;
}

int main() {
int b, t;
scanf("%d %d", &b, &t);

int i, count = 0;
for(i = b; ; i++)
{
int tmp = convert_to_base(i, b);
int sod = compute_sum_of_digits(tmp);
if(i % sod == 0)
{
count++;
}
else
{
if(count == t)
{
break;
}
count = 0;
}
}

printf("%u", convert_to_base(i - t, b));


return 0;
}

Q2Test Case

Input
10
Output
89

Weightage – 10

Input
20
Output
10946
Weightage – 10

Input
29
Output
832040

Weightage – 15

Input
36
Output
24157817

Weightage – 25

Input
25
Output
121393

Weightage – 15

Input
30
Output
1346269

Weightage – 25

Sample Input
45
Sample Output
1836311903

Solution
#include <stdio.h>

int recur(int curr_step, long int n, int results_cache[])


{
if(curr_step > n)
{
return 0;
}
if(curr_step == n)
{
return 1;
}
if(results_cache[curr_step] != -1)
{
return results_cache[curr_step];
}

int total_ways =recur(curr_step + 1, n, results_cache) + recur(curr_step + 2, n,


results_cache);

results_cache[curr_step] = total_ways;
return total_ways;
}

int total_ways(long int n)


{
int results_cache[n+1], i;
for(i = 0; i <= n; i++)
{
results_cache[i] = -1;
}
return recur(0,n,results_cache);
}
int main() {
long int n;
scanf("%ld",&n);
printf("%lld",total_ways(n));
return 0;
}
Section 2 - Additional Coding

Q1Test Case

Input
1
5
90 80 70 60 50
Output
0

Weightage – 10
Input
3
7
100 30 15 10 8 25 80
Output
72

Weightage – 15

Input
3
8
12 14 17 10 14 13 12 15
Output
12

Weightage – 25

Input
3
5
56 77 89 34 27
Output
33

Weightage – 15

Input
4
7
76 12 34 90 15 19 26
Output
89

Weightage – 25 Input
2
3
10 20 30
Output
20
Weightage – 10 Sample Input
2
3
2 4 1
Sample Output
2

Sample Input
2
6
3 2 6 5 0 3
Sample Output
7

Solution
#include <climits>
#include <iostream>
using namespace std;

int maxProfit(int price[], int n, int k)


{
int profit[k + 1][n + 1];
for (int i = 0; i <= k; i++)
profit[i][0] = 0;

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


profit[0][j] = 0;

for (int i = 1; i <= k; i++) {


for (int j = 1; j < n; j++) {
int max_so_far = INT_MIN;

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


max_so_far = max(max_so_far,
price[j] - price[m] + profit[i - 1][m]);

profit[i][j] = max(profit[i][j - 1], max_so_far);


}
}

return profit[k][n - 1];


}

int main()
{
int k;
cin >> k;
int n;
cin >> n;

int price[n];
for (int i = 0; i < n; ++i) {
cin >> price[i];
}

cout << maxProfit(price, n, k);

return 0;
}

You might also like