Time spent here:

Tuesday, 12 April 2016

MAXIMUM WIDTH OF BINARY SEARCH TREE

C IMPLEMENTATION:

#include <stdio.h>
#include <stdlib.h>
struct node
{
   int data;
   struct node* left;
   struct node* right;
};
struct node* newNode(int data)
{
    struct node* node=(struct node* )malloc(sizeof(struct node));
    node->data=data;
    node->left=NULL;
    node->right=NULL;
   
    return (node);
}
struct node* insert(struct node* node,int data)
{
    if(node==NULL)
       return newNode(data);
    if(data < node->data)
     node->left=insert(node->left,data);
    else
      node->right=insert(node->right,data);

    return(node);
}

int height(struct node* node)
{
   if (node==NULL)
     return 0;
   else
   {
     /* compute the height of each subtree */
     int lHeight = height(node->left);
     int rHeight = height(node->right);
     /* use the larger one */
   
     return (lHeight > rHeight)? (lHeight+1): (rHeight+1);
   }
}
int getWidth(struct node* root, int level)
{
    
  if(root == NULL)
    return 0;
  
  if(level == 1)
    return 1;
            
  else if (level > 1)
    return getWidth(root->left, level-1) +
             getWidth(root->right, level-1);
}
int printwidth(struct node* node)
{
    int max=0;
    int h=height(node);
    int width,i;
    for(i=1;i<=h;i++)
    {
        width=getWidth(node,i);
        if(max < width)
          max=width;
    }

    return max;
}

void inorder(struct node* node)
{
 
    if(node==NULL)
      return;
    inorder(node->left);
    printf("%d ",node->data);
     inorder(node->right);

   
}
int main()
{
    struct node* root=NULL;
    root=insert(root,5);
    insert(root,8);
      insert(root,6);
        insert(root,4);
          insert(root,1);
          insert(root,3);
    printf("\ninorder travesal\n");        
  inorder(root);
   printf("\nmaximum width of binary tree\n");
   printf("%d",printwidth(root));
    return 0;
}

OUTPUT:

print nodes at k distance from root

c implementation:

#include <stdio.h>
#include <stdlib.h>
struct node
{
   int data;
   struct node* left;
   struct node* right;
};
struct node* newNode(int data)
{
    struct node* node=(struct node* )malloc(sizeof(struct node));
    node->data=data;
    node->left=NULL;
    node->right=NULL;
   
    return (node);
}
struct node* insert(struct node* node,int data)
{
    if(node==NULL)
       return newNode(data);
    if(data < node->data)
     node->left=insert(node->left,data);
    else
      node->right=insert(node->right,data);

    return(node);
}
void printknode(struct node* node,int k)
{
    if(node==NULL)
     return;
    if(k==0)
     printf("%d ",node->data);
   else
   {     
      printknode( node->left, k-1 ) ;
      printknode( node->right, k-1 ) ;
   }    
}
void inorder(struct node* node)
{
 
    if(node==NULL)
      return;
    inorder(node->left);
    printf("%d ",node->data);
     inorder(node->right);

   
}
int main()
{
    struct node* root=NULL;
    root=insert(root,5);
    insert(root,6);
      insert(root,3);
        insert(root,4);
          insert(root,7);
    printf("\ninorder travesal\n");        
  inorder(root);
   printf("\nkth distance node from root\n");
   printknode(root,1);
    return 0;
}
output:

PAINT THE TILES(HOUR RANK 7)

PROBLEM STATEMENT:
Nikita has a line of NN tiles indexed from 00 to N−1N−1. She wants to paint them to match a color configuration, CC, which is comprised of 22 colors: Red(R)Red(R) and Blue(B)Blue(B).

In one stroke, Nikita can paint 11 or more adjacent tiles a single color. After she finishes painting, each tile ii should be painted color CiCi.

It should be noted that it is not allowed to apply more than 11 stroke on a tile.

Given the required color configuration, find and print the minimum number of strokes required for Nikita to paint all NN tiles.

Note: In a line of tiles, 22 tiles with the indices ii and jj are considered adjacent only if |j−i|=1|j−i|=1.

Input Format

The first line contains a single integer, NN, denoting the number of tiles to be painted.
The second line contains a string, CC, denoting the desired color configuration.

For each character CiCi in CC:

    If Ci="R"Ci="R", it means the ithith tile must be painted red.
    If Ci="B"Ci="B", it means the ithith tile must be painted blue.

Constraints

    1≤N≤10001≤N≤1000
    Ci∈{"R", "B"}Ci∈{"R", "B"}

Output Format

Print the minimum number of strokes required to paint all NN tiles in the desired color configuration.

CODE:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n;
    scanf("%d",&n);
    char* c = (char *)malloc(10240 * sizeof(char));
    scanf("%s",c);
    int i,count=0;
    for(i=0;c[i];i++)
    {
        if(c[i]!= c[i+1])
            count=count+1;
    }   
    printf("%d",count);
    return 0;
}
OUTPUT: