Subarray Division 1

Sort by

recency

|

281 Discussions

|

  • + 0 comments

    c++20

    int birthday(vector<int> s, int d, int m) {
        int nCombos = 0;
        for(int i = 0; i < s.size() ; i++){
            if(std::accumulate(s.begin() + i, s.begin() + i + m, 0) == d){
                nCombos +=1;
            }
        }
        return nCombos;
    }
    
  • + 0 comments

    logic on python

    def testResult(result,d):
        count = 0
        for xy in result:
            temp = 0
            for record in xy:
                temp+=record
            if temp == d:
                count +=1
        return count
    def pushMRecord(idx, m, s):
        res = []
        for idxs in range(m):
            if idx + idxs < len(s): 
                res.append(s[idx + idxs])
        return res
    
    def birthday(s, d, m):
        # Write your code here
        temp = []
        result = []
        print(len(s),"sds")
        print(d,"sdac")
        if len(s) == d:
            return 1 
        for idx in range(len(s)):
            if idx == len(s)-m+1:
                continue
            temp = pushMRecord(idx, m, s)
            print("Tem", temp)
            if len(temp)==m:
                result.append(temp)
                temp = []
        print(result)
        testRes = testResult(result, d)
        return testRes
    
  • + 0 comments

    Python solution:

    def birthday(s, d, m):
    
        return len([i for i in range(0, len(s) + 1) if sum(s[i: i + m]) == d])
    
  • + 1 comment

    c# Solution:

    public static int birthday(List<int> s, int d, int m)
        {
            int possibleCombinations = 0;
            for(int i=0; i<s.Count; i++)
            {
                if((s.Count) - i < m) break;
    
                if(s.GetRange(i, m).Sum(e => e) == d)
                {
                    possibleCombinations++;
                }
            }
            
            return possibleCombinations;
        }
    
  • + 0 comments

    Java Solution:

        public static int birthday(List<Integer> s, int d, int m) {
    
            int count = 0;
            for(int index=0; index < s.size() - (m-1); index++){
                int sum = s.get(index);
                for(int i=1; i<m; i++){
                    int current = s.get(index + i);
                    sum = sum + current;
                }
                if(sum == d){
                    count++;
                }
            }
            return count;
        }