You have a 1-indexed binary string of length n where all the bits are 0 initially. We will flip all the bits of this binary string (i.e., change them from 0 to 1) one by one. You are given a 1-indexed integer array flips where flips[i] indicates that the bit at index i will be flipped in the ith step.

A binary string is prefix-aligned if, after the ith step, all the bits in the inclusive range [1, i] are ones and all the other bits are zeros.

Return the number of times the binary string is prefix-aligned during the flipping process.

&nbsp;
Example 1:

Input: flips = [3,2,4,1,5]
Output: 2
Explanation: The binary string is initially "00000".
After applying step 1: The string becomes "00100", which is not prefix-aligned.
After applying step 2: The string becomes "01100", which is not prefix-aligned.
After applying step 3: The string becomes "01110", which is not prefix-aligned.
After applying step 4: The string becomes "11110", which is prefix-aligned.
After applying step 5: The string becomes "11111", which is prefix-aligned.
We can see that the string was prefix-aligned 2 times, so we return 2.


Example 2:

Input: flips = [4,1,2,3]
Output: 1
Explanation: The binary string is initially "0000".
After applying step 1: The string becomes "0001", which is not prefix-aligned.
After applying step 2: The string becomes "1001", which is not prefix-aligned.
After applying step 3: The string becomes "1101", which is not prefix-aligned.
After applying step 4: The string becomes "1111", which is prefix-aligned.
We can see that the string was prefix-aligned 1 time, so we return 1.


&nbsp;
Constraints:


	n == flips.length
	1 &lt;= n &lt;= 5 * 104
	flips is a permutation of the integers in the range [1, n].


class Solution:
    def numTimesAllBlue(self, flips: List[int]) -> int:
        
        
        l = len(flips)
        s = 0
        c = 0
        
        for i in range(len(flips)):
            f = flips[i]
            s = 1 << (f - 1) | s
            if s ==  (1 << (i+1))-1:
                c += 1
        
        return c
class Solution {
    public int numTimesAllBlue(int[] flips) {
        int counter=0,total=0,max=Integer.MIN_VALUE;
        for(int i=0;i<flips.length;i++){
            if(max<flips[i])max=flips[i];
            if(++counter==max)total++;
        }
        return total;
    }
}
class BIT{
  public:
vector<int>bit;
int n;
BIT(int n){
    bit.resize(n+1,0);
    this->n=n;
}

int findSum(int i){
    
    int sum=0;
    while(i>0){
        sum+=bit[i];
        i-=(i&(-i));
    }
    return sum;
}

void update(int i,int val){
    

    while(i<=n){
        bit[i]+=val;
        i+=(i&(-i));
    }  
}

};


class Solution {
public:
int numTimesAllBlue(vector<int>& flips) {

    int n=flips.size();
    BIT tree(n+1);
    int res=0;
    string s=string(n,'0');
   
    int maxi=0;
    for(auto &x:flips){
        if(s[x-1]=='1'){
            tree.update(x,-1);
            s[x-1]='0';
        }
        else {
            s[x-1]='1';
            tree.update(x,1);
        }
        maxi=max(maxi,x);
        if(tree.findSum(x)==x && tree.findSum(maxi)==maxi)
            res++;
        
     
    
    }
    
	 return res;   
	}
};
var numTimesAllBlue = function(flips) {
    const flipped = new Array(flips.length).fill(0);
    let prefixAlignedCount = 0;
    flips.forEach((i, step) => {
        flipped[i - 1] = 1;
        if(isPrefixAligned(step)) {
            ++prefixAlignedCount;
        }
    })
    return prefixAlignedCount;
    
    function isPrefixAligned(step) {
        for(let i = 0; i < flips.length; ++i) {
            if((i < step && flipped[i] === 0) || (i > step && flipped[i] === 1)) {
                return false;
            }
        }
        return true;
    }
};
Number of Times Binary String Is Prefix-Aligned