Showing posts with label Implementation. Show all posts
Showing posts with label Implementation. Show all posts

Tuesday, 26 September 2017

Codeforces Problem 864A Solution in (C++)

Welcome:

The question to the problem can be found here:

Explanation:
1. a[entered input] is the location of the input, say 10 it means the element is in the 10th position in the array.
2. Increase the value in that position by 1 when you get a different match of integers.
3. Store the first identical element in the 1st place of another array com[0] , second element to com[1]
4. Print YES if there are at exactly 2 different number in all cases.Then print those two numbers.
5. If not Print NO
6. Happy Coding!

Code:
#include<bits/stdc++.h>
using namespace std;
int a[100], com[2];
main(){
    int sto=0;
int n;
cin>>n;
int x;
for(int i=0;i<n;i++){
cin>>x;
cin.ignore();
if(++a[x] == 1){ //if more than 1 loop condition becomes false;
    com[sto++] = x;
  
}
}

if(sto == 2 && a[com[0]]== a[com[1]]){
    cout<<"YES"<<endl;
    cout<<com[0]<<" "<<com[1];
}
else{
    cout<<"NO"<<endl;
}

}


Tuesday, 19 September 2017

CodeForces Problem 862A - Mahmoud and Ehab and the MEX Solution (C++)

Problem:

Please find the problem here.


Solution:

This is the simplest problem I ever had, just implement the given formulas! However, it does take me some time to write.

Explanation:
1.MEX has two condition
2.MEX equal to entered input ... MEX will increase by 1 as element is erased.
3.MEX less than input...MEX will decline as element is inserted before MEX
4.Goodluck!

Code:
#include<bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<n;i++)
using namespace std;
main(){
int n;
int k;

int x;
cin>>n>>k;
int ans=k;
FOR(i,n){
cin>>x;
if(x<k){
ans--;
}
else if(x==k){
ans++;
}
}
cout<<ans;
}

Thursday, 7 September 2017

Uva Problem 11936 - The Lazy Lumberjacks Solution (C++)

Problem:

11936 - The Lazy Lumberjacks


Please find the problem here.

Solution:

This is the simplest problem I ever had, just implement the given formulas! However, it does take me some time to write.

Code:


#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
int x,y,z;
cin>>t;
while(t--){
cin>>x>>y>>z;
if(x+y>z && y+z>x && z+x>y){
cout<<"OK"<<endl;
}
else if(x==y && y ==z && z==x)
    cout<<"OK"<<endl;
else cout<<"Wrong!!"<<endl;
}

return 0;}

Spoj Problem ACMCEG2C - Pick the candies (C++)

  The problem link may be found here.       Explanation: Use Deque to keep track of elements of the variety of candies. If i is gre...