Range Assign, Number of Distinct Elements Minimize CodeChef Solution

Problem -Range Assign, Number of Distinct Elements Minimize CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Range Assign, Number of Distinct Elements Minimize CodeChef Solution in C++17

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;cin>>t;
	while(t--){
	    int n;cin>>n;
	    vector<int>v(n);
	   
	    for(int i=0;i<n;i++){
	        cin>>v[i];
	        
	    } /*
	    set<int>s;int e=0;
	   s.insert(v[0]);
	    bool ff=false,f=false;
	    for(auto i:m){
	        if(i.second>=2){ff=true;break;}
	    }
	    for(int i=1;i<n;i++){
	        if(s.count(v[i])==1){
	            auto it = s.end();
                it--;
                if(*it!=v[i]){f=true;break;}
	        }
	        else
	        s.insert(v[i]);
	    }
	    
	   /* for(auto i:m){
	        if(i.second>=2){f=true;break;}
	    }
	    
	    auto it = s.end();
        it--;
	    */
	   int a=v[0];int b=v[n-1];bool f=false;
	   if(a==v[n-2]||a==b||v[1]==b){
	       f=true;goto m;
	   }
	   
	   for(int i=0;i<n-1;i++){
	       if(a==v[i]){
	           if(v[i+1]==b){
	               f=true;goto m;
	           }
	       }
	   }
	   m:
	   if(f)cout<<"YES";
	   else cout<<"NO";
	   cout<<'\n';
	   
	   
	}
	return 0;
}

Range Assign, Number of Distinct Elements Minimize CodeChef Solution in C++14

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    long long n;
	    cin>>n;
	    vector<long long> v(n);
	    for(long long i =0;i<n;i++){
	        cin>>v[i];
	    }
	    if(n==1 || n==2){
	        cout<<"YES"<<endl;
	    }
	    else if((v[0]==v[n-1]) || (v[0]==v[n-2]) || (v[1]==v[n-1])){
	        cout<<"YES"<<endl;
	    }
	    else{
	        
	        bool flag = false;
	        long long first = v[0];
	        for(long long i=1;i<=n-3;i++){
	            if(v[i]==first){
	                if(v[i+1]==v[n-1]){
	                    flag =true;
	                    break;
	                }
	            }
	        }
	       if(flag){
	           cout<<"YES"<<endl;
	       }
	       else{
	           cout<<"NO"<<endl;
	       }
	       
	    }
	}
	return 0;
}

Range Assign, Number of Distinct Elements Minimize CodeChef Solution in PYTH 3

# cook your dish here
for _ in range(int(input())):
    N=int(input())
    A=list(map(int,input().split(' ')))
    start=A[0]
    end=A[-1]
    ex=0
    if start==end:
        print("YES")
        ex=-1
    i=0
    while i<N-1 and ex==0:
        if A[i]==start and A[i+1]==end:
            print("YES")
            ex=1 
        i+=1 
    if ex==0:
        print('NO')

Range Assign, Number of Distinct Elements Minimize CodeChef Solution in C

#include <stdio.h>

int main(void) {
	// your code goes here
	int t;
	scanf ("%d",&t);
	while(t--){
	    int n,i,tag1,tag2;
	    scanf ("%d",&n);
	    int a[n];
	    for(i=0;i<n;++i){
	        scanf ("%d",&a[i]);
	    }
	    if(n<=2){
	        printf ("YES\n");
	    }else if(a[0]==a[n-1]||a[0]==a[n-2]||a[1]==a[n-1]){
	        printf ("YES\n");
	    }else{
	        
	        for(i=1;i<n;++i){
	            if(a[i]==a[0]){
	                tag1=i+1;
	                if(a[tag1]==a[n-1]){
	                    printf ("YES\n");
	                    break;
	                }
	            }
	        }
	        if(i>=n){
	            printf ("NO\n");
	        }
	    }
	}
	return 0;
}

Range Assign, Number of Distinct Elements Minimize CodeChef Solution in JAVA

 import java.io.*;
import java.util.*;
 class Codechef
 {  
	

	static void BFS(ArrayList<ArrayList<Integer>> adj,int s, boolean[] visited) 
	{ 
    	Queue<Integer> q=new LinkedList<>();
    	
    	visited[s] = true; 
    	q.add(s); 
    
    	while(q.isEmpty()==false) 
    	{ 
    		int u = q.poll(); 
    		 
    		for(int v:adj.get(u)){
    		    if(visited[v]==false){
    		        visited[v]=true;
    		        q.add(v);
    		    }
    		} 
    	} 
	} 
	static int BFS(ArrayList<ArrayList<Integer>> adj,pair s, boolean[] visited,int ar[],int m) 
	{ 
    	Queue<pair> q=new LinkedList<>();
    	
    	visited[s.a] = true; 
    	q.add(s); 
    
		int count=0;
    	while(q.isEmpty()==false) 
    	{ 
    		pair u = q.poll(); 
			// if(adj.get(u.a).size()==0)
			// count++;
	 
			boolean end=true;
    		for(int v:adj.get(u.a)){
    		    if(visited[v]==false){
    		        visited[v]=true;
					end=false;
					int cat=ar[v]==0?0:ar[v]+u.b;
					if(cat>m)
					continue;
    		        q.add(new pair(v, cat));
					// System.out.print("--"+v+" "+cat+"--");
    		    }
    		} 
			if(end)
			count++;
			// System.out.println(u.a+" "+adj.get(u.a).size()+" count "+count);
    	} 
		return count;
	} 
	static void addEdge(ArrayList<ArrayList<Integer>> adj, int u, int v) 
	{ 
		adj.get(u).add(v); 
		adj.get(v).add(u); 
	} 
	static void ruffleSort(long[] a) {
        int n=a.length;
		Random random = new Random();
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n);
			long temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
	static void ruffleSort(int[] a) {
        int n=a.length;
		Random random = new Random();
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n);
			int temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
	public static int Lcm(int a,int b)
	{ int max=Math.max(a,b);
		for(int i=1;;i++)
		{
			if((max*i)%a==0&&(max*i)%b==0)
			return (max*i);
		}
 
	}
  static void sieve(int n,boolean prime[])
    {
       
        // boolean prime[] = new boolean[n + 1];
        for (int i = 0; i <= n; i++)
            prime[i] = true;
 
        for (int p = 2; p * p <= n; p++) {
            
            if (prime[p] == true) {
               
                for (int i = p * p; i <= n; i =i+ p)
                    prime[i] = false;
            }
        }
 
        
    }
 
	// public static String run(int ar[],int n)
	// { 
			
	// }
	
	
	public static int upperbound(int s,int e, long ar[],long x)
	{
		int res=-1;
		while(s<=e)
		{ int mid=((s-e)/2)+e;
			if(ar[mid]>x)	
			{e=mid-1;res=mid;}
			else if(ar[mid]<x)
			{s=mid+1;}
			else
			{e=mid-1;res=mid;
			 if(mid>0&&ar[mid]==ar[mid-1])
			 e=mid-1;
			 else
			 break;
			}

		}
		return res;
	}
	public static long lowerbound(int s,int e, long ar[],long x)
	{
		long res=-1;
		while(s<=e)
		{ int mid=((s-e)/2)+e;
			if(ar[mid]>x)	
			{e=mid-1;}
			else if(ar[mid]<x)
			{s=mid+1;res=mid;}
			else
			{res=mid;
				if(mid+1<ar.length&&ar[mid]==ar[mid+1])
			    s=mid+1;
			    else
				break;}

		}
		return res;
	}
	
	static long modulo=1000000007;
	public static long power(long a, long b)
	{
		if(b==0) return 1;
		long temp=power(a,b/2)%modulo;
		
		if((b&1)==0)
		return (temp*temp)%modulo;
		else 
		return (((temp*temp)%modulo)*a)%modulo;
	}
	public static long powerwithoutmod(long a, long b)
	{
		if(b==0) return 1;
		long temp=power(a,b/2);
		
		if((b&1)==0)
		return (temp*temp);
		else 
		return ((temp*temp)*a);
	}
	public static double log2(long a)
	{ double d=Math.log(a)/Math.log(2);
		return d;

	}
	public static int log10(long a)
	{ double d=Math.log(a)/Math.log(10);
		return (int)d;

	}
	
	public static long gcd(long a, long b)
    {
        if (a == 0)
            return b;
 
        return gcd(b % a, a);
    }
	
	public static void tree(int s,int e,int ar[],int c)
	{
	  if(s<=e)
	  {
		  int max=s;
		  for(int i=s;i<=e;i++)
		   if(ar[i]>ar[max])
			 max=i;
			 
		  ar[max]=c++;
		  tree(s,max-1,ar,c);
		  tree(max+1,e,ar,c);
	  }
	}
	
 static int resturant=0;
 static void DFS(ArrayList<ArrayList<Integer>> al,boolean visited[],int s,int max,int curr,int ar[])
 {
	
	visited[s]=true;
	if(al.get(s).size()==1&&visited[al.get(s).get(0)]==true)
	{resturant++;return;}
	// System.out.println(s+" "+curr);
	for(int x:al.get(s))
	{
		if(visited[x]==false)
		{
			if(ar[x]==0)
			DFS(al, visited, x, max, 0, ar);
			else if(curr+ar[x]<=max)
			DFS(al, visited, x, max, curr+ar[x], ar);
			
		}
	}
	
 }
	
	public static void main(String[] args) throws Exception
   {
		FastIO sc = new FastIO();
		
 
		//sc.println();
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx CODE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
		int test=sc.nextInt();
		
		// // double c=Math.log(10);
		// boolean prime[]=new boolean[233334];
		// sieve(233334, prime);
		// HashMap<Character,Integer> hm=new HashMap<>(9);
		// char c='1';
		// for(int i=1;i<=9;i++)
		//  hm.put(c++,i);

		

		while(test-->0)
		{
		
		
		  
		int n=sc.nextInt();	
		int ar[]=new int[n];
		HashMap<Integer,Integer> hm=new HashMap<>();
		for(int i=0;i<n;i++)
		{
			ar[i]=sc.nextInt();
			if(ar[i]==ar[0])
			hm.put(ar[0],i);
		}
		String res="NO";
		if(ar[0]==ar[n-1])
		{
			sc.println("YES");
			continue;
		}
		for(int i=0;i<n-1;i++)
		{
			if(ar[i]==ar[0]&&ar[i+1]==ar[n-1])
			{
				res="YES";
				break;
			}
		}

		
		
		sc.println(res);

		






		}
				
	
	   
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx CODE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
sc.close();	
}	
}


class pair implements Comparable<pair>{
	int a;
	int b;
	pair(int a,int b)
	{this.a=a;
		this.b=b;

	}
	public int compareTo(pair p)
	{
		
		return (this.b-p.b);
	}
}
class triplet implements Comparable<triplet>{
	int first,second,third;
	triplet(int first,int second,int third)
	{this.first=first;
		this.second=second;
		this.third=third;

	}
	public int compareTo(triplet p)
	{ if(this.third-p.third==0)
		return this.first-p.first;
		else
		return -this.third+p.third;
	}
}
// class triplet
// {
// 	int x1,x2,i;
// 	triplet(int a,int b,int c)
// 	{x1=a;x2=b;i=c;}
// }
 class FastIO extends PrintWriter {
	private InputStream stream;
	private byte[] buf = new byte[1<<16];
	private int curChar, numChars;
 
	// standard input
	public FastIO() { this(System.in,System.out); }
	public FastIO(InputStream i, OutputStream o) {
		super(o);
		stream = i;
	}
	// file input
	public FastIO(String i, String o) throws IOException {
		super(new FileWriter(o));
		stream = new FileInputStream(i);
	}
 
	// throws InputMismatchException() if previously detected end of file
	private int nextByte() {
		if (numChars == -1) throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars == -1) return -1; // end of file
		}
		return buf[curChar++];
	}
 
	public String nextLine() {
		int c; do { c = nextByte(); } while (c <= '\n');
		StringBuilder res = new StringBuilder();
		do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n');
		return res.toString();
	}
 
	public String next() {
		int c; do { c = nextByte(); } while (c <= ' ');
		StringBuilder res = new StringBuilder();
		do { res.appendCodePoint(c); c = nextByte(); } while (c > ' ');
		return res.toString();
	}
 
	public int nextInt() { 
		int c; do { c = nextByte(); } while (c <= ' ');
		int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); }
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res = 10*res+c-'0';
			c = nextByte();
		} while (c > ' ');
		return res * sgn;
	}
 
  public long nextLong() { 
		int c; do { c = nextByte(); } while (c <= ' ');
		long sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); }
		long res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res = 10*res+c-'0';
			c = nextByte();
		} while (c > ' ');
		return res * sgn;
	}
 
	public double nextDouble() { return Double.parseDouble(next()); }
}

Range Assign, Number of Distinct Elements Minimize CodeChef Solution in PYPY 3

def solve():
    n=int(input())
    lis=list(map(int,input().split()))
    if lis[0]==lis[-1]:print('YES');return
    for i in range(n-1):
        if lis[0]==lis[i] and lis[i+1]==lis[-1]:print('YES');return
    print('NO')

for _ in range(int(input())):solve();

Range Assign, Number of Distinct Elements Minimize CodeChef Solution in C#

using System;
using System.Collections.Generic;

public class Test
{
	public static void Main()
	{
		// your code goes here
		int n = int.Parse(Console.ReadLine());
		while (n-- > 0)
		{
			int len = int.Parse(Console.ReadLine());
			HashSet<int> res = new HashSet<int>();
			string[] str = Console.ReadLine().Split();
			bool flag = false;
			int[] arr = new int[len];
			int t1 = 0;
			for (int i = 0; i < len; i++)
			{
				arr[i] = int.Parse(str[i]);
				res.Add(arr[i]);
			}
			if (res.Count <= 2)
				Console.WriteLine("YES");
			else if (res.Count == len && len > 2)
				Console.WriteLine("NO");
			else
			{
				if (arr[0] == arr[len - 1] || arr[0] == arr[len - 2] || arr[len - 1] == arr[1])
					Console.WriteLine("YES");

				else
				{
					for (int i = 1; i < len; i++)
					{
						if (arr[0] == arr[i])
						{
							t1 = i;
							if (arr[t1 + 1] == arr[len - 1])
								flag = true;
						}

					}
					if (flag)
						Console.WriteLine("YES");
					else
						Console.WriteLine("NO");
				}
			}


		}
	}
}

Range Assign, Number of Distinct Elements Minimize CodeChef Solution in NODEJS

var input = '';

function cacheInput(data) {
    input += data;
}

function prepareInput() {
    input = input.split('\n');
}

function main() {
    prepareInput();
    const num = input[0];
    for (var i = 0; i < num; i++) {
        const test = input[2*i+1];
        const arr = input[2*i+2].split(' ').map(x => parseInt(x));
        // console.log(test, arr);
        let found = false;
        if(arr.at(0) === arr.at(-1)) {
            console.log('YES');
            continue;
        } 
        const start = arr.at(0);
        const end = arr.at(-1);
        for(let x in arr) {
            // if(x == arr.length - 1) break;
            x = parseInt(x);
            if(arr[x] === start && arr[x+1] === end) found = true;
        }
        
        if(found) console.log("YES")
        else console.log('NO');
    }
}

function pass(arr) {
    return arr.filter(onlyUnique).length == 2 || arr.filter(onlyUnique).length == 1;
}

function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', cacheInput).on('end', main);

Range Assign, Number of Distinct Elements Minimize CodeChef Solution in GO

package main
import (
	"bufio"
	"fmt"
	"os"
)

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(s string, v ...interface{}) {
	fmt.Fscanf(reader, s, v...)
}

func printf(s string, v ...interface{}) {
	fmt.Fprintf(writer, s, v...)
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n",&t)
	for T := 0; T < t; T++ {
	    var n int
	    scanf("%d\n",&n)
	    
	    a := make([]int, n)
	    m := make(map[int]struct{})
	    for i := 0; i < n; i++ {
	        scanf("%d ", &a[i])
	        m[a[i]] = struct{}{}
	    }
	    
	    if len(m) <= 2 || a[0] == a[n-1] {
	        printf("YES\n")
	        continue
	    }
	    
	    yes := false
	    for i, _ := range a {
	        if a[i] == a[0] && a[i+1] == a[n-1] {
	            yes = true
	            break
	        }
	    }
	    if yes {
	        printf("YES\n")
	        
	    } else {
	        printf("NO\n")
	    }
	    
	   
	    
	}
}
Range Assign, Number of Distinct Elements Minimize CodeChef Solution Review:

In our experience, we suggest you solve this Range Assign, Number of Distinct Elements Minimize CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Range Assign, Number of Distinct Elements Minimize CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Range Assign, Number of Distinct Elements Minimize CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *