News Leaflets
A leading news portal.

Count of indices in Array having all prefix elements less than all in suffix

0 46

<script>

    

 

    const INT_MIN = -2147483647 - 1;

    const INT_MAX = 2147483647;

 

    

    

    const countSortedPoints = (arr, N) => {

 

        let left = new Array(N).fill(0);

        let right = new Array(N).fill(0);

 

        

        let Min = INT_MAX;

        let Max = INT_MIN;

        let Count = 0;

 

        

        for (let i = 0; i < N; i++) {

 

            Max = Math.max(arr[i], Max);

            left[i] = Max;

        }

 

        

        for (let i = N - 1; i >= 0; i--) {

 

            Min = Math.min(arr[i], Min);

            right[i] = Min;

        }

 

        

        for (let i = 0; i < N - 1; i++) {

            if (left[i] <= right[i + 1])

                Count++;

        }

 

        

        return Count;

    }

 

    

 

    let arr = [1, 5, 4, 2, 3, 8, 7, 9];

    let N = arr.length;

 

    

    document.write(countSortedPoints(arr, N));

 

 

</script>

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! News Leaflets is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment