<p><img alt="" src="https://assets.leetcode.com/uploads/2020/07/09/change.png" style="width: 635px; height: 312px;" /></p>

<p>Winston was given the above mysterious function <code>func</code>. He has an integer array <code>arr</code> and an integer <code>target</code> and he wants to find the values <code>l</code> and <code>r</code> that make the value <code>|func(arr, l, r) - target|</code> minimum possible.</p>

<p>Return <em>the minimum possible value</em> of <code>|func(arr, l, r) - target|</code>.</p>

<p>Notice that <code>func</code> should be called with the values <code>l</code> and <code>r</code> where <code>0 &lt;= l, r &lt; arr.length</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [9,12,3,7,15], target = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Calling func with all the pairs of [l,r] = [[0,0],[1,1],[2,2],[3,3],[4,4],[0,1],[1,2],[2,3],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[0,4]], Winston got the following results [9,12,3,7,15,8,0,3,7,0,0,3,0,0,0]. The value closest to 5 is 7 and 3, thus the minimum difference is 2.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [1000000,1000000,1000000], target = 1
<strong>Output:</strong> 999999
<strong>Explanation:</strong> Winston called the func with all possible values of [l,r] and he always got 1000000, thus the min difference is 999999.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [1,2,4,8,16], target = 0
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= arr[i] &lt;= 10<sup>6</sup></code></li>
	<li><code>0 &lt;= target &lt;= 10<sup>7</sup></code></li>
</ul>

class SegmentTree{
    public:
    vector<int> tree;
    int n;

    SegmentTree(int x){
        n = x;
        tree = vector<int>(4*n,-1);
    }

    int construct(vector<int> &arr,int start,int end,int idx){
            // cout<<idx<<" "<<n<<" "<<start<<" "<<end<<endl;
        if(start==end){
            return tree[idx] = arr[start];
        }
        int mid = (start + end)/2;
        return tree[idx] = construct(arr,start,mid,2*idx + 1) & construct(arr,mid + 1,end,2*idx + 2);
    }

    int query(int start,int end,int low,int high,int idx){
        if(high<start || low>end) return INT_MAX;
        // if(low==start && high==end) return tree[idx];
        if(start>=low && end<=high) return tree[idx];
        int mid = (start + end)/2;
        // cout<<start<<" "<<end<<" "<<low<<" "<<high<<" "<<idx<<endl;
        int nidx = 2*idx;
        return query(start,mid,low,high,nidx + 1) & query(mid + 1,end,low,high,nidx + 2);
    }

};

class Solution {
public:
    int getIndex(vector<int> &arr,SegmentTree* &tree,int idx,int target){
        int low = idx;
        int high = arr.size()-1;
        int n = arr.size();
        int ans = INT_MIN;
        int start = low;
        while(low<=high){
            int mid = (low + high)/2;
            // cout<<mid<<endl;
            // cout<<"val at : "<<low<<" "<<mid<<endl;
            int val = tree->query(0,n-1,start,mid,0);
            if(val >= target){
                ans = max(ans,mid);
                low = mid + 1;
            }
            else high = mid - 1;
            // cout<<"val : "<<start<<" "<<mid<<" "<<val<<" "<<ans<<endl;
        }
        return ans;
    }
    
    int helper(vector<int> vec){
        for(int i = 0;i<vec.size();i++){
            int ans = vec[i];
            for(int j = i;j<vec.size();j++){
                ans = ans&vec[j];
                cout<<i<<" "<<j<<" "<<ans<<endl;
            }
        }
        return 0;
    }
    
    int closestToTarget(vector<int>& arr, int target) {
        // helper(arr);
        int n = arr.size();
        SegmentTree* st = new SegmentTree(n);
        st->construct(arr,0,n-1,0);
        cout<<endl;

        int ans = INT_MAX;
        for(int i = 0;i<n;i++){
            int j = getIndex(arr,st,i,target);
            if(j==INT_MIN) j = i;
            int a = st->query(0,n-1,i,j,0);
            // cout<<i<<" djf "<<j<<" "<<arr[i]<<" "<<a<<endl;
            ans = min(ans,abs(a - target));
            if(j<n-1){
                // cout<<'j'<<endl;
                a = st->query(0,n-1,i,j+1,0);
                ans = min(ans,abs(a - target));

            }
        }
        return ans;

    }
};
74500