1
class Solution:
    def subarraysDivByK(self, A, K):
        """
        :type A: List[int]
        :type K: int
        :rtype: int
        """
        l = len(A)
        div = [[0 for col in range(l)] for row in range(l)]
        sums = [[None for col in range(l)] for row in range(l)]
        global cnt
        cnt = 0
        for i in range(l):
            if A[i]%K == 0:
                div[i][i] = 1
                cnt = cnt+1

        def impl(self,st,en):
            print(st,en)
            if sums[st][en]!=None:
                return sums[st][en]

            if A[st] == A[en]:
                if A[st]%K == 0:
                    sums[st][st] = A[st]
                    div[st][st] = 1
                    print(st,en,sums[st][en])
                    if sums[st][en]%k == 0:
                        div[st][en] = 1
                        cnt = cnt+1
                    return sums[st][st]
            elif st+1==en:
                sums[st][en] = A[st]+A[en]
                print(st,en,sums[st][en])
                if sums[st][en]%K == 0:
                    div[st][en] = 1
                    cnt = cnt+1
                return sums[st][en]
            else:
                if sums[st+1][en]==None:
                    sums[st][en] = impl(self,st+1,en)+A[st]
                if sums[st][en-1]==None:
                    sums[st][en] = impl(self,st,en-1)+A[en]
                #sums[st][en] = sums[st+1][en]+ A[st]
                print(st,en,sums[st][en])
                if sums[st][en]%K == 0:
                    div[st][en] = 1  
                    cnt = cnt+1
                return sums[st][en]


        impl(self,0,len(A)-1)
        print(cnt)
        #print(sums)
iz_
  • 15,923
  • 3
  • 25
  • 40

1 Answers1

0

You should add global cnt inside the function impl so that the global variable will be visible.

def impl(self,st,en):
    global cnt
    ...

For more information, please check: enter link description here.

keineahnung2345
  • 2,635
  • 4
  • 13
  • 28
  • Hi @Nivedita Rao if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. – keineahnung2345 Jan 15 '19 at 09:16