Balancing longest balanced sequence array with javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Balancing longest balanced sequence array with javascript

How to balance and array the code like input 1,2,-2,-1,3,4,-3 the output is 4 because 1,2,-2,-1 is balance one rather than 3,4,-3 You must open parentheses must be closed by the same type of parentheses. this is my code : var ans, s, temp, n, a; n = 7; a = 1,2,-2,-1,3,4,-3; s = []; ans = 0; for (var i = 0, a = n; i < a.length; i += 1) { if (a[i] > 0) { s.append(i); } else { if (a[i] < 0) { if (s && a[i] === -a[s.slice(-1)[0]]) { s.pop(); if (!s) { temp = -1; } else { temp = s.slice(-1)[0]; } ans = max(ans, i - temp); } else { s.append(i); } } } } console.log(ans); please teach me with hard ways not simple ways if can Thank You

28th Jun 2022, 1:31 PM
Jansen Su
Jansen Su - avatar
14 Answers
+ 4
Please try this: const BalanceSeq = arr => { let Seq = []; for(let i = 0; i < arr.length; i++) { if(arr[i]==-arr[i+1]){ for(let a = 0; a < arr.length/2+1; a++) { if(arr[i-a]!=-arr[i+1+a]) { Seq = arr.slice(i-a+1,i+a+1); break } } } } return `Array: [${Seq}] - Length: ${Seq.length}\n`; } console.log(BalanceSeq([1,2,-2,-1,3,4,-3])) console.log(BalanceSeq([7,5,5,1,2,-2,-1,-5,3,4,-3])) console.log(BalanceSeq([3,-4,1,2,-2,-1,3,4])) console.log(BalanceSeq([2,3,2,-8,1,3,-3,-1,8,-2,-3,4,1])) https://code.sololearn.com/WtAIo8flOYXl/?ref=app
28th Jun 2022, 3:57 PM
SAN
SAN - avatar
+ 4
if(arr[i]==-arr[i+1]) for finding the center of sequence (like 2,-2 is center)
28th Jun 2022, 5:14 PM
SAN
SAN - avatar
+ 3
Jansen Su I edited my code for must be start by positive (re cheak please)
30th Jun 2022, 4:57 AM
SAN
SAN - avatar
+ 2
let x = [1,2,-2,-1,3,4,-3]; console.log(x.reduce((a,b)=> a+b, 0))
28th Jun 2022, 1:48 PM
SAN
SAN - avatar
+ 1
@Ipang, the balanced sequence is like 1 2 -2 -1 = balanced sequence that's why the output is 4 because the balance subarray has 4 numbers. If you do like 1 2 -1 -2 this is not balanced. You must have open parentheses must be closed by the same type of parentheses.
28th Jun 2022, 2:49 PM
Jansen Su
Jansen Su - avatar
+ 1
@SAN if(arr[i]==-arr[i+1]) what is this statement mean? for(let a = 0; a < arr.length/2+1; a++) { at this statement why is the arr.length/2+1 like this? if(arr[i-a]!=-arr[i+1+a]) { Seq = arr.slice(i-a+1,i+a+1); break } what does this statement do for the case I know this is for placement and checking the balance but how to explain properly about this statement? return `Array: [${Seq}] - Length: ${Seq.length}\n`; what is this mean? Thank you for the help, I need to understand this more for more experience
28th Jun 2022, 5:02 PM
Jansen Su
Jansen Su - avatar
+ 1
@Ipang 1, 2, -2, -1 // longest balanced sub sequence
29th Jun 2022, 3:24 PM
Jansen Su
Jansen Su - avatar
0
What was the criteria for a balanced sequence here? is it cause the sequence result in zero? as in 1 + 2 + (-2) + (-1) or is it cause the sequence is palindromic, disregarding sign?
28th Jun 2022, 2:38 PM
Ipang
0
Thanks Jansen Su I get it now So your goal was to find the longest balanced sequence or to balance a given sequence. The post's title seem to be asking for how to balance the sequence ...
28th Jun 2022, 2:54 PM
Ipang
0
Yes, I need to learn the way how to implement it without any simple solution, so I can learn about it more
28th Jun 2022, 3:36 PM
Jansen Su
Jansen Su - avatar
0
@SAN I get it now So after we get the middle, this code now on going for(let a = 0; a < arr.length/2+1; a++) { if(arr[i-a]!=-arr[i+1+a]) { Seq = arr.slice(i-a+1,i+a+1); break why must arr.length/2+1? and the if is the checker if the one we check is not balanced like the other we remove the number, right?
28th Jun 2022, 6:34 PM
Jansen Su
Jansen Su - avatar
0
Jansen Su I'm still unclear whether you want to find the longest balanced sub sequence 1, 2, -2, -1 // longest balanced sub sequence Or make the entire array balanced 1, 2, -2, -1, -2, 2, 1 // modify last 3
29th Jun 2022, 12:30 AM
Ipang
0
@SAN if the case for this one console.log(BalanceSeq([2,3,2,-8,1,3,-3,-1,8,-2,-3,4,1])) so I want the case to be like balanced sub sequence must be positive first then negative close so for that result should be 1,3,-3,-1 from your code how to change that to be only open positive and closing negative?
29th Jun 2022, 3:26 PM
Jansen Su
Jansen Su - avatar
0
Привет всем
29th Jun 2022, 6:57 PM
Шодибек Эгамуродзода
Шодибек Эгамуродзода - avatar