Find the number of subarrays in an array which has the given sumBest way to find if an item is in a JavaScript array?How do I find out which DOM element has the focus?How to find the sum of an array of numbersFind the min/max element of an Array in JavaScriptFind object by id in an array of JavaScript objectssum of consecutive numbers javascriptGive two integer Arrays find all subarrays whose sum equal a given target numberJavascript summing numbers from strings nested in arrayAll ways of dividing an array (element combinations) into a custom partitionMaximum Subarray using javascript
How to address players struggling with simple controls?
What is the precise meaning of "подсел на мак"?
How to ask if I can mow my neighbor's lawn
How to make all magic-casting innate, but still rare?
What is the color associated with lukewarm?
Have Steve Rogers (Captain America) and a young Erik Lehnsherr (Magneto) interacted during WWII?
Manager wants to hire me; HR does not. How to proceed?
How did Avada Kedavra get its name?
How do I become a better writer when I hate reading?
Why can't I craft scaffolding in Minecraft 1.14?
What does a/.b[c][[1]] mean?
I have found ports on my Samsung smart tv running a display service. What can I do with it?
Is there a term for someone whose preferred policies are a mix of Left and Right?
Redirecting output only on a successful command call
Why are almost all the people in this orchestra recording wearing headphones with one ear on and one ear off?
Can you cover a cube with copies of this shape?
Converting 3x7 to a 1x7. Is it possible with only existing parts?
How did space travel spread through the galaxy?
Does anyone recognize these rockets, and their location?
1960s sci-fi anthology with a Viking fighting a U.S. army MP on the cover
Cut power on a remote Raspberry Pi 3 via another raspi
How to write a nice frame challenge?
How can I ping multiple IP addresses at the same time?
Background for black and white chart
Find the number of subarrays in an array which has the given sum
Best way to find if an item is in a JavaScript array?How do I find out which DOM element has the focus?How to find the sum of an array of numbersFind the min/max element of an Array in JavaScriptFind object by id in an array of JavaScript objectssum of consecutive numbers javascriptGive two integer Arrays find all subarrays whose sum equal a given target numberJavascript summing numbers from strings nested in arrayAll ways of dividing an array (element combinations) into a custom partitionMaximum Subarray using javascript
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Here is the problem: Find the number of subarrays in an array, which has the given sum.
this program enter 2 parameters number array and sum.
for example:
subArrayCnt([1,2,3,2,1,8,-3],5)
and the output should be number of subarrays accoding to given sum.
(output should be 3 for above example 2,3, 3,2, 8,-3 (number of subarrays))
I tried to do that but there is a problem with it isn't fulfilling the requirment of "The answer should be valid for any given input"
Here is my code:
function subArrayCnt(arr, sum)
for (var i = 0; i < arr.length; i++)
var str = [];
var csum= 0;
var output = 0;
for (var j = i; j < arr.length; j++)
csum+= arr[j];
str.push(arr[j]);
if (csum== sum)
return(str[i]);
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
this program provide number of subarrays but it isn't fulfilling the requirment of "The answer should be valid for any given input" where should be corrected? any suggetions please.
javascript
|
show 6 more comments
Here is the problem: Find the number of subarrays in an array, which has the given sum.
this program enter 2 parameters number array and sum.
for example:
subArrayCnt([1,2,3,2,1,8,-3],5)
and the output should be number of subarrays accoding to given sum.
(output should be 3 for above example 2,3, 3,2, 8,-3 (number of subarrays))
I tried to do that but there is a problem with it isn't fulfilling the requirment of "The answer should be valid for any given input"
Here is my code:
function subArrayCnt(arr, sum)
for (var i = 0; i < arr.length; i++)
var str = [];
var csum= 0;
var output = 0;
for (var j = i; j < arr.length; j++)
csum+= arr[j];
str.push(arr[j]);
if (csum== sum)
return(str[i]);
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
this program provide number of subarrays but it isn't fulfilling the requirment of "The answer should be valid for any given input" where should be corrected? any suggetions please.
javascript
2
what about1,2,3,2,-3
and2,3,2,1,-3
?
– Thomas
Mar 25 at 4:16
here the answer should be valid for any given input also negative numbers should me accept
– SoWeLaugh _
Mar 25 at 4:20
Why you are returning ` return(str[i]); `?
– Omurbek Kadyrbekov
Mar 25 at 4:21
for getting the number of element in str. is it the wrong way?
– SoWeLaugh _
Mar 25 at 4:24
@SoWeLaugh_ no, these are the numbers from your input array and they also have a sum of 5. Are these valid results? If not, why not? What defines a valid result?
– Thomas
Mar 25 at 4:24
|
show 6 more comments
Here is the problem: Find the number of subarrays in an array, which has the given sum.
this program enter 2 parameters number array and sum.
for example:
subArrayCnt([1,2,3,2,1,8,-3],5)
and the output should be number of subarrays accoding to given sum.
(output should be 3 for above example 2,3, 3,2, 8,-3 (number of subarrays))
I tried to do that but there is a problem with it isn't fulfilling the requirment of "The answer should be valid for any given input"
Here is my code:
function subArrayCnt(arr, sum)
for (var i = 0; i < arr.length; i++)
var str = [];
var csum= 0;
var output = 0;
for (var j = i; j < arr.length; j++)
csum+= arr[j];
str.push(arr[j]);
if (csum== sum)
return(str[i]);
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
this program provide number of subarrays but it isn't fulfilling the requirment of "The answer should be valid for any given input" where should be corrected? any suggetions please.
javascript
Here is the problem: Find the number of subarrays in an array, which has the given sum.
this program enter 2 parameters number array and sum.
for example:
subArrayCnt([1,2,3,2,1,8,-3],5)
and the output should be number of subarrays accoding to given sum.
(output should be 3 for above example 2,3, 3,2, 8,-3 (number of subarrays))
I tried to do that but there is a problem with it isn't fulfilling the requirment of "The answer should be valid for any given input"
Here is my code:
function subArrayCnt(arr, sum)
for (var i = 0; i < arr.length; i++)
var str = [];
var csum= 0;
var output = 0;
for (var j = i; j < arr.length; j++)
csum+= arr[j];
str.push(arr[j]);
if (csum== sum)
return(str[i]);
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
this program provide number of subarrays but it isn't fulfilling the requirment of "The answer should be valid for any given input" where should be corrected? any suggetions please.
javascript
javascript
edited Mar 25 at 6:41
Guilherme
218
218
asked Mar 25 at 4:10
SoWeLaugh _SoWeLaugh _
63
63
2
what about1,2,3,2,-3
and2,3,2,1,-3
?
– Thomas
Mar 25 at 4:16
here the answer should be valid for any given input also negative numbers should me accept
– SoWeLaugh _
Mar 25 at 4:20
Why you are returning ` return(str[i]); `?
– Omurbek Kadyrbekov
Mar 25 at 4:21
for getting the number of element in str. is it the wrong way?
– SoWeLaugh _
Mar 25 at 4:24
@SoWeLaugh_ no, these are the numbers from your input array and they also have a sum of 5. Are these valid results? If not, why not? What defines a valid result?
– Thomas
Mar 25 at 4:24
|
show 6 more comments
2
what about1,2,3,2,-3
and2,3,2,1,-3
?
– Thomas
Mar 25 at 4:16
here the answer should be valid for any given input also negative numbers should me accept
– SoWeLaugh _
Mar 25 at 4:20
Why you are returning ` return(str[i]); `?
– Omurbek Kadyrbekov
Mar 25 at 4:21
for getting the number of element in str. is it the wrong way?
– SoWeLaugh _
Mar 25 at 4:24
@SoWeLaugh_ no, these are the numbers from your input array and they also have a sum of 5. Are these valid results? If not, why not? What defines a valid result?
– Thomas
Mar 25 at 4:24
2
2
what about
1,2,3,2,-3
and 2,3,2,1,-3
?– Thomas
Mar 25 at 4:16
what about
1,2,3,2,-3
and 2,3,2,1,-3
?– Thomas
Mar 25 at 4:16
here the answer should be valid for any given input also negative numbers should me accept
– SoWeLaugh _
Mar 25 at 4:20
here the answer should be valid for any given input also negative numbers should me accept
– SoWeLaugh _
Mar 25 at 4:20
Why you are returning ` return(str[i]); `?
– Omurbek Kadyrbekov
Mar 25 at 4:21
Why you are returning ` return(str[i]); `?
– Omurbek Kadyrbekov
Mar 25 at 4:21
for getting the number of element in str. is it the wrong way?
– SoWeLaugh _
Mar 25 at 4:24
for getting the number of element in str. is it the wrong way?
– SoWeLaugh _
Mar 25 at 4:24
@SoWeLaugh_ no, these are the numbers from your input array and they also have a sum of 5. Are these valid results? If not, why not? What defines a valid result?
– Thomas
Mar 25 at 4:24
@SoWeLaugh_ no, these are the numbers from your input array and they also have a sum of 5. Are these valid results? If not, why not? What defines a valid result?
– Thomas
Mar 25 at 4:24
|
show 6 more comments
5 Answers
5
active
oldest
votes
From your example, I assumed that the sum of two consecutive elements should be the sum to qualify.
function subArrayCnt(arr, sum)
let outputArr = [];
for(var i=0; i<arr.length; i++)
if(arr[i]+arr[i+1]==sum)
let obj = l:arr[i],r:arr[i+1];
outputArr.push(obj);
;
return outputArr.length;
;
not only two), it may be 1 or more
– Omurbek Kadyrbekov
Mar 25 at 4:22
in here if we give subArrayCnt([1,2,3,4],10) answer should be 1. but it will give 0.
– SoWeLaugh _
Mar 25 at 4:34
1
Should [1,2,3,0,4] return 1 or 0?
– vcode
Mar 25 at 4:38
@vcode subArrayCnt([1,2,3,4],10) sholud return 1
– SoWeLaugh _
Mar 25 at 4:47
add a comment |
Try this approach:
function subArrayCnt(arr, sum)
var count = 0;
for(var i = 0; i < arr.length-1; i++)
for(var n = i+1; n < arr.length; n++)
if(arr[i] + arr[n] == sum)
count++;
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 3
add a comment |
The other answers seem to be ignoring the fact that the sum can be obtained from any number of elements of the array. Recursion is the best approach here.
function subArrayCnt(arr, sum)
return subArrayRecurse(arr, sum, 0, [], 0)
function subArrayRecurse(arr, sum, currentSum, curArray, i)
var count = 0;
//check the current index
var newSum = currentSum + arr[i];
var newSubArray = curArray.concat([arr[i]]);
if(newSum == sum)
console.log('found another: ' + newSubArray);
count++;
if(i + 1 < arr.length)
//try including the current in further sums
count += subArrayRecurse(arr, sum, newSum, newSubArray, i + 1);
//try not including the current in further sums
count += subArrayRecurse(arr, sum, currentSum, curArray, i + 1);
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 8
The 8 combos from the above example are:
1,2,3,2,-3
1,2,2
1,3,1
2,3
2,3,2,1,-3
2,2,1
3,2
8,-3
add a comment |
You can do that using nested loop. This will get all the possible adjacent sums and then you can compare that sum with the given sum and increase count
.
function func(arr,sum)
if(!Array.isArray(arr)) return 0;
let count = 0;
let cur = 0;
for(let i = 0;i<arr.length-1;i++)
cur = arr[i];
for(let j = i+1;j<arr.length;j++)
cur += arr[j];
if(cur === sum)
count++;
break;
if(cur > sum) break;
return count+'';
console.log(func([1,2,3,2,1,8,-3],5)) //3
console.log(func([1,2,3,4],10)) //1
getSubArrayCount([1,2,3,2,1,8,-3],5) should equal to 3. , getSubArrayCount([1,2,3,4],10) these 2 is ok but not fulfill the last requirement "The answer should be valid for any given input." and there is a tip in problem say "please consider the adjacent numbers only"
– SoWeLaugh _
Mar 25 at 5:33
@SoWeLaugh _ Give link to problem I would see it.
– Maheer Ali
Mar 25 at 5:33
the link is not working to external users that is the problem. here is the problem as in site. Find the number of subarrays in an array, which has the given sum. (tip: please consider the adjacent numbers only) Example : Input getSubArrayCount([1,2,3,2,1,8,-3],5) Output should be 3 example combinations (2,3, 3,2, 8,-3) and these are the requirment to be fulfill: The return type should be a string. subArrayCnt([1,2,3,2,1,8,-3],5) should equal to 3. subArrayCnt([1,2,3,4],10) should equal to 1. The answer should be valid for any given input.
– SoWeLaugh _
Mar 25 at 6:09
@SoWeLaugh _ I have edit the code that will check element is array or not. Can you please tell for what particular input its not working
– Maheer Ali
Mar 25 at 6:15
@SoWeLaugh _ The return value should be string. I edited the code now test it
– Maheer Ali
Mar 25 at 6:24
|
show 1 more comment
Try this:
<script>
function subArray(arr,sum)
var subArray=new Array();
count=0;
for(var i=0;i<arr.length;i++)
if(arr[i]+arr[i+1]==sum)
subArray[count]=[arr[i],arr[i+1]]
count++;
return subArray;
console.log(subArray([1,2,3,2,1,8,-3],5));
</script>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331160%2ffind-the-number-of-subarrays-in-an-array-which-has-the-given-sum%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
From your example, I assumed that the sum of two consecutive elements should be the sum to qualify.
function subArrayCnt(arr, sum)
let outputArr = [];
for(var i=0; i<arr.length; i++)
if(arr[i]+arr[i+1]==sum)
let obj = l:arr[i],r:arr[i+1];
outputArr.push(obj);
;
return outputArr.length;
;
not only two), it may be 1 or more
– Omurbek Kadyrbekov
Mar 25 at 4:22
in here if we give subArrayCnt([1,2,3,4],10) answer should be 1. but it will give 0.
– SoWeLaugh _
Mar 25 at 4:34
1
Should [1,2,3,0,4] return 1 or 0?
– vcode
Mar 25 at 4:38
@vcode subArrayCnt([1,2,3,4],10) sholud return 1
– SoWeLaugh _
Mar 25 at 4:47
add a comment |
From your example, I assumed that the sum of two consecutive elements should be the sum to qualify.
function subArrayCnt(arr, sum)
let outputArr = [];
for(var i=0; i<arr.length; i++)
if(arr[i]+arr[i+1]==sum)
let obj = l:arr[i],r:arr[i+1];
outputArr.push(obj);
;
return outputArr.length;
;
not only two), it may be 1 or more
– Omurbek Kadyrbekov
Mar 25 at 4:22
in here if we give subArrayCnt([1,2,3,4],10) answer should be 1. but it will give 0.
– SoWeLaugh _
Mar 25 at 4:34
1
Should [1,2,3,0,4] return 1 or 0?
– vcode
Mar 25 at 4:38
@vcode subArrayCnt([1,2,3,4],10) sholud return 1
– SoWeLaugh _
Mar 25 at 4:47
add a comment |
From your example, I assumed that the sum of two consecutive elements should be the sum to qualify.
function subArrayCnt(arr, sum)
let outputArr = [];
for(var i=0; i<arr.length; i++)
if(arr[i]+arr[i+1]==sum)
let obj = l:arr[i],r:arr[i+1];
outputArr.push(obj);
;
return outputArr.length;
;
From your example, I assumed that the sum of two consecutive elements should be the sum to qualify.
function subArrayCnt(arr, sum)
let outputArr = [];
for(var i=0; i<arr.length; i++)
if(arr[i]+arr[i+1]==sum)
let obj = l:arr[i],r:arr[i+1];
outputArr.push(obj);
;
return outputArr.length;
;
answered Mar 25 at 4:21
vcodevcode
816
816
not only two), it may be 1 or more
– Omurbek Kadyrbekov
Mar 25 at 4:22
in here if we give subArrayCnt([1,2,3,4],10) answer should be 1. but it will give 0.
– SoWeLaugh _
Mar 25 at 4:34
1
Should [1,2,3,0,4] return 1 or 0?
– vcode
Mar 25 at 4:38
@vcode subArrayCnt([1,2,3,4],10) sholud return 1
– SoWeLaugh _
Mar 25 at 4:47
add a comment |
not only two), it may be 1 or more
– Omurbek Kadyrbekov
Mar 25 at 4:22
in here if we give subArrayCnt([1,2,3,4],10) answer should be 1. but it will give 0.
– SoWeLaugh _
Mar 25 at 4:34
1
Should [1,2,3,0,4] return 1 or 0?
– vcode
Mar 25 at 4:38
@vcode subArrayCnt([1,2,3,4],10) sholud return 1
– SoWeLaugh _
Mar 25 at 4:47
not only two), it may be 1 or more
– Omurbek Kadyrbekov
Mar 25 at 4:22
not only two), it may be 1 or more
– Omurbek Kadyrbekov
Mar 25 at 4:22
in here if we give subArrayCnt([1,2,3,4],10) answer should be 1. but it will give 0.
– SoWeLaugh _
Mar 25 at 4:34
in here if we give subArrayCnt([1,2,3,4],10) answer should be 1. but it will give 0.
– SoWeLaugh _
Mar 25 at 4:34
1
1
Should [1,2,3,0,4] return 1 or 0?
– vcode
Mar 25 at 4:38
Should [1,2,3,0,4] return 1 or 0?
– vcode
Mar 25 at 4:38
@vcode subArrayCnt([1,2,3,4],10) sholud return 1
– SoWeLaugh _
Mar 25 at 4:47
@vcode subArrayCnt([1,2,3,4],10) sholud return 1
– SoWeLaugh _
Mar 25 at 4:47
add a comment |
Try this approach:
function subArrayCnt(arr, sum)
var count = 0;
for(var i = 0; i < arr.length-1; i++)
for(var n = i+1; n < arr.length; n++)
if(arr[i] + arr[n] == sum)
count++;
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 3
add a comment |
Try this approach:
function subArrayCnt(arr, sum)
var count = 0;
for(var i = 0; i < arr.length-1; i++)
for(var n = i+1; n < arr.length; n++)
if(arr[i] + arr[n] == sum)
count++;
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 3
add a comment |
Try this approach:
function subArrayCnt(arr, sum)
var count = 0;
for(var i = 0; i < arr.length-1; i++)
for(var n = i+1; n < arr.length; n++)
if(arr[i] + arr[n] == sum)
count++;
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 3
Try this approach:
function subArrayCnt(arr, sum)
var count = 0;
for(var i = 0; i < arr.length-1; i++)
for(var n = i+1; n < arr.length; n++)
if(arr[i] + arr[n] == sum)
count++;
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 3
answered Mar 25 at 4:40
Adnan TokyAdnan Toky
732412
732412
add a comment |
add a comment |
The other answers seem to be ignoring the fact that the sum can be obtained from any number of elements of the array. Recursion is the best approach here.
function subArrayCnt(arr, sum)
return subArrayRecurse(arr, sum, 0, [], 0)
function subArrayRecurse(arr, sum, currentSum, curArray, i)
var count = 0;
//check the current index
var newSum = currentSum + arr[i];
var newSubArray = curArray.concat([arr[i]]);
if(newSum == sum)
console.log('found another: ' + newSubArray);
count++;
if(i + 1 < arr.length)
//try including the current in further sums
count += subArrayRecurse(arr, sum, newSum, newSubArray, i + 1);
//try not including the current in further sums
count += subArrayRecurse(arr, sum, currentSum, curArray, i + 1);
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 8
The 8 combos from the above example are:
1,2,3,2,-3
1,2,2
1,3,1
2,3
2,3,2,1,-3
2,2,1
3,2
8,-3
add a comment |
The other answers seem to be ignoring the fact that the sum can be obtained from any number of elements of the array. Recursion is the best approach here.
function subArrayCnt(arr, sum)
return subArrayRecurse(arr, sum, 0, [], 0)
function subArrayRecurse(arr, sum, currentSum, curArray, i)
var count = 0;
//check the current index
var newSum = currentSum + arr[i];
var newSubArray = curArray.concat([arr[i]]);
if(newSum == sum)
console.log('found another: ' + newSubArray);
count++;
if(i + 1 < arr.length)
//try including the current in further sums
count += subArrayRecurse(arr, sum, newSum, newSubArray, i + 1);
//try not including the current in further sums
count += subArrayRecurse(arr, sum, currentSum, curArray, i + 1);
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 8
The 8 combos from the above example are:
1,2,3,2,-3
1,2,2
1,3,1
2,3
2,3,2,1,-3
2,2,1
3,2
8,-3
add a comment |
The other answers seem to be ignoring the fact that the sum can be obtained from any number of elements of the array. Recursion is the best approach here.
function subArrayCnt(arr, sum)
return subArrayRecurse(arr, sum, 0, [], 0)
function subArrayRecurse(arr, sum, currentSum, curArray, i)
var count = 0;
//check the current index
var newSum = currentSum + arr[i];
var newSubArray = curArray.concat([arr[i]]);
if(newSum == sum)
console.log('found another: ' + newSubArray);
count++;
if(i + 1 < arr.length)
//try including the current in further sums
count += subArrayRecurse(arr, sum, newSum, newSubArray, i + 1);
//try not including the current in further sums
count += subArrayRecurse(arr, sum, currentSum, curArray, i + 1);
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 8
The 8 combos from the above example are:
1,2,3,2,-3
1,2,2
1,3,1
2,3
2,3,2,1,-3
2,2,1
3,2
8,-3
The other answers seem to be ignoring the fact that the sum can be obtained from any number of elements of the array. Recursion is the best approach here.
function subArrayCnt(arr, sum)
return subArrayRecurse(arr, sum, 0, [], 0)
function subArrayRecurse(arr, sum, currentSum, curArray, i)
var count = 0;
//check the current index
var newSum = currentSum + arr[i];
var newSubArray = curArray.concat([arr[i]]);
if(newSum == sum)
console.log('found another: ' + newSubArray);
count++;
if(i + 1 < arr.length)
//try including the current in further sums
count += subArrayRecurse(arr, sum, newSum, newSubArray, i + 1);
//try not including the current in further sums
count += subArrayRecurse(arr, sum, currentSum, curArray, i + 1);
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 8
The 8 combos from the above example are:
1,2,3,2,-3
1,2,2
1,3,1
2,3
2,3,2,1,-3
2,2,1
3,2
8,-3
function subArrayCnt(arr, sum)
return subArrayRecurse(arr, sum, 0, [], 0)
function subArrayRecurse(arr, sum, currentSum, curArray, i)
var count = 0;
//check the current index
var newSum = currentSum + arr[i];
var newSubArray = curArray.concat([arr[i]]);
if(newSum == sum)
console.log('found another: ' + newSubArray);
count++;
if(i + 1 < arr.length)
//try including the current in further sums
count += subArrayRecurse(arr, sum, newSum, newSubArray, i + 1);
//try not including the current in further sums
count += subArrayRecurse(arr, sum, currentSum, curArray, i + 1);
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 8
function subArrayCnt(arr, sum)
return subArrayRecurse(arr, sum, 0, [], 0)
function subArrayRecurse(arr, sum, currentSum, curArray, i)
var count = 0;
//check the current index
var newSum = currentSum + arr[i];
var newSubArray = curArray.concat([arr[i]]);
if(newSum == sum)
console.log('found another: ' + newSubArray);
count++;
if(i + 1 < arr.length)
//try including the current in further sums
count += subArrayRecurse(arr, sum, newSum, newSubArray, i + 1);
//try not including the current in further sums
count += subArrayRecurse(arr, sum, currentSum, curArray, i + 1);
return count;
console.log(subArrayCnt([1,2,3,2,1,8,-3],5));
// 8
answered Mar 25 at 5:26
Sean FSean F
2,136614
2,136614
add a comment |
add a comment |
You can do that using nested loop. This will get all the possible adjacent sums and then you can compare that sum with the given sum and increase count
.
function func(arr,sum)
if(!Array.isArray(arr)) return 0;
let count = 0;
let cur = 0;
for(let i = 0;i<arr.length-1;i++)
cur = arr[i];
for(let j = i+1;j<arr.length;j++)
cur += arr[j];
if(cur === sum)
count++;
break;
if(cur > sum) break;
return count+'';
console.log(func([1,2,3,2,1,8,-3],5)) //3
console.log(func([1,2,3,4],10)) //1
getSubArrayCount([1,2,3,2,1,8,-3],5) should equal to 3. , getSubArrayCount([1,2,3,4],10) these 2 is ok but not fulfill the last requirement "The answer should be valid for any given input." and there is a tip in problem say "please consider the adjacent numbers only"
– SoWeLaugh _
Mar 25 at 5:33
@SoWeLaugh _ Give link to problem I would see it.
– Maheer Ali
Mar 25 at 5:33
the link is not working to external users that is the problem. here is the problem as in site. Find the number of subarrays in an array, which has the given sum. (tip: please consider the adjacent numbers only) Example : Input getSubArrayCount([1,2,3,2,1,8,-3],5) Output should be 3 example combinations (2,3, 3,2, 8,-3) and these are the requirment to be fulfill: The return type should be a string. subArrayCnt([1,2,3,2,1,8,-3],5) should equal to 3. subArrayCnt([1,2,3,4],10) should equal to 1. The answer should be valid for any given input.
– SoWeLaugh _
Mar 25 at 6:09
@SoWeLaugh _ I have edit the code that will check element is array or not. Can you please tell for what particular input its not working
– Maheer Ali
Mar 25 at 6:15
@SoWeLaugh _ The return value should be string. I edited the code now test it
– Maheer Ali
Mar 25 at 6:24
|
show 1 more comment
You can do that using nested loop. This will get all the possible adjacent sums and then you can compare that sum with the given sum and increase count
.
function func(arr,sum)
if(!Array.isArray(arr)) return 0;
let count = 0;
let cur = 0;
for(let i = 0;i<arr.length-1;i++)
cur = arr[i];
for(let j = i+1;j<arr.length;j++)
cur += arr[j];
if(cur === sum)
count++;
break;
if(cur > sum) break;
return count+'';
console.log(func([1,2,3,2,1,8,-3],5)) //3
console.log(func([1,2,3,4],10)) //1
getSubArrayCount([1,2,3,2,1,8,-3],5) should equal to 3. , getSubArrayCount([1,2,3,4],10) these 2 is ok but not fulfill the last requirement "The answer should be valid for any given input." and there is a tip in problem say "please consider the adjacent numbers only"
– SoWeLaugh _
Mar 25 at 5:33
@SoWeLaugh _ Give link to problem I would see it.
– Maheer Ali
Mar 25 at 5:33
the link is not working to external users that is the problem. here is the problem as in site. Find the number of subarrays in an array, which has the given sum. (tip: please consider the adjacent numbers only) Example : Input getSubArrayCount([1,2,3,2,1,8,-3],5) Output should be 3 example combinations (2,3, 3,2, 8,-3) and these are the requirment to be fulfill: The return type should be a string. subArrayCnt([1,2,3,2,1,8,-3],5) should equal to 3. subArrayCnt([1,2,3,4],10) should equal to 1. The answer should be valid for any given input.
– SoWeLaugh _
Mar 25 at 6:09
@SoWeLaugh _ I have edit the code that will check element is array or not. Can you please tell for what particular input its not working
– Maheer Ali
Mar 25 at 6:15
@SoWeLaugh _ The return value should be string. I edited the code now test it
– Maheer Ali
Mar 25 at 6:24
|
show 1 more comment
You can do that using nested loop. This will get all the possible adjacent sums and then you can compare that sum with the given sum and increase count
.
function func(arr,sum)
if(!Array.isArray(arr)) return 0;
let count = 0;
let cur = 0;
for(let i = 0;i<arr.length-1;i++)
cur = arr[i];
for(let j = i+1;j<arr.length;j++)
cur += arr[j];
if(cur === sum)
count++;
break;
if(cur > sum) break;
return count+'';
console.log(func([1,2,3,2,1,8,-3],5)) //3
console.log(func([1,2,3,4],10)) //1
You can do that using nested loop. This will get all the possible adjacent sums and then you can compare that sum with the given sum and increase count
.
function func(arr,sum)
if(!Array.isArray(arr)) return 0;
let count = 0;
let cur = 0;
for(let i = 0;i<arr.length-1;i++)
cur = arr[i];
for(let j = i+1;j<arr.length;j++)
cur += arr[j];
if(cur === sum)
count++;
break;
if(cur > sum) break;
return count+'';
console.log(func([1,2,3,2,1,8,-3],5)) //3
console.log(func([1,2,3,4],10)) //1
function func(arr,sum)
if(!Array.isArray(arr)) return 0;
let count = 0;
let cur = 0;
for(let i = 0;i<arr.length-1;i++)
cur = arr[i];
for(let j = i+1;j<arr.length;j++)
cur += arr[j];
if(cur === sum)
count++;
break;
if(cur > sum) break;
return count+'';
console.log(func([1,2,3,2,1,8,-3],5)) //3
console.log(func([1,2,3,4],10)) //1
function func(arr,sum)
if(!Array.isArray(arr)) return 0;
let count = 0;
let cur = 0;
for(let i = 0;i<arr.length-1;i++)
cur = arr[i];
for(let j = i+1;j<arr.length;j++)
cur += arr[j];
if(cur === sum)
count++;
break;
if(cur > sum) break;
return count+'';
console.log(func([1,2,3,2,1,8,-3],5)) //3
console.log(func([1,2,3,4],10)) //1
edited Mar 25 at 6:24
answered Mar 25 at 4:48
Maheer AliMaheer Ali
21.5k41937
21.5k41937
getSubArrayCount([1,2,3,2,1,8,-3],5) should equal to 3. , getSubArrayCount([1,2,3,4],10) these 2 is ok but not fulfill the last requirement "The answer should be valid for any given input." and there is a tip in problem say "please consider the adjacent numbers only"
– SoWeLaugh _
Mar 25 at 5:33
@SoWeLaugh _ Give link to problem I would see it.
– Maheer Ali
Mar 25 at 5:33
the link is not working to external users that is the problem. here is the problem as in site. Find the number of subarrays in an array, which has the given sum. (tip: please consider the adjacent numbers only) Example : Input getSubArrayCount([1,2,3,2,1,8,-3],5) Output should be 3 example combinations (2,3, 3,2, 8,-3) and these are the requirment to be fulfill: The return type should be a string. subArrayCnt([1,2,3,2,1,8,-3],5) should equal to 3. subArrayCnt([1,2,3,4],10) should equal to 1. The answer should be valid for any given input.
– SoWeLaugh _
Mar 25 at 6:09
@SoWeLaugh _ I have edit the code that will check element is array or not. Can you please tell for what particular input its not working
– Maheer Ali
Mar 25 at 6:15
@SoWeLaugh _ The return value should be string. I edited the code now test it
– Maheer Ali
Mar 25 at 6:24
|
show 1 more comment
getSubArrayCount([1,2,3,2,1,8,-3],5) should equal to 3. , getSubArrayCount([1,2,3,4],10) these 2 is ok but not fulfill the last requirement "The answer should be valid for any given input." and there is a tip in problem say "please consider the adjacent numbers only"
– SoWeLaugh _
Mar 25 at 5:33
@SoWeLaugh _ Give link to problem I would see it.
– Maheer Ali
Mar 25 at 5:33
the link is not working to external users that is the problem. here is the problem as in site. Find the number of subarrays in an array, which has the given sum. (tip: please consider the adjacent numbers only) Example : Input getSubArrayCount([1,2,3,2,1,8,-3],5) Output should be 3 example combinations (2,3, 3,2, 8,-3) and these are the requirment to be fulfill: The return type should be a string. subArrayCnt([1,2,3,2,1,8,-3],5) should equal to 3. subArrayCnt([1,2,3,4],10) should equal to 1. The answer should be valid for any given input.
– SoWeLaugh _
Mar 25 at 6:09
@SoWeLaugh _ I have edit the code that will check element is array or not. Can you please tell for what particular input its not working
– Maheer Ali
Mar 25 at 6:15
@SoWeLaugh _ The return value should be string. I edited the code now test it
– Maheer Ali
Mar 25 at 6:24
getSubArrayCount([1,2,3,2,1,8,-3],5) should equal to 3. , getSubArrayCount([1,2,3,4],10) these 2 is ok but not fulfill the last requirement "The answer should be valid for any given input." and there is a tip in problem say "please consider the adjacent numbers only"
– SoWeLaugh _
Mar 25 at 5:33
getSubArrayCount([1,2,3,2,1,8,-3],5) should equal to 3. , getSubArrayCount([1,2,3,4],10) these 2 is ok but not fulfill the last requirement "The answer should be valid for any given input." and there is a tip in problem say "please consider the adjacent numbers only"
– SoWeLaugh _
Mar 25 at 5:33
@SoWeLaugh _ Give link to problem I would see it.
– Maheer Ali
Mar 25 at 5:33
@SoWeLaugh _ Give link to problem I would see it.
– Maheer Ali
Mar 25 at 5:33
the link is not working to external users that is the problem. here is the problem as in site. Find the number of subarrays in an array, which has the given sum. (tip: please consider the adjacent numbers only) Example : Input getSubArrayCount([1,2,3,2,1,8,-3],5) Output should be 3 example combinations (2,3, 3,2, 8,-3) and these are the requirment to be fulfill: The return type should be a string. subArrayCnt([1,2,3,2,1,8,-3],5) should equal to 3. subArrayCnt([1,2,3,4],10) should equal to 1. The answer should be valid for any given input.
– SoWeLaugh _
Mar 25 at 6:09
the link is not working to external users that is the problem. here is the problem as in site. Find the number of subarrays in an array, which has the given sum. (tip: please consider the adjacent numbers only) Example : Input getSubArrayCount([1,2,3,2,1,8,-3],5) Output should be 3 example combinations (2,3, 3,2, 8,-3) and these are the requirment to be fulfill: The return type should be a string. subArrayCnt([1,2,3,2,1,8,-3],5) should equal to 3. subArrayCnt([1,2,3,4],10) should equal to 1. The answer should be valid for any given input.
– SoWeLaugh _
Mar 25 at 6:09
@SoWeLaugh _ I have edit the code that will check element is array or not. Can you please tell for what particular input its not working
– Maheer Ali
Mar 25 at 6:15
@SoWeLaugh _ I have edit the code that will check element is array or not. Can you please tell for what particular input its not working
– Maheer Ali
Mar 25 at 6:15
@SoWeLaugh _ The return value should be string. I edited the code now test it
– Maheer Ali
Mar 25 at 6:24
@SoWeLaugh _ The return value should be string. I edited the code now test it
– Maheer Ali
Mar 25 at 6:24
|
show 1 more comment
Try this:
<script>
function subArray(arr,sum)
var subArray=new Array();
count=0;
for(var i=0;i<arr.length;i++)
if(arr[i]+arr[i+1]==sum)
subArray[count]=[arr[i],arr[i+1]]
count++;
return subArray;
console.log(subArray([1,2,3,2,1,8,-3],5));
</script>
add a comment |
Try this:
<script>
function subArray(arr,sum)
var subArray=new Array();
count=0;
for(var i=0;i<arr.length;i++)
if(arr[i]+arr[i+1]==sum)
subArray[count]=[arr[i],arr[i+1]]
count++;
return subArray;
console.log(subArray([1,2,3,2,1,8,-3],5));
</script>
add a comment |
Try this:
<script>
function subArray(arr,sum)
var subArray=new Array();
count=0;
for(var i=0;i<arr.length;i++)
if(arr[i]+arr[i+1]==sum)
subArray[count]=[arr[i],arr[i+1]]
count++;
return subArray;
console.log(subArray([1,2,3,2,1,8,-3],5));
</script>
Try this:
<script>
function subArray(arr,sum)
var subArray=new Array();
count=0;
for(var i=0;i<arr.length;i++)
if(arr[i]+arr[i+1]==sum)
subArray[count]=[arr[i],arr[i+1]]
count++;
return subArray;
console.log(subArray([1,2,3,2,1,8,-3],5));
</script>
<script>
function subArray(arr,sum)
var subArray=new Array();
count=0;
for(var i=0;i<arr.length;i++)
if(arr[i]+arr[i+1]==sum)
subArray[count]=[arr[i],arr[i+1]]
count++;
return subArray;
console.log(subArray([1,2,3,2,1,8,-3],5));
</script>
<script>
function subArray(arr,sum)
var subArray=new Array();
count=0;
for(var i=0;i<arr.length;i++)
if(arr[i]+arr[i+1]==sum)
subArray[count]=[arr[i],arr[i+1]]
count++;
return subArray;
console.log(subArray([1,2,3,2,1,8,-3],5));
</script>
answered Mar 25 at 7:38
Suraj SinghSuraj Singh
766
766
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331160%2ffind-the-number-of-subarrays-in-an-array-which-has-the-given-sum%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
what about
1,2,3,2,-3
and2,3,2,1,-3
?– Thomas
Mar 25 at 4:16
here the answer should be valid for any given input also negative numbers should me accept
– SoWeLaugh _
Mar 25 at 4:20
Why you are returning ` return(str[i]); `?
– Omurbek Kadyrbekov
Mar 25 at 4:21
for getting the number of element in str. is it the wrong way?
– SoWeLaugh _
Mar 25 at 4:24
@SoWeLaugh_ no, these are the numbers from your input array and they also have a sum of 5. Are these valid results? If not, why not? What defines a valid result?
– Thomas
Mar 25 at 4:24