Mitsogo Coding Questions and Answers 2023
Sample Mitsogo Coding Questions with Solutions 2023
Sample Mitsogo Coding Questions and Answers page will help you to get sample Coding Questions asked in the Online Assessment and Technical Interviews of Mitsogo.
Go through this page to get all Sample Mitsogo Coding Questions to preparing for Online Technical Assessment and Technical Interviews of Mitsogo. Apart from that you will get FAQ’s related to Mitsogo Recruitment Process.
Sample Mitsogo Coding Questions with Solutions for Freshers
Question 1: Maximum Toys
In a toy shop there are a number of toys presented with several various – priced toys in a specific order. You have a limited budget and would like to select the greatest number of consecutive toys that fit within the budget. Given prices of the toys and your budget, what is the maximum number of toys that can be purchased for your child?
Example:
prices=[1,4,5,3,2,1,6]
money=6
All sub arrays that sum to less than or equal to 6 .
length 1: [1] [4] [5] [3] [2] [1] [6]
length 2: [1,4] [3,2] [2,1]
length 3: [3,2,1]
The longest of these or the maximum number of toys that can be purchased is 3.
Function description
Complete the function getMaxToys in the editor below
getMaxToys has the following parameters:
int prices[n] : the prices of the various toys
int money: the amount of money you can spend on toys
Returns
Int the maximum number of toys you can purchase
Constraints
1<=n<=10^5
1<=price[i]<=100
1<=money<=10^6
Sample case
Sample input 0
7->n=7
1-> price[]=[1,4,5,3,2,1,6]
4
5
3
2
1
6
6 ->money
Sample Output
1
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
#include< bits/stdc++.h> using namespace std; int getMaxToys(vector< int>& prices, int money) { int sum = 0; int count = 0; int maxCount = 0; for (int i = 0; i < prices.size(); i++) { sum = count = 0; for (int j = i; j < prices.size(); j++) { if ((sum + prices[j]) <= money) { sum += prices[j]; count++; maxCount = max(count, maxCount); } else { count = 0; sum = 0; break; } } } return maxCount; } int main() { int n; cin >> n; vector< int> prices(n); for (int i = 0; i < n; i++) { int t; cin>>t; prices[i] = t; } int money; cin >> money; cout << getMaxToys(prices, money) << endl; return 0; }
import java.util.*; public class Main { public static int getMaxToys (int prices[], int money) { int sum = 0; int count = 0; int max = 0; for (int i = 0; i < prices.length; i++) { sum = count = 0; for (int j = i; j < prices.length; j++) { if ((sum + prices[j]) <= money) { sum = sum + prices[j]; count = count + 1; max = Math.max (count, max); } else { count = 0; sum = 0; break; } } } return max; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int prices[] = new int[n]; for (int i = 0; i < n; i++) prices[i] = sc.nextInt (); int money = sc.nextInt (); System.out.println (getMaxToys (prices, money)); } }
def getMaxToys(n, arr, money): L, H = 0, n - 1 while L <= H: if sum(arr[L:H + 1]) <= money: break else: if arr[L] > arr[H]: L += 1 else: H -= 1 return H - L + 1 n = int(input()) arr = list(map(int, input().split())) # Read and split the input line money = int(input()) print(getMaxToys(n, arr, money))
Question 2: Stepping Numbers
Problem Description :
Stepping Numbers are numbers in which the adjacent digits differ by 1. For example, 123, 545, and 98 are stepping numbers, while 321, 444, and 75 are not. The task is to find all stepping numbers in a given range [n, m].
- For example
- Range: [100, 500]
- Stepping Numbers: 101, 121, 123, 210, 212, 232, 234, 321, 323, 343, 345
- Explanation: The stepping numbers between 100 and 500 are 101, 121, 123, 210, 212, 232, 234, 321, 323, 343, and 345. These numbers have adjacent digits that differ by 1.
Write code to find out all the stepping numbers in the given range.
Input Format: First line contains two numbers N,M
Output Format: Print all the stepping numbers present in the range.
Constraints: 0 <= N < M <= 1,000,000,000
#include<bits/stdc++.h> using namespace std; void displaySteppingNumbers(int n, int m) { queue q; for (int i = 1; i <= 9; i++) { q.push(i); } while (!q.empty()) { int stepNum = q.front(); q.pop(); if (stepNum >= n && stepNum <= m) { cout << stepNum << " "; } if (stepNum == 0 || stepNum > m) { continue; } int lastDigit = stepNum % 10; int stepNumA = stepNum * 10 + (lastDigit - 1); int stepNumB = stepNum * 10 + (lastDigit + 1); if (lastDigit == 0) { q.push(stepNumB); } else if (lastDigit == 9) { q.push(stepNumA); } else { q.push(stepNumA); q.push(stepNumB); } } } int main() { int n, m; cout << "Enter the range [n, m]: "; cin >> n >> m; cout << "Stepping Numbers between " << n << " and " << m << ": "; displaySteppingNumbers(n, m); return 0; }
import java.util.LinkedList; import java.util.Queue; public class Main { public static void displaySteppingNumbers(int n, int m) { Queue queue = new LinkedList<>(); for (int i = 1; i <= 9; i++) { queue.add(i); } while (!queue.isEmpty()) { int stepNum = queue.poll(); if (stepNum >= n && stepNum <= m) { System.out.print(stepNum + " "); } if (stepNum == 0 || stepNum > m) { continue; } int lastDigit = stepNum % 10; int stepNumA = stepNum * 10 + (lastDigit - 1); int stepNumB = stepNum * 10 + (lastDigit + 1); if (lastDigit == 0) { queue.add(stepNumB); } else if (lastDigit == 9) { queue.add(stepNumA); } else { queue.add(stepNumA); queue.add(stepNumB); } } } public static void main(String[] args) { int n = 0, m = 100; System.out.print("Stepping Numbers between " + n + " and " + m + ": "); displaySteppingNumbers(n, m); } }
def displaySteppingNumbers(n, m): queue = [] for i in range(1, 10): queue.append(i) while queue: stepNum = queue.pop(0) if stepNum >= n and stepNum <= m: print(stepNum, end=' ') if stepNum == 0 or stepNum > m: continue lastDigit = stepNum % 10 stepNumA = stepNum * 10 + (lastDigit - 1) stepNumB = stepNum * 10 + (lastDigit + 1) if lastDigit == 0: queue.append(stepNumB) elif lastDigit == 9: queue.append(stepNumA) else: queue.append(stepNumA) queue.append(stepNumB) n = 0 m = 100 print("Stepping Numbers between", n, "and", m, ": ", end='') displaySteppingNumbers(n, m)
Question 3: Amusement park
Problem Statement – Akshay loves to go to WONDERLA , an amusement park. They are offering students who can code well with some discount. Our task is to reduce the cost of the ticket as low as possible.
The cost of tickets can be removed by removing the digits from the price given. They will give some k turns to remove the digits from the price of the ticket. Your task is to help Akshay in coding a program that can help him to reduce the cost of a ticket by removing the digits from its price and getting the maximum possible discount.
Note – You cannot make the cost of a ticket zero. For eg -: If the cost of a ticket is 100, and you have 2 turns to reduce the price, the final price will be 1 and not zero.
Constraints:
- 1 <= number of tickets <= 10^5
- 1 <= K < Number of digits in Price of ticket
Input Format for Custom Testing:
- The first line contains a string,Tickets, denoting the given cost of each ticket.
- The next line contains an integer, K, denoting the number of tickets that is to be removed.
Sample Cases:
- Sample Input 1
203
2 - Sample Output 1
0
#include<bits/stdc++.h> using namespace std; int smallestNumber(string num, int k) { if (num.length() <= k) { return 0; } unordered_map<char, int> pos; for (int i = 0; i < num.length(); i++) { pos[num[i]] = i; } string temp = num; // Sort the characters in ascending order sort(num.begin(), num.end()); // Get the required substring string ans = num.substr(0, num.length() - k); vector v; for (int i = 0; i < ans.length(); i++) { v.push_back(pos[ans[i]]); } // Sort the positions sort(v.begin(), v.end()); string ret; for (int i = 0; i < v.size(); i++) { ret += temp[v[i]]; } int final = stoi(ret); return final; } int main() { string s; cin >> s; int k; cin >> k; int ans = smallestNumber(s, k) % static_cast(pow(10, 9) + 7); cout << ans; return 0; }
import sys n=input() k=int(input()) n1=len(n) if len(n)<=k: print(0) sys.exit() a='' i=0 while i < (n1-1) and k>0: if int(n[i])>int(n[i+1]): i+=1 k-=1 continue else: a+=n[i] i+=1 a+=n[i] i+=1 if k>0: a=a[:-k] if i<=(n1-1): while i < n1: a+=n[i] i+=1 print(int(a)%((10**9)+7))
import java.util.*; class Main { public static void main (String[]args) { Scanner sc = new Scanner (System.in); String s = sc.nextLine (); int k = sc.nextInt (); int ans; System.out.println (smallestNumber (s, k) % (int) (1e9 + 7)); } static int smallestNumber (String num, int k) { if (num.length () <= k) return 0; HashMap < Character, Integer > pos = new HashMap <> (); for (int i = 0; i < num.length (); i++) { pos.put (num.charAt (i), i); } String temp = num; num = sortString (num); String ans = num.substring (0, num.length () - k); ArrayList < Integer > v = new ArrayList <> (); for (int i = 0; i < ans.length (); i++) { v.add (pos.get (ans.charAt (i))); } Collections.sort (v); String ret = ""; for (int i = 0; i < v.size (); i++) { ret += temp.charAt (v.get (i)); } int result = Integer.parseInt ("" + ret); return result; } public static String sortString (String inputString) { char tempArray[] = inputString.toCharArray (); Arrays.sort (tempArray); return new String (tempArray); } }
Question 4: Airport Authority
Problem Statement -:
In an airport, the Airport authority decides to charge a minimum amount to the passengers who are carrying luggage with them. They set a threshold weight value, say, T, if the luggage exceeds the weight threshold you should pay double the base amount. If it is less than or equal to threshold then you have to pay $1.
Function Description:
Complete the weightMachine function in the editor below. It has the following parameter(s):
Parameters:
Name | Type | Description |
N | Integer | number of luggage |
T | Integer | weight of each luggage |
weights[ ] | Integer array | threshold weight |
Returns: The function must return an INTEGER denoting the required amount to be paid.
Constraints:
- 1 <= N <= 10^5
- 1 <= weights[i] <= 10^5
- 1 <= T <= 10^5
Input Format for Custom Testing:
- The first line contains an integer, N, denoting the number of luggage.
- Each line i of the N subsequent lines (where 0 <= i <n) contains an integer describing the weight of ith luggage.
- The next line contains an integer, T, denoting the threshold weight of the boundary wall.
Sample Cases:
- Sample Input 1
4
1
2
3
4
3 - Sample Output 1
5 - Explanation:
Here all weights are less than threshold weight except the luggage with weight 4 (at index 3) so all pays base fare and it pays double fare.
#include<stdio.h> long int weightMachine(long int N,long int weights[],long int T) { long int amount=0,i; for(i=0;i<N;i++) { amount++; if(weights[i]>T) { amount++; } } return amount; } int main() { long int N,i,T; scanf("%ld",&N); long int weights[N]; for(i=0;i<N;i++) { scanf("%ld",&weights[i]); } scanf("%ld",&T); printf("%ld",weightMachine(N,weights,T)); return 0; }
#include <bits/stdc++.h> using namespace std; long int weightMachine(long int N,long int weights[],long int T) { long int amount=0,i; for(i=0;i<N;i++) { amount++; if(weights[i]>T) { amount++; } } return amount; } int main() { long int N,i,T; cin>>N; long int weights[N]; for(i=0;i<N;i++) { cin>>weights[i]; } cin>>T; cout<<weightMachine(N,weights,T); return 0; }
import java.util.*; class Main { static int weightMachine (int N, int weights[],int T) { int amount = 0, i; for (i = 0; i < N; i++) { amount++; if (weights[i] > T) { amount++; } } return amount; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int weights[]= new int[n]; for(int i=0; i<n; i++) weights[i] = sc.nextInt(); int t = sc.nextInt (); System.out.println (weightMachine(n, weights, t)); } }
def weightMachine(N,weights,T): amount=0 for i in weights: amount+=1 if(i>T): amount+=1 return amount N=int(input()) weights=[] for i in range(N): weights.append(int(input())) T=int(input()) print(weightMachine(N,weights,T))
Question 5: Majority Element
The majority element in an array is defined as the element that appears more than ⌊n/2⌋ times, where n is the length of the array.
In other words, it is the element that occurs most frequently and makes up more than half of the array.
Given an array of integers, the task is to find the majority element and return it. If there is no majority element, If there is no majority element, the algorithm should indicate that.
Examples:
Example 1:
Input: [3, 3, 4, 2, 4, 4, 2, 4, 4]
Output: 4
Explanation:
In the given array, the number 4 appears 5 times, which is more than half of the array size (9/2 = 4.5). Therefore, 4 is the majority element.
Example 2:
Input: [1, 2, 3, 4, 4, 4, 4]
Output: 4
Explanation:
In this case, the number 4 appears 4 times, which is more than half of the array size (7/2 = 3.5). Thus, 4 is the majority element.
Example 3:
Input: [1, 2, 3, 4, 5]
Output: -1
Explanation:
There is no majority element in this array since no number appears more than half of the array size (5/2 = 2.5).
Example 4:
Input: [2, 2, 2, 3, 3, 4, 4, 4, 4]
Output: -1
Explanation:
In this case, although the number 4 appears 4 times, it does not occur more than half of the array size (9/2 = 4.5).
Hence, there is no majority element.
#include<bits/stdc++.h> int majorityElement(std::vector& nums) { int count = 0; int candidate = 0; for (int num : nums) { if (count == 0) { candidate = num; count = 1; } else if (num == candidate) { count++; } else { count--; } } return candidate; } int validateMajorityElement(std::vector& nums, int candidate) { int count = 0; for (int num : nums) { if (num == candidate) { count++; } } if (count > nums.size() / 2) { return candidate; } else { return -1; // No majority element found } } int findMajorityElement(std::vector& nums) { int candidate = majorityElement(nums); return validateMajorityElement(nums, candidate); } int main() { std::vector nums = {3, 3, 4, 2, 4, 4, 2, 4, 4}; int result = findMajorityElement(nums); if (result != -1) { std::cout << "Majority Element: " << result << std::endl; } else { std::cout << "No majority element found." << std::endl; } return 0; }
def majority_element(nums): count = 0 candidate = None for num in nums: if count == 0: candidate = num count = 1 elif num == candidate: count += 1 else: count -= 1 return candidate def validate_majority_element(nums, candidate): count = 0 for num in nums: if num == candidate: count += 1 if count > len(nums) // 2: return candidate else: return None def find_majority_element(nums): candidate = majority_element(nums) return validate_majority_element(nums, candidate) # Example usage nums = [3, 3, 4, 2, 4, 4, 2, 4, 9] result = find_majority_element(nums) if result is not None: print("Majority Element:", result) else: print("No majority element found.")
import java.util.HashMap; import java.util.Map; public class Main { public static int majorityElement(int[] nums) { int count = 0; int candidate = 0; for (int num : nums) { if (count == 0) { candidate = num; count = 1; } else if (num == candidate) { count++; } else { count--; } } return candidate; } public static int validateMajorityElement(int[] nums, int candidate) { int count = 0; for (int num : nums) { if (num == candidate) { count++; } } if (count > nums.length / 2) { return candidate; } else { return -1; // No majority element found } } public static int findMajorityElement(int[] nums) { int candidate = majorityElement(nums); return validateMajorityElement(nums, candidate); } public static void main(String[] args) { int[] nums = {3, 3, 4, 2, 4, 4, 2, 4, 4}; int result = findMajorityElement(nums); if (result != -1) { System.out.println("Majority Element: " + result); } else { System.out.println("No majority element found."); } } }
Question 6: Buying Stocks
You are given a list of daily prices of a stock. You can buy a stock on one day and sell it later on another day after the day you bought the stock. You can perform the above operation only once. What is the maximum loss possible?
Example
Prices=[10,4,2,9]
The greatest loss is incurred when you buy at a price of 10 and sell at a price of 2. Return the difference:9.
Example
Price=[1,2,3,4]
The Price went up every day. Return 0.
Sample Input for Custom Testing
STDIN Function
———– ————–
- 7 → Prices [] size n=7
- 1 → prices =[1,8,4,2,10,3,2]
- 8
- 4
- 2
- 10
- 3
- 2
Sample Output
- 8
Explanation
Using zero-based index notation, the correct answer is a[4]-a[6]=10-2=8. There is a greater difference between 10 and 1 but that would imply selling before buying, and short selling is not allowed in this problem.
#include<bits/stdc++.h> #define ll long long using namespace std; int solve(vector v) { int n = v.size(); if (n == 0) return 0; int mx = v[0]; for (int i = 1; i < n; i++) mx = max(mx, v[i]); if (mx <= 0) return 0; int mxSum = 0; int cSum = 0; for (int i = 0; i < n; i++) { cSum += v[i]; if (cSum < 0) cSum = 0; mxSum = max(mxSum, cSum); } return mxSum; } int main() { int n; cin >> n; int price[n]; for (int i = 0; i < n; i++) { cin >> price[i]; } vector diff; for (int i = n-2; i >=0 ; i--) diff.push_back(price[i] - price[i+1]); int ans = solve(diff); if(ans<0) { cout<<0<< endl; } else { cout<< ans<< endl; } return 0; }
n=int(input()) arr=[] ans=[0] for i in range(n): arr.append(int(input())) for i in range(n-1): x=min(arr[i+1:])-arr[i] if x<0: ans.append(x) print(-1*min(ans))
import java.util.*; class Solution { public static int solve(ArrayList < Integer > list) { int n = list.size(); if (n == 0) return 0; int max = list.get(0); for (int i = 1; i < n; i++) max = Math.max(max, list.get(i)); if (max <= 0) return 0; int maxSum = 0; int sum = 0; for (int i = 0; i < n; i++) { sum = sum + list.get(i); if (sum < 0) sum = 0; maxSum = Math.max(maxSum, sum); } return maxSum; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); ArrayList < Integer > list = new ArrayList(); for (int i = n - 2; i >= 0; i--) list.add(arr[i] - arr[i + 1]); int res = solve(list); if (res < 0) System.out.println(0); else System.out.println(res); } }
Question 7: Loki’s Mind Stone
Problem Statement :
Loki, the God of mischief can brainwash any living person by touching him/her with his Mind stone, and has decided to break the avengers (a warrior group) to face into each other, so that they can turn against each other and make Loki’s evil plans easier. Now all the avengers have some amount of strength that is denoted in integers. Loki wants to brainwash the least amount of people possible, because he is lazy. But he wants his team of avengers to win the battle. What is the number of avengers Loki will get brainwashed.
Input Format:
First line contains an integer n, denoting the number of total avengers
the next line contains n space separated integers denoting the power of each avenger.
Output Format:
One line denoting the total number of avengers brainwashed by Loki’s Mind stone.
Constraints:
2<=n<=10^6
Test case:
Sample Input:
6
9 3 1 2 4 2
Sample Output:
2
Output Specifications:
Loki can brainwash the avengers with power 9 and 3, or with 9 and 2, or with 9,4, and the rest will be losing cause cumulative power of rest avengers is less than the brainwashed total power by Loki.
#include <bits/stdc++.h> using namespace std; map < int, int > m; int main() { int n, sum = 0, sum2 = 0, ans = 0; cin >> n; vector < int > v(n); for (int i = 0; i < n; i++) { cin >> v[i]; sum += v[i]; } sort(v.begin(), v.end(), greater < int > ()); sum /= 2; while (sum2 <= sum && ans < n) { sum2 += v[ans]; ans++; } cout << ans; }
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int arr[] = new int[n]; int sum = 0; for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextInt(); sum = sum + arr[i]; } Arrays.sort(arr); int sum1 = 0, count = 0; for (int i = arr.length - 1; i >= 0; i--) { sum1 = sum1 + arr[i]; sum = sum - arr[i]; count++; if (sum1 > sum) break; } System.out.println(count); } }
n = int(input()) arr = list(map(int, input().split())) s = sum(arr) arr.sort(reverse=True) dup, sum1, ans = 0, 0, 0 for i in arr: dup += i sum1 = s - dup ans += 1 if sum1 < dup: break print(ans)
Question 8: Hostel warden
Problem Statement :
There is a hostel warden in some XYZ hostel. He is very strict with students. He has some set of rules to allow students to let them out.
Students are in random order but warden don’t allow them in that way.
Rule 1- whose initial is a prime value should go out before whose initial is a composite value.
Rule 2- if two students have prime value , a student with less value goes out first
Rule 3- if two students have composite value , a student with greater value goes out first
NOTE:- consider the ASCII value of the initial to find it is prime or composite.
Input format:
The first line of input contains the number of students N
The second line of input contains random order of the students S.
(students at index zero goes out first and students at index N-1 goes last)
Constraints
1<=number of students <=105
33<=ASCII Of Characters<=126
Output Format
The single line of output should contain the required modified order of students to go out.
Sample Input
13
Kkunjkhahorin
Sample Output
akkuronnjihhK
Explanation
For primes: a<k
For Composite: K<h<i<j<n<o<r<u
Adding up logic, the required answer is akkuronnjihhK
#include <bits/stdc++.h> using namespace std; int main() { int n;cin>>n; string s;cin>>s; int prime[128]; prime[0]=prime[1]=0; int p=2; for(int i=2;i<128;i++) prime[i]=1; while(p*p<=127) { if(prime[p]) { for(int i=p*p;i<=127;i+=p) prime[i]=0; } p++; } string s1,s2; for(auto i:s) { if(prime[i]) s1+=i; else s2+=i; } sort(s1.begin(),s1.end()); sort(s2.begin(),s2.end()); reverse(s2.begin(),s2.end()); cout<< s1<< s2; }
import java.util.Collections; import java.util.Scanner; import java.util.Vector; public class Applicartion22 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); boolean primes[] = new boolean[127]; for (int i = 0; i < 127; i++) { primes[i] = true; } primes[0] = false; primes[1] = false; int p = 2; while (p * p <= 126) { if (primes[p]) { for (int i = p * p; i < 127; i += p) { primes[i] = false; } } p += 1; } Vector < Character > s1 = new Vector < > (); Vector < Character > s2 = new Vector < > (); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (primes[(int) ch]) { s1.add(ch); } else { s2.add(ch); } } Collections.sort(s1); Collections.sort(s2); for (int i = 0; i < s1.size(); i++) { System.out.print(s1.get(i)); } for (int i = s2.size() - 1; i >= 0; i--) { System.out.print(s2.get(i)); } } }
n = int(input()) s = input() prime = [True] * 127 prime[0] = prime[1] = False p = 2 while p * p <= 126: if prime[p]: for i in range(p * p, 127, p): prime[i] = False p += 1 s1 = [] s2 = [] for i in s: if prime[ord(i)]: s1.append(i) else: s2.append(i) s1.sort() s2.sort(reverse=True) s1.extend(s2) print("".join(s1))
Question 9: HR issues
Problem statement -:
Shovon is an HR in a renowned company and he is assigning people to work. Now he is assigning people work in a fashion where if he assigns somework a work of cost 2, the next person will be strictly getting a job with cost equal or more than 2. Given that Shovon’s company has infinite work and a number of employees, how many distributions can be possible. The cost of jobs can go 0 to 9.
Function Description:
Complete the special_numbers function in the editor below. It has the following parameter(s):
Parameters:
Name | Type | Description |
N | Integer | The number of depts. |
arr[ ] | Integer array | The number of employees in each dept.. |
Return: The function must return an INTEGER denoting the sum of answers for all distinct distributions.
Constraints:
- 1 <= n <= 100
- 1 <= arr[i] <= 200
Sample Cases:
- Sample Input 1
2
4
1 - Sample Output 1
725 - Description
The ans if m = 1 is 10, which is all numbers from 0 to 9
The ans for m = 2 is 55
The answer for m = 3 is 220
The answer for m = 4 is 715
So fun(4) + fun(1) = 725
#include<bits/stdc++.h> using namespace std; int func(int s,int p,int n) { if(p==n-1) return 1; int ans=0; for(int i=s;i<=9;i++) ans+=func(i,p+1,n); return ans; } int main() { int n,a, ans=0; cin>>n; vector v(n); for(int i=0;i<n;i++) { cin>>a; for(int i=0;i<=9;i++) ans+=func(i,0,a); } cout<<ans; }
n=int(input()) a1=[] for i in range(n): a1.append(int(input())) dp=[0]*201 dp[1]=10 dp[2]=55 a=[1]*10 i=3 while i<201: s=0 for i1 in range(10): s+=a[i1] a[i1]=s dp[i]+=(s*(10-i1)) dp[i]=dp[i]%((10**9)+7) i+=1 s1=0 for i in a1: s1+=dp[i] s1=s1%((10**9)+7) print(s1)
import java.util.*; class Main { static int func(int s,int p,int n) { if(p==n-1) return 1; int ans=0; for(int i=s;i<=9;i++) ans += func(i,p+1,n); return ans; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int ans=0; for(int i=0; i<n; i++){ int a = sc.nextInt (); for(int j=0; j <=9; j++) ans+=func(j,0,a); } System.out.println (ans); } }
Question 10 :A Good Prime Number
Problem Statement :
A prime number is a number which is divisible by one and itself. Also a number is called a good prime number if the sum of its digits is a prime number. For example a number 23 is a good prime number because the sum of 2 and 3 ( 2+3=5) is 5 which is a prime number. You are given an integer K. Your task is to find the kth good prime number that is greater than a provided number N.
For example , 232 is a good prime number since the sum of all digits is 7 which is a prime number whereas 235 is not a good prime number.
Input format :
- The first line contains an integer N.
- The next line contains an integer K.
Output format :
A single integer which is a Kth good prime number that is greater than a provided number N.
Constraints :
- 1<=N<=10^5
- 1<=K<<=10^5
Sample Input 1:
4 4
Sample Output 1:
12
Explanation :
Good prime numbers starting from 4 are 5,7,11(1+1=2 which is prime number),12(1+2=3 which is prime number),14(1+4=5 which is a prime number) and so on. Because the sum of digits of an individual number is a prime number And 4 th good prime number is 12 in this series.Hence the output is 12.
Sample Input 2:
17 5
Sample Output 2:
29
Explanation :
Good prime numbers starting from 17 are 20,21,23,25,29…and the 5th prime number is 29.Hence the output is 29.
#include<bits/stdc++.h> using namespace std; bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i += 6) if ((n % i == 0) || (n % (i + 2) == 0)) return false; return true; } int solve(int n, int k) { int c = 0; vector list; // Find k numbers greater than n with the property that the sum of their digits is prime for (int i = n + 1; c < k; i++) { int temp = i; int sum = 0; while (temp != 0) { sum += temp % 10; temp /= 10; } if (isPrime(sum)) { list.push_back(i); c++; } } return list[k - 1]; } int main() { int n; cin >> n; int k; cin >> k; cout << solve(n, k); return 0; }
import java.util.*; class Main { public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if ((n % i == 0) || (n % (i + 2) == 0)) return false; return true; } public static int solve(int n, int k) { int c = 0; ArrayList < Integer > list = new ArrayList < > (); for (int i = n + 1; c < k; i++) { int temp = i; int sum = 0; while (temp != 0) { sum = sum + temp % 10; temp = temp / 10; } if (isPrime(sum)) { list.add(i); c++; } } return list.get(k - 1); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); System.out.println(solve(n, k)); } }
import math def isPrime(n): if n <= 1: return False if n <= 3: return True if n % 2 == 0 or n % 3 == 0: return False for i in range(5, int(math.sqrt(n)) + 1, 6): if n % i == 0 or n % (i + 2) == 0: return False return True def solve(n, k): c = 0 list = [] for i in range(n + 1, 1000000000): temp = i sum = 0 while temp != 0: sum = sum + temp % 10 temp = temp // 10 if isPrime(sum): list.append(i) c += 1 if c == k: break return list[k - 1] n, k = map(int, input().split()) print(solve(n, k))
FAQs on BNY Mellon Coding Questions
Question 1: How many rounds are there in Mitsogo?
Mitsogo usually conducts 1 Technical Interview followed by HR interview to hire freshers.
Question 2: How to prepare for Mitsogo interview?
For Mitsogo Technical Interview the applicants must have good knowledge of Computer Science Fundamentals along with Data Structures and Algorithms based Coding.
Question 3: What is the salary of Mitsogo?
For the entry level role of Software Development Engineer in Mitsogo, ₹ 4.5 – 6 LPA is offered to the freshers.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment