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
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.
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.
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?
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.
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;
}`
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!