But in that case how will you justify for arr = [1, 9, 17]
2 MCQs + 3 Coding questions , 2hours time MCQ Q1 : Given graph , find number of edges that are bridge. Q2 : Clean code principles
We are given an array containing integers. We have to convert this array such that the resulting array is a geometric progression. What we need to return is the multiplying ratio of this geometric progression. We have to make sure we make a resulting array such that ratio is maximum.
Example
Given array = [64 , 100 , 125]
Resulting array = [64 , 80 , 100 , 125]
Answer : ratio in simplest form = 5/4
contraints : Array size <= 1e5
Equation : (a*m)%n = r Given a , n , r : Find smallest m such that the above equation satisfies.
constraints : a,n,r <= 1e18
Given a list of strings. and a 2d matrix containing different letters.
We have to choose letters from the matrix to create the list of string with the condition that if you choose some letter from ith row , that row gets erased. (Basically , you can choose only one letter per row)
return if its possible or not to create all words.
Example :
words = ["sony"]
matrix:
s s s s
s n n n
n s o s
y o s y
answer = true , cells = [(0,0) , (2,2) , (1,1) , (3,1)]
constraints : list size <= 1e5 , matrix columns = 6 , matrix rows <= 15
Question 1 : Approach ( All tests passed )
-> For each element in the array , prime factorize it (store it like a map)
-> The max ratio that needs to be printed would be surely gcd of the powers of individual primes for each array integer. (think !)
-> Some primes will increase from left to right , while some will decrease from left to right.
-> this will be useful for understanding which primes would be part of numerator / denominator.
-> Eg=> [64 , 100 , 125] -> [ {2:6 , 5:0} , {2:2 , 5:2} , {2:0 , 5:3} ]
-> increasing prime = 5 (will come in numerator)
-> decreasing prime = 2 (will come in denominator)
-> our ratio will be (5^a)/(2^b)
-> a = gcd(0,2,3) = 1
-> b = gcd(6,2,0) = 2
-> answer = (5^1) / (2^2) == 5/4
-> If there is some prime which is not monotonic, then return -1. (However , they gave only valid arrays as tests)
complexity = O(Nlogn) (for sieve) , however O(Nsqrt(N)) was also accepted.
We can reduce this problem to the classical Maximum Bipartite Matching problem.
Question 2
Prerequisite: Linear Congruence Equation
> Step By Step Solution :
> There can be Multiple cases for the solution
Question 3
Understanding Problem
Approach (For smaller constraints where N amd M are less than 5)
Question 1
Breakdown
Approach
The 9th testcase for Problem 1 are probably wrong, seeing that problem author himself has incorrect solution. Here is an alternate solution to the problem:
It can be proven that this solution runs in amortized O(n).
Code:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ll n;
vector<ll> a;
bool check_ratio(ll num, ll deno, bool debug = false) {
// Check if the GP generated by the a[0] as starting value and
// ratio (num / deno) is a valid GP for our question
unordered_set<ll> s;
ll cur = a[0];
while(cur <= a[n-1]) {
s.insert(cur);
if(debug) cout << cur << "\n";
if((cur * num) % deno != 0 && cur < a[n-1]) return false;
cur = (cur * num) / deno;
}
for(auto x : a) {
if(!s.count(x)) return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
a = vector<ll>(n);
for(auto &x : a) cin >> x;
if(n == 0 || n == 1) { // degenerate cases
for(auto x : a) cout << x << " ";
return 0;
}
if(a[0] == 0 && a[n-1] != 0) { // degenerate cases
cout << "-1"; return 0;
}
if(n == 2 || a[0] == a[n-1]) { // degenerate cases
for(auto x : a) cout << x << " ";
return 0;
}
// Find the minimum difference and run the check ratio function on it
ll min_diff_i = 1;
for(ll i = 1; i < n; i++) {
if(a[i] - a[i-1] < a[min_diff_i] - a[min_diff_i - 1]) {
min_diff_i = i;
}
}
// now ratio has to be less than or equal to a[min_diff_i]/a[min_diff_i-1]
ll ratio_num = a[min_diff_i], ratio_denom = a[min_diff_i - 1];
while(ratio_num != ratio_denom && !check_ratio(ratio_num, ratio_denom)) ratio_num--;
if(ratio_num == ratio_denom) { // nothing worked, -1 case
cout << "-1";
return 0;
}
// we found some valid ratio, lets print the GP
vector<ll> ans;
ll cur = a[0];
while(cur <= a[n-1]) {
ans.push_back(cur);
cur = (cur * ratio_num) / ratio_denom;
}
for(auto x : ans) cout << x << " ";
return 0;
}
Gigaliths In question 1, were there any other constraints? Like, Is it necessary for array elements to be integer, etc?
As of now we are only have this details only . If we get more information we will update it ..yes array elements are integers only as it is written on first line : -) Keep grinding 🙂
What if we are not able to convert it to a geometric progression @Akshay Sharma-2766 , should we return -1 or something else ? like in the case of [1, 9, 17]
Given input array is integer. But resulting array need not be an integer array.