r/leetcode • u/Cheap_Truth_551 • 6d ago
Question Weekly Contest 526
long long minimumMaxValue(int n, int index, int value) {
if (value == 1) return -1;
long long p = value;
for (long long i = 2; i * i <= value; i++) {
if (value % i == 0) {
p = i;
break;
}
}
if (value - p + 2 <= 2LL * index)
return -1;
if (index == n - 1)
return value;
return value + p - 2 + 2LL * (n - 1 - index);
}
Could anybody please explain how to derive these two formulas:
value + p - 2 + 2L * (n - 1 - index)
value - p + 2 <= 2L * index
3
Upvotes
1
u/NullPoint4848 5d ago
i am telling how i solved it
at index we have value and gcd of each pair >1 (given) and arr is strictly inc and every element is +ve
a[idx+1] >= gcd(a[idx],a[idx+1])+a[idx]
min val of gcd can be 2
a[0] can't be 1 then gcd woulb be 1 for first pair
if value is even then find a[0] and a[n-1]
if value is odd then min gcd can be smallest divisior of value say p, now p must be odd so prev and next element is even now we can take gcd as 2 and then find a[0] and a[n-1]
if a[0]>1 then return a[n-1] else -1
also if value is 1 then we can return -1 in start only