|
Input
|
Output
|
|
|
Test
Case 1
|
2
1 1
|
1
|
|
Test
Case 2
|
3
1 -1 -1
|
2
|
|
Test
Case 3
|
2
-1 -1
|
2
|
|
Test
Case 4
|
8
1 1 -1 1 1 1 -1 1
|
3
|
|
Test
Case 5
|
10 1 -1 1 1 -1 1 -1 1 -1 -1
|
5
|
#include <stdio.h> int main(){ // Number of ants int n; scanf("%d", &n); int a[n]; int i; // Directions of ants for(i=0; i<n; i++){ scanf("%d", &a[i]); } int left = 0, right = 0; // Number of contiguous ants at left edge and moving towards left for(i=0; i<n; i++){ if(a[i] == -1){ left ++; } else{ break; } } // Number of contiguous ants at the right edge and moving towards right for(i=n-1; i>=0; i--){ if(a[i] == 1){ right++; } else{ break; } } // Number of ants between 'left' and 'right' position and moving towards left int leftgoingants = 0; for(i=left; i <= n-right-1; i++){ if(a[i] == -1){ leftgoingants ++; } } if(left >= right){ printf("%d", left + leftgoingants); } else{ printf("%d", left + leftgoingants + 1); } return 0; } // Check the following link for a detailed explanation. // https://drive.google.com/a/nptel.iitm.ac.in/#folders/0BwzHeT8eBmvCUUlwNWo1VzM0Xzg
Labels: c program, last ant on rod