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 A of N positive integers.

In one operation, you can do the following:

  • Choose integers i and j (1 \le i < j \le N), such that A_i = A_j;
  • For all k (i < k < j), change the value of A_k to A_i.

Find out whether A can have at most 2 distinct values after using any (possibly zero) number of operations.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of two lines of input:
    • The first line of each test case contains N - the size of the array.
    • The next line contains N integers, A_1, A_2, A_3, \ldots, A_N - the elements of the array.

Output Format

For each test case, print YES if A can have at most 2 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 YESyEsyes, and yeS will all be treated as identical).

Constraints

  • 1 \leq T \leq 1000
  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • The sum of N over all test cases won't exceed 10^5.

Sample 1:

Input
Output
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 1: The array A already has 2 distinct elements.

Test case 2: It is impossible to make operations such that A has 2 distinct elements.

Test case 3: We can make an operation as:

  • Choose i = 1 and j = 3. Thus, we change A_2 to A_1 = 1.

The final array is [1,1,1,3] which has two distinct elements.

Test case 4: We can make an operation as:

  • Choose i = 1 and j = 4. Thus, we change A_2 and A_3 to A_1 = 1.

The final array is [1,1,1,1] 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;

}

Next Post Previous Post
No Comment
Add Comment
comment url