Sunday, 21 July 2019

Count the number of times a character has occurred before a given index in a given string: C, C++ and Python Program

Sample Input:
11                             is length of string
aabcdbcdbcd            given string       
4                               Number of Queries
1                               we have to find the number of times character at index 1 occurred before index 1
3                               we have to find the number of times character at index 3 occurred before index 3
4                               we have to find the number of times character at index 4 occurred before index 4
5                               we have to find the number of times character at index 5 occurred before index 5


Sample Output:
1
0
0
1

In C:
#include<stdio.h>

int main()
{
 unsigned long long int n,i,q,p;
 scanf("%llu",&n);
 char st[n+1];
 scanf(" %s",st);

 scanf("%llu",&q);
 long long int h[26][500000]={{0}};

 char j;
 for(i=1;st[i]!='\0';i++)
 {
     for(j='a';j<='z';j++)
     {
         if(st[i] == j)
            

             h[st[i]-'a'][i]=h[j-'a'][i-1]+1;
         else 
            h[j-'a'][i]=h[j-'a'][i-1];
            
     }
 }

 while(q--)
 {
     scanf("%llu",&p);
     char temp=st[p-1];
     printf("%lld\n",h[temp-'a'][p-1]);
 }
}

In C++


#include <bits/stdc++.h>
using namespace std;

int main()
{
    unsigned long long int n,i,q,p;
    scanf("%llu",&n);
    char st[n+1];
    scanf(" %s",st);
    scanf("%llu",&q);
    while(q--)
    {
        scanf("%llu",&p);
        printf("%lld\n",count(st,st+p,st[p]));
    }
 

    return 0;

Monday, 15 July 2019

how to cure asthma naturally

Geethanjali yoga for bronchitis
https://www.youtube.com/watch?v=MOfMpMGLdRQ

https://www.youtube.com/watch?v=p32exTXYxIs
https://www.youtube.com/watch?v=ja4qRWHrXyI

https://www.youtube.com/watch?v=7ugAorNfzYo


https://www.youtube.com/watch?v=lmWPRyu2cus

https://www.youtube.com/watch?v=VVxi4IgyNTc

https://www.youtube.com/watch?v=ZQRMHOsxdyI
https://drshalini.com/cure-50-asthma-bronchitis-within-30-days/

https://www.youtube.com/watch?v=wQscx7pGKMk

herbs that will help cure asthma
punarnava- makes lungs flexible, removes infection and swelling, mucus

agar wood oil: removes swelling

ashok ke chal: works like inhaler...removes infections very fast.--control forming mucus.

mulati or licorice: anti infection and removes allergy....removes brown part and wash it and smash it and put it in mouth. juice will help

cocoa nibs: cures asthma

cocoa nibs:- http://amzn.to/2hMsqib
punarnava :- http://amzn.to/2hC5TSl
Arjun chaal :- http://amzn.to/2gQQLn2
agar wood oil :- http://amzn.to/2gHSbvb
mulethi :- http://amzn.to/2gQM64H
Vasaka :- http://amzn.to/2hCerZA
Aniseed :- http://amzn.to/2hCboAN
Amla powder:- http://amzn.to/2gR266y

yoga series by baba ramdev

https://www.youtube.com/watch?v=8np0zh9c51A

https://www.youtube.com/watch?v=jNXIxMByn2M

https://www.youtube.com/watch?v=eVAuAZDwsc4

brahmari and udgeet
https://www.youtube.com/watch?v=TJFOaebCH_k


yoga by baba ramdev
https://www.youtube.com/watch?v=n824WPrEqug

https://www.youtube.com/watch?v=OvFN0wjyLr4

https://www.youtube.com/watch?v=zdbj7WElppA

https://www.youtube.com/watch?v=0JSQqRAi10A

Buteyko Breathing excercises
https://www.youtube.com/watch?v=tKaUEVnducI

sadhguru:


Thursday, 11 July 2019

Sherlock and Valid String Hacker Rank Solution in C

https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem

Explanation:

ex: aabbcc
then a occurs 2 times
b occurs 2 times and
c occurs 2 times
max of 2,2,2 is 2
min of 2,2,2 is 2

so if max == min then "YES"

ex: aaabbcc

then a occurs 3 times
b occurs 2 times and
c occurs 2 times
max of 3,2,2 is 3
min of 3,2,2 is 2

here max value is 3 and 3 occurred only once

so if max -min == 1 and maxcount == 1 then "YES"

ex: aabbc
then a occurs 2 times
b occurs 2 times and
c occurs 1 times
max of 2,2,1 is 2
min of 2,2,1 is 1
here min value 1 occurred only once
if min == 1 and mincount == 1  then "YES"

BUT

ex: aaabbc
then a occurs 3 times
b occurs 2 times and
c occurs 1 times
max of 3,2,1 is 3
min of 3,2,1 is 1

here min value 1 occurred only once but here answer must be "NO"
 so we need to find second maximum and if second maximum is equal to minimum then the answer is "YES"

so
if min == 1 and mincount == 1  and secondmaximum == min  then "YES"


Program

#include<stdio.h>
#include<limits.h>
int main()
{
    char st[100000];
    scanf("%s",&st);
   
    int smax=0,max=INT_MIN,smin=INT_MAX,min =INT_MAX,i,a[26]={0},c1=0,c2=0;
    for(i=0;st[i]!='\0';i++)
        a[st[i]-'a']++;
   
    for(i=0;i<26;i++)
    {
        if(a[i]!=0)
        {
            if(a[i]==max)
                  c1++;
             else if(a[i]> max)
            {
                 c1=1;
                 smax=max;
                 max=a[i];

               }
              else if (a[i]>smax && a[i]<max)
                     smax=a[i];

             if(a[i]==min)
                  c2++;
              else if(a[i]< min)
             {
                    c2=1;
                    min=a[i];
            }
        }
    }
 
    if(max == min)
        printf("YES");
    else if((max - min == 1 && c1 == 1)|| (min == 1 && c2 == 1&& smax == min))
        printf("YES");
    else
        printf("NO");
}

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


Monday, 1 July 2019

Constructing a Number Hacker Rank Solution in C, Strange Grid Again Hacker Rank Solution in C, Points on a Line Hacker Rank Solution in C

Strange Grid Again

https://www.hackerrank.com/challenges/strange-grid/problem

#include <stdio.h>
int main()
{
    unsigned long long int r,c;
    scanf("%llu%llu",&r,&c);
    if(r%2 == 0)
    {
        unsigned long long int term = ((r-1)/2)*5+c;
        printf("%llu",2*term-1); // nth term in odd series = 2*n-1
    }
    else{
        unsigned long long int term = ((r)/2)*5+c;
        printf("%llu", 2*term-2); // nth term in even series = 2*n-2
    }
    return 0;
}

Points on a Line


#include <stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    int x,y,res=0;
    scanf("%d%d",&x,&y);
    t--;
    while(t--)
    {
        int t1,t2;
        scanf("%d%d",&t1,&t2);
        if(!(t1 == x || t2 == y))
            res=1;

    }
    if(res == 1)
        printf("NO");
    else
        printf("YES");
}


Constructing a Number 


https://www.hackerrank.com/challenges/constructing-a-number/problem


Function:
char* canConstruct(int a_count, int* a)
 {
   
              long long int i,sum=0;
              for(i=0;i<a_count;i++)
                          sum=sum+a[i];
              if(sum%3 == 0)
                          return "Yes";
              return "No";
}


Restaurant Hacker Rank Solution in C, Filling Jars Hacker Rank Solution in C, Connecting Towns Hacker Rank Solution in C,

                                    

Restaurant


GCD of length and breadth of the bread will divide both the length and breadth giving squares of bread pieces of size GCD x GCD and remaining part of bread can only be cut into 1x1 pieces.
The number of square pieces of maximum size = length *breadth/GCDxGCD

#include <stdio.h>
int gcd(int a, int b)
{
    if(a == b)
        return a;
    if(a == 0)
        return b;
    return gcd(b%a,a);
}
int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int l,b,ans;
        scanf("%d%d",&l,&b);

        if(l<b)
            ans=gcd(l,b);
        else
            ans=gcd(b,l);

        printf("%d\n",(l*b)/(ans*ans));
    }
    return 0;

}
Connecting Towns
https://www.hackerrank.com/challenges/connecting-towns/submissions/code/112096613

int connectingTowns(int n, int* routes) {

     int i,ans=1;
     for(i=0;i<n-1;i++) // if n cities are there then edges between them will be n-1
        ans = ((ans%1234567)*(routes[i]%1234567))%1234567;
    return ans;

}
Filling Jars


#include <stdio.h> int main() { long long int n,m; scanf("%ld%ld",&n,&m); unsigned long long int sum=0; while(m--) { long int a,b,k; scanf("%ld%ld%ld",&a,&b,&k); sum = sum+(b-a+1)*k; } printf("%lld",sum/n); return 0; }