algorithms – find the bit and subnet closest to the given number

It was a question that was asked to me during an online evaluation for a company
help me please in this question-

Given a table, of size N and an integer P, find the sub-table B = A[i…j] such that i <= j, computes the bit-level value of the sub-network elements say K = B[i]& B[i+1]& .. & B[j]. Displays the minimum value of
| K-P | among all the values ​​of K.
size of table 1 <= A.size <= 100000
A[i] 0 to 10 ^ 8

I could only think of the brute force.