Posts

Write code to reverse C-style string.

Note :- c-string means that 'abcd' is represented as five characters, including the null character. Algorithm: Void reverse(char *str) {       char *end=str;       char temp;       if(str)       {            while(*end)        {             ++end;        }             --end;             while(str < end)             {                   temp=*str;                   *str++ =*end;                   *end-- =temp;             }       } }

Write an algorithm to determine if a string has all unique charcters.

public static boolean isUniqueChars2(String str) {        boolean [] char_set =new boolean[256];           for(int i=0; i<str.length(); i++)           {               int val=str.charAt(i);               if (char_set[val])               return false;              char_set[val]=true;                 }            return true; } 2nd Method : public static boolean isUniqueChars(string str) {       int checker=0;       for(int i=0;i<str.length();++i)       {           int val=str.charAt(i) -'a';           if((checker & (1<<val))>0)           return ...

Prove that every tree has at most one perfect matching.

Image
Since every tree with 2 or more vertices is 2-chromatic. We see that a tree with even no. of vertices will have perfect matching as all vertices with the same color can be grouped together and a matching can be established between the two groups. If the tree has odd number of vertices,then no perfect matching can be established for obvious reason.    [proved ]

Prove that every tree with 2 or more vertices is 2-chromatic.

Image
Since a tree is connected and has no cycle if one starts coloring any one vertex with a specific color and keeps on coloring every alternate vertex with another color, the entire graph can be colored with only two colors and since there is one and only one path between any two vertices in a tree, no two vertices will have the same color. Hence every Tree with 2 or more vertices is 2-Chromatic.    [Proved]                                                                 FIG (I)                                                                  FIG (II)

Show that every Bi-partite graph is 2 chromatic.

Image
Let G be a Bi-partite graph.            So, V(G)=V1 U V2    Color every vertex in V1 by one color and every vertex in V2 by another Colour.    Then, clearly, this coloring is proper. Thus only two colors suffice to color G. so, G is 2 chromatic.    [Proved] image source :Google

Prove that the number of circular permutations of n different objects is (n-1)! .

Since n different objects are arranged in a circle, the relative positions not the linear positions determine the arrangement . Hence, if one of them is kept fixed, the remaining n-1 can be arranged in (n-1)! ways. This is exactly the number of circular permutations with n distinct objects.

State the "Pigeon Hole Principle" and the " Generalised pigeon Hole principle".

If n pigeons are assigned to m pigeonholes and n>m, then at least one pigeonhole contains two or more pigeons. if there are n pigeonholes occupied by nk+1 pigeons, then there must be at least one pigeonhole occupied by k+1 or more pigeons.

Show that number of primes is infinite.

If possible, let the number of prime numbers be finite and equal to n. Now according to the order of increasing magnitude. Let the prime number be  P1, P2, P3,...........Pn Let K=P1.P2.P3...........Pn Now, none of the P's is a division of (K+1) Therefore,(K+1) is either a prime  > Pn or has a prime (>Pn) as a divisor. But this contradicts our assumption, namely Pn is the greatest prime number. Hence numbers of primes are infinite.

If gcd(a,b)=1 ,prove that gcd (a^2,b^2)=1.

Let :          au+bv=1          au=1-bv ............................(i) Squaring both sides of equation (i) : we get,       (au)^2=(1-bv)^2 ........................(ii)       a^2(u^2) + b(2v-bv^2)= 1   so,         gcd(a^2,b)=1 =>  (a^2)K1 + bK2 =1 =>  (bK2)^2=(1-a^2K1)^2 =>  b^2{(K2)^2}=1+a^4(K1)^2 -2(a^2)(K1) =>  b^2(K2)^2 +a^2(2K1-(a^2)(K1)^2)=1 =>  gcd(a^2,b^2)=1  {Proved}

Prove that the product of any m consecutive integers is divisible by m.

Let all natural numbers be grouped as : {1,2,3,.............m-1,m},{m+1,m+2,m+3,................2m},{2m+1,2m+2,2m+3,...................3m},{3m+1,3m+2,............3m} If the sequence of m consecutive integers begin with 1,evidently the product contains m as a factor and hence is divisible by m. Every other string of m consecutive integers starting with 2 or 3 etc. up to m contains m as a factor and hence is divisible by m. If the sequence of m consecutive integers starting with 2m+1 or 2m+2 upto 2m contains 2m as a factor and hence is divisible by m. The argument is similar for every other strings of m consecutive integers. Hence,this proves that the product of any m consecutive integers is divisible by m.