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;
}

}


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;}


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...