# 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

6/28/2022 1:31:39 PM

Jansen Su14 Answers

New AnswerPlease 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

@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.

@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

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?

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 ...

Yes, I need to learn the way how to implement it without any simple solution, so I can learn about it more

@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?

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

@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?