Programming 1

Qn - 1

You are an adventurer. You have found N dungeons. Some are open, and others are closed. All dungeons contain a valuable item that will be useful in the future. You should always start from the left and go to the right. Once you retrieve the item from a dungeon and come out, the dungeons that were closed will be opened, and the ones that were open will be closed. So your task is to find the number of valuable items that you can retrieve from these dungeons.

To represent the state of the dungeon, 

0 - Closed dungeon

1 - Opened dungeon


Input

N - number of dungeons

l - list of dungeons and their states


Output

Return the number of valuable items retrieved from the dungeons

Sample Input 1:

5

1, 1, 0, 0, 0, 1, 0

Sample Output 1:

4

Sample Input 2:

3

1, 1, 1

Sample Output 2:

1


Note: Solve this question using Python.

Qn - 2

Input: 

n number of phone numbers 

p phone numbers


Objective:

Given n number of phone numbers from a city your goal is to find the length of the city phone code within the given phone numbers. The city phone code is present at the prefix (the beginning) of each phone number, it can go up to length n//2. If the length of phone numbers doesn't match or if there is no similar pattern in the given set of phone numbers print -1.

The number of test cases will be the first line of the input, followed by n  for each test case.


Sample input:

3

3

00209

00219

00999

4

7334234546253434

7334234862544242

7334234336415424

7334234363654132

3

637676232

637223

63721244241


Output:

2

7

-1


Note: Solve this question using Python.

Qn - 3

Eminem and Rihanna are in a binary forest (a forest filled with 1s and 0s only).


If a tree is 0, you can see through it. You cannot see through trees that are 1. Eminem and Rihanna both start arguing over whose eyesight is better, and who can see farther. Rihanna claims she can see further, whereas Eminem knows for a fact that he can actually see farther. 


They come to a truce - they will play a game. Starting from outside the forest initially (even if the first tree is 1), both of them will stand at each "1" tree, and note down the farthest point to which they can see (they stop looking when they see the next "1" tree). At the end of all of them, both of them will state the farthest point to which they can see, and both of them have agreed to be honest. One of them is definitely correct. 


Your task is to verify which one of them is correct. Find out the actual farthest distance and verify which of the artists is correct!


Input format: 

t -> number of test cases

n -> number of trees in the forest 

n integers -> the value of each tree 


Example:

Input:

5

5

1 0 0 1 0

4

0 1 1 1

1

0

3

1 1 1

9

1 0 0 0 1 0 0 0 1


Output:

2

1

1

0

3


Note: Solve this question using Python.

Qn - 4

We have invented our own sequence of numbers. The sequence is generated by calculating the XOR value of the previous 2 numbers in the sequence. 


Input:

t

x y

n


Where x and y are the first 2 numbers in the sequence, and you need to return the nth number from the sequence (index-wise). 


Sample input:

2

3 8 

4

43 2

10943


Sample output:

11

41


Note: Solve this question using C.

Qn - 5

You and your friend are playing a game with magic leaves. There are 2 types of leaves in the big pile (think of it as a deck/stack of leaves, where only the top leaf is accessible at any given time). 


The 2 types of leaves are: 

    - Golden leaf -> the magic of this leaf is 0 

    - Silver leaf -> the magic of this leaf is always greater than 0


You have a separate bucket where you collect leaves from this pile. 

You can do the following with this pile of leaves: 

    - take a leaf from the top of the pile 

    - if this leaf is golden, you can put it on the top of your bucket pile, or discard it

    - if this leaf is silver, then the magic of the top leaf in your bucket is added to its magic (if it is not empty), after that the leaf is added to your bucket, and the used silver leaf is discarded


Your objective is to use these moves to gather a bucket of leaves with the maximum possible magic. 


Input format:

t -> number of test cases 

n -> number of leaves in the pile 

n integers, containing the magic of each leaf in top-down order 


Example:

Input:

5

5

3 3 3 0 0

6

0 3 3 0 0 3

7

1 2 3 0 4 5 0

7

1 2 5 0 4 3 0

5

3 1 0 0 4


Output:

6

6

8

9

4


Note: Solve this question using C/C++.