Project Euler #17: Number to Words | Hackerrank | Project Euler
Project Euler #6: Sum square difference | Hackerrank | Project Euler
Sum square difference
The sum of the squares of the first ten natural numbers is, 1^2+2^2+...+10^2=385 . The square of the sum of the first ten natural numbers is, (1+2+...+10)^2 = 55^2 = 3025 . Hence the absolute difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025-385=2640.
Find the absolute difference between the sum of the squares of the first N natural numbers and the square of the sum.
Input Format
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.
Constraints
1<=T<=10^4
1<=N<=10^4
Output Format
Print the required answer for each test case.
Sample Input 0
2
3
10
Sample Output 0
22
2640
Explanation 0
For N=3 , (1+2+3)^2 - (1^2+2+^2+3^2) = 22
For N=10 , (1+2+...+10)^2 - (1^2+2^2+...10^2) = 2640
Solution
You can also run it on an online IDE:
https://ide.geeksforgeeks.org/6601fb42-0c0f-4c75-8058-27c871f50f58
Your feedback are always welcome! If you have any doubt you can contact me or leave a comment! Happy Coding !! Cheers!!!
Square Free Numbers - CodeVita 9 | Round 1
In the theory of numbers, square free numbers have a special place. A square free number is one that is not divisible by a perfect square (other than 1).
Problem Description
In the theory of numbers, square free numbers have a special place. A square free number is one that is not divisible by a perfect square (other than 1). Thus 72 is divisible by 36 (a perfect square), and is not a square free number, but 70 has factors 1, 2, 5, 7, 10, 14, 35 and 70. As none of these are perfect squares (other than 1), 70 is a square free number.
For some algorithms, it is important to find out the square free numbers that divide a number. Note that 1 is not considered a square free number.
In this problem, you are asked to write a program to find the number of square free numbers that divide a given number.
Input
Output
Constraints
N < 10^9
Complexity
Time Limit
Examples
Example 1
Input
20
Output
3
Explanation
N=20
If we list the numbers that divide 20, they are
1, 2, 4, 5, 10, 20
1 is not a square free number, 4 is a perfect square, and 20 is divisible by 4, a perfect square. 2 and 5, being prime, are square free, and 10 is divisible by 1,2,5 and 10, none of which are perfect squares. Hence the square free numbers that divide 20 are 2, 5, 10. Hence the result is 3.
Example 2
Input
72
Output
3
Explanation
N=72. The numbers that divide 72 are
1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72
1 is not considered square free. 4, 9 and 36 are perfect squares, and 8,12,18,24 and 72 are divisible by one of the. Hence only 2, 3 and 6 are square free. (It is easily seen that none of them are divisible by a perfect square). The result is 3
290990700
Output
255
4491411836
Output
31
Grooving Monkeys- Code Vita 8 | Pre-Qualifier Zonal Round
Grooving Monkeys
Problem Description
N monkeys are invited to a party where they start dancing. They dance in a circular formation, very similar to a Gujarati Garbaor a Drum Circle. The dance requires the monkeys to constantly change positions after every 1 second.
The change of position is not random & you, in the audience, observe a pattern. Monkeys are very disciplined & follow a specific
pattern while dancing.
Consider N = 6, and an array monkeys = {3,6,5,4,1,2}.
This array (1-indexed) is the dancing pattern. The value at monkeys[i], indicates the new of position of the monkey who is standing
at the ith position.
Given N & the array monkeys[ ], find the time after which all monkeys are in the initial positions for the 1st time.
Constraints
1<=t<=10 (test cases)1<=N<=10000 (Number of monkeys)
Input Format
First line contains single integer t, denoting the number of test cases.Each test case is as follows -
Integer N denoting the number of monkeys.
Next line contains N integer denoting the dancing pattern array, monkeys[].
Output
t lines,Each line must contain a single integer T, where T is the minimum number of seconds after which all the monkeys are in their
initial position.
Test Case
Example 1
Input
1
6
3 6 5 4 1 2
Output
6
Explanation
Consider N = 6, and an array monkeys = {3,6,5,4,1,2}.
Suppose monkeys are a,b,c,d,e,f, & Initial position (at t = 0) -> a,b,c,d,e,f
At t = 1 -> e,f,a,d,c,b
a will move to 3rd position, b will move to 6th position, c will move to 5th position, d will move to 4th position, e will move to
1st position and f will move to 2nd position. Thus from a,b,c,d,e,f at t =0, we get e,f,a,d,c,b at t =1. Recursively applying same
transpositions, we get following positions for different values of t.
At t = 2 -> c,b,e,d,a,f
At t = 3 -> a,f,c,d,e,b
At t = 4 -> e,b,a,d,c,f
At t = 5 -> c,f,e,d,a,b
At t = 6 -> a,b,c,d,e,f
Since at t = 6, we got the original position, therefore the answer is 6.
Program:
#include <stdio.h>
int isequal(int x[],int m)
{
int c1=0;
for(int k=1;k<=m;k++)
{
if(x[k]==k)
c1++;
}
return c1;
}
int main() {
int n,a[100],i,b[100],k,c[100],cnt,t;
scanf("%d",&t);
for(k=0;k<t;k++)
{
cnt=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=i;
}
z: for(i=1;i<=n;i++)
c[a[i]]=b[i];
cnt++;
if(isequal(c,n)==n)
printf("%d\n",cnt);
else
{
for(i=1;i<=n;i++)
b[i]=c[i];
goto z;
}
}
return 0;
}
You can also run it on an online IDE: https://ide.geeksforgeeks.org/IEgUyvHl1N
Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!! Cheers!!!
Related Links:
Philaland Coin : https://codepiggy.blogspot.com/2020/03/philaland-coin-code-vita-8-pre.html
Philaland Coin - Code Vita 8 | Pre-Qualifier Zonal Round
Philaland Coin
Problem Description
Constraints
Input Format
Output
Program:
Min Combinations - Code Vita 8 | Pre-Qualifier Zonal Round
Min Combinations
Problem Description
Alexander The great, while roaming the stretch of Turkey, came across a wise man.He asked the wise man, "Who is the greatest conqueror of all?". The wise man replied, "A person with great strength and
intelligence. Whosoever can solve my puzzle will go on to become the greatest!". The puzzle is as follows; Given two integers
'n1' and 'n2', select two integers 'a' and 'b', such as to solve the equation (n1 * a + n2 * b = x). But there is a catch, 'x' is the smallest
positive integer which satisfies the equation. Can you help Alexander become the greatest?
Constraints
1 <= T <= 1000-10^7 <= a, b <= 10^7
0 <= n1, n2 <= 10^7
Input Format
The first line contains the number of Test cases T.Next T lines contains two space-separated integers, n1 and n2.
Output
Print the value of x.Test Case
Example 1
Input
1
34818 45632
Output
2
Explanation
Given n1 = 34818 and n2 = 45632, if we choose a = 3553 and b = -2711, we get
=> n1 * a + n2 * b = x
=> 34818 * 3553 + 45632 * (-2711)
=> 2
Note: No other value of a and b, within the range, will give smaller value than 2.
Program
Prime Counters - Code Vita 2017 | Round 1
Prime Counters
Problem :
Given a number N, let CP(N) denote the number of primes between 1 and N (inclusive of N). We call N a prime counter if CP(N) is a prime (N need not be a prime). For example, CP(3) = 2, CP(4) = 2, CP(5) = 2, CP(7) = 4.Input Format:
An integer T, number of test cases T lines each containing two positive integers L, R separated by spaceOutput Format:
T lines containing the number of prime counters between L and R (both inclusive) in the ith test case (or NONE is no prime counter exists in that range)Constraints:
L ≤ R ≤ 106Output
4
Explanation
CP(1) = 0, CP(2) = 1, CP(3) = 2, CP(4) = 2, CP(5) = 3, CP(6) = 3, CP(7) = 4= CP(8) = CP(9) = CP(10) Hence there are 4 prime counters, 3, 4, 5, 6 in the range 1 to 10.
8
Explanation
Upto 10, we have 4 prime counters. Between 11 and 20 the prime counters are 11, 12, 17, 18 and hence the count is 8. Between 21 and 30, we have no prime counters.
Program:
#include <stdio.h>
int prime(int n)
{
int k,j,p=0;
for(k=2;k<=n;k++)
{
int c=0;
for(j=1;j<=k;j++)
if(k%j==0)
c++;
if(c==2)
p++;
}
return p;
}
int main()
{
int l,t,r,i,cp[1000],k,f,c=0,j,count;
scanf("%d",&t);
for(int z=1;z<=t;z++)
{
count=0;
scanf("%d %d",&l,&r);
for( i=l;i<=r;i++)
cp[i]=prime(i);
for(j=l;j<=r;j++)
{
c=0;
for (k = 1;k<=cp[j]*cp[j];k++)
{
if (cp[j] % k == 0)
c++;
}
if (c == 2)
count++;
}
printf("%d\n",count);
}
return 0;
}
You can also run it on an online IDE: https://ide.geeksforgeeks.org/YphGa2McDy
Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!! Cheers!!!
Related Links:
Air in the balloon: https://codepiggy.blogspot.com/2020/03/air-in-balloons.html
Jumble With Numbers
Jumble With Numbers
Problem
A Mathew number is computed as follows using the formula:
H(n) = n(2n-1)
And scientist John invented another number which is built by the following formula which is called John number.
T(n) = n(n+1)/2
Now Mathew and John are jumbled while combining their work. Now help them combine their research work by finding out number in a given range that satisfies both properties. Using the above formula, the first few Mathew-John numbers are:
1 6 15 28 …
Input consists of 3 integers T1,T2,M separated by space . T1 and T2 are upper and lower limits of the range. The range is inclusive of both T1 and T2. Find Mth number in range [T1,T2] which is actually a Mathew-John number.
Line 1 | T1 T2 M,where T1 is upper limit of the range, T2 is lower limit of the range and M ,where Mth element of the series is required |
Constraints:
Line 1 | For Valid Input,print Print Mth number from formed sequence between T1 and T2 Or No number is present at this index For Invalid Input,print Invalid Input |
Sample Input and Output:
SNo. | Input | Output |
---|---|---|
1 | 90 150 2 | 120 |
2 | 20 80 6 | No number is present at this index |
3 | -5 3 a | Invalid Input |
Program:
else
printf("Invalid Input");
Output:
Related Links: Test vita
Super Market Problem | TCS Code Vita 2023 - Zone 1 | Super Market TCS Code Vita 2023 Solution | Code Vita 2023 | Code Vita 2023 season 11 solution
Problem Description: In a Super market we will find many variations of the same product. In the same way we can find many types of rice bag...
-
Bride Hunting Problem Description Sam is an eligible bachelor. He decides to settle down in life and start a family. He goes bri...
-
Milk Man and His Bottles Problem A Milkman serves milk in packaged bottles of varied sizes. The possible size of the ...
-
Philaland Coin Problem Description The problem solvers have found a new Island for coding and named it as Philaland. These smart ...