Find last ant to fall off the rod - output a single integer


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.

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Find last ant to fall off the rod - output a single integer
Reference No:- TGS0754642

Expected delivery within 24 Hours