Sort by

recency

|

3043 Discussions

|

  • + 0 comments

    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

    Go solution:

    func birthday(s []int32, d int32, m int32) int32 {
        var ways int32 = 0
        
        var geralSum int32 = 0
        for i, value := range s {
            geralSum += value
            segmentSum := s[i]
            
            for j := int32(1); j < m; j++ {
                newIndex := int32(i) + j
                
                if newIndex >= int32(len(s)) {
                    break    
                }
                
                segmentSum += s[newIndex]
            }
    
            if segmentSum == d {
                ways += 1
            }
        }
        
        if geralSum == m {
            ways += 1     
        }
        
        return ways
    }
    
  • + 0 comments

    Can you help me to integrate this code algorithms with my [Microsoft Copilot Solution](https://codehyper.com.au/copilot/ ) page? I want to use it on my WordPress webiste.

  • + 0 comments

    Python 3 Solution

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

    Kotlin

    fun birthday(s: Array<Int>, d: Int, m: Int): Int {
            // Write your code here
            var many = 0
            if(m> s.size) return 0
            for( i in 0..s.size-m){
                var nBar  = s.slice(i..m-1+i)
                if( nBar.reduce { acc, s -> acc + s } == d){
                    many++
                }
            }
            return many
        }