One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1.
Anna was given several numbers and arranged them in a circle to fulfill the task.
Then she wanted to check if she had arranged the numbers correctly, but at this point her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what’s done is done and the results of her work had been destroyed.
But please tell Anna: could she have hypothetically completed the task using all those given numbers?
Input:
The first line contains integer n — how many numbers Anna had ().
The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.
Output:
Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary).
If Anna couldn't have fulfilled the task, no matter how hard she would try, print "NO" (without the quotes).
#include <stdio.h> #include<stdlib.h> int cmp(const void *a,const void *b) { return (*(int*)a -*(int*)b); } int main() {int N,i; scanf("%d",&N); int *aa=(int*)malloc(N*sizeof(int)); for(i=0;i<N;i++) scanf("%d",aa+i); qsort(aa,N,sizeof(int),cmp); N--; if((aa[N]-aa[0])>2) printf("NO"); else printf("YES"); return 0; }
INPUT_1:
7
3 4 5 2 3 4 5
OUTPUT:
NO
INPUT_2:
6
1 1 2 2 2 3
OUTPUT:
YES
ILLUSTRATION
Morae Q!
- Find the length of the array’s longest increasing sub-sequence.
- Arrange numbers in a circle so that any two neighbouring numbers differ by 1.
- Find partial names and count the total numbers.
- Find the minimized sum of non-deleted elements of the array after the end of the game.
- Find the maximum number of good sleeping times optimally.
- Sort the elements of the array in the order in which they are required.
- Find the minimum number of flats, monkey needs to visit to catch ninjas.
- Convert the square matrix to matrix in Z form.
- Shift the K elements of each row to right of the matrix.
- Find maximum possible number of students in a balanced team with skills.
- Find the Maximum number of pairs of points you can match with each other.
- Calculate the number of non-empty good subarrays of given array.
- Transform the binary string A into the string B using finite operations.
- Find the number of potion must the character take to jump the hurdles.
- Count the total number of vowels and consonants.
- Return all elements of the matrix in spiral order.
- Return all palindromic paths of the matrix.
- Write the code to change the display in reverse order using pointer.
- Find the number of days the expedition can last.
- Find the minimum size of the sub-segment to make the array pairwise distinct.