Thursday 11 July 2019

cutting paper squares Hacker Rank Solution in C, Diwali Lights Hacker Rank Solution in C, modular arithmetic

https://www.hackerrank.com/challenges/p1-paper-cutting/problem

Explanation:
when we cut n x m paper into n horizontal pieces using n-1 cuts
each of these n horizontal pieces can be cut using m-1 cuts
so number of cuts= n-1+(n*(m-1))
when we simplify n-1+(n*(m-1))  we get n-1+n*m -n =n*m-1


Program:

long solve(long int n, long int m)
{

return (n-1)+(n *(m-1)); // we can simplify it as n-1+nm-n tht is nm-1
}

Diwali Lights

https://www.hackerrank.com/challenges/diwali-lights/problem

Explanation: each bulb can have 2 states "on" or "off"

if there are n bulbs then we can get 2 power n combinations.

Since we cannot count all bulbs off as pattern so answer is 2 power n-1

Function:

long lights(unsigned long long int n)
{

unsigned long long int i,ans=1;
for(i=1;i<=n;i++)
{
ans =(ans%100000*2)%100000;
}
return ans-1;
}

or complete program is below

#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
    unsigned long long int n,i,ans=1;
    scanf("%llu",&n);
    for(i=1;i<=n;i++)
        ans =(ans%100000*2)%100000;

    printf("%llu\n",ans-1);
    }
}

Link to modular arithmetic used in competitive programming is below

https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/tutorial/ Day 2 operators

Problem:

Solution:

#include<stdio.h> #include <math.h> int main() { float mc,tipper,taxper,tip,tax,tc; scanf("%f%f%f",&mc,&tipper,&taxper); tip=(mc*tipper/100); tax=(mc*taxper/100); tc=round(mc+tip+tax); printf("%.0f",tc); }


3 comments:

  1. diwali light program is not working

    ReplyDelete
    Replies
    1. it is working just fine.
      Just copy and paste it in the code already given and remove the function prototype

      Delete
    2. or try this

      #include
      int main()
      {
      int t;
      scanf("%d",&t);
      while(t--)
      {
      unsigned long long int n,i,ans=1;
      scanf("%llu",&n);
      for(i=1;i<=n;i++)
      ans =(ans%100000*2)%100000;

      printf("%llu\n",ans-1);
      }
      }

      Delete