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

Prepare a script for providing instagram followers, Prepare a script for pr...

Prepare a script for providing Instagram Followers/Likes I need to prepare a panel where my customer able to get Instagram followers or likes automatically. - A panel where I

C program to check display days and months , C program to check display ...

C program to check display days and month main() {           int months, days;           clrscr();           printf("ENTER DAYS: ");           scanf("%d", &days);

Source code for decoe to code, i am using mobile phone if i want to communi...

i am using mobile phone if i want to communicate via massage but that should be very secret

#padovan string, program that counts the number of occurrences of the strin...

program that counts the number of occurrences of the string in the n-th padovan string p(n)

Quick Homework, Write three functions in C or C++: one that declares a larg...

Write three functions in C or C++: one that declares a large array statically, one that declares the same large array on the stack, and one that creates the same large array from t

Define static storage class - computer programming, Define Static Storage C...

Define Static Storage Class in C Programming? The Static declarations have distinct and important uses. This allows the local variable to keep its previous value when the block

Explain the parts of operators, Explain the parts of operators Assignm...

Explain the parts of operators Assignment Operators The equal (=) sign is used for assigning a value to another. The left hand side has to be a variable (lvalue, which exc

Thermodynamics, the program that solve the efficiency of otto cycle

the program that solve the efficiency of otto cycle

Palindrome, string S convert it to a palindrome by doing chara, C/C++ Progr...

string S convert it to a palindrome by doing chara, C/C++ Programming

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