CODECHEF CONTEST WRITEUP (RANGEASSIGN DIV 4)
Logic =>
We can have atmost 2 distinct numbers
- if we have 1 distinct number then whole array is same or arr[0] == arr[n-1]
- [1,1,1,1] = [1,1,1,1] = [1]
- [1,2,3,1] = [1,1,1,1] = [1]
- If we have 2 distinct number then whole array then array will be divided into two parts =>
- [1,2,3,1,5,6,8,5] = [1,1,1,1,5,5,5,5] = [1,5]
- if a[0] == a[n-1] then yes
- else
- for(int i = 1 ; i < n ; i++){
- if(arr[0] == arr[i] & arr[i+1] == arr[n-1] then YES
- then NO
CODE =
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int arr[n];
for(int i = 0 ; i < n ; i++){
scanf("%d",&arr[i]);
}
int flag = 0;
if(arr[0] == arr[n-1]){
flag = 1;
}else{
for(int i = 0 ; i < n-1 ; i++){
if(arr[i] == arr[0] && arr[i+1] == arr[n-1]){
flag = 1;
}
}
}
if(flag){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}
Comments
Post a Comment