Missing ranges of numbers

PROBLEM :

Given an array A[ ] of N positive integers, print out the missing elements in the range [0 - 999].If
there are more than one missing, collate them using hyphen ( - ), otherwise just print the number.

Example:
 Input:   {88 3, 2, 400, 0, 10}
 Output:   [ 1 4-9 11-87 89-399 401-999 ]

Input
The first line of input contains an integer T denoting the number of test cases. Then T test cases
follow.
The first line of each test case contains a positive integer N, denoting the length of the array A[ ].
The second line of each test case contains a N space separated positive integers, denoting the
elements of the array A[ ].  

Output
Print out the range of missing number or the number itself  separated by space enclosed in square
brackets : [ n1 n2 n3-n6 n8-n9 ]

Constraints
1 <= T <= 100
0 <   N <= 30
0 <= A[ ] < 1000

Examples

Input
3
5
62 8 34 5 332
4
13 0 32 500
5
2 0 9 15 999

Output
[ 0-4 6-7 9-33 35-61 63-331 333-999 ]
[ 1-12 14-31 33-499 501-999 ]
[ 1 3-8 10-14 16-998 ]

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
void Missing_ranges_of_numbers(int [],int ) ;
int remove_dublicate(int [],int ) ;
void bubble(int [],int ) ;
int main()
 {
int t,no,arr[30],i ;
cin>>t ;
while(t--)
{
   cin>>no ;
   for(i=0;i<no;i++)
       cin>>arr[i] ;
     
   Missing_ranges_of_numbers(arr,no);
   cout<<endl ;
}
return 0;
}

void Missing_ranges_of_numbers(int arr[],int no)
{
    bubble(arr,no) ;
   
    no=remove_dublicate(arr,no) ;
   
    int i,beg,end ;
    beg=0 ;
   
    cout<<"[ " ;
    for(i=0;i<no;i++)
    {
        end=arr[i] ;
        if(beg!=end)
        {
            if(beg==end-1)
            {
                cout<<beg<<" " ;
                beg=arr[i]+1 ;
            }
            else
            {
                cout<<beg<<"-"<<end-1 ;
                beg=arr[i]+1 ;
               
                if(beg!=1000)
                    cout<<" " ;
            }
        }
        else
            beg=beg+1 ;
    }
    if(beg==999)
        cout<<"999" ;
    else if(beg!=1000)
        cout<<beg<<"-"<<"999" ;
       
    cout<<" ]" ;
}

void bubble(int a[],int no)
{
    int i,j,temp ;
    for(i=0;i<no-1;i++)
    {
        for(j=0;j<no-i-1;j++)
        {
            if(a[j]>a[j+1])
                {
                    temp=a[j] ;
                    a[j]=a[j+1] ;
                    a[j+1]=temp ;
                }
        }
    }
}

int remove_dublicate(int arr[],int no)
{
    int i,k ;
    k=0 ;
    for(i=1;i<no;i++)
    {
        if(arr[i]!=arr[k])
        {
            arr[++k]=arr[i] ;
        }
    }
    return k+1 ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )