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);
}

Countries Grouping Hacker Earth Solution in C

 https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/countries-grouping-1-5b13620a/editorial/


Explanation copied from editorial

If we start analyzing the answer of first person then there must be that number of people after him(including him) and all of their answers must be same as the first person's.

Let, answer[0] is 7. Then, this also means that answer[0], answer[1], answer[2], answer[3], answer[4], answer[5] and answer[6] must exist and each be 7. So, from answer[7] onwards, same algorithm can be applied to check the authenticity of the data provided and answer[7] will be considered as the starting index for the next country. After checking validity of the data, if it's invalid the answer is "Invalid Data" otherwise the distinct_countries count is incremented by 1 and when finally all of the people's responses are checked to be valid, the answer is calculated as distinct_countries.


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int a[n],i,j,c,flag=0;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int dc = 0;
for(i=0;i<n;i=i+c)
        {
dc++;
c = a[i];
if(i+c>n)
            {
flag = 1;
break;
}
for(j=i;j<i+c;j++)
            {
if(a[j]!=c)
                {
flag = 1;
break;
}
}
if(flag==1)
                break;
}
if(flag==1)
            printf("Invalid Data\n");
else
printf("%d\n",dc);
}
}

The savior? Hacker Earth Solution in C

https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/the-savior-3/description/?layout=old

 

Below is copied from editorial

Let's start with two simple math facts:

  1. From elementary math we know that for any two integers a and b, a + b is even if and only if both a and b are even or both a and b are odd.
  2. If you have an array of K elements, you can produce K * (K - 1) / 2 pairs of its elements

Based on these fact, we can produce an optimal solution. Let ODD be the number of odd integers in A and EVEN be the number of even integers in it.

Then the number of pairs of integers (a, b) from A for which a + b is even equals ODD * (ODD - 1) / 2 + EVEN * (EVEN - 1) / 2. We are almost there, but notice that we are interested in the number of pairs of different integers, so we are possibly counting too much here. For example, if A contains number 3 two times, then it will be taken into the result, but we do not want it to be there.


So this program will fail when there are duplicates:

#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int a[n],i,ne=0,no=0;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
            if(a[i]%2 == 0)
                ne++;
            else
                no++;
        }
        printf("%llu\n", (ne*(ne-1)/2)+(no*(no-1)/2));
            
    }
}

Solution:

#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int a[n],i,j;
        unsigned long long int c=0;;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=0;i<n-1;i++)
            for(j=i+1;j<n;j++)
                if((a[i]+a[j])%2 == 0 && a[i] != a[j])
                    c++;    
        printf("%llu\n",c);     
    }
}