what is the need to sort the array?
Thanks for the logic
but for 7 its coming 7 as i have written (please if you can see that above and tell if its wrong then why?) and yours is 8.
In my approach, I used binary search. You need to sort the array to use binary search. But DIDNāT sort anything because the array is already sorted in non-decreasing order.
abey dekh lo yaar
yes mine is coming 7
1 2 3 4 5 6 7
LCM(4,5)=20;
LCM(6,7)=42;
1 2 3 20 20 42 4 2
LCM(20,42);
LCM(20,42)=420;
1 2 3 420 420 420 420
4 + 3 = 7 operations.
can anybody tell where i am wrong
please if anyone can tell
if there are l primes if we only have to deal with these primes we have to do minimum l-1 no. of operations and we can make 1 of them the required LCM. then for n-1 remaining no.s we have to do at least n-2 operations so total l-1+n-2 operations. Why it is optimal? First l-1 operation canāt be omitted as they are all primes and other n-2 operation are also required because every n-1 remaining no.s has some prime no. missing because the product of primes up to n is always greater than n so there is no no. with all the primes as its divisor.
Helping Hands:
We need the highest power of all primes b/w [1,n] in the final lcm. It can be proved that a number [1,n] cannot have the highest power of two or more primes.
Letās prove it by contradiction. Suppose X contains highest power of two primes, X = (a^m)*(b^n); (m>=1 and n>=1). Letās take numbers Y = a^(m+1) and Z = b^(n+1). For our claim to be false, there should not exist a number less than X with power of a or b higher than m and n respectively. If our claim is false then X < Y and X < Z => (b^n) < a and (a^m) < b => a^(mn) < a. This is not possible for m >=1 and n>=1. Hence, our claim is correct.
Now, steps to join all numbers with the highest power of primes => p-1. After these p-1 steps, 2 numbers are equal to the final LCM. We need to perform additional n-2 steps to make the rest of the n-2 steps equal to the lcm.
Hence, the answer is n+p-3. p = | {x | x is primes, 1<=x<=n} |.
For n = 1, answer is 0.
For n = 2, answer is 1.
Problem CodeChef: Practical coding for everyone
whatās wrong with this solution?
void solve() {
/// write your code here
int n;
cin >> n;
vector<int64_t> arr(n);
for(int i = 0; i < n; i++) cin >> arr[i];
sort(arr.begin(), arr.end());
uint64_t p1 = 0, p2 = 0;
p1 += arr[0];
p2 += arr[1] + arr[2];
for(int i = 3; i < n; i++) {
if (i % 2) {
p1 += arr[i];
} else {
p2 += arr[i];
}
}
if (p1 == p2) cout << "draw\n";
else if (p1 > p2) cout << "first\n";
else cout << "second\n";
}
jst sort in descending order
First lcm(20,42)!=420
1 2 3 4 5 6 7
ā¦
1 2 3 20 20 42 42
1 2 3 20 420 420 42
1 2 3 420 420 420 42
1 2 3 420 420 420 420
ā¦
420 420 420 420 420 420 420 (total=8)
oops what the hell i did
Not much. Just tried it for 2 hours XD
it should be 7 mine is correct
I didnāt understand. Can you show for some n as a example