Sunday 24 September 2017

Time Complexity analysis


A program with time complexity 0.01*n2 is more complex than the program with time complexity 1600*n
For ex: Let n=1,000,000
0.01*n2 = 0.01*1,000,000*1,000,000 =10,000,000,000
1600*n  = 1600*1,000,000=1,600,000,000
Hence for larger values of n, a program with time complexity 0.01*n2 is more complex than the program with time complexity 1600*n  
Time complexity of 0.01*n2 +1600*n  would be O(n2)

Wednesday 20 September 2017

Sum of Two Primes : Hacker Rank Solution in C

Solution to below link problem
https://www.hackerrank.com/contests/primary-prime/challenges/is-prime-or-not-2

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int prime(long long int i)
{
            int j;
            for(j=2;j<=sqrt(i);j++)
            {
                        if(i%j == 0)
                                            return 0;
                 }
                return 1;
}

int main()
{

            long long int i,flag=0,n;
            scanf("%lld",&n);
             if(n<=4)
                    printf("%d",-1);
             else if((n-2)%2 !=0 && prime(n-2))
                    printf("%d\t%lld",2,n-2);
             else
            {
                         for(i=3;i< n-i;i=i+2)
                        {
                                    if((n-i)%2 !=0 && prime(i))
                                    {
                                                if(prime(n-i))
                                                 {
                                                            printf("%lld\t%lld",i,n-i);
                                                            flag=1;
                                                            break;
                                                }
                                    }
                        }
                        if(flag == 0)
                                printf("%d",-1);
             }  
 return 0;

}

Monday 18 September 2017

Time complexity Calculation


A program with time complexity 4n is more complex than the program with time complexity 16*n16
For ex: Let n=1024 =210
4n can be written as  22n
and substituting n value in 22n, we get
22*1024  =22048

and substituting n value in 16*(1024)16, we get
24 *(210)16
= 24 *2160
=2164
Hence for larger values of n, a program with time complexity 4n is more complex than the program with time complexity 16*n16
Time complexity of 4n+16*n16 would be O(22n)



Tuesday 12 September 2017

C program to remove duplicate values from an array



#include <stdio.h>
main()
{
               int a[10]={1,2,1,1,2,3,2,3,3,4},b[10],i,j=0,c,flag;
               b[0]=a[0];
               for(i=1;i<10;i++)
               {
                              flag=0;
                              for(c=0;c<=j;c++)
                              {
                                             if(a[i]==b[c])//  check whether a[i] is present in entire b array
                                             {
                                                            flag=1;
                                                            break;
                                             }
                              }
                              if(flag == 0)
                              {
                                             b[++j]=a[i];
                              }
               }
               printf("the array elements after removing duplicate values are\n");
               for(i=0;i<=j;i++)
                              printf("%d\t",b[i]);
}


Output:



Time complexity :

In worst case : Worst case occurs when there are no duplicate values in 'a' array

b[0]= a[0]
i.e, a[1]  is present in b array or not  --1 comparison as b array contains only one element at that moment
Since there are no duplicate elements in 'a' array then b[1] will contain a[1] i.e, b contains 2 elements now.

then 

a[2] is checked if it is present in b[0] or b[1]---doing 2 comparisons 
a[3] is checked if it is present in b[0] or b[1] or b[2]---doing 3 comparisons
:
:
a[n-1] is checked if it is present in b[0] or b[1] or......b[n-2] --- doing n-1 comparisons


adding all the comparisons 
1+2+3+....n-1 .

this is nothing but sum of first n-1 natural number i.e, = ((n-1)*(n-2))/2 = n^2 

Hence Time complexity of above program is O(n^2)

In Best Case:  occurs when all the array elements in 'a' array are same
b[0]=a[0]

a[1] is compared with b[0] for equality ...since a[1] and b[0] are same, b array will contain only one element.....1 comparison is done
a[2] is compared with b[0] for equality  ...1 comparison is done
a[3] is compared with b[0] for equality  ...1 comparison is done
:
:
a[n-1] is compared with b[0] for equality  ...1 comparison is done

Adding all comparisons
1+1+1+......n-1 times.
that is n-1 comparisons

Hence Best Time complexity is 'n'




Saturday 9 September 2017

Compiling and running first c# program using default compiler that comes with Windows Operating System



windows sdk comes with a default c# compiler.
In order to use the default c# compiler that comes with windows operating system, follow the steps shown below:
1. include the below url in path environment variable
C:\Windows\Microsoft.NET\Framework64\v4.0.30319

2. Open notepad and write your first c# program and save the file with the same name as class name with .cs extension

For ex: if you copy and paste the below code in notepad
using System;
namespace DeclaringConstants
{
    class Program
    {
        static void Main(string[] args)
        {
            const double pi = 3.14159;  
           
            // constant declaration
            float r;
            Console.WriteLine("Enter Radius: ");
            r = (float)Convert.ToDouble(Console.ReadLine());
            double areaCircle = pi * r * r;
            Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);
            Console.ReadLine();
        }
    }
}

Save the file as Program.cs

3. Open command prompt and type
>>csc Program.cs

4. If incase u get no error, type
>>Program

5. Output will be as follows

Friday 8 September 2017

Recursive Function in C to Convert Decimal number to Binary



Write a program to convert the given decimal number into equivalent binary number using recursion.

Program:
#include <stdio.h>
int dec2bin(int a)
{
               if(a>1)
                      dec2bin(a/2);
               printf("%d\t",a%2);
}
main()
{
               int n;
               printf("enter a number");
               scanf("%d",&n);
               dec2bin(n);
}

Output:


Decimal to Binary Using Loops

#include <stdio.h>
int main()
{
int n,ans=0,p=1,r;
scanf("%d",&n);
while(n>0)
{
r=n%2;
n=n/2;
ans=ans+p*r;
p=p*10;
}
printf("%d",ans);
}