ALEXTASK - Editorial

Can anyone please tell what I have done wrong in this code in Java.

Thank You

can someone plz point out whats wrong in my


[1]?


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

can anybody tell me why this code gives WA for the last testcase ?

thank you…

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

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: danimohamadnejad@gmail.com to know if i am wrong!

@srd091 don’t think it will be a problem as t declared in inner while will hide outer t in the inner while block.

1 Like

check in answers , have tried to explained this … if anything isn’t clear feel free to ask

@error_503_404 I think there is a slight issue with the test case you’re failing. More specifically, there is extra whitespace in the input file. Using strip() on each line solves the problem, see this. Or it is always safer to use the split() function without any parameters, as here. According to the Python documentation
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

Hope this solves your problem.

Thanks for helping, smsubham. It was a trivial mistake.

1 Like