Last ant on rod, C/C++ Programming

Assignment Help:

There are ''n'' ants on a ''n+1'' length rod. The ants are numbered from 1 to n and are initially placed at positions starting from position 1 till position n. They are moving either in left direction (denoted by ''-1'') or in the right direction (denoted by ''1''). Whenever an ant crosses the boundary of the rod it falls off the rod. You are given the initial direction of the ants. Now, whenever two ants collide their direction switches, i.e. the ant going in left direction (''-1) changes it''s direction towards right (''1'') and the ant going in the right direction (''1'') changes it''s direction towards left (''-1''). Find last ant to fall off the rod.
Note: In case two ants are falling simultaneously in the end print the index of the lower indexed ant.

Input Format:
First line contains the integer ''n'' denoting the total number of ants s.t. 1 <= n <= 1,000
Second line contains ''n'' space separated numbers (either ''1'' or ''-1'') denoting the initial directions of the ants.

Output Format:
Output a single integer which is the index (lower index in case two ants are falling simultaneously in the end) of the last ant to fall off the table.

Related Discussions:- Last ant on rod

Write a procedure to add and subtract-money, Money is immutable and is asso...

Money is immutable and is associated with a currency. When two money entities are added or subtracted be sure to consider currency conversion. You must create a money object result

Gene program, Many human diseases could be controlled by the knowledge of t...

Many human diseases could be controlled by the knowledge of the gene’s structure and pattern. The human gene could be represented by four nucleotides. Each nucleotide is represente

Applied scientific computing in c++, i have a project that is due this Wedn...

i have a project that is due this Wednesday and i was wondering if i can get help in doing it?

C program for function of count the interest , C Program for FUNCTION OF CO...

C Program for FUNCTION OF COUNT THE INTEREST float si(float,float,float); void main() {           float p=0,r=0,n=0,k=0;           clrscr();           printf("E

Boardcoloring., Smugglers are becoming very smart day by day. Now they have...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

COMPUTER, THEORY OF A COMPUTER PROGRAMMING

THEORY OF A COMPUTER PROGRAMMING

How do you access the values within an array, How do you access the values ...

How do you access the values within an array? - Arrays comprise a number of elements, which depends on the size you assigned it during variable declaration. - Every element

Linear iterative process, (a) Write a recursive procedure (digits n) that c...

(a) Write a recursive procedure (digits n) that computes the number of digits in the integer n using a linear recursive process. For example, (digits 42) should return 2 and (digit

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd