Range Assign, Number of Distinct Elements Minimize Codechef solutions Today | Codechef Starters 64✅ (100/100) FULL | AC Code ✅| RANGEASSIGN
IF ANYONE DONE THIS AND IS YOU WANT TO HELP SO JUST WRITE DOWN CODE IN COMMENTS
For Solution
“Coming Soon After 10 Minutes“
Problem
You are given an array of positive integers.
In one operation, you can do the following:
- Choose integers and , such that ;
- For all , change the value of to .
Find out whether can have at most distinct values after using any (possibly zero) number of operations.
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- Each test case consists of two lines of input:
- The first line of each test case contains - the size of the array.
- The next line contains integers, - the elements of the array.
Output Format
For each test case, print YES
if can have at most distinct values after using any (possibly zero) number of operations and NO
otherwise.
You may print each character of the string in uppercase or lowercase (for example, the strings YES
, yEs
, yes
, and yeS
will all be treated as identical).
Constraints
- The sum of over all test cases won't exceed .
Sample 1:
4 5 5 9 5 5 5 3 1 2 3 4 1 2 1 3 4 1 2 3 1
YES NO YES YES
Explanation:
Test case : The array already has distinct elements.
Test case : It is impossible to make operations such that has distinct elements.
Test case : We can make an operation as:
- Choose and . Thus, we change to .
The final array is which has two distinct elements.
Test case : We can make an operation as:
- Choose and . Thus, we change and to .
The final array is which has one distinct element.
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of array of
// numbers
int findGCD(int arr[], int n)
{
int result = arr[0];
for (int i = 1; i < n; i++)
{
result = gcd(arr[i], result);
if(result == 1)
{
return 1;
}
}
return result;
}
int main() {
// your code goes here
int tc;
cin>>tc;
while(tc--){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int g=findGCD(arr, n);
int cnt=0;
for(int i=0;i<n;i++)
{
if(arr[i]==g)
cnt++;
}
cout<<n-cnt<<endl;
}
return 0;
}