- #include< iostream >
- using namespace std;
- int main() {
- int n, x;
- cin >> n;
- int ar[10001] = {0};
- for(int i = 0; i < n; i++) {
- cin >> x;
- ar[x]++;
- if(ar[x] > (n+1)/2) return cout << "NO\n", 0;
- }
- cout << "YES\n";
- return 0;
- }
Showing posts with label array. Show all posts
Showing posts with label array. Show all posts
Monday, September 16, 2019
Codeforce A. Yaroslav and Permutations
Codeforce A. Collecting Beats is Fun
- #include< bits/stdc++.h >
- using namespace std;
- int main(){
- int k;
- char a;
- cin >> k;
- int ar[10] = {0};
- for(int i = 0; i < 16; i++){
- cin >> a;
- ar[a]++;
- }
- for(int i = 1; i < 10; i++)
- if(ar[i+'0'] > 2*k) return cout << "NO\n", 0;
- cout << "YES\n";
- return 0;
- }
Saturday, September 14, 2019
Double ended queue in cpp
#include< iostream > using namespace std; | |
int queue[1001], front = 0, rear = 0; | |
void enqueueBack(int element, int n) | |
{ | |
if(rear != n) { | |
queue[rear] = element; | |
rear++; | |
} | |
} | |
void enqueueFront(int element, int n) | |
{ | |
if(rear != n) { | |
for(int i = rear; i > front; i--) { | |
queue[i] = queue[i-1]; | |
} | |
queue[front] = element; | |
rear++; | |
} | |
} | |
void dequeueBack() | |
{ | |
if(front != rear) { | |
queue[--rear] = 0; | |
} | |
} | |
void dequeueFront() | |
{ | |
if(front != rear){ | |
queue[front++] = 0; | |
} | |
} | |
int get_front() | |
{ | |
return queue[front]; | |
} | |
int get_back() | |
{ | |
return queue[rear-1]; | |
} | |
int size() | |
{ | |
return rear - front; | |
} | |
bool isEmpty() | |
{ | |
return front == rear; | |
} | |
int main() | |
{ | |
int n, k, m; | |
cin >> n >> m; | |
for(int i = 0; i < n; i++) { | |
cin >> k; | |
enqueueFront(k, n); | |
} | |
cout << "Queue element are :"; | |
for(int i = 0; i < n; i++) cout << queue[i] << " "; | |
cout << endl; | |
for(int i = 0; i < m; i++) { | |
cin >> k; | |
enqueueBack(k,n+m); | |
} | |
cout << "Queue element are :"; | |
for(int i = 0; i < n+m ; i++) cout << queue[i] << " "; | |
cout << endl; | |
cout <<"Queue size :" << size() << endl; | |
cout << "Queue is empty = " << (isEmpty()? "True":"False") << endl; | |
cout << "Queue front element = " << get_front() << endl; | |
cout << "Queue back element = " << get_back() << endl; | |
dequeueFront(); | |
dequeueBack(); | |
cout << "Queue new front element = " << get_front() << endl; | |
cout << "Queue new back element = " << get_back() << endl; | |
cout <<"Queue new size :" << size() << endl; | |
return 0; | |
} |
Queue implement by array in c++
#include< iostream > using namespace std; int front = 0, rear = 0; | |
void enqueue(int queue[], int element, int arrSize) | |
{ | |
if(rear != arrSize){ | |
queue[rear] = element; | |
rear++; | |
} | |
} | |
void dequeue(int queue[]) | |
{ | |
if(front != rear){ | |
queue[front] = 0; | |
front++; | |
} | |
} | |
int frontData(int queue[]) | |
{ | |
return queue[front]; | |
} | |
int size() | |
{ | |
return rear - front; | |
} | |
bool isEmpty() | |
{ | |
return front == rear; | |
} | |
int main() | |
{ | |
int n, k; | |
cin >> n; | |
int queue[n+1]; | |
for(int i = 0; i < n; i++){ | |
cin >> k; | |
enqueue(queue, k, n); | |
} | |
cout << "Queue size : " << size() << endl; | |
cout << "Queue first element = " << frontData(queue) << endl; | |
dequeue(queue); | |
cout << "Queue new first element = " << frontData(queue) << endl; | |
cout << "Queue is empty = " << (isEmpty()? "True":"False") << endl; | |
cout << "Queue data are : " ; | |
while(!isEmpty()) { | |
cout << frontData(queue) << " "; | |
dequeue(queue); | |
} | |
cout << endl; | |
cout << "Queue is empty = " << (isEmpty()? "True":"False") << endl; | |
return 0; | |
} |
Subscribe to:
Posts (Atom)