C Programming – HackerRank Solution | Save India from Aliens |

(HackerRank) Write a Modular C Programming code to solve QUEUES – Save India from Aliens, Name of the Superhero who leads the team.

During the attack of Aliens on Earth to destroy India, Kaal also plans to attack along with them. Superheroes Krrish and Shaktiman decides to save India from Kaal and Aliens. They decide that, one should lead the team. They play the following game using a sequence of N numbers to decide who will lead the team. Krrish’s favourite number is 3. He chooses the numbers from sequence which are multiple of 3, then he adds the numbers to his score. Shaktiman chooses the numbers from sequence whose square are even, then he adds the numbers to his score. Game ends when there are no more numbers left in the sequence. The Superhero with the highest score wins.

Apply Problem Solving Framework and write a modular C program to decide who leads the team to save the India from attack.

Input Format

First line indicates N numbers.

Second line indicates numbers with respect to each superhero.

Constraints

N should be positive.

Each number in an QUEUE should be positive.

Output Format

Name of the Superhero who leads the team.

Sample Input 0

3
23
56
67

Sample Output 0

Shaktiman

Sample Input 1

10
5
7
3
6
8
4
1
9
11
15

Sample Output 1

krrish

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 100
struct queue
{
    int data[SI],front,rear;
};
void enqueue(struct queue *qptr,int num)
{
    if(qptr->rear==SI-1)
    {
        printf("Queue Overflow\n");
    }
    else
    {
        qptr->rear++;
        qptr->data[qptr->rear]=num;
    }
}
int dequeue(struct queue *qptr)
{
    int num=0;
    if(qptr->front==qptr->rear)
    {
        return num;
    }
    else
    {
        qptr->front++;
        num=qptr->data[qptr->front];
        return num;
    }
}
void check(struct queue *qptr)
{
    int n,sum1=0,sum2=0;
    while(qptr->front!=qptr->rear)
    {
        n=dequeue(qptr);
        if(n%3==0)
        {
            sum1=sum1+n;
        }
        if(n%2==0)
        {
            sum2=sum2+n;
        }
    }
    if(sum1<sum2) { printf("Shaktiman"); } else { printf("krrish"); } } int main() { struct queue * qptr; struct queue q; qptr=&q; qptr->front=qptr->rear=-1;
    int num,n,i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&num);
        enqueue(qptr,num);
    }
    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)

3
23
56
67
Your Output (stdout)

Shaktiman
Expected Output

Shaktiman

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 !!!