Big Chandan is a dire lover of Biryani, especially Old Monk's Biryani. Today, he went over to have some of it. To his surprise, the waiter turns out to be a coding geek and refuses to serve him unless Chandu solves his two- arrays problem, stated as:
Given two non-increasing array of integers A,B i.e A[i] >= A[i+1] and B[i] >= B[i+1] and for all i, 0 ≤ i < n-1.
The monkiness of two numbers is given by: M (A[i],B[j]) = j - i , if j >=i and B[j] >= A[i], or 0 otherwise.
Find the monkiness of the two arrays, that is given by: M (A,B)= max (M(A[i],B[j])) for 0≤ i, j< n-1.
Example:
Input: n = 9, arr[n] = {7, 7, 3, 3, 3, 2, 2, 2, 1}, brr[n] = {8, 8, 7, 7, 5, 5, 4, 3, 2}
Output: 5
Approach
C++
#include <bits/stdc++.h>using namespace std;long long theOldMonk(int n, long long arr[],long long brr[]){long long ans = 0, i = 0, j = 0;while (i < n && j < n){if (brr[j] >= arr[i]){ans = max(ans, j - i);j++;}elsei++;}return ans;}int main(){int n = 9;long long arr[n] = {7, 7, 3, 3, 3, 2, 2, 2, 1};long long brr[n] = {8, 8, 7, 7, 5, 5, 4, 3, 2};cout << theOldMonk(n, arr, brr) << "\n";return 0;}
Read Interview Questions
Exception Handling Interview Questions
DBMS Interview Questions Set -1
DBMS Interview Questions Set -2
JPA Interview Questions Set -1
Spring Boot Interview Questions Set 1
Spring Boot Interview Questions Set 2
No comments:
Post a Comment