C Programming – HackerRank Solution | Traffic Signals |

(HackerRank) Write a Modular C Programming code to solve QUEUES – Traffic Signals, The experiment ends when either Ptolem or Neil or both stacks are empty.

Trump has been stuck in traffic in the streets of Mexico due to the malfunctioning of the traffic signal. Trump has a professional hacker with him travelling. Hacker can get into the traffic system and change the colour of the lights. In his country, Trump had different traffic rule such that there are N bulbs on the traffic board and only when all of them are green (G) the cars can pass. there are 2 other colours which the bulbs show: i.e. Red (R) & Yellow (Y). Note that the lights are designed such that they follow the colour change in cyclic pattern as follows:

Y—–>G—–>R—–>Y

Trump ordered his co-passenger, Hacker, to get into the system, he can select any position of light and update start updating the lights by increasing their colour by 1 next colour of lights as given in above sequence. This whole process takes 1 second for changing one light and he can repeat this process any number of times until he gets all the lights Green. Find the minimum time to do the process as Trump was getting late for Video Conference with Narendra.

Explanation: Consider 3 lights on a Signal: R Y G.

During first check, R updates to Y, next colour Y updates to G, and last colour does not update. Colour changes = 2.

Now lights on the signal are: Y G G.

During the second check, Y updates to G. Next colour G does not update. Similarly for the 3rd light in the signal, does not update. Therefore Colour changes = 1. Now lights on signal are: G G G.

No further updates in the colour of the lights on the signal.

Therefore minimum colour changes = 3.

Input Format

First Line N indicates number of lights on the signal.

Second Line indicates colours of N lights. single character, seperated by space.

Constraints

N value is integer between 0 to 100.

Color of each light is single character ( R, G or Y).

Output Format

Minimum colour changes to make all lights Green (G).

Sample Input 0

4
R Y G R

Sample Output 0

5

Sample Input 1

6
R R R R R R

Sample Output 1

12

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define SI 10
struct queue
{
    char data[SI];
    int front,rear;
};
void enqueue(struct queue *qptr,char num)
{
    if(qptr->rear==-1&&qptr->front==-1)
    {
        qptr->front=qptr->rear=0;
        qptr->data[qptr->rear]=num;
    }
    else if((qptr->rear+1)%SI==qptr->front)
    {
        printf("Queue Overflow\n");
    }
    else
    {
        qptr->rear=(qptr->rear+1)%SI;
        qptr->data[qptr->rear]=num;
    }
}
int dequeue(struct queue *qptr)
{
    char num='0';
    if(qptr->front==-1&&qptr->rear==-1)
    {
        return num;
    }
    else if(qptr->front==qptr->rear)
    {
        num=qptr->data[qptr->front];
        qptr->front=qptr->rear=-1;
    }
    else
    {
        num=qptr->data[qptr->front];
        qptr->front=(qptr->front+1)%SI;
    }
    return num;
}
void check(struct queue *qptr)
{
    char c1;
    int count=0;
    while(qptr->front!=qptr->rear||(qptr->rear==0&&qptr->front==0))
    {
        c1=dequeue(qptr);
        if(c1=='R')
        {
            enqueue(qptr,'Y');
            count++;
        }
        else if(c1=='Y')
        {
            enqueue(qptr,'G');
            count++;
        } 
    }
    printf("%d",count);
}
int main() 
{
    struct queue * qptr;
    struct queue q;
    qptr=&q;
    qptr->front=qptr->rear=-1;
    int n,ch,i;
    scanf("%d",&n);
    if(n>0&&n<100)
    {
        for(i=0;i<n;i++)
    {
        scanf(" %lc ",&ch);
        enqueue(qptr,ch);
    }
    check(qptr);
    }
    return 0;
}


OUTPUT

Congratulations, you passed the sample test case.
Click the Submit Code button to run your code against all the test cases.

Input (stdin)

4
R Y G R
Your Output (stdout)

5
Expected Output

5

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!