Showing posts with label CodeForces. Show all posts
Showing posts with label CodeForces. Show all posts

Tuesday, 20 March 2018

Codeforces Problem 950D - A Leapfrog in the Array (C++)

Welcome:
The question to the problem can be found here:

Explanation:

1. It is a tricky one but easy to code if you understand it.
2. using the index of the array we can solve it.
3. First lets test your attention skill. Look at the IV Figure and then the I figure.
4. If the array index number is a odd number(see input) if you add one and divide by 2, you get the required answer(the value at that index)
5. On the other hand as long as the index number is even, do x+=n- x/2.
6. Do them in Bitwise so it will reduce a lot of time to run.
7. Happy Coding.


Code:

#include<bits/stdc++.h>
using namespace std;
int main(){
long long int n,q,x;
cin>>n>>q;
while(q--){
    cin>>x;
    while(!(x&1)){
        x+=n-x/2;
    }
    cout<<(x+1>>1)<<endl;

}



return 0;}

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

}


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