Project Euler #13: Large sum | Hackerrank | Project Euler

  Large sum


Work out the first ten digits of the sum of N 50 - digit numbers.

Input Format

First line contains N, next N lines contain a 50 digit number each.

Constraints

1<=N<=10^3

Output Format

Print only first 10 digit of the final sum

Sample Input

5
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676


Sample Output

2728190129

Explanation

Summing the numbers we get 2728190129820036131461476701043585006837989465343, first 10 digits are 2728190129.

Solution in python:


n = int(input())
tot=0
for i in range(n):
    a=int(input())
    tot=int(tot)+a;
while (tot >= 10000000000):
    tot = tot // 10
print(tot)



You can also run it on an online IDE :



Your feedback are always welcome! If you have any doubt you 
can contact me or leave a comment!  
Happy Coding!! Cheers!!!


Project Euler #34: Digit factorials | Hackerrank | Project Euler

 Digit factorials 


19 is a curious number, as 1!+9!=1+362880=362881 which is divisible by 19.

Find the sum of all numbers below N which divide the sum of the factorial of their digits.

Note: as 1!,2!,...,9! are not sums they are not included.

Input Format

Input contains an integer N

Constraints

10<=N<=10^5

Output Format

Print the answer corresponding to the test case.

Sample Input

20

Sample Output

19

Solution:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    long long n;
    scanf("%lld",&n);
    long long total=0;
    for(long long i=10;i<=n;i++)
    {
        long long m,k,sum=0;
        k=i;
        while(k>0)    
         {   
         long long fact=1;
         m=k%10
         if(m==0)
         fact=1;
         else{
         for(long long j=1;j<=m;j++)  
         fact*=j;
         }
         sum+=fact;
         k=k/10;
         }
         //cout<<sum<<endl;
         if(sum%i==0)
          total+=i;
    }
     cout<<total;
    return 0;
}




You can also run it on an online IDE :


Your feedback are always welcome! If you have any doubt you 
can contact me or leave a comment!  
Happy Coding!! Cheers!!!

Project Euler #22: Names scores | Hackerrank | Project Euler

 Names scores

You are given around five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list in sample is sorted into alphabetical order, PAMELA, which is worth 16 + 1 + 13 + 5 + 12 +1 = 48, is the 5th name in the list. So, PAMELA would obtain a score of 5*48=240.

You are given Q queries, each query is a name, you have to print the score.

Input Format

The first line contains an integer N, i.e., number of names.
Next N lines will contain a Name.
Followed by integer Q followed by Q lines each having a word.

Constraints

1<=N<=5200
length of each word will be less than 
1<=Q<=100

Output Format

Print the values corresponding to each test case.

Sample Input

5
ALEX
LUIS
JAMES
BRIAN
PAMELA
1
PAMELA


Sample Output

240


Solution:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int getIndex(vector<string> names, string K)
{
    auto it = find(names.begin(), names.end(), K);
    if (it != names.end()) 
    {
        int index = it - names.begin();
        return index ;
    }
    else {
        return -1 ;
    }
}
bool mycomp(string a, string b){
    return a<b;
}
vector<string> alphabaticallySort(vector<string> a){
    int n=a.size();
    sort(a.begin(),a.end(),mycomp);
    return a;
}
int main()
{   
    int n,q;
    scanf("%d",&n);
    vector<string> names;
    string name;
    for(int i=0;i<n;i++){
        cin>>name;
        names.push_back(name); 
    }
    names=alphabaticallySort(names);
    scanf("%d",&q);
    for(int q1=0;q1<q;q1++)
    {
      string query;
      int ind,sum=0;
      cin>>query;
      ind=getIndex(names, query);
      for(int j=0;j<query.length();j++)
            sum+=(int)(query[j])-64;
      cout<<sum*(ind+1)<<endl;   
    }
    return 0;
}


You can also run it on an online IDE :


Your feedback are always welcome! If you have any doubt you 
can contact me or leave a comment!  
Happy Coding!! Cheers!!!

Project Euler #8: Largest product in a series | Hackerrank | Project Euler

 Largest product in a series

Find the greatest product of K consecutive digits in the N digit number.

Input Format

First line contains T that denotes the number of test cases.
First line of each test case will contain two integers N & K.
Second line of each test case will contain a N digit integer.

Constraints

1<=T<=100
1<=K<=7
K<=N<=1000

Output Format

Print the required answer for each test case.

Sample Input 0

2
10 5
3675356291
10 5
2709360626

Sample Output 0

3150
0

Explanation 0

For 3675356291 and selecting K=5 consecutive digits, we have 36753, 67535, 75356, 53562, 35629 and 56291. Where 6*7*5*3*5 gives  maximum product as 3150
For 2709360626, 0 lies in all selection of 5 consecutive digits hence maximum product remains 0

Solution:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int t;
    cin >> t;
    for(int a0 = 0; a0 < t; a0++){
        int n;
        int k;
        cin >> n >> k;
        string num;
        cin >> num;
    int j=k;
    int l=0;
    int max=0;
    while(j<=n)
    {
        int mul=1;
        for(int i=l;i<j;i++)
        {
            mul=(int)(num[i]-48)*mul;
        }
    
        j++;
        l++;
        
        if(mul>max)
        max=mul;
    }
     cout<<max<<endl;
    }
    return 0;
}



You can also run it on an online IDE :

https://ide.geeksforgeeks.org/1a3d68dc-6e98-408d-9c36-f0dd139e8f4d

Your feedback are always welcome! If you have any doubt you 
can contact me or leave a comment!  
Happy Coding!! Cheers!!!

Project Euler #17: Number to Words | Hackerrank | Project Euler

 Number to Words


The numbers 1 to 5 written out in words are One, Two, Three, Four, Five 

First character of each word will be capital letter for example:
 104382426112 is 

One Hundred Four Billion Three Hundred Eighty Two Million Four Hundred Twenty Six Thousand One Hundred and Twelve

Given a number, you have to write it in words.

Input Format

The first line contains an integer T, i.e., number of test cases.
Next T lines will contain an integer N.

Constraints

1<=T<=10
0<=N<=10^2

Output Format

Print the values corresponding to each test case.

Sample Input

2
10
17

Sample Output

Ten 
Seventeen

Solution : Source (Hackerrank)


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
char digits[20][10] = {"","One","Two","Three","Four",
"Five","Six","Seven","Eight","Nine","Ten","Eleven",
"Twelve","Thirteen","Fourteen",
"Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
char tens[11][10] =  {"","","Twenty","Thirty","Forty",
"Fifty","Sixty","Seventy","Eighty","Ninety"};

void word(int num){
    if(num/100)
        printf("%s Hundred ", digits[num/100]);
    if((num%100) < 20 && (num%100) > 0)
        printf("%s ",digits[num%100]);
    else if((num/10)%10){
        printf("%s ", tens[(num/10)%10]);
        if(num%10)
            printf("%s ", digits[num%10]);
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while(t--){
        long long int num;
        scanf("%lld", &num);
        int tn = num / 1000000000000;
        int bn = (num / 1000000000) % 1000;
        int mn = (num / 1000000) % 1000;
        int th = (num / 1000) % 1000;
        int hd = num % 1000;
        //printf("%d %d %d %d %d\n", tn, bn, mn, th, hd);
        if((tn + bn + mn + th + hd) == 0)
            printf("Zero");
        if(tn){
            word(tn);
            printf("Trillion ");
        }
        if(bn){
            word(bn);
            printf("Billion ");
        }
        if(mn){
            word(mn);
            printf("Million ");
        }
        if(th){
            word(th);
            printf("Thousand ");
        }
        if(hd){
            word(hd);
        }
       printf("\n"); 
    }
    return 0;
}


You can also run it on an online IDE :


Your feedback are always welcome! If you have any doubt you 
can contact me or leave a comment!  
Happy Coding!! Cheers!!!




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 

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int t; 
    scanf("%d",&t);
    for(int a0 = 0; a0 < t; a0++){
        long long n; 
        scanf("%lld",&n);
        long long f1,f2,diff;
        f1=(n*(n+1))/2;
        f1=f1*f1;
        f2=(n*(n+1)*(2*n+1))/ 6;
        diff=(f1-f2);
        if (diff<0)
        diff=diff*-1;
        printf("%lld\n",diff);
    }
    return 0;
}


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


Project Euler #2: Even Fibonacci numbers | Hackerrank | Project Euler

 Even Fibonacci Numbers


Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1,2,3,5,8,13,21,3,55,89,...

By considering the terms in the Fibonacci sequence whose values do not exceed N, find the sum of the even-valued terms.

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^5
10<=N<=4*10^16

Output Format

Print the required answer for each test case.

Sample Input 0

2
10
100

Sample Output 0

10
44

Explanation 0

For N=10, we have {2,8}, sum is 10.
For N=100, we have {2,8,34}, sum is 44.


Solution:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int t; 
    scanf("%d",&t);
    for(int a0 = 0; a0 < t; a0++){
        long n,sum=2,p1 = 1p2 = 2, fib = 0
        scanf("%ld",&n);
         fib= p1 + p2;
        while (fib <= n) {
         if(fib%2==0)
            sum=sum+fib;
         p1 = p2;
         p2 = fib;
         fib = p1 + p2;
    }
        printf("%ld\n",sum);
    }
    return 0;
}


You can also run it on an online IDE: 


Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!  Happy Coding !! Cheers!!!

Project Euler #1: Multiples of 3 and 5 | Hackerrank | Project Euler

Multiple of 3 and 5 


If we list all the natural numbers below 10 that are multiples of 3 or 5 , we get 3,5,6 and 9. The sum of these multiples is 23 .

Find the sum of all the multiples of  3 or 5  below N.

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^5
1<=T<=10^9

Output Format

For each test case, print an integer that denotes the sum of all the multiples of  3 or 5 below  N.

Sample Input 0

2

10

100

Sample Output 0

23

2318

Explanation 0

For N=10, if we list all the natural numbers below 10 that are multiples of  3 or 5 , we get 3,5,6 and 9 . The sum of these multiples is 23.

Similarly for N=100, we get 2318.

Solution:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int t;
    long int s3,s5,s15,f3,f5,f15; 
    scanf("%d",&t);
    for(int a0 = 0; a0 < t; a0++){
        long int sum=0,n;
        scanf("%ld",&n);
        f3=(n-1)/3;
        f5=(n-1)/5;
        f15=(n-1)/15;
        s3=3*((f3*(f3+1))/2);
        s5=5*((f5*(f5+1))/2);
        s15=15*((f15*(f15+1))/2);
        sum=s5+s3-s15;
        printf("%ld\n",sum);
    }
    return 0;
}

You can also run it on an online IDE:   


Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!  Happy Coding !! Cheers!!!


Related Links:


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