Question 1.)
Given two number a and b.Count how many numbers in the range a to b(including both a and b) has odd number of divisors.
Input
First line of input contains number of test cases T.Each of the next T lines contains two number a and b.
Output
For each test case output a single line containing resulting count.
Constraints
T<=100
1<=a<=b<=10^12
Sample Input
1
2 5
Sample Output
1
Explanation
Divisors of 2:1,2
Divisors of 3:1,3
Divisors of 4:1,2,4
Divisors of 5:1,5
So only 4 has odd number of divisors which gives count as 1.
Lets take some examples
Number Divisors Number of Divisors
1 1 1(odd)
2 1,2 2(even)
3 1,3 2(even)
4 1,2,4 3(odd)
5 1,5 2(even)
6 1,2,3,6 4(even)
7 1,7 2(even)
8 1,2,4,8 4(even)
9 1,3,9 3(odd)
10 1,2,5,10 4(even)
11 1,11 2(even)
12 1,2,3,4,6,12 6(even)
13 1,13 2(even)
14 1,2,7,14 4(even)
15 1,3,5,15 4(even)
16 1,2,4,8,16 5(odd)
17 1,17 2(even)
18 1,2,9,18 4(even)
19 1,19 2(even)
20 1,2,4,5,10,20 6(even)
21 1,3,7,21 4(even)
22 1,2,11,22 4(even)
23 1,23 2(even)
24 1,2,3,4,6,8,12,24 8(even)
25 1,5,25 3(odd)
See the numbers who have odd number of divisors ,
Only 1,4,9,16,25 in range(1-25) have odd number of divisors and all these numbers are perfect square numbers.
1=1*1
4=2*2
9=3*3
16=4*4
25=5*5
Solution ::
#include"stdio.h"
#include "math.h"
int main()
{
int t;
scanf("%d",&t); // Test cases
while(t--)
{
long long int a,b,ans=0;
scanf("%lld%lld",&a,&b);
ans=(int)(floor(sqrt(b)))-(int)(ceil(sqrt(a)))+1;
printf("%lld\n",ans);
}
return 0;
}
Given two number a and b.Count how many numbers in the range a to b(including both a and b) has odd number of divisors.
Input
First line of input contains number of test cases T.Each of the next T lines contains two number a and b.
Output
For each test case output a single line containing resulting count.
Constraints
T<=100
1<=a<=b<=10^12
Sample Input
1
2 5
Sample Output
1
Explanation
Divisors of 2:1,2
Divisors of 3:1,3
Divisors of 4:1,2,4
Divisors of 5:1,5
So only 4 has odd number of divisors which gives count as 1.
Lets take some examples
Number Divisors Number of Divisors
1 1 1(odd)
2 1,2 2(even)
3 1,3 2(even)
4 1,2,4 3(odd)
5 1,5 2(even)
6 1,2,3,6 4(even)
7 1,7 2(even)
8 1,2,4,8 4(even)
9 1,3,9 3(odd)
10 1,2,5,10 4(even)
11 1,11 2(even)
12 1,2,3,4,6,12 6(even)
13 1,13 2(even)
14 1,2,7,14 4(even)
15 1,3,5,15 4(even)
16 1,2,4,8,16 5(odd)
17 1,17 2(even)
18 1,2,9,18 4(even)
19 1,19 2(even)
20 1,2,4,5,10,20 6(even)
21 1,3,7,21 4(even)
22 1,2,11,22 4(even)
23 1,23 2(even)
24 1,2,3,4,6,8,12,24 8(even)
25 1,5,25 3(odd)
See the numbers who have odd number of divisors ,
Only 1,4,9,16,25 in range(1-25) have odd number of divisors and all these numbers are perfect square numbers.
1=1*1
4=2*2
9=3*3
16=4*4
25=5*5
Solution ::
#include"stdio.h"
#include "math.h"
int main()
{
int t;
scanf("%d",&t); // Test cases
while(t--)
{
long long int a,b,ans=0;
scanf("%lld%lld",&a,&b);
ans=(int)(floor(sqrt(b)))-(int)(ceil(sqrt(a)))+1;
printf("%lld\n",ans);
}
return 0;
}
Question 2.)
Given a string as input you have to remove the duplicates characters from the string .
String contains only small letters a-z (lowercase) .
Input
First line of input contains number of test cases T.Each of the next T lines contains a single string .
Output
For each test case output a single string with no any duplicates characters.
Constraints
1<=T<=10
1<=length(string)<=200
Sample Input
2
ymca
manan
Sample Output
ymca
man
String contains only small letters a-z (lowercase) .
Input
First line of input contains number of test cases T.Each of the next T lines contains a single string .
Output
For each test case output a single string with no any duplicates characters.
Constraints
1<=T<=10
1<=length(string)<=200
Sample Input
2
ymca
manan
Sample Output
ymca
man
Solution ::
#include"stdio.h"
#include"string.h"
int main()
{
char str[205];
scanf("%s",str); //read the string.
int string_length=strlen(str),i;
int hash[26]={0};
for(i=0;i< string_length; i++)
{
int hash_position=str[i]-'a';
if(hash[hash_position]==0)
{
printf("%c",str[i]);
hash[hash_position]=1;
}
}
printf("\n");
return 0;
}
No comments:
Post a Comment