Thursday, 20 October 2016

Problem : Cats And Dogs


                                   Problem : Cats And Dogs



Cats & Dogs have become friends now. They are going to a picnic and they decided to sit together in groups to have their lunch.

There are N Cats & M Dogs. They can form any number of groups to sit together, subject to rules below. 

Rules:
  1. They will sit in a straight line
  2. A Cat cannot sit beside another Cat or alone
  3. A Dog cannot sit beside another Dog or alone


You need to find minimum number of groups they need to form. If no group can be formed then print -1.

Input Format:

First line of input will be a number of Test Cases T

Next T lines of input will contains two numbers N and M, delimited by space
Output Format:

Print minimum number of groups to be formed for each Test Case in new line
OR

Print -1 if no group can be formed
Constraints:
  1. 0<T<=10^6
  2. 0<N,M<=10^18

Example:

EXAMPLE NUMBERSAMPLE INPUTSAMPLE OUTPUT
1
2
5 5
1 3

1
-1
2
3
7 8
3 5
6 7

1
2
1

Solution:-
#include<iostream>
using namespace std; 
int main()
{
int T,N,M;
cout<<"Enter No of Test case: ";
cin>>T;
for(int i=0;i<T;i++)
{
cout<<"\nEnter cat and dog :";
cin>>N>>M;
if(M/2>N||N/2>M||M<1||N<1)
cout<<"Answer : -1";
else if(M==N)
cout<<"Answer : 1";
else
{
int temp;
if(M>N)
{
temp=M-N-1;
cout<<"Answer : "<<temp+1;
}
else
{
temp=N-M-1;
cout<<"Answer : "<<temp+1;
}
}
}
}

Wednesday, 19 October 2016

Double And Add One



Problem : Double And Add One
If you like numbers, you may have been fascinated by prime numbers.
Here's a problem related to prime numbers: Accept input numbers N and i. Identify all prime numbers P up to N with the following property:

P1=2*P+1 is also prime
P2=2*P1+1 is also prime
...
Pi=2*P(i-1)+1 is also prime

Example: Inputs N=100, i=3

Let's start with p=2(it is also prime ). Since i=3 we need 3 consecutive prime numbers that satisfy the Double and Add 1 property explained below:
p1=2*p+1 translates to p1=2*2+1=5, which is prime
p2=2*p1+1 translates to p2=2*5+1=11, which is prime
p3=2*p2+1 translates to p3=2*11+1=23, which is prime

Hence p=2 is to be included in the output.

Next, if p=3, the derived numbers are 7, 15, 31 of which 15 is not prime. Hence p=3 is not a solution

Exploring other primes up to 100 in this fashion, we identify the following additional numbers to be included in the solution for i=3:

5 (since the derived numbers 11, 23, 47 are all prime)
89 (since the derived numbers 179, 359, 719 are all prime)

Hence the output would be: 2, 5, 89

Input format for the example: 100 3
Output format for the example: 2 5 89
(Numbers separated by single space)
Input Format:

First line contains an integer N
Second line contains integer i
Output Format:

Space delimited prime numbers satisfying Double and Add 1 property in the given range N

Constraints:
  1. 2< N <= 100000 
  2. 1< i <= 10 

Sample Input and Output


EXAMPLE NUMBERSAMPLE INPUTSAMPLE OUTPUT
1
100
3
2 5 89
2
20
2

2 5 11

Solution Source code :-

#include<iostream> 
using namespace std; 
bool primcheck ( int a)
{
int temp=0; 
for(int i=2;i<a;i++)
if(a%i==0)
temp++; 
}
if(temp==0)
return true; 
else 
return false; 
}
int main()
{
int N,in,temp=0,a;
cout<<"Enter N and i =";
cin>>N>>in;
for(int i=2;i<N;i++)
{
if(primcheck(i))
{
a=i;
for(int j=0;j<in;j++)
{
a=a*2+1;
if(!(primcheck(a)))
temp++;
}
if(temp==0)
cout<<i<<endl;
}
temp=0;
}
}

Tuesday, 18 October 2016

Start with programming in C

Start with C language


So, if you don’t have any idea about C or programming and why it is use, did is useful etc. question in your mind 


Then single answer is Yes, it is very important if you  want  to develop any computer Application like games, software even in hacking.


then first you need


   è Dev C++   (for windows)                 link
   è Cppdroid  (for Android)                   link
   è Gcc compiler according to your Operating System( OS )


History:- It is very boring part for skip it but if you want then go to wiki


Advantages:-
    
     1.       Portable language means your created  program can share with others
     2.       Close to machine so it give you to direct hardware access
     3.       Variety of data type with powerful operators
Disadvantages:-
     
     1.        Not have OOP’s concepts (that why c ++ developed)
     2.       C doesn’t have namespace concepts
     3.       It Application is only portable on same type OS ( NOTE:- Application not programs  )

 it is only some point there are many more


NOTE very important :- if not do any programming then forget all things and set your mind that “ learning of programming important if you want to do some things in computer and you must read this post completely    ”

My First Program :-
Open dev c++ and press Ctl+N

        
Past this code in window

#include<stdio.h>
int main()
{
printf("My First program by TrueProgrammerS");
return 0;

}

And  save it as c source file


Now click F9 if all things are okey then you get no error like this report window if any error are their check the code and try again


After done click F10 for run your program then you get output window in black color it know as console window and its is console application now



Like this
So how it works


So 1st line
#include<stdio.h>  :- this line add stdio.h file in your program from C library for printf



int mian() :- it is a position where your program start running and this { } show it’s body


printf(): it use to display any thing on that console window and what ever you write in this “ 


Any thing“ it show on window


return 0 :- it show that your main is end


last one ; :- this   ‘;’ this use to show end of line it means your line is end

Note= in C small letter and capital letter are different ( that why C is case sensitive language)


so try and comment if you get any problem 


thanks for reading
next post I will show to take input and do some math’s with C.


 GOOD NIGHT