## UVA Problem 12541 ( Birthdates ) Solution

UVA Problem 12541 ( Birthdates ) Solution:

Solving Technique:

Rank 363, Run time 9 ms ( 0.009 s ).

This problem seems easy at first glance but requires some thinking. There will be only two output.

First one will be the name of the youngest person ( The person whose was born after everyone ). Meaning his/her birth year, month, date is greater than others. Ex, a person born on 1991 is younger than person born on 1990.

Second one will be the name of the oldest person ( The person whose was born before everyone ). Meaning his/her birth year, month, date is less than others. Ex, a person born on 1990 is older than person born on 1991.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.

Code Explanation:

Since the name doesn’t exceed 15 letter so an array of size 16 ( i use 1 more for safety ) will suffice. Next there are 4 inputs each time. One is name string, the rest are integers such as date, month and year respectively.

Since the inputs are all related I used a struct to keep all data in a structure named person.

Now I use two more struct variable named young and old. By default I set both of them to first value of our structure. Now I compare if year is greater I set young to that struct variable in that loop. If less than I set old to the struct variable in that loop. If both of these are not true then I follow the same logic for month and date.

In the and the young and old struct variable get set to youngest and oldest struct variable based on logic.

Since I only need to print the youngest and oldest person name so, I just access our young and old structure with name property using dot operator and print them each on a new line.

Input:

```10
Machel 31 12 4999
Amar 27 9 1991
Kara 18 9 5001
Tom 29 1 1991
Priyanka 1 12 5001
Melissa 25 10 1991
Ping 16 10 5001
Amidala 10 1 1991
Xena 17 10 5001```

Output:

```Priyanka
Amidala```

Code:

```/*
* Author: Quickgrid ( Asif Ahmed )
* Site: https://quickgrid.wordpress.com
* Problem: UVA 12541 ( Birth dates  )
*/

#include<stdio.h>

struct person{
char name[16];
unsigned int date, month, year;
};

int main(){
register unsigned int n, i = 0;

scanf("%u", &n);

struct person p[n], maxp, minp;

for(; i<n; ++i){
scanf("%s%u%u%u", &p[i].name, &p[i].date, &p[i].month, &p[i].year);
}

maxp = p[0];    /*younger person*/
minp = p[0];    /*older person*/

for(i=0; i<n; ++i){
if(p[i].year > maxp.year)
maxp = p[i];

else if(p[i].year < minp.year)
minp = p[i];

else{
if(p[i].month > maxp.month && p[i].year >= maxp.year)
maxp = p[i];

else if(p[i].month < minp.month && p[i].year <= minp.year)
minp = p[i];

else{
if(p[i].date > maxp.date && p[i].month >= maxp.month && p[i].year >= maxp.year)
maxp = p[i];
else if(p[i].date < minp.date && p[i].month <= minp.month && p[i].year <= minp.year)
minp = p[i];
}
}

}

printf("%s\n%s\n", maxp.name, minp.name);

return 0;
}
```

## UVA Problem 12289 ( One Two Three ) Solution

UVA Problem 12289 ( One Two Three ) Solution:

Solving Technique:

Very very easy problem. It requires us to only print 1 or 2 or 3 based on input.

There are only 3 types of string (one, two, three) input and among them only one character is changed for each input. Also Their length is always the same.

So the very basic idea that comes to mind is any input with length of five must be three ( since length doesn’t change ). Now if that is not true it can either be one or two.

Code Explanation:

I took a different approach for my code. Instead of including another header file and calling a function to get the string length, I simply checked if the third position in the array ( array starts from zero ) is not a NULL character. If there is no NULL character in that position then I simply outputted 3. Why? because the string three does not contain a NULL character in position 3.

Now above is not true then I check if the string is one or two. If i only check for sub-strings ( part of strings ) in one and find any two characters of ‘o’, ‘n’, ‘e’ then it is definitely one. Then print 1. Else simply print 2.

Input:

```3
owe
too
theee```

Output:

```1
2
3```

Code:

```/**
* @author  Quickgrid ( Asif Ahmed )
*/

#include<stdio.h>

#define O 111
#define N 110
#define E 101

static char s[8];

int main(){
register unsigned n;
scanf("%u", &n);

while (n--){
scanf("%s", s);
if (s[3]){
printf("3\n");
}else{
unsigned s0 = s[0];
unsigned s1 = s[1];
unsigned s2 = s[2];
if ((s0 == O && s1 == N) || (s1 == N && s2 == E) || (s0 == O && s2 == E))
printf("1\n");
else
printf("2\n");
}
}
return 0;
}
```

## UVA Problem 591 ( Box of Bricks ) Solution

UVA Problem 591 ( Box of Bricks ) Solution:

Solving Technique:

If we read this problem carefully then there are a some very important clues,

1. make all stacks the same height (line 2)
2. he sets out to rearrange the bricks, one by one (line 3)
3. The total number of bricks will be divisible by the number of stacks (line 7)

From the above points and problems description it is clear that Little Bob can only move the blocks one by one. Also the first line of input will be less or equal to 50 and the block heights will be less than or equal to 100. So we can use integer ( also unsigned integer ) for them.

Another thing it is said that dividing sum of stack heights with stack count is always possible. Meaning that gives us the height of the wall ( total stacks height sum, h[i] / stack count, n ).

The technique is there can be three types of box stack. That is equal to, less than or greater than height of the wall( total stacks height sum, h[i] / stack count, n ).

Since Bob can move boxes one by one it doesn’t matter which stack to start from. We can find the number of blocks to move from by only subtracting those stack that are greater in height than the max wall height. No need to perform this action on stacks that are equal or less high than max wall height.

So the result is the sum of subtraction of each stacks that are greater than from ( all stack sum / stack count ). Also since blocks are moved one by one this is the final result.

Important:   We normally print a new line after each output line but for this problem asks us to Output a blank line after each set. Meaning with our normal line break we need to add another line break. Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.

Code Explanation:

1. First I take the stack count and loop that many times. [ also reset moves, and c ( count ) variable each time ]
2. Now for each loop I take the height of stack in an array and sum it.
3. Next I find the wall height by dividing the sum with stack count.
4. Now I use another loop to loop through the array. In it subtract elements greater than stack height and also sum up this value to moves ( number of box movement needed ).
5. Lastly I print in the exact format the problem asked.
6. One more thing I printed an extra newline because the problem asked for a blank line after each set.

Input:

```6
5 2 4 1 7 5
0```

Output:

```Set #1
The minimum number of moves is 5.```

### Code:

```/*
* @author  Quickgrid ( Asif Ahmed )
*/

#include<stdio.h>

static int a[128];

int main(){
register int i, c = 1;
int n, sum, maxHeight, moves, temp;

while (scanf("%d", &n) && n){
moves = sum = 0;

for (i = 0; i < n; ++i){
scanf("%d", &a[i]);
sum += a[i];
}

maxHeight = sum / n;

for (i = 0; i < n; ++i){
if(a[i] > maxHeight)
moves += a[i] - maxHeight;
}

printf("Set #%d\nThe minimum number of moves is %d.\n\n", c++, moves);
}
return 0;
}
```

## UVA Problem 10324 ( Zeros and Ones ) Solution

UVA Problem 10324 ( Zeros and Ones ) Solution:

Solving Technique:

Here I have provided naive solution for this problem which is very slow. This code can be solved much much faster. I’ll update to it later.

This One was painful. Got Runtime Error a few times before solving this. Still my best time for this so far is 1.899 sec. My main problem was that it said the problem could end with both an EOF or a new line character. The main problem was new line. So I used getchar() ( found it here one of the best sites for contest programming ) and checked for EOF and string length in the string input while condition.

The problem is quite descriptive. It says our string or character array size can be 1000000The string will contain only ‘0’ and ‘1’ characters. Basically we are to find if the characters between the given interval min(i,j)  and max(i,j)Meaning check for the characters from the minimum number between i or j to the maximum number of i or j.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.

Code Explanation:

Here the input format is first take a string or character array ( of size 1000000 ) and next take an integer which is the number if inputs to come. Now loop that many times and take two integers ( i and j ) every loop.

I used a flag named works which is set to 1 by default and use this to check condition if the characters match or not.

Now i loop thorough the given interval ( i and jto check if the character in the given interval are same.

In the loop i check every character of the interval string with the first character of that given interval. Since all character need to be same to output is this code is valid.

Now in the loop if any character doesn’t match I set the flag ( works ) to 0, print No since all characters don’t match and break from loop immediately.

Next thing I check if flag is 1 then I print Yes.

Lastly the getchar()

Input:

```0000011111
3
0 5
4 2
5 9
01010101010101010101010101111111111111111111111111111111111110000000000000000
5
4 4
25 60
1 3
62 76
24 62
1
1
0 0```

Output:

```Case 1:
No
Yes
Yes
Case 2:
Yes
Yes
No
Yes
No
Case 3:
Yes```

Code Naive Solution ( Slow ):

```/**
* Author:  Quickgrid ( Asif Ahmed )
* Site:    https://quickgrid.wordpress.com
* Problem: UVA 10324 - Zeros and Ones
*/

#include<stdio.h>
#include<string.h>

#define N 1000000

static char s[N];

int main(){

int i;
int works, c = 1;
int n, a, b;

while(gets(s)){

scanf("%d", &n);
printf("Case %d:\n", c++);

while( n-- ){

works = 1;

scanf("%d%d",&a,&b);

if(a == b){
printf("Yes\n");
continue;
}

if(a > b){
for(i = b; i <= a; ++i){
if(s[i] != s[a]){
works = 0;
printf("No\n");
break;
}
}
}else{
for(i = a; i <= b; ++i){
if(s[i] != s[a]){
works = 0;
printf("No\n");
break;
}
}
}

if(works)
printf("Yes\n");

}
getchar();
}
return 0;
}
```

## UVA Problem 11192 ( Group Reverse ) Solution

UVA Problem 11192 ( Group Reverse ) Solution:

Solving Technique:

This one is a bit tricky. Also it seems to be closely related to this ( 483 word scramble ) uva problem. Basically for this problem we are to take an integer ( which is the number of groups ) and a string until we are given a 0. Now our input is given like this,

3 ABCEHSHSH

5 FA0ETASINAHGRI0NATWON0QA0NARI0

0

So, the first problem for beginner is taking input for this. From the input we can easily see that there are two parts and the first part is an integer and second part is a string. Also we need to keep taking input until we find 0. What if we divide our scanf or input taking into two parts. First take the integer and check if its 0 or not then if it’s not zero then take the string and continue like this.

Our first input is an integer which is group number or number of parts the string can be divided to. So, from the given problem statement,

TOBENUMBERONEWEMEETAGAINANDAGAINUNDERBLUEICPCSKY

This string has length 48. We have divided into 8 groups of equal length and so the length of each group is 6. Meaning each group or each part of sentence will consist of 6 characters.

Our task is to reverse print/output each and every group. We can easily find how many members are in each group by dividing the length of the string with input ( number of groups ) we are given.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.

Code Explanation:

Here I have used stack data structure ( see this and this ) to solve this problem. First I find the length of the string then find how many characters in each group by dividing string length with group number. I also keep a counter ” x “ and keep pushing each character of the string in stack until I reach the last character or until our counter is not equal to the group member count. Also increment the counter each time i push into the stack. Also I keep a condition to check if  I have reach the last character or until our counter is equal to the group member count then pop until our loop counter has reached zero. Since stack is a LIFO data structures the last character i pushed into the stack are the first one to come out ( meaning they are outputted in reverse order ). This process keeps running until we reach the end of the sentence.

Input:

```3 ABCEHSHSH
5 FA0ETASINAHGRI0NATWON0QA0NARI0
0```

Output:

```CBASHEHSH
ATE0AFGHANISTAN0IRAQ0NOW0IRAN0```

#### Commented Code ( See the next Code for easier understanding of stack ):

```/**
* @author Quickgrid ( Asif Ahmed )
* @url https://quickgrid.wordpress.com
*/

#include<stdio.h>

/**
* Input string buffer
*/
static char s[128];

/**
* Global index for accessing input array, may be declared inside main
*/
int top = -1;

int main(){
register unsigned j,u,n,i,x;

/**
* Input group count and Check if count is Not Zero
*/
while (scanf("%u", &n) && n) {
scanf("%s", s);

/**
* Find out input string size
*/
for (j = 0; s[j]; ++j);

/**
* Find out length of each group
*/
n = j / n;

/**
* Group Reverse loop
*/
for (x = i = 0; i < j; ++i) {
/**
* For checking is the item after is NUL terminator
*/
u = s[i + 1];

/**
* @var u
* If the character after current is NUL then NOT u, is True
*
* @var n
* Check to see if characters in group count is NOT full, then push more to stack
*/
if ( !u || x != n ) {
/**
* Push current character on top of stack
*/
s[++top] = s[i];
/**
* Keep a count of characters pushed on stack
*/
++x;
}

/**
* If character limit per group is reached then Condition is True
*/
if ( !u || x == n ) {
/**
* Empty the the stack for every pushed character
*/
while (x--){
/**
* Output character by character in reversed form
*/
printf("%c", s[top]);
--top;
}
x = 0;
}

}
printf("\n");
}
return 0;
}
```

#### Code:

```/*
* Author: Quickgrid ( Asif Ahmed )
* Site: https://quickgrid.wordpress.com
*/

#include<stdio.h>

char s[101];
int top = -1;

void push(char c){
if(top + 1 >= 101)
return;

s[++top] = c;
}

void pop(){
if (top == -1)
return;

printf("%c", s[top]);
s[top--] = '\0';
}

int main(){
int n,j,i,x;
while (scanf("%d", &n) == 1 && n != 0){
x = 0;
scanf("%s", &s);
for (i = 0; s[i]; ++i);
n = i / n;

for (i = 0; s[i]; ++i){
if (s[i+1] == '\0' || x != n){
push(s[i]);
++x;
}

if (s[i+1] == '\0' || x == n){
while (x--)
pop();

x = 0;
}
}

printf("\n");
}
return 0;
}
```