Algorithm to determine if array contains n…n+m?

前端 未结 30 3104
清酒与你
清酒与你 2020-11-28 01:45

I saw this question on Reddit, and there were no positive solutions presented, and I thought it would be a perfect question to ask here. This was in a thread about interview

30条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 02:00

    Any contiguous array [ n, n+1, ..., n+m-1 ] can be mapped on to a 'base' interval [ 0, 1, ..., m ] using the modulo operator. For each i in the interval, there is exactly one i%m in the base interval and vice versa.

    Any contiguous array also has a 'span' m (maximum - minimum + 1) equal to it's size.

    Using these facts, you can create an "encountered" boolean array of same size containing all falses initially, and while visiting the input array, put their related "encountered" elements to true.

    This algorithm is O(n) in space, O(n) in time, and checks for duplicates.

    def contiguous( values )
        #initialization
        encountered = Array.new( values.size, false )
        min, max = nil, nil
        visited = 0
    
        values.each do |v|
    
            index = v % encountered.size
    
            if( encountered[ index ] )
                return "duplicates"; 
            end
    
            encountered[ index ] = true
            min = v if min == nil or v < min
            max = v if max == nil or v > max 
            visited += 1
        end
    
        if ( max - min + 1 != values.size ) or visited != values.size
            return "hole"
        else
            return "contiguous"
        end
    
    end
    
    tests = [ 
    [ false, [ 2,4,5,6 ] ], 
    [ false, [ 10,11,13,14 ] ] , 
    [ true , [ 20,21,22,23 ] ] , 
    [ true , [ 19,20,21,22,23 ] ] ,
    [ true , [ 20,21,22,23,24 ] ] ,
    [ false, [ 20,21,22,23,24+5 ] ] ,
    [ false, [ 2,2,3,4,5 ] ]
    ]
    
    tests.each do |t|
        result = contiguous( t[1] )
        if( t[0] != ( result == "contiguous" ) )
            puts "Failed Test : " + t[1].to_s + " returned " + result
        end
    end
    

提交回复
热议问题