Wednesday, 7 October 2020

C programs that can be solved without using loops and if else

Q) A computer programming contest requires teams of 5 members each. Write a program that asks for the number of players and then give the number of teams and number of players leftover?

Q) write a c program to read a 4 digit number and calculate sum of first 2 digits and last 2 digits.

Example: 

four digit number: 4536

first two digits: 45

last two digits: 36

sum = 45+36 = 81

Q) write a c program to read a 4 digit number and calculate sum of first 2 digits and last 2 digits and 

do the square of sum obtained

Example: 

four digit number: 4536

first two digits: 45

last two digits: 36

sum = 45+36 = 81

squareOfsum=81*81 = 6561

Q) In a town, the percentage of men is 52. The percentage of total literacy is 48. If total percentage of literate men is 35 of the total population, write an algorithm to find the total number of illiterate men and women if the population of the town is 80,000.

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

Q) https://www.hackerrank.com/challenges/maximum-draws/problem

Q) https://www.hackerrank.com/challenges/handshake/problem

Q) https://www.hackerrank.com/challenges/lowest-triangle/problem

Q) https://www.hackerrank.com/challenges/30-operators/problem

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

Q) https://www.hackerrank.com/challenges/halloween-party/problem

Q) Write a C program to calculate simple interest

Q) Write a C program to convert the given temperature in Celsius to Fahrenheit 

Thursday, 1 October 2020

POSET, HASSE diagrams and Lattices

 when there is partial ordering, we represent partial ordering by hasse diagrams

Example of partial ordering: Dressing Up

we cannot wear a tie before wearing a shirt. There is partial ordering between wearing a tie and wearing a shirt

                                   shirt

                                      |

                                    tie

we cannot wear shoes before wearing pants.          

                                   pants

                                      |

                                    shoes

Similarly

                                   pant

                                      |

                                    belt


but there is no ordering required to  between wearing left shoe and right shoe. We can also assume there is no ordering required in wearing shirt and wearing pants. Hence our complete hasse diagram to represent partial ordering in dressing up is shown below


To check whether a given hasse diagram is lattice or not:


1. every pair must have unique least upper bound (luh or supremum or join ) and greatest lower bound (glb or meet or infimum)

for example:
upper bound of pair belt,tie ={belt, shirt, pant}
i.e, all that are above tie

least upper bound = belt
(immediately above tie)

lower bound of pair belt, tie={tie,left sock, right sock, left shoe, right shoe}
greatest lower bound of pair belt,tie = tie

but the hasse diagram is not lattice as for the pair {left sock, right sock} there is no lower bound

One real life example of lattice is when we consider natural numbers partially ordered by divisibility, the unique supremum is LCM of those two numbers and unique infimum is GCD. (as we know that GCD and LCM is unique for a given pair of numbers




https://www.slideshare.net/rupalirana07/ch-2-lattice-boolean-algebra




Thursday, 3 September 2020

Cheapest Subarray hacker earth solution in C

https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/cheapest-subarray-d628cb65/submissions/?utm_source=header&utm_medium=search&utm_campaign=he-search 


#include<stdio.h>

void main ()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n;
        scanf("%d",&n);
        int a[n],i=0;
        scanf("%d",&a[i]);
        for (i=1;i<n;i++)
        {
            
            scanf("%d",&a[i]);
            a[i-1]= a[i-1] + a[i];
            
        }
        int min = a[0];
        for (i=1;i<n-1;i++)
            if (min>a[i])
                min = a[i];
        
        printf("%d\n",min);
    }
}

Palindromic Numbers Hacker Earth Solution in C

 https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/palindromic-numbers-7/submissions/?utm_source=header&utm_medium=search&utm_campaign=he-search


int ispallin(int n)
{
int ar[10],c = 0;
    for(;n!=0;n=n/10)
        ar[c++] = n%10;

    for(int i =0, j= c-1; i<j; i++,j--)
        if(ar[i]!=ar[j])
            return 0;
    return 1;
}
int main()
{
int t;
    scanf("%d",&t);
while(t--)
    {
    int a,b;
    scanf("%d%d",&a,&b);
    int ans = 0,c=0;;
    for(int i=a;i<=b;i++)
             if(ispallin(i))
                 c++;
    printf("%d\n",c);
}
return 0;
}

The Dawn Hacker Earth Solution in C

https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/title-abhi-socha-nahi/submissions/?utm_source=header&utm_medium=search&utm_campaign=he-search 

#include <stdio.h>

#include <math.h>
int main()
{
    unsigned long long int num,a,b;
    scanf("%llu", &num);
    for(a = sqrt(num);num%a != 0; a--);
    b=num/a;
    printf("%llu", a+b);
}

Tuesday, 1 September 2020

Arania Exumai Blast !! Hacker Earth Solution in C

 https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/reflection-is-every-thing-2fff0566/submissions/


#include <stdio.h>
int main()
{
    char str[10001];
gets(str);
    int c=0,i;
for(i=1;str[i] != '\0'; i++)
        if(str[i] == str[i-1])
            c++;
    printf("%d",c);
}