 # [Appfolio Technical Phone Screen] Sum with two consecutive elements the same. Is this the best approach?

“Given an array [1, 1, 2, 3, 4, 4, 4, 5], find the sum such that two consecutive elements are same. For example, 1 + 4 + 4 = 9 as there are two 1s and three 4s”

This is an Appfolio technical question from online, but it’s worded weird. I think the question is to add the current element to the sum if the next element is equal to current element.

My approach is:
`if (nums[i + 1] == nums[i]) sum += nums[i];`

Is this correct, or is there a better approach?

1 Like

So I think a more “google online assessment” rewording of this question might be:

``````Given an array of integers, return the total sum of all
pair-consecutive elements. Two numbers count as a
single pair-consecutive element if they are consecutive,
and are the same value. A number can be
pair-consecutive with the number before, and separately
with the number after itself (if all three numbers are
the same).

Given {2 2 2 2}, for example, there are 3
pair-consecutive elements, as denoted by the
[brackets]: {[2 2] 2 2}, {2 [2 2] 2}, and {2 2 [2 2]}
``````

Let me know if that makes sense to people

1 Like

Another interesting note that I’ve seen many times too is examples given as a sorted array, but nowhere in the question does it mention the input will guaranteed be sorted.

This sometimes happens on Online Assessments where if you make that assumption, you will likely miss hidden test cases in the end. Kinda tricky on the company’s part, but I suppose it could be viewed as testing attention to detail. In any case, a good thing to recognize instantly for all array questions like that!

1 Like

I’m sorry the question is still unclear to me.

Yea, this is definitely a confusing question. One of those where a large part of the time you’re trying to figure out what it’s exactly asking. I think an example would help.

Given [1, 1, 2, 3, 4, 4, 4, 5] return the sum of all “pair-consecutive” elements.

(1 1) in [ (1 1) 2 3 4 4 4 5] is a pair consecutive element because it is the same number next to each other.

2 is not a pair

3 is not a pair

This (4 4) in [1 1 2 3 (4 4) 4 5] is a pair consecutive element because it is the same number next to each other. The next 4 doesn’t matter in this case.

Then, the next (4 4) in [1 1 2 3 4 (4 4) 5] is a pair consecutive element because it is the same number next to each other. The middle 4 can be used for both of these cases, because the pair-consecutive elements are still different from each other.

5 is not a pair

The answer is then one pair-consecutive element of (1s), and two pair-consecutive elements of (4s), which results in a sum of 1 + 4 + 4 = 9

1 Like