We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Subarray Division
  5. Discussions

Subarray Division

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 2715 Discussions, By:

recency

Please Login in order to post a comment

  • alban_tyrex
    1 day ago+ 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|
    Permalink
  • cmvyshnavireddy
    1 day ago+ 0 comments

    Java Solution little bit length but easy to understand

    public static int birthday(List<Integer> s, int d, int m) {
        // Write your code here
        int res = 0;
        if(s.size()==1 && s.size()==m){
            if(s.get(0)==d)
                return 1;
        }
        for(int i=0; i<s.size(); i++){
            for(int j=i+1; j<s.size(); j++){
                int count=0;
                for(int k=i; k<=j; k++){
                    count++;
                }
                int sum = 0;
                if(count==m){
                    for(int k=i; k<=j; k++){
                        sum = sum + s.get(k);
                    }
                    if(sum==d)
                        res++;
                }
            }
        }
        return res;
        }
    
    0|
    Permalink
  • bskavitha21
    3 days ago+ 0 comments
    int n=0;
    for(int i=0;i<s.size();i++)
    {
        int sum=0;
        for (int j=i;(j<=i+m-1)&(i+m-1<s.size());j++)
        {
            sum=sum+s[j];
        }
        if(sum==d)
            n++;
    }
    return n;
    
    0|
    Permalink
  • shawaf
    5 days ago+ 0 comments

    Kotlin solution

    fun birthday(s: Array<Int>, d: Int, m: Int): Int {
        var segments= 0
        for(i in s.indices){
          if(i+m-1 <= s.lastIndex && s.copyOfRange(i,i+m).sum() == d){
                segments++
            }
        }
        return segments 
    }
    
    0|
    Permalink
  • junivensaavedra
    1 week ago+ 0 comments

    Here's my short and sweet Python one liner. Enjoy:)

    def birthday(s, d, m):
        # Write your code here
        return len([i for i in range(len(s)) if sum(s[i : i + m]) == d])
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy