Skip to content

1.8 Ranged Sequence Array

Farrukh Ahmad edited this page Dec 15, 2015 · 1 revision

In this program we will check whether an array has it's all sequence number for a given range start and end

For Example

[1,2,3,4,5] for range 1-5 all sequence elements are present hence will return ture else shall return false

Solution:

public class RangedSequenceArray {
    public static void main(String args[]){
    int a[] = {1,2,3,4,5};
    int start = 1;
    int end = 5;
    System.out.println(isRangedSequence(a,start,end));
}

static boolean isRangedSequence(int a[], int start, int end){
   
    if (a.length ==0) return false;
    if( a.length < (end-start)+1) return false;
    
    for(int i=0; i<a.length-1; i++){
        if(a[i] > a[i+1]) return false;
        
        if( a[i]<start && a[i] > end) return false;
        
        if( a[i+1]<start && a[i+1] > end) return false;
        
    }
    return  true;
}
}

Clone this wiki locally