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.
Subarray Division
Subarray Division
+ 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 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 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 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 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])
Load more conversations
Sort 2715 Discussions, By:
Please Login in order to post a comment