Friday 9 June 2017

                             Maximum No. Of Subarray

You are given an array A of N integers. Tell me the maximum number of subarrays such that all the numbers in the array having same value must be in a same subarray


problem taken from https://csacademy.com/contest/round-32/task/subarray-partition/

Constraints:

 

INPUT:              OUTPUT
4                           2
1 1 2 2

Explanation:
1 1 is in a subarray {1,1}
2 2 is in other subarray {2,2}
so output is 2

Solution in C:



/*
Sundar Lal Baror═╩═╦═╩═╦═╩═╦═╩═╦═╩═
█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
█═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═█
█═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═█
█═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═█
█═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═█
█═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═█
█═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═█
█═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═█
█═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═█
█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█
*/
#include <stdio.h>
#define ll long long int 
ll max(ll a,ll b){return a>b?a:b;}


ll arr[100000];
ll arr1[100005];
int main() {
    
    ll n,i;
   // ll sum=0;
   // ll count=0;
    scanf("%lld",&n);
    for(i=1;i<=n;i++)
    {scanf("%lld",&arr[i]);
    arr1[arr[i]]=i;
       
    }
    
    ll j=1;
    ll cnt=0;
    for(i=1;i<=n;i++)
    {
        if(j<i)
        cnt++;
        j=max(j,arr1[arr[i]]);
    }

    printf("%lld\n",cnt+1);

    
    return 0;
}


 


No comments:

Post a Comment