ALEXTASK - Editorial

Can somebody help me solve why this solution is wrong,


[1]
This code works for most cases but don't know for which it is failing. This code works in O(N).

Below are the test cases that i tested, ( which passed successfully )

    10
    10 2 3 10 4 5 4 3 3 4
    
    6
    3 4 5 4 4 3
    
    3
    2 3 5
    
    4
    1 8 7 11
    
    4
    4 4 5 6
    
    10
    1 3 4 2 6 7 10 44 1 2
    
    10
    10 2 3 10 4 5 4 3 3 4
    
    10
    1 3 4 2 6 7 10 44 1 2
    
    7
    58 59 100 10266 6844 10000 1000000
    
    4
    1000000000 1000000000 999999999 999999222
    
    5
    1000000000 2000000000 989898989 878787878
    
    2
    1 1
    
    2
    4 1


  [1]: https://www.codechef.com/viewsolution/12399435

hey @radek_rak , check the constraints. 1 ≤ Ai ≤ 10^9 . You would have to use “long long int” .
TC where your code is failing :
test_cases = 1
N = 2
1000000000 999999999

Also, your code has some logical issues . wrong answer on this TC too,
N = 5
array = 2 5 7 9 9

. Your ans=10 , correct ans=9.

hey @vikasj554, thanks for the help, I modified the code and it is working for all the above cases along with your cases but it is still failing. :frowning:

In short, I am maintaining two variables, min1, min2 and then calculating LCM of all the other inputs and swapping min1, min2 based on the newly read value along with the LCM.

can anyone explain task… actually i am not getting it

what are u not getting

@soni_singh

rather than checking every possibility we should lake the LCM of two smallest numbers in the array the should check for only those pair which in which element are then the LCM of smallest
for EXAMPLE

we have an array say

3,3498,45,435,656,67,4,10,5

we sort it

3,4,5,10,45,67,435,656,3498

then we took LCM OF 3 & 4 which is 12 and check for only those element which are less than 12

Hi all,
I have tried the same logic first sorted the list ,
If there is ‘1’ in the array then the next element is the result.
Otherwise fining any similar elements , as the list is sorted the first such element is taken.
Then the lcm is taken between first element and rest of the array and min of that is returned.
Then depending on which is the lesser one, the result is calculate.
The code is here . what is it I am doing wrong?

My python code passes all tasks except the last one for large numbers. Can somebody tell me what I can do?

from fractions import gcd
def lcm(a,b):
return (a*b)/gcd(a,b)
t=int(input() )
while t:
t=t-1
n=int(input())
l=map(int,raw_input().split(" "))
min1 =[]
for i in range(len(l)):
for j in range(i+1,len(l)):
val = lcm(l[i],l[j])
min1.append(val)
min1.sort()
print min1[0]

I am getting NZEC for the last task, what is the problem here ? Thank you.

We have to find the minimum LCM right?
Minimum LCM can be found by finding the lcm of 2 smallest numbers

Can someone please tell me why my code is not working correctly ?
https://www.codechef.com/viewsolution/15494387

@kishynivas try using


n=int(input().strip(""))

sometimes the test cases has extra white space attached to it at the beginning and at the end, so this helps in removing it.

Hi All,
I tried the same logic, could someone please point out the error in my code.
https://www.codechef.com/viewsolution/15824052

Sainath

Visual c++ says vector subscript out of range but the range is well within the given ‘n’ . Please help me identify the flaw.
#include
#include
#include

using namespace std;

long long int hcf(long long int a,long long int b) {
if (b == 0) { return a; }
else {
long long int c = a%b;
return hcf(b, c);
}
}

int main() {
int t;
cin >> t;
while (t–) {
int n;
long long int ans;
cin >> n;
vector a;
for(int i=0;i<n;i++) {
long long int temp;
cin >> temp;
a.push_back(temp);
}
ans = (a[0] * a[1] / hcf(a[0], a[1]));
for (int i = 0; i < n; i++) {
for (int j = i+1; j <n , i!=j; j++) {
min(ans, (a[i] * a[j] / hcf(a[i], a[j])));
}
}
cout << ans << endl;
}
return 0;
}`

Bonus problem

Can this problem be solved for larger constraints such as N <= 10^5 and Ai <= 10^9 ?

If yes, please discuss your approach…

#include<bits/stdc++.h>

using namespace std;

const int N = 503;
int n;
int t;
long long a[N];
long long ans;

long long gcd(long long a, long long b){
if (b == 0) return a;
else return gcd(b,a % b);
}

long long lca(long long a,long long b){
return (a * b) / gcd(a,b);
}

int main(){
//freopen(“2.in.txt”,“r”,stdin);
//freopen(“2.out.txt”,“w”,stdout);

cin >> t;

while(t --> 0){

    cin >> n;

    ans = 1e18;
    for(int i = 0; i < n; ++i)
        cin >> a[i];

    for(int i = 0; i < n; ++i){
        for(int j = i + 1; j < n; ++j){
            ans = min(ans,lca(a[i], a[j]));
        }
    }
    cout << ans << "\n";
}

return 0;

}

///the code above is setter’s solution!!! isnt there a bug in you opinion??!! look at two for loops in main
//method the j index is exceeding limit and this is a runtime error!!! i wonder who design this problem?? an //idiot??? please email me at: [email protected] to know if i am wrong!