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

C++ programming, give a program to accept and print 2_D Array

give a program to accept and print 2_D Array

Change to palindrome, A palindrome is a string that reads the same from bot...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Create complex number using constructor , Create Complex number using const...

Create Complex number using constructor: class complex                                 {                                                   private:

Describe difference between malloc()/free() & new/delete?, for object, mall...

for object, malloc allocates memory in heap however doesn't invoke object's constructor to initialize the object. new allocates memory & also invokes constructor to initialize the

Program with inbuilt functions, write a atm program in c with inbuilt funct...

write a atm program in c with inbuilt functions for 1782?

C++, Write a program to find the area under the curve y = f(x) between x = ...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Compiler Design - Limit In The Method Instructions, Raj is a newbie to the ...

Raj is a newbie to the programming and while learning the programming language he came to know the following rules: · Each program must start with ''{'' and end with ''}''

I want auto-rank script, Project Description: I play this game called mo...

Project Description: I play this game called mobstar (mobstar) and I would like the program to grade my account for me. It would require doing a crime, GTA, jail busting, lead a

Inheritance, example of program to ad two numbers

example of program to ad two numbers

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