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, 26 September 2017
Sunday, 24 September 2017
Finding Array of Prime and Factors using Sieve of Eratosthenes Algorithm in C++
Theory: Using the sieve of Eratosthenes find an array of prime numbers.
Explanation:
1.Declare an int array sieve[]
2.Using the Sieve of Eratosthenes algorithm an array of prime can be obtained.Again factors of non prime can also be obtained;
3.sieve[i] == 0 : i is a prime number.
4. When not equal to zero the factor is displaced within the array element.
Code
#include <bits/stdc++.h>
using namespace std;
int sieve[100];
int main(){
int n;
cin>>n;
for(int x=2; x<=n; x++){
if(sieve[x]){
continue;
}
for(int i=2*x;i<=n;i+=x){
sieve[i]=x;
}
}
cout<<"Prime factors is those elements with 0 and other elements are factors";
for(int i=2;i<=n;i++){
cout<<i<<" "<<sieve[i]<<endl;
}
return 0;}
Explanation:
1.Declare an int array sieve[]
2.Using the Sieve of Eratosthenes algorithm an array of prime can be obtained.Again factors of non prime can also be obtained;
3.sieve[i] == 0 : i is a prime number.
4. When not equal to zero the factor is displaced within the array element.
Code
#include <bits/stdc++.h>
using namespace std;
int sieve[100];
int main(){
int n;
cin>>n;
for(int x=2; x<=n; x++){
if(sieve[x]){
continue;
}
for(int i=2*x;i<=n;i+=x){
sieve[i]=x;
}
}
cout<<"Prime factors is those elements with 0 and other elements are factors";
for(int i=2;i<=n;i++){
cout<<i<<" "<<sieve[i]<<endl;
}
return 0;}
Thursday, 21 September 2017
CodeForces Problem 863B - Kayaking Solution (C++)
Problem:
Please find the problem here.
Solution:
This is the easiest problem I ever had, just implement the given formulas! However, it does take me some time to write.
Please find the problem here.
Solution:
This is the easiest problem I ever had, just implement the given formulas! However, it does take me some time to write.
Code:
using namespace std;
int a[100],b[100];
main(){
int n;
cin>>n;
for(int i=0;i<2*n;i++){
cin>>a[i];
}
int maximum = 1000000;
for(int i=0;i<2*n;i++){
for(int j=i+1;j<2*n;j++){
for(int k=0;k<2*n;k++){
b[k]=a[k];
}
b[i]=b[j]= -1;
sort(b,b+2*n);
int ans=0;
for(int k=2;k<2*n-1;k+=2){
ans+= b[k+1]- b[k];
}
maximum=min(maximum,ans);
}
}
cout<<maximum;
}
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:
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:
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, 14 September 2017
Sum of Maximum SubArray O(n) Time Complexity Short Code (C++)
//Find the maximum sum of subarray
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
n=8; //size of array
int arr[] ={-2,-3,4,-1,-2,1,5,-3};
int best= 0, sum =0;
for(int i=0;i<n;i++){
sum = max(arr[i],sum+ arr[i]); // max of array
best= max(best,sum); // Largest sum of subarray
}
cout<< best<<"\n";
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
n=8; //size of array
int arr[] ={-2,-3,4,-1,-2,1,5,-3};
int best= 0, sum =0;
for(int i=0;i<n;i++){
sum = max(arr[i],sum+ arr[i]); // max of array
best= max(best,sum); // Largest sum of subarray
}
cout<< best<<"\n";
return 0;
}
Codeforce Problem 854A - Fraction 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.
Code:
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:
using namespace std;
int main(){
int n,sum;
cin>> n;
for(int i=n/2;i>=1;i--){
if(__gcd(i,n-i)== 1){
cout<<i<< " "<<n-i<<endl;
return 0;
}
}
}
Sunday, 10 September 2017
UVa Problem 594 - One Little, Two Little, Three Little Endians 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.
Hint: swaping the character pointer for each bits reverses the entire byte. And displaying the value after reversal.
Code:
#include<bits/stdc++.h>
using namespace std;
#define swp(a,b) a=a^b, b=a^b, a=a^b
main(){
int n,reverse;
while(cin>>n){
reverse =n;
char* bits =(char*) &reverse;
swp(bits[0],bits[3]);
swp(bits[1],bits[2]);
cout<<n<<" converts to "<<reverse<<endl;
}
}
UVa Problem 483 - Word Scramble (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.
Code:
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 <string>
#include <iostream>
using namespace std;
int main(){
string line;
while(getline (cin,line)) {
int tam = line.size();//10 size of first line
int cont;
string sub;
for(int i=0;i<tam;i++){ //I
cont=0;
if(line[i]!=' '){ // If there is no space
while(line[i+cont]!=' ' && i+cont<tam){
cont++; //count becomes 1
}
for(int j=(i+cont)-1;j>=i && j<tam;j--){ //print last word j then decrease j--
cout<<line[j];
}
i+=cont-1; //the next word which is a space
}else{ //if there is space
cout<<line[i];
}
}
cout<<endl;
}
return 0;
}
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:
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;}
OjLight Problem 1294 - Positive Negetive Number (C++)
Problem:
Solution:
This is the one of the simplest problem I ever had, just implement using the given formulas! However, it does take me some time to write.
Code:
Solution:
This is the one of the simplest problem I ever had, just implement using the given formulas! However, it does take me some time to write.
Code:
using namespace std;
int main(){
long long t;
long long n,m,result;
cin>>t;
for(int i=1;i<=t;i++){
cin>>n>>m;
result = m*(n/2);
cout<<"Case "<<i<<": "<<result<<endl;
}
return 0;
}
OjLight Problem 1053 - Higher Math Solution (C++)
Problem: OjLight Problem 1053 - Higher Math
Solution:
This is the simplest problem I ever had, just implement the given formulas! However, it does take me some time to write.
Code:
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;
main(){
long long n,a,b,c,f,g;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a>>b>>c;
if(a>b && a>c)
{
f= a*a;
g=(b*b)+(c*c);
}
else if(b>a&&b>c){
f= (b*b);
g= a*a+c*c;
}
else if(c>a&& c>b){
f=c*c;
g=a*a+b*b;
}
(f==g?cout<<"Case "<<i<<":"<<" yes"<<endl:cout<<"Case "<<i<<":"<<" no"<<endl);
}
return 0;
}
Sunday, 3 September 2017
CodeForce Problem 588A - Duff and Meat 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.
Code:
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:
The code is explained below using Comments(//)
//http://codeforces.com/problemset/problem/588/A
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<=b;i++) // Loop starts from a and ends at b
main(){
int n,a,p;
cin>>n; //Input taken
int mi =200;
int cost=0;
REP(i,0,n-1){
cin>>a;
cin>>p;
mi=min(mi,p); //Finding the minimum and replacing the minimum.
cost =cost+(a*mi); // Multiplying minimum with required amount .
} // Add to Cost
cout<<cost<<endl;
}
CodeForce Problem 318A - Even Odds (C++)
Problem:
Please find the problem here.
Solution:
This is one of the easiest problem I ever had, just implement the given formulas! However, it does take me a few minutes to write.
Code:
Please find the problem here.
Solution:
This is one of the easiest problem I ever had, just implement the given formulas! However, it does take me a few minutes to write.
Code:
//Even Odds Solution
#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
int main(){
ll n, k;
cin>>n>>k;
//Suppose n=10.
//Odd series then Even series 1,3,5,7,9,2,4,6,8,10.
n=(n+1)/2; //Finding the mid point.In above case it is 9.
//Condition 1.greater than 9 Even and vice-versa(Odd).
cout<<(k>n? 2*(k-n):2*k-1)<<endl;//if and else condition
return 0;
}
#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
int main(){
ll n, k;
cin>>n>>k;
//Suppose n=10.
//Odd series then Even series 1,3,5,7,9,2,4,6,8,10.
n=(n+1)/2; //Finding the mid point.In above case it is 9.
//Condition 1.greater than 9 Even and vice-versa(Odd).
cout<<(k>n? 2*(k-n):2*k-1)<<endl;//if and else condition
return 0;
}
CodeForce Problem 849A - Odds and Ends Solution (C++)
Problem:
You can see the question to 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:
You can see the question to 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;
main(){
int n;
int s[101];
cin>>n;
for(int i=0;i<n;i++){
cin>>s[i]; //Numbers are stored into array
}
/*Algorithm explanation
//when n is divided by 2 remainder is not 0, result is always odd.
//Plus First element and last element.. both of them must always be odd for this Algorithm to work as mentioned in question. */
(n%2 && s[0]%2 && s[n-1]%2)?cout<<"Yes":cout<<"No"; //Another form of if else.
}
UVa Problem 272 - TEX Quotes 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.
Code:
#include <bits/stdc++.h>
using namespace std;
main(){
int c =0;
string s;
while(getline(cin,s) ){
while(s.find('"')+1){
if(c==0 ){
s.replace(s.find('"'),1,"``");
c=1;
}
else{
s.replace(s.find('"'),1,"''");
c=0;
}
}
cout<<s<<endl;
}
}
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;
main(){
int c =0;
string s;
while(getline(cin,s) ){
while(s.find('"')+1){
if(c==0 ){
s.replace(s.find('"'),1,"``");
c=1;
}
else{
s.replace(s.find('"'),1,"''");
c=0;
}
}
cout<<s<<endl;
}
}
Subscribe to:
Posts (Atom)
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...