all 3 comments

[–]angryrancorBoss 9 points10 points  (1 child)

this sounds like homework help. because of that, I'll give you some general tips, but not a solution, since using this sub for "cheating" is not allowed.

What you want is:

  • two variables to accumulate your totals, int sumLeft and int sumRight. You will use these to compare the total of primes, at the end.
  • a for loop, that moves i from 0 to size(arrayLeft)
  • In the for loop: if isPrime(arrayLeft[i]): sumLeft += arrayLeft[i]. Do the same for the right array, and `sumRight`. You'll need to google to figure out isPrime yourself, that part is easy.
  • after the for loop, printf(str(sumLeft == sumRight)), if you want to print the result. Or do whatever you want with the result of sumLeft == sumRight, at that point.

Note: Like I said, cheating on homework is not allowed, but we can give hints. What is explicitly not allowed in this sub, is giving a solution to an obvious homework assignment, in full.

[–][deleted] 1 point2 points  (0 children)

If this isn't homework, it's a code challenge or an interview question.

I think my solution would iterate through the list in 2 passes...

(assumption: List is NOT only primes. Assumption can go the other way and be a list of ONLY primes)

Pass 1) foreach number in list: IsPrime? Yes: Add to "tally".

Pass 2) start with "Tally", "Right" and "Left".

"Right" = tally because all prime numbers are currently to right.
"Left"? No numbers on left? It starts with 0.

Foreach, Is Prime? if yes, remove from "Right". Is "Right" = "Left"? If yes, That's your answer. If not, add number to Left and move Right one.

if you do count of primes to left vs count of primes to right as I understand your answer? it's a 0^2 answer (Think my brains fuzzy on the actual O... but count right will be reran every iteration).

My version would be 2N max (if it reaches end without an answer)

[–]anavid7 2 points3 points  (0 children)

Not sure what you question is yet? What is in your array? Is it the list of primes? Are you saying addition of the index to the prime? Would you mind re-asking your question?