Friday 29 December 2017

Divisible Pairs Sum Hacker Rank Solution in C

Problem link :https://www.hackerrank.com/challenges/linkedin-practice-divisible-sum-pairs/problem

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

int main(){
    int n,j,i;
    int k,c[100]={0},res=0;
    scanf("%d %d",&n,&k);
    int *a = malloc(sizeof(int) * n);
    for(int a_i = 0; a_i < n; a_i++){
       scanf("%d",&a[a_i]);
        c[a[a_i]%k]++;
    }
    res=c[0]*(c[0]-1)/2;
    for(i=1,j=k-1;i<j;i++,j--)
        res=res+(c[i]*c[j]);
    if(k%2 == 0)
        res=res+c[k/2]*(c[k/2]-1)/2;
    printf("%d",res);
    return 0;
}

Tuesday 26 December 2017

Gemstones Hacker Rank Solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int a[26]={0};
int main() {
    int n,i,count=0,j,l;
    scanf("%i", &n);
    char* *arr = malloc(sizeof(char*) * n);
    for(int i = 0; i < n; i++){
       arr[i] = (char *)malloc(10240 * sizeof(char));
       scanf("%s",arr[i]);
        l=strlen(arr[i]);
        for(j=0;j<l;j++)
        {
            int c=arr[i][j]-97;
            if(a[c] == i)
            a[c]++;
        }
    }
    for(i=0;i<26;i++)
        if(a[i] == n)
            count++;
    printf("%d\n", count);
    return 0;
}

Day of the Programmer Hacker Rank Solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int days = 215; //256th days will be in 9th month so in 8 months, feb has variable days so
                //remaining 7 months days will be 215
void solve(int year){
    int febdays;
    if(year<=1917) //julian calender
        if(year%4 == 0)
            febdays=29;
        else
            febdays=28;
    else if(year == 1918)
        febdays=15;
    else
    {
        if((year%400 == 0)|| (year%4 == 0)&&(year%100 !=0))
            febdays=29;
        else
            febdays=28;
    }
    printf("%d.09.%d",256-(febdays+days),year);//febdays+days will be 8 months days
}

int main() {
    int year;
    scanf("%d", &year);
    int result_size;
    solve(year);
    return 0;
}

Monday 25 December 2017

Reverse of a binary number : C program : CHANGE THE EVEN Hacker rank solution in C

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>


 int inv(int n) {
    int rev = 0,r;
    while(n > 0) {
        r=n&1;
        rev = rev<<1 | r;
        n =n>> 1;
    }
    return rev;
}

int main()
{
    unsigned int t, i, num;
    scanf("%u", &t);
    for (i = 0; i < t;i++) {
        scanf("%u", &num);
        if (num%2 != 0) {
            printf("%u\n", num);
        } else {
            printf("%u\n", inv(num));
        }
    }
    return 0;
}

Explanation:

 int inv(int n) {
    int rev = 0,r;
    while(n > 0) {
        r=n&1;
        rev = rev<<1 | r;
        n =n>> 1;
    }
    return rev;
}

=========================
The above function is analogous to reverse of decimal number function below

 int inv(int n) {
    int rev = 0,r;
    while(n > 0) {
        r=n%10;
        rev = rev*10+ r;
        n =n/10;
    }
    return rev;
}

=============================

Here to extract last bit (LSB) using
n&1
It will only extract the last bit----
Since the base of binary system is 2 we multiply by using left shift operator as follows

rev<<1 is same as rev*2

Similarly division by 10 is replaced by division by2 which is performed using right shift operator as 
n=n>>1

Saturday 23 December 2017

Competitive Programming : To write Power Function with less complexity

 To Find Common Divisors between 2 numbers
1. find the gcd of two numbers
2. find the divisors of that gcd


https://primes.utm.edu/curios/page.php/23.html
http://www.shyamsundergupta.com/

Tuesday 12 December 2017

Recursive digit sum hacker rank solution in c

Problem:
https://www.hackerrank.com/challenges/recursive-digit-sum/

Solution is very simple for this problem but since the value of n can be 10^1000000 which cannot be stored in a variable of any data type in c, we need to store each digit in n in a string.

13 points solution is as follows: Using the fact that 
 recursive sum of digits is 9 if number is multiple of 9, else n % 9

#include<stdio.h>

int main()
{
        unsigned long long int n,k;
        scanf("%llu%llu",&n,&k);
        int ans=((n%9)*(k%9))%9;  // ans=(n*k)%9
        if(ans)
            printf("%llu",ans);
        else
            printf("9");
}


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

int superDigit(char* n, int k) {
       int k1,i;
    k1=k%9;
    if(k1 == 0)
        return 9;
    else
    {
        int sum=0;
        for(i=0;n[i] != '\0';i++)
            sum=(sum+n[i]-48)%9;
        if((sum*k)%9 == 0)
            return 9;
        else
            return (sum*k)%9;
     }
}

int main() {
    char* n = (char *)malloc(512000 * sizeof(char));
    int k;
    scanf("%s %i", n, &k);
    int result = superDigit(n, k);
    printf("%d\n", result);
    return 0;
}


Sunday 10 December 2017

Hacker Rank Char Pat SP4 solution in C

Problem: https://www.hackerrank.com/contests/spojklu/challenges/char-pat-sp-4

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void fun(int c,int h,int w)
{
    int i,j,k;
    for(i=1;i<=h;i++)
    {
        for(j=1;j<=c;j++)
        {
            printf("*");
            for(k=1;k<=w;k++)
                printf(".");
        }
        printf("*");
        printf("\n");
    }
}
int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int r,c,h,w,i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&r,&c,&h,&w);
        for (i=1; i<=((w+1)*c)+1;i++)
        printf("*");
    printf("\n");
    for(i=1;i<=r;i++)
    {
        fun(c,h,w);
        for(j=1;j<=((w+1)*c)+1;j++)
        printf("*");
        printf("\n");
       
    }
        printf("\n");
       
    }
   
    return 0;

}
=====================================================

#include<stdio.h>
main()
{
    int i,j,k,c=4,d=0;
    for(i=1;i<=5;i++)
    {
        for(k=1;k<=5-i;k++)
            printf(" ");
        for(j=1;j<=i+d;j++)
            printf("*");
        printf("\n");
        d++;
    }
    for(i=5;i>=1;i--)
    {
        for(k=1;k<=5-i;k++)
            printf(" ");
        for(j=1;j<=i+c;j++)
            printf("*");
        printf("\n");
        c--;
    }
}

How to do Literature Survey in Scopus







Thursday 7 December 2017

SPOJ Crucial Equation Solution in C

Problem:  http://www.spoj.com/problems/CEQU/

 #include <stdio.h>
long long int gcd(long long int a, long long int b)
{
    // Everything divides 0
    if (a == 0 || b == 0)
       return 0;

    // base case
    if (a == b)
        return a;

    // a is greater
    if (a > b)
        return gcd(a-b, b);
    return gcd(a, b-a);
}
int main(void) {
    // your code here
    long long int t,g,a,b,c,u=0;
    scanf("%lld",&t);
    while(t--)
    {
        u++;
        scanf("%lld%lld%lld",&a,&b,&c);
        g=gcd(a,b);
      
        if(c%g == 0)
            printf("Case %lld: Yes\n",u);
        else
            printf("Case %lld: No\n",u);
    }
    return 0;
}