Bear and Finding Criminals (B)

 avatar
jackoup
c_cpp
9 days ago
791 B
0
Indexable
Never
#include <iostream>

using namespace std;



int main(){
    int n;cin>>n;
    int index; cin>>index;
    int maxDistance = max(n-index,index-1 );
    index--;
    int distance = 1;
    int arr[n];
    for(int i =0 ;i<n;i++) {
        cin>>arr[i];
    }

    int counter = (arr[index] == 1)? 1:0;
    for(;distance <= maxDistance ;distance++) {
        if(index - distance >= 0 && index + distance < n)  {
            if(arr[index + distance] == 1 && arr[index - distance] == 1)
                counter+=2;
        }
        else if(index + distance < n && arr[index + distance] == 1) {
            counter++;
        }
        else if(index - distance >= 0 && arr[index - distance] == 1)
            counter++;

    }

    cout<<counter;

    return 0;

}
Leave a Comment