Sort by

recency

|

3013 Discussions

|

  • + 1 comment

    Here is my O(N) c++ solution, you can watch the explanation here : https://youtu.be/L2fkDuGrxiI

    int birthday(vector<int> s, int d, int m) {
        if(s.size() < m) return 0;
        int su = accumulate(s.begin(), s.begin() + m, 0), result = 0;
        if(su == d) result++;
        for(int i = m; i < s.size(); i++){
            su += s[i];
            su -= s[i-m];
            if(su == d) result++;
        }
        return result;
     }
    
  • + 0 comments

    In java:

    public static int birthday(List s, int d, int m) { // Write your code here

    //m --> array lenght
    //d --> total sum of lenght
    //s --> array
    
    Integer numberOfPossibleCombinations = 0;
    Integer chocolateBarSize = s.size();
    
    for (int i = 0; i < chocolateBarSize ; i++){  
    
        List<Integer> sumList = s.stream().skip(i).limit(m).collect(Collectors.toList());    
        if(m > sumList.size()){
            break;
        }
    
        System.out.print("sumList " + sumList);
    
        Integer sum = sumList.stream().mapToInt(Integer::intValue).sum();
        System.out.println("sum " + sum);
    
        if(sum == d){
            numberOfPossibleCombinations++;
        }
    }
    
    return numberOfPossibleCombinations;
    }
    
  • + 0 comments
    > def birthday(s, d, m):
        # Write your code here
        left = 0
        right = m
        contiguous_sum = 0
        for i in range (len(s)-m+1):
            current_sum = 0
            for j in range(m):
                current_sum += s[i+j]
    

    sliding wondow technique
    if current_sum==d: contiguous_sum += 1 return contiguous_sum

  • + 0 comments

    How can i inegrate this code on living room wall art prints ? can any one guide me I am using kadence theme in wordpress.

  • + 0 comments

    Sliding window technique O(n)

    JAVA

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