You are given an integer array nums. The factor score of an array is defined as the…
- User
You are given an integer array nums.
The factor score of an array is defined as the product of the LCM and GCD of all elements of that array.
Return the maximum factor score of nums after removing at most one element from it.
Note that both the LCM and GCD of a single number are the number itself, and the factor score of an empty array is 0.
The term lcm(a, b) denotes the least common multiple of a and b.
The term gcd(a, b) denotes the greatest common divisor of a and b.
Note: Please do not copy the description during the contest to maintain the integrity of your submissions. /******************************************************************************
Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/ #include #include<bits/stdc++.h> using namespace std; long long maxScore(vector& nums) { if(nums.size()==0) return 0; if(nums.size()==1) return nums[0]*nums[0]; sort(nums.begin(),nums.end()); long long gcdd=0; long long lcmm=0; if(nums.size()==2) return nums[1]*nums[1]; long long test=gcd(nums[0],nums[1]); long long test2=gcd(nums[1],nums[2]); if(test>=test2) { gcdd=nums[0]; lcmm=nums[0]; for(int i=1;i<nums.size();i++) { gcdd=gcd(gcdd,nums[i]); lcmm=lcm(lcmm,nums[i]); } } else{ gcdd=nums[1]; lcmm=nums[1]; for(int i=2;i<nums.size();i++) { gcdd=gcd(gcdd,nums[i]); lcmm=lcm(lcmm,nums[i]); } }
return lcmm*gcdd; }int main(void) { vector v={11,21,14}; cout<<maxScore(v); }
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.