Search code examples
pythonalgorithmsub-array

Finding all contiguous subarrays


Here i have a elementList which holds the data with only 0's and 1's .I throw the data to temporaryList from elementList to examine all of the subarrays . I am trying to find the total number of contiguous subarrays that contains only one '1' .

I checked the subarrays whether they are correct or not by printing them . They are fine but my subarrayCounter does not gives the correct value and i can not see my problem ( i am sure that there is a stupid mistake , sorry) .

any idea would be ok. thanks

for i in range (0,len(elementlist)):
    maxwidth = len(elementlist)  - i 
    for j in range (0 , maxwidth):
        tempList.append(elementlist[i+j])
   
        for m in range (0 , len(tempList)) : 
            if tempList[m] == '1' : 
                counter += 1

        if counter == int(numberOne) : 
            subarrayCounter += 1
        counter = 0
    
        
            
    tempList.clear()

for example when i have 0 1 1 0 1 in my list if i try when i try to print the print the contiguous subarrays it gives the correct answer :

    for i in range (0,len(elementlist)):
        maxwidth = len(elementlist)  - i 
        for j in range (0 , maxwidth):
            tempList.append(elementlist[i+j])
            print(tempList) # added print here
            for m in range (0 , len(tempList)) : 
                if tempList[m] == '1' : 
                    counter += 1

            if counter == int(numberOne) : 
                subarrayCounter += 1
            counter = 0
    
        
            
        tempList.clear()

OUTPUT :

    ['0']
    ['0', '1']
    ['0', '1', '1']
    ['0', '1', '1', '0']
    ['0', '1', '1', '0', '1']
    ['1']
    ['1', '1']
    ['1', '1', '0']
    ['1', '1', '0', '1']
    ['1']
    ['1', '0']
    ['1', '0', '1']
    ['0']
    ['0', '1']
    ['1']

Solution

  • I think the following code is a simple way to solve this question.

    def countSubArraysWithSingle1(elementlist):
      subarrayCounter = 0
      for i in range (0,len(elementlist)):
        for j in range (i , len(elementlist)):
          if elementlist[i:j+1].count('1') == 1:
            print(elementlist[i:j+1])
            subarrayCounter += 1
      print("Total count: ",subarrayCounter)
    

    I/P: ['0', '1', '1', '0', '1']
    O/P:

    ['0', '1']
    ['1']
    ['1']
    ['1', '0']
    ['0', '1']
    ['1']
    Total count: 6
    

    Explanation:
    The code finds out all the sub-arrays in the following order and runs a count function on each sub-array which counts the number of '1's in it and returns the count. We check if the count returned is 1 or not, if it is then we increment the subarrayCounter by 1:

    ['0'].count('0') => 0
    **['0', '1'].count('1') => 1 {increment subarrayCounter by 1}**
    ['0', '1', '1'].count('1') => 2
    ['0', '1', '1', '0'].count('1') => 2
    ['0', '1', '1', '0', '1'].count('1') = 3
    **['1'].count('1') => 1 {increment subarrayCounter by 1}**
    ['1', '1'].count('1') => 2
    ['1', '1', '0'].count('1') => 2
    ['1', '1', '0', '1'].count('1') => 3
    **['1'].count('1') => 1 {increment subarrayCounter by 1}**
    **['1', '0'].count('1') => 1 {increment subarrayCounter by 1}**
    ['1', '0', '1'].count('1') => 2
    ['0'].count('1') => 0
    **['0', '1'].count('1') => 1 {increment subarrayCounter by 1}**
    **['1'].count('1') => 1 {increment subarrayCounter by 1}**
    

    Hope it helps!