# C Programming – HackerRank Solution | Asteroid Game |

### (HackerRank) Write a Modular C Programming code to solve STACKS – Asteroid Game, The experiment ends when either Ptolem or Neil or both stacks are empty.

Ptolem and Neil making experiments about the asteroids collision. Each asteroid will be having Size with a sign representing its direction (positive meaning right, negative meaning left). Both of them are provided with the stack of N aseroids. Ptolem and Neil pick asteroids Size from their stacks. Conditions about collision:

a. If both asteroids Size are Positive or Negative then they will move in same direction (non-collision), then output is 0. Both the asteroids Size are removed from their stack.

b. If both asteroids Size have different signs then they will collide and output will be 1. Both the asteroids Size are removed from their stack.

c. If the asteriods Size is 0, it is neither positive nor negative. Hence only 0 will be removed from their stack.

NOTE: Size – an integer indicating the Size of asteroid with Positive or Negative signs.

The experiment ends when either Ptolem or Neil or both stacks are empty.

Input Format

First line indicates N total number of asteroids i.e size of the stacks S1 and S2.

Second line indicates Ptolem stack S1, contains Size of all the N asteroids separated by space.

Third line indicates Neil stack S2, contains Size of all the N asteroids separated by space.

Constraints

1 <= N <= 100

Output Format

First line indicates status of all the N collisions separated by space.

Second line indicates Count of all collisions.

Third line indicates Count of all non-collisions.

Sample Input 0

```5
3 -5 6 7 -2
-1 -5 9 0 -4
```

Sample Output 0

```0 0 1 0
1
3
```

Sample Input 1

```-7
```

Sample Output 1

```Invalid input.
```

Sample Input 2

```5
1 2 -1 -2 -3
3 4 -5 -6 7
```

Sample Output 2

```1 0 0 0 0
1
4```

CODE:

```#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define SIZE 100
struct stack
{
int data[SIZE];
int top;
};
void push(struct stack *sptr)
{
int num;
scanf("%d",&num);
if(sptr->top==SIZE-1)
{
printf("Stack Overflow\n");
}
else
{
if(num!=0)
{
sptr->top++;
sptr->data[sptr->top]=num;
}
}
}
int pop(struct stack *sptr)
{
int num;
if(sptr->top==-1)
{
printf("Stack underflow\n");
}
else
{
num=sptr->data[sptr->top];
sptr->top--;
}
return num;
}
void asteriod(struct stack * s1ptr,struct stack * s2ptr)
{
int c1=0,c2=0,num1,num2;
while(s1ptr->top!=-1&&s2ptr->top!=-1)
{
num1=pop(s1ptr);
num2=pop(s2ptr);
if(num1>0&&num2>0)
{
printf("0 ");
c1++;
}
else if(num1<0&&num2<0)
{
printf("0 ");
c1++; }
else
{
printf("1 ");
c2++;
}
}
printf("\n%d",c2);
printf("\n%d",c1);
}
int main() {
struct stack * s1ptr;
struct stack * s2ptr;
struct stack s2,s1;
s1ptr=&s1;
s2ptr=&s2;
s1ptr->top=-1;
s2ptr->top=-1;
int n;
scanf("%d",&n);
if(n<1)
{
printf("Invalid input.");
exit(0);
}
for(int i=0;i<n;i++)
{
push(s1ptr);
}
for(int i=0;i<n;i++)
{
push(s2ptr);
}
asteriod(s1ptr,s2ptr);
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)

5
3 -5 6 7 -2
-1 -5 9 0 -4

0 0 1 0
1
3
Expected Output

0 0 1 0
1
3```

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 