[Question]
  [
Of course, the SE network is very knowledgeable about how to be respectful in the restroom, but for those of you who need a recap, being respectful means flushing the toilet, etc. Most importantly, though, it means using the stall as far away from others as possible. 
**The challenge**
Given a blueprint of a set of stalls with indications of which ones are in use as a string, you must return or print from a function or program where the most respectful place to do your business is. 
**The input**
```
 0 1 2 3 4 5    <- The stall number which is not actually visible in the input. 
| | |-| |-|-|   <- the stalls
```
The stalls are numbered in ascending order from left to right. There will always be at least one empty stall. There can be up to 50 stalls in an input. You can also take the input as an array or string of `0`s and `1`s or booleans if you prefer to do so. 
Stalls in use have `-` in them (in between the pipes). 
**The output**
The most respectful stall to go to is the one that is on average farthest away from the ones in use. The distance between two stalls is the absolute value of the difference of the numbers above them. 
Just to be clear: you are finding the average distance from *all* of the stalls‚Äînot just the neighboring ones. 
You must output the lowest number of the most respectful stall to go to **that is empty**. 
**Examples**
```
Input:
|-| |-| OR 101
Output:
1
Input:
| | |-| |-|-| OR 001011
Output:
0
Input:
|-| |-| | | | |-|-| OR 101000011
Output:
1
Input: 
|-| | | | | |-|-| | | | | OR 100000110000
Output:
11
Input:
|-|-|-|-| | | | | | |-| OR 11110000001
Output:
9
Input:
|-| | OR 10
Output:
1
Input:
|-| | |-| OR 1001
Output:
1
```
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest code in bytes wins!
You can use 0 or 1 based indexing in your answer ‚Äî whichever you prefer; if you use 1 based indexing, then you must say so explicitly in your answer. 
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~10~~ 9 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
J·∫°√æTS√ó¬¨M·∏¢
```
Uses 1-based indexing. [Try it online!](http://jelly.tryitonline.net/#code=SuG6ocO-VFPDl8KsTeG4og&input=&args=WzEsMCwwLDAsMCwwLDEsMSwwLDAsMCwwXQ) or [verify all test cases](http://jelly.tryitonline.net/#code=SuG6ocO-VFPDl8KsTeG4ogrDh-KCrEc&input=&args=WzEsMCwxXSwgWzAsMCwxLDAsMSwxXSwgWzEsMCwxLDAsMCwwLDAsMSwxXSwgWzEsMCwwLDAsMCwwLDEsMSwwLDAsMCwwXSwgWzEsMSwxLDEsMCwwLDAsMCwwLDAsMV0sIFsxLDBdLCBbMSwwLDAsMV0).
### How it works
```
J·∫°√æTS√ó¬¨M·∏¢  Main link. Argument: A (array of Booleans)
J          Yield all indices of A.
   T       Yield all truthy indices of A.
 ·∫°√æ        Compute the table of absolute differences.
    S      Compute the sums of all columns.
           For each index, this yields the sum of all distances to occupied stalls.
     √ó¬¨    Multiply each sum by the logical NOT of the corresponding Boolean in A.
           This zeroes sums that correspond to occupied stalls.
       M   Maximal; yield an array of all indices of maximal sums.
        ·∏¢  Head; extract the first index.
```
[Answer]
# Swift, ~~158, 157, 128,~~ 100 Bytes
Takes input from the `Array<Bool>` variable `i`, returns answer from the last expression.
```
let e=i.characters.map{$0>"0"}.enumerate()
e.flatMap{$1 ?nil:$0}.map{a in(a,e.flatMap{$1 ?$0:nil}.map{abs(a-$0)}.reduce(0){$0+$1})}.maxElement{$0.1 < $1.1}!.0
```
Edit 1:
Saved a byte by converting to bools via string comparison
```
let e=i.characters.map{$0=="1"}.enumerate()
e.flatMap{$1 ?nil:$0}.map{a in(a,e.flatMap{$1 ?$0:nil}.map{abs(a-$0)}.reduce(0){$0+$1})}.maxElement{$0.1 < $1.1}!.0
```
Edit 2:
Reworked my algorithm:
```
let e=i.characters.map{$0=="1"}.enumerate()
e.map{x in(x.0,x.1 ?0:e.reduce(0){$1.1 ?$0+abs(x.0-$1.0):$0})}.max{$0.1<$1.1}!.0
```
Edit 3:
Took advantage of new rule that allows taking input directly from a boolean array.
```
let e=i.enumerated()
e.map{x in(x.0,x.1 ?0:e.reduce(0){$1.1 ?$0+abs(x.0-$1.0):$0})}.max{$0.1<$1.1}!.0
```
Ungolfed:
```
// for the sake of easier copy/pasting of input, take it as string
let s = "100000110000"
// convert input to true for taken, false for free
// this is the input the golfed version actually uses
let input = s.characters.map{$0>"0"}
// Returns an array of tuples storing the array values (vacancy of the stall) and their index (their location)
let valueIndexPairs = bools.enumerated()
// Returns an array of pairs of locations and their avg distance to others
let locationDistancePairs = valueIndexPairs.map{(valueIndexPair: (Int, Bool)) -> (Int, Int) in
    let averageDistance = valueIndexPairs.reduce(0) {partialSum, otherStall in
        let otherStallIsTaken = otherStall.1
        if otherStallIsTaken {
            //don't let other stalls effect average if they're taken
            return partialSum
        }
        else {
            let thisStallLocation = valueIndexPair.0
            let otherStallLocation = otherStall.0
            let distanceToOtherStall = abs(thisStallLocation - otherStallLocation)
            return partialSum + distanceToOtherStall 
        }       
    }
        
    //if this stall is taken, treat its average distance to others as 0
    let thisStallsLocation = valueIndexPair.0
    let isThisStallTaken = valueIndexPair.1
    return (thisStallsLocation, isThisStallTaken ? 0 : averageDistance)
}
//find location where average distance is maxiumum
let bestLocationIndexPair = locationDistancePairs.max{$0.1 < $1.1}!
let bestLocation = bestLocationIndexPair.0
print(bestLocation)
```
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 13 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
1-indexed.
```
¬≥T·∫°‚Å∏S
JU√á√û·∏üT·π™
```
[Try it online!](http://jelly.tryitonline.net/#code=wrNU4bqh4oG4UwpKVcOHw57huJ9U4bmq&input=&args=WzAsMSwwLDEsMCwwXQ)
### Algorithm
Naive implementation of the question.
[Answer]
# Java "only" ~~270 200 196 187 196 138 148~~ 146 bytes!
saved  ~~4 13~~ countless bytes thanks to Leaky Nun! ~~1 byte thanks to Micheal~~
Golfed
```
int m(boolean[]b){int r=0,l=b.length,i,j,k=0,z=r;for(i=0;i<l;i++){if(b[i])for(j=0,k=0;j<l;j++)if(!b[j])k+=i>j?i-j:j-i;if(k>z){r=i;z=k;}}return r;}
```
Ungolfed
```
int m(int[] s) {
        int l=s.length,i,j=0,k=0;
    boolean[] b = new boolean[l];
    int[] a = new int[l];
    //see what stalls are open
    for (i = 0; i < s.length; i++) {
        if (s[i] == 0){
            b[i] = true;
        }
    }
    //assign the sum of distance to the a[]
    for (i = 0; i < l; i++) {
        if (b[i]) {
            for (j = 0; j < l; j++) {
                if (!b[j]) {
                    a[i]+= Math.abs(i - j);
                }
            }
        }
    }
    //find the stall the greatest distance away breaking ties based on the furthest left
    for (i = 0; i < l; i++) {
        if (b[i] && (a[i] > k || k == 0)) {
            k = a[i];
            j=i;
        }
    }
    //return the index
    return j;
}
```
input as an boolean array where true implies an open stall.
[Answer]
# Ruby, ~~79~~ ~~78~~ 76 + `n` flag = 77 bytes
Output is 0-based indexing. Input is STDIN line of 0's and 1's.
```
p (r=0...~/$/).max_by{|i|k=0;$_[i]>?0?0:r.map{|j|k+=$_[j]<?1?0:(j-i).abs};k}
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), 14 bytes
```
~ftGf!-|Xs&X>)
```
[**Try it online!**](http://matl.tryitonline.net/#code=fmZ0R2YhLXxYcyZYPik&input=WzEgMCAxIDAgMCAwIDAgMSAxXQ)
Output is 1-based.
### Explanation
```
~f     % Implicitly take input. Compute row vector with indices of zeros
t      % Duplicate that
Gf!    % Push input again. Compute column vector of indices of ones
-|     % Absolute differences with broadcast. Gives 2D array with all combinations
Xs     % Sum of each column
&X>    % Arg max. Gives the index of the first maximizer if there are several
)      % Index into row vector of indices of zeros. Implictly display
```
[Answer]
## Perl 84 + 3 (`-alp` flags) = 87 bytes
```
for$i(0..$#F){$t=0;map{$t+=abs($i-$_)*$F[$_]}0..$#F;($m,$_)=($t,$i)if$m<$t&&!$F[$i]}
```
Needs `-alp` flags to run. Takes a string of 1 and 0 separated by spaces as input. For instance :
```
perl -alpe '$m=0;for$i(0..$#F){$t=0;map{$t+=abs($i-$_)*$F[$_]}0..$#F;($m,$_)=($t,$i)if$m<$t&&!$F[$i]}' <<< "1 0 1
0 0 1 0 1 1
1 0 1 0 0 0 0 1 1
1 0 0 0 0 0 1 1 0 0 0 0
1 1 1 1 0 0 0 0 0 0 1
1 0"
```
Note that I added `$m=0` at the begining, but that's only to test it on multiple entries. 
[Answer]
## Matlab, 87 bytes
```
n=input('');k=numel(n);[a b]=ndgrid(1:k);[x y]=max(sum(abs(a-b).*repmat(n,k,1)').*~n);y
```
Takes array of ones and zeros; uses 1-based indexing.  
Like some other answers maximises total not average distance.  
Probably there's some more golfing possible...
[Answer]
## JavaScript (ES6), ~~87~~ ~~86~~ ~~82~~ 75 bytes
```
a=>a.map((u,i)=>u||(a.map((v,j)=>u+=v*(i>j?i-j:j-i)),u>x&&(x=d,r=i)),x=0)|r
```
Takes a boolean array (true/false or 1/0). No point calculating the average distance since they're all using the same common factor, so just calculating the total distance for each stall and finding the first index of the highest one. Edit: Saved 1 byte by using `*` instead of `&&`. Saved 5 bytes by finding the highest distance manually based on a comment by @Dendrobium. Saved 7 bytes by reusing `u` as the pseudo-reduce accumulator based on a comment by @edc65.
[Answer]
# J, 27 bytes
```
(#{:@-.~]/:[:+/]|@-/~#)i.@#
```
[Online interpreter](http://tryj.tk/).
[Answer]
# Ruby, ~~87~~ 76 bytes
Threw this first draft quickly together, but in the meantime [Value Ink](https://codegolf.stackexchange.com/a/89255/4372) had already posted an 80 byte Ruby answer...
edit: took off some bytes with help from Value Ink:
```
->a{(r=0...a.size).max_by{|i|a[i]?0:r.map{|j|a[j]?(i-j).abs: 0}.reduce(:+)}}
```
It's an anonymous function that takes an array of truthy/falsy values, like for instance so:
```
f=->->a{(r=0...a.size).max_by{|i|a[i]?0:r.map{|j|a[j]?(i-j).abs: 0}.reduce(:+)}}
# Test case number 5:
p f[[1, 1, 1, 1, nil, nil, nil, nil, nil, nil, 1]] # => 9
```
[Answer]
# Mathematica, 53 bytes
```
MaximalBy[a=PositionIndex@#;a@0,Tr@Abs[#-a@1]&][[1]]&
```
Uses 1-based indexing and takes input as a list of 0s and 1s.
[Answer]
# Javascript ES6 - ~~98~~ ~~95~~ ~~91~~ ~~86~~ ~~84~~ 88 bytes
Edit: Seems that the leftmost-stall should be used in the case of a tie. Squared distances no longer work, reverted to absolute distance.
```
(r,x=0,f=g=>r.reduce(g,0))=>f((p,o,i)=>x<(o=f((p,c,j)=>p+c*!o*Math.abs(i-j)))?(x=o,i):p)
```
Ungolfed:
```
(r,                            // string input
 x=0,                          // current max distance
 f=g=>r.reduce(g,0))=>         // iterator function
   f((p,o,i)=>                 // for each stall
     x<(o=f((p,c,j)=>          // iterate through all stalls and
       p+c*!o*Math.abs(i-j)))? //   calculate sum of distances from current stall
     (x=o,i):                  // if total dist is greater than x, update x, return index
     p)                        //   else return previous max index
```
Test runs:
```
f=(r,x=0,f=g=>r.reduce(g,0))=>f((p,c,i)=>x<(c=+c?0:f((p,c,j)=>p+c*Math.abs(i-j)))?(x=c,i):p)
f([1,0,1])                   // 1
f([0,0,1,0,1,1])             // 0
f([1,0,1,0,0,0,0,1,1])       // 1
f([1,0,0,0,0,0,1,1,0,0,0,0]) // 11
f([1,1,1,1,0,0,0,0,0,0,1])   // 9
f([1,0])                     // 1
```
[Answer]
# Lua, 165150 Byes
```
n=arg[1]n=n:gsub("%|%-","1"):gsub("%| ","0")i=0 for s in n:gmatch("0+")do i=(i<#s)and(#s)or(i)end n,l=n:find(("0"):rep(i))print(n+math.floor((l-n)/2))
```
This cheats a little using the fact that generally, lua passes a table called arg containing any command line inputs to it.
I'm a bit disappointed that I used a for in loop, but I couldn't think of a smaller way to pull it off.
Also, Because lua, 1 based indexing was used.
Edit
Snipped 15 bytes from a wasteful gsub.
[Answer]
# C#, 127 bytes
```
public int G(char[]s){int i=0;var l=s.ToLookup(b=>b,b=>i++);return l['0'].OrderBy(j=>l['1'].Average(p=>Math.Abs(p-j))).Last();}
```
# Test Bed
```
public static void Main() {
    var respectful = new Respectful();
    foreach (var kvp in testCases) {
        $"{kvp.Key}: Expected {kvp.Value} Actual {respectful.G(kvp.Key.ToCharArray())}".Dump();
    }
}
public static readonly List<KeyValuePair<string, int>> testCases = new List<KeyValuePair<string, int>> {
    new KeyValuePair<string, int>("101", 1),
    new KeyValuePair<string, int>("001011", 0),
    new KeyValuePair<string, int>("101000011", 1),
    new KeyValuePair<string, int>("100000110000", 11),
    new KeyValuePair<string, int>("11110000001", 9),
    new KeyValuePair<string, int>("10", 1),
    new KeyValuePair<string, int>("1001", 1),
};
public class Respectful {
    public int G(char[]s){int i=0;var l=s.ToLookup(b=>b,b=>i++);return l['0'].OrderBy(j=>l['1'].Average(p=>Math.Abs(p-j))).Last();}
}
```
]