Sunday, 30 June 2019

Halloween Party Hacker Rank Solution in C, Minimum Height Triangle Hacker Rank Solution in C,Summing the N series Hacker Rank Solution in C,



Minimum Height Triangle

https://www.hackerrank.com/challenges/lowest-triangle/submissions/code/112097635

int lowestTriangle(int base, int area){
 
    return ceil((2.0*area)/base);
}
summing-the-n-series

https://www.hackerrank.com/challenges/summing-the-n-series/problem

Tn = n*n - (n-1)*(n-1) = n*n -n*n - 1 +2n = 2n-1

So the series is 1+3+ 5+....+2*n-1
Sum of n terms in Arithmetic Progression =  (n/2)*(2a+(n-1)*d) here a is 1 (first term)
d is common difference = 2
So sum = (n/2)*(2*1+(n-1)*2)  = (n/2)*(2+2*n-2)  = (n/2)*(2*n) = n*n

int summingSeries(long n) {
return ((n%1000000007)*(n%1000000007))%1000000007;

}
Halloween Party

Here in k lines, some must be horizontal and some vertical. We get maximum 1x1 pieces
if number of horizontal lines and vertical lines are equal

So if k is even then we cut the chocolate by (k/2) horizonatl lines and (k/2) vertical lines
giving (k/2)*(k/2) pieces

when k is odd we give extra one horizontal cut.
(K+1)/2 horizontal and
remaining lines would be k-((k+1)/2) vertical lines


unsigned long long int halloweenParty(unsigned long long int k) {
   
if(k%2 == 0)
    return (k/2)*(k/2);
return((k+1)/2)*((k-((k+1)/2)));

}

Find the Point Hacker Rank solution in C

https://www.hackerrank.com/challenges/find-point/problem

if point P(px,py) is rotated around point Q(qx,qy) by 180 degrees then it reaches a point R(rx,ry)

hence qQ would be mid point between P and R

so (px+rx)/2 = qx  and (py+ry)/2 = qy

from the above equatiions, px,py,qx and qy are given we need to find rx,ry

hence rx = 2*qx-px and ry = 2*qy-py

#include <stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int px,py,qx,qy,rx,ry;
        scanf("%d%d%d%d",&px,&py,&qx,&qy);
        rx=2*qx-px;
        ry=2*qy-py;
        printf("%d %d\n",rx,ry);
    }
    return 0;
}


Thursday, 27 June 2019

Army game Hacker rank solution in c

If a supply drop can cover 4 blocks. Therefore if the grid is 2x2, 1 suppy drop is enough. But what if the grid is 3x2?
1 suppy drop will cover 2x2 portion of the grid but still there is 1x2 part and you need a supply drop there anyway. Same as how many you would need in a 4x2 grid.
Similarly if the region was 3x3 you would need as much as drops as you would need in a 4x4 grid. Otherwise some blocks would be left empty.
So for simplicity of calculation we increment the odd integer to it's nearest bigger even (yes, that means odd+1) and then multiply n and m and divide it by 4. That's how many supply drops we must need to cover all the blocks.  This explanation is given by user sojal in hacker rank discussions

https://www.hackerrank.com/challenges/game-with-cells/problem

Program:

 #include<stdio.h>
int main()
{
int n,m;
scanf("%d%d",&n,&m);
if(n%2 != 0)
    n++;
if(m%2 != 0)
    m++;
printf("%d",(m*n)/4);
return 0;
}

Sunday, 23 June 2019

Sub array Programs in C


Q) Number of sub arrays in a given array and sum of sub array elements.
For example given an array as shown below
23
14
39
42

Subarrays that can be formed are
[23], [23, 14], [23 , 14, 39], [23, 14, 39, 42]
[14], [14, 39], [14, 39, 42]
[39], [39, 42]
[42]
here 0th index element 23 occurs 4 times as first element in subarray.
1st index element 14 occurs 6 times of which
3 times it occurs as first element in subarray and
remaining 3 times it occurs in combination with first element 23

 2nd index element 39 occurs 6 times of which
2 times it occurs as first element in subarray and
2 times it occurs in combination with first element 23
2 times it occurs in combination with second element 14

3rd index element 42 occurs 4 times of which
1 times it occurs as first element in subarray and
1 times it occurs in combination with first element 23
1 times it occurs in combination with second element 14
1 times it occurs in combination with third element 39

if n is the number of elements in an array, here n=4
23 occurs n-i times as first element where i =0 as 23 is 0th index element
14 occurs n-i times as first element where i =1 as 14 is 1st index element.
remaining n-i times it occurs in combination with first element.
39 occurs n- i times as first element where i=2
n-i times in combination with first element 23
n-I times in combination with second element 14….so i *(n-i) times
and so on..
so general formula is
number of times ith element occurs = (n – i)+ i* (n-i)
Sum of sub array elements = number of times it occurs * element
So the program is:

#include <stdio.h>
int main()
{
               int n;
               printf("enter the number of elements\n");
               scanf("%d",&n);
               int a[n],sum=0,i;
               printf("\nEnter array elements\n");
               for(i=0;i<n;i++)
                              scanf("%d",&a[i]);
               for(i=0;i<n;i++)
               {
                              sum = sum+((n-i)+(n-i)*i)*a[i];
               }
               printf("sum = %d",sum);
}

Output:


Q) find the maximum possible length of subarray In that subarray a[i+1]%a[i]==0 Every element in subarray is divisible by its previous element Your task is to find maximum length of subarray.

#include <stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
{
    int n;
scanf("%d",&n);
    int a[n],i,j,max1=0,res=0,max2;
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
   
    for(i = 0 ; i < n ; i++)
    if(max1 < a[i])
    max1=a[i];
    
    int cnt[max1+1] = {0};
    int dp[max1+1] = {0};
    
    for( i = 0 ; i < n ; i++)
    cnt[ a[i] ]++ ;
    
    for (i = max1 ; i > 0 ; i--)
    {
        max2=0;
        for( j = 2*i ; j <= max1 ; j+=i )
        if(max2<dp[j])
        max2 = dp[j];
            dp[i] = cnt[i] + max2;
    }
    
    
    for( i = 0 ; i < max1+1 ; i++)
    if(res<dp[i])
    res = dp[i];
    printf("%d\n",res);
   
    }
    return 0;

}

Wednesday, 19 June 2019

Merge two strings picking a character alternatively from each string

#include<stdio.h>
#include <string.h>
int main() {
   char st1[100],st2[100],st3[200];
   scanf("%s%s",st1,st2);
   int l1,l2,i,k=0,j=0;
   l1=strlen(st1);
   l2=strlen(st2);

   for(i=0;j<l1 && k<l2;i++)
   {
       if(i%2 == 0)
            st3[i]=st1[j++];
       else
            st3[i]=st2[k++];
   }
   while(j<l1)
        st3[i++] = st1[j++];
   while(k<l2)
        st3[i++] = st2[k++];
   st3[i]='\0';
   printf("%s",st3);
}

Sunday, 16 June 2019

Print the maximum number of composite numbers that sum up to n.

Example:
given n = 81

81 can be represented as 18 * 4 + 9
so n can be represented as 18 4's and one 9. So together 18+1 = 19 number of composite numbers

Output: 19

The least composite number is 4.
so when n is represented as multiple of 4, we get maximum number of composite numbers.
When n is not divisible by 4 we get a remainder 1,2,3
When remainder is 1 then
excess = 1
to make number divisible by 4
we cannot subtract 4+1 = 5 as 5 from n as 5  is not composite number
so we take two 4's = 8 and 8+1 = 9 is next least composite number and hence we subtract 9 from n to make n divisible by 4.

When remainder is 2 then
excess = 2
to make number divisible by 4 we subtract 4+2 = 6 from n.

When remainder is 3 then
excess = 3
we cannot use 4+3= 7 as 7 is not composite number
so  we take two 4's = 8 and 8+3 = 11 is not composite number
so  we take three 4's= 12 and 12+3 = 15 is next least composite number and hence we subtract  15 from n to make n divisible by 4.
and 15 can be expressed as 9 and 5

#include<stdio.h>

int main()
{
   int n,ans= -1,r;
   scanf("%d",&n);
   if(n >= 4)
   {
       r=n%4;
       if(r == 0)
            ans = n/4;
       else if (r == 1)
       {
           if(n >= 9)
                ans = (n - 9)/4+1;
       }
       else if(r == 2)
            ans = (n - 6)/4 + 1;
       else if(r == 3)
       {
           if(n >= 15)
                ans = (n - 15)/4+2;
       }
   }
   if(ans == -1)
         printf("Not Possible");
  else
         printf("%d",ans);
}

Efficient Solution
If the number is 5, 7, 11 then it is impossible to represent it as sum of composite numbers.

#include<stdio.h>

int main()
{
   int n,ans= -1,r;
   scanf("%d",&n);
   if( n == 5 || n == 7 || n == 11)
          ans = -1;
   else if(n >= 4)
   {
       r=n%4;
       if(r == 0 || r == 2)
            ans = n/4;
       else if (r == 1 || r == 3)
                ans = n /4-1;
    }
    if(ans == -1)
         printf("Not Possible");
    else
         printf("%d",ans);
}