Sunday, 29 October 2017

Book Exercises Code Chef Solution in C

Harry is a bright student. To prepare thoroughly for exams, he completes all the exercises in his book! Now that the exams are approaching fast, he is doing book exercises day and night. He writes down and keeps updating the remaining number of exercises on the back cover of each book.
Harry has a lot of books messed on the floor. Therefore, he wants to pile up the books that still have some remaining exercises into a single pile. He will grab the books one-by-one and add the books that still have remaining exercises to the top of the pile.
Whenever he wants to do a book exercise, he will pick the book with the minimum number of remaining exercises from the pile. In order to pick the book, he has to remove all the books above it. Therefore, if there are more than one books with the minimum number of remaining exercises, he will take the one which requires the least number of books to remove. The removed books are returned to the messy floor. After he picks the book, he will do all the remaining exercises and trash the book.
Since number of books is rather large, he needs your help to tell him the number of books he must remove, for picking the book with the minimum number of exercises.
Note that more than one book can have the same name.

Input

The first line contains a single integer N denoting the number of actions. Then N lines follow. Each line starts with an integer. If the integer is -1, that means Harry wants to do a book exercise. Otherwise, the integer is number of the remaining exercises in the book he grabs next. This is followed by a string denoting the name of the book.

Output

For each -1 in the input, output a single line containing the number of books Harry must remove, followed by the name of the book that Harry must pick.

Constraints

1 < N ≤ 1,000,000
0 ≤ (the number of remaining exercises of each book) < 100,000
The name of each book consists of between 1 and 15 characters 'a' - 'z'.
Whenever he wants to do a book exercise, there is at least one book in the pile.

Example

Input:
6
9 english
6 mathematics
8 geography
-1
3 graphics
-1

Output:
1 mathematics
0 graphics

Solution:


#include <stdio.h>
struct be
{
                int a[100];
                char s[100][16];
};
struct be bre;
int main()
{
                int N, k=0,min,i,j;
                scanf("%d",&N);
                while(N--)
                {
                                scanf("%d",&bre.a[k]);
                                if(bre.a[k] != -1)
                                {                             
                                                scanf("%s",&bre.s[k]);
                                                k++;
                                }
                                else
                                {
                                               /* Finding the book with minimum no. of remaining exercises*/
                                                min = bre.a[0];
                                                for(i=1;i<k;i++)
                                                                if(min > bre.a[i])
                                                                                min = bre.a[i];
                                                /* Number of books to remove*/
                                                 for(i=0;i<k;i++)
                                                {
                                                                if(min == bre.a[i])
                                                                {
                                                                                printf("%d  %s", i,bre.s[i]);
                                                                                /* delete the completed book*/
                                                                                for(j=i;j<k-1;j++)
                                                                                {
                                                                                bre.a[j]=bre.a[j+1];
                                                                                strcpy(bre.s[j],bre.s[j+1]);
                                                                                }
                                                                }
                                                }
                                                k--;
                                }

                }
return 0;

}



Saturday, 28 October 2017

Maximum element in stack hacker rank solution in C


 Problem:
https://www.hackerrank.com/challenges/maximum-element/problem

Solution

#include <stdio.h>
#include <stdlib.h>
int data[100000],top,max=0;
void push()
{
   int item;
   scanf("%d",&item);
   top++;
   data[top]=item;
   if(max < data[top])
        max = data[top];
}

void pop()
{
    int i;
    if(max == data[top])
        max=0;
    top--;
    for(i= top;i>=0;i--)
        if(max < data[i])
            max = data[i];
}
int main()
{
    int t,n,choice;
    top = -1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&choice);
        switch(choice)
        {
            case 1 : push();
                     break;
            case 2:  pop();
                     break;
            case 3: printf("%d\n",max);
                    break;
        }
    }
    return 0;

}

===============(or)  using structure=======================
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

struct stack
{
    int data[100000];
    int top;
}st;
int max=0;
void push()
{
    int item;
    scanf("%d",&item);
    st.top++;
    st.data[st.top]=item;
    if(max<st.data[st.top])
        max=st.data[st.top];
   
}
void pop()
{
    int i;
    if(max == st.data[st.top])
        max=0;
    st.top--;
     for(i=st.top;i>=0;i--)
        if(max<st.data[i])
            max=st.data[i];
}

int main()
{
    int t,n,choice;
    st.top = -1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&choice);
        switch(choice)
        {
            case 1 :push();
            break;
            case 2:pop();
            break;
            case 3: printf("%d\n",max);
            break;
        }
    }
    return 0;
}




Queue using Two stacks Hacker rank solution in c and Questions on Queue

Questions on Queue:
https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/passing-the-parcel/

Problem:
https://www.hackerrank.com/challenges/queue-using-two-stacks/problem

Solution:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct stack
{
    int data[100000];
    int top;
};
struct stack st,s1;

void enq()
{
    int item;
    scanf("%d",&item);
    st.data[++st.top]=item;
}
void deq()
{
    int del;
    while(st.top>=0)
    {
        del = st.data[st.top];
        st.top--;
        s1.top++;
        s1.data[s1.top]=del;
    }
    s1.top--;
    while(s1.top>=0)
    {
        del = s1.data[s1.top];
        s1.top--;
        st.top++;
        st.data[st.top]=del;
     }
}
int main() {

st.top=-1;
s1.top=-1;
int t,c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&c);
        switch(c)
        {
            case 1: enq();
                    break;
            case 2: deq();
                    break;
            case 3: printf("%d\n",st.data[0]);
                    break;
        }
    }
    return 0;
}


Wednesday, 25 October 2017

Pangrams Hacker rank solution in C


 #include <stdio.h>
int main()
{
               char st[1000],alpha;
               int i,flag;
               scanf("%[^\n]",st);
               for(alpha='a'; alpha <= 'z';alpha++)
               {
                              flag=0;
                              for(i=0;st[i]!='\0';i++)
                              {
                                             if(st[i]== alpha || st[i]== alpha-32)//in entire string if ‘a’ is present
                                             {
                                                            flag=1;
                                                            break;
                                             }
                              }
                              if(flag == 0) //in entire string ‘a’ is not present
                                             break;
               }
               if(flag == 0)
                              printf("not pangram");
               else
                              printf("pangram");
    return 0;
}

Output:

Single Linked List Complete Program in C



Algorithm to insert a node at the beginning of single linked list:
Let head be a pointer that stores the address of first node
step 1: Read the value to inserted into linked list
step 2: create a new node using malloc function
step 3: Assign the value read to the data field of newnode (newnode -> data =value)
step 4: set the next of newnode to head (newnode ->next = head)
step 5: set the head pointer to newnode (head = newnode)




Algorithm to insert a node at the end of single linked list:
Let head be a pointer that stores the address of first node
step 1: Read the value to inserted into linked list
step 2: create a new node using malloc function
step 3: Assign the value read to the data field of newnode
step 4: if there is no linked list then newnode becomes first node
step 5: otherwise go to the lastnode in linked list
                                        Step 5.1: attach newnode to last node (lastnode ->next = newnode)
                                        Step 5.2: newnode->next = NULL
 





Algorithm to insert a node at a given position of single linked list:
Let head be a pointer that stores the address of first node
step 1: Read the value to inserted into linked list and the position at which newnode has to be inserted
step 2: create a new node using malloc function
step 3: Assign the value read to the data field of newnode
step 4: store the node before the given position in a pointer c
step 5: newnode->next = c->next
step 6: c->next = newnode
  


 


Algorithm to delete a node at the beginning of single linked list:
Let head be a pointer that stores the address of first node
step 1: If there is no linked list then print Linked list is empty
step 2: If linked list exists
                                        step 2.1: delete the first node
                                        step 2.2: second node becomes first node 


Algorithm to delete a node at the end of single linked list:
Let head be a pointer that stores the address of first node
step 1: If there is no linked list then print Linked list is empty
step 2: If linked list exists
                    step 2.1: move to last but one node and store the address of last but one node in pointer c
                    step 2.2: store the address of last node in pointer p and detele the last node (free(p))
                    step 2.3: c->next = NULL



Algorithm to delete a node at given position of single linked list:
Let head be a pointer that stores the address of first node
step 1: If there is no linked list then print Linked list is empty
step 2: If linked list exists
                                        step 2.1: Read the position at which the node has to deleted
                                        step 2.2: Store the address of node before the to be deleted node in pointer c
                                        step 2.3: c->next = c->next->next
                             
step 2.4: delete the node at given position






Algorithm to display the elements of single linked list
Let head be a pointer that stores the address of first node
step 1: If there is no linked list then print Linked list is empty
step 2: If linked list exists store the address of first node in pointer c ( c = head)
                step 2.1: print the data field in c (print c->data)
                step 2.2 move c to store the address of next node ( c =  c-> next)
step 3: go to step 2 until c is not equal to NULL

Program


#include <stdio.h>
#include <stdlib.h>
struct node
{
               int data;
               struct node *next;
};
struct node *head =NULL,*c,*p;
create()
{
               int value;
               printf("enter the value ");
               scanf("%d",&value);
               struct node *new = (struct node *)malloc(sizeof(struct node));
               new->data=value;
               new->next=NULL;
               if(head == NULL)
               {
                              head=new;
               }
               else
               {
                              c=head;
                              while(c->next != NULL)
                              {
                                             c=c->next;
                              }
                              c->next=new;                   
               }
}

insertbeg(int value)
{
               struct node *new = (struct node *)malloc(sizeof(struct node));
               new->data=value;
              
               new->next=head;
               head = new;
}
insertend(int value)
{
               struct node *new = (struct node *)malloc(sizeof(struct node));
               new->data=value;
               c=head;
               while(c->next != NULL)
               {
                              c=c->next;
               }
              
               c->next=new;
               new->next=NULL;
}
insertmiddle(int value)    
{
               int pos;
               printf("enter the position at which new node has to be inserted");
               scanf("%d",&pos);
               struct node *new = (struct node *)malloc(sizeof(struct node));
               new->data=value;
               c=head;
               int i=1;
               while(i<pos)
               {
                              p=c;
                              c=c->next;
                              i++;
               }
               new->next=c;
               p->next=new;
}

deletebeg()
{
               p=head;
               head=head->next;
               free(p);
}

deletemiddle()
{
    int pos, i=1;
               printf("enter the position of the node that has to be deleted");
               scanf("%d",&pos);
               c=head;
               while(i<pos)
               {
                              p=c;
                              c=c->next;
                              i++;
               }
               p->next=c->next;
               free(c);
}

deleteend()
{
               c=head;
               while(c->next != NULL)
               {
                              p=c;
                              c=c->next;
               }
               free(p->next);
               p->next=NULL;
}
display()
{
               if(head == NULL)
                              printf("list is empty");
               else
               {
                              c=head;
                              while(c->next !=NULL)
                              {
                                             printf("%d -> ",c->data);
                                             c=c->next;
                              }
                              printf("%d\n",c->data);
               }
}

main()
{
               int choice,option,value;
               while(1)
               {
                              printf("Press 1. Create\n 2. Insert \n 3. Delete  \n 4. Display\n 5. Exit \n");
                              printf("enter ur choice");
                              scanf("%d",&choice);
                              switch(choice)
                              {
                                             case 1: create();
                                                                           break;
                                             case 2:
                                               if(head != NULL)            
                                               {
                                                                           option=0;
                                                                           while(option != 4)
                                                                           {
                                                                                                        
                                                                                          printf("Press 1. insert as first node  \n 2. insert as middle node  \n 3. insert as last node \n 4. go to main menu\n");
                                                                                          printf("enter ur choice\n");
                                                                                          scanf("%d",&option);
                                                                                          if(option != 4)
                                                                                          {
                                                                                                         printf("enter the value ");
                                                                                                         scanf("%d",&value);
                                                                                          }
                                                                                          switch(option)
                                                                                          {
                                                                                         
                                                                                                         case 1: insertbeg(value);
                                                                                                                                       display();
                                                                                                                                       break;
                                                                                                         case 2: insertmiddle(value);
                                                                                                                                       display();
                                                                                                                                       break;
                                                                                                         case 3: insertend(value);
                                                                                                                                       display();
                                                                                                                                       break;
                                                                                                         case 4:  break;
                                                                                                         default: printf("invalid choice\n");
                                                                                          }
                                                                           }
                                             }
                                             else
                                                            printf("list is empty");
                                             break;
                                             case 3:
                                                                           option=0;
                                                                           if(head != NULL)
                                                                           {
                                                                                          while(option != 4)
                                                                                          {
                                                                          
                                                                                                         printf("Press 1. delete first node  \n 2. delete middle node  \n 3. Delete last node \n 4. go to main menu\n");
                                                                                                         printf("enter ur choice\n");
                                                                                                         scanf("%d",&option);
                                                                                                         switch(option)
                                                                                                         {
                                                                                         
                                                                                                                        case 1: deletebeg();
                                                                                                                                                      display();
                                                                                                                                                      break;
                                                                                                                        case 2: deletemiddle();
                                                                                                                                                      display();
                                                                                                                                                      break;
                                                                                                                        case 3: deleteend();
                                                                                                                                                      display();
                                                                                                                                                      break;
                                                                                                                        case 4:  break;
                                                                                                                        default: printf("invalid choice\n");
                                                                                                         }
                                                                                          }
                                                                           }
                                                                           else
                                                                                          printf("list is empty and cannot be deleted");
                                                                                          break;
                                             case 4: display();
                                                                           break;
                                             case 5: exit(0);
                                             default: printf("invalid choice");
                              }
                             
               }
}