Friday, 8 February 2019

Simple example to learn backtracking produced by Tail Recursion

Tail Recursion allow you to do backtracking.

Below is a simple program that prints 3 2 1 1 2 3 when n=3.

f(3) will print 3 and will call f(2) but after the call to f(2) there is print statement.
Since there are statements after recursive call such recursion is called Tail Recursion.

#include <stdio.h>
void f(int n)
{
    if(n == 0)
        return;
    printf("%3d",n);
    f(n-1);
    printf("%3d",n);
}
main()
{
    int n;
    scanf("%d",&n);
    f(n);
}

Another example that makes use of backtracking

Generate k bit binary strings without consecutive ones 
#include <stdio.h>
void rec(int a[],int l,int prev,int k)
{
    if(l == k)
    {
        int i;
        for(i=0;i<k;i++)
            printf("%d",a[i]);
        printf("\n");
        return;
    }
    if(prev == 0)
    {
        a[l]=0;
        rec(a,l+1,0,k);
        a[l]=1;
        rec(a,l+1,1,k);
    }
    else{
        a[l]=0;
        rec(a,l+1,0,k);
    }
       
}
main()
{
    int k;
    scanf("%d",&k);
    int a[k];
    rec(a,0,0,k);
}

Monday, 4 February 2019

How to send Whats App Messages Automatically Using the data in Excel

To Send What's App messages automatically to contact numbers provided in excel.

1. First export the contacts in excel to your gmail account.
you can watch the video in the below link or follow the instructions below

https://www.youtube.com/watch?v=rDbXq-EemdM

You can go to google contacts and download the contacts.csv file and copy the contacts in excel
and paste it in contacts.csv file.

2. Once all the contacts are exported to gmail. Sync those contacts in your mobile

3. You will get those contacts added automatically and will show as contacts in your mobile.

4. You can now broad cast messages to your contacts by selecting new Broadcast.
In Whatsapp account, click on 3 dots and from drop down menu select New Broadcast. When you select New Broadcast in your mobile, you can same message to 256 contacts with out forming a group.


5. Or you can also download the vb script in the below link
https://drive.google.com/file/d/1qx9nPX9r9GcukAj3Nb7AkmhrnBIDu49x/view?usp=sharing

and follow the instructions in the below video
https://www.youtube.com/watch?v=vhcgZDKVQT8

Sunday, 23 December 2018

Hanging Posters Hacker Rank solution in python3

Problem:
https://www.hackerrank.com/contests/hourrank-31/challenges/hanging-posters/problem

Solution:
#!/bin/python3

from math import *

n,h=map(int, input().split())
wp = list(map(int, input().split()))
pl = list(map(int, input().split()))
ans=0
for i in range(n):
    if wp[i] <= h:
        temp=0
    else:
        temp = wp[i]+pl[i]*1/4  #height of top of the poster from ground level
        temp=temp-pl[i]*1/2-h   #reduce the height of arthur and half of height of poster
   
    if ans<temp:
        ans=temp
print(ceil(ans))       


Thursday, 8 November 2018

Chef and Easy Problem CodeChef March Challenge solution in C++ using bitset

 Problem : https://www.codechef.com/MARCH18B/problems/XXOR/


 Solution:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <bitset>
using namespace std;

int main() {

    long long int n,q;
    cin>>n>>q;
    long long int temp,bc[n];
    bitset<31> bin[n];
    for(long long int i=0;i<n;i++)
    {
        cin>>temp;
        bin[i]=temp;
        bc[i]=floor(log2(temp))+1;
    }
    //cout<<bin[0];
     while(q--)
    {
        long long int a1,b1;
        bitset<31> b;
        b.set();
        cin>>a1>>b1;
        a1--;
        b1--;
        long long int max=bc[a1];
        for(long long int i=a1+1;i<=b1;i++)
        {
            if(max<bc[i])
                max=bc[i];
        }
      
        for(long long int j=max-1;j>=0;j--)
        {
            long long int c0=0,c1=0;
            for(long long int i=a1;i<=b1;i++)
            {
                if(bin[i][j]==1)
                    c1++;
                else
                    c0++;
            }
            if(c1>=c0)
            b.reset(j);
        }
        cout<<b.to_ulong() << '\n';
    }
    return 0;
}

Tuesday, 23 October 2018

Comparator function in C

int compare (const void * a, const void * b)
{
 if(*(int*)a < *(int*)b )
    return 1; //if a<b do swapping
 return -1;   //else donot do swapping
}

int main ()
{
int arr[] = {10, 5, 15, 12, 90, 80};
int n = sizeof(arr)/sizeof(arr[0]), i;

qsort (arr, n, sizeof(int), compare);

for (i=0; i<n; i++)
    printf ("%d ", arr[i]);
return 0;
}

Thursday, 6 September 2018

Strong number, Spy number C program

Check whether a given number is strong or not C program
#include<stdio.h>
main()
{
    int n,f,sum=0,temp,r,i;
    scanf("%d",&n);
    temp=n;
    while(n>0)
    {
        f=1;
        r=n%10;
        for(i=1;i<=r;i++)
            f=f*i;
        sum=sum+f;
        n=n/10;
    }
    if(sum == temp)
        printf("strong");
    else
        printf("not strong");
}
 Check whether a given number is spy number or not C program
#include<stdio.h>
main()
{
    int n,f=1,sum=0,r,i;
    scanf("%d",&n);
   
    while(n>0)
    {
        r=n%10;
        f=f*r;
        sum=sum+r;
        n=n/10;
    }
    if(sum == f)
        printf("spy");
    else
        printf("not spy");
}