1. Iterate through given array starting element at index 1 till penultimate element.
2.if following element more than previous element assign current element with following element
3.if following element less than previous element assign current element with previous element
4. return array
I wrote my code as below

public int[] notAlone(int[] nums, int val) { for(int i=1;i<nums.length-1;i++){ if(nums[i-1]<nums[i+1]){ nums[i]=nums[i+1]; } else nums[i]=nums[i-1]; } return nums;}

This is the first time I've read a codingbat problem and I'm not clear on what they're asking for.

It seems to me this should be the solution:

public int[] notAlone(int[] nums, int val) { int[] result = new int[nums.length] ; for (int i = 0 ; i < nums.length ; i++) { boolean isAlone = (i > 0 && i < nums.length-1) && (nums[i-1] != nums[i]) && (nums[i+1] != nums[i]) ; int value = isAlone ? Math.max(nums[i-1], nums[i+1]) : nums[i] ; result[i] = value ; } return result ;}

public int[] notAlone(int[] nums, int val) { int[] result = new int[nums.length] ; for (int i = 0 ; i < nums.length ; i++) { boolean isAlone = (i > 0 && i < nums.length-1) && (nums[i-1] != nums[i]) && (nums[i+1] != nums[i]) ; int value = isAlone ? Math.max(nums[i-1], nums[i+1]) : nums[i] ; result[i] = Math.max(nums[i],value) ; // This line is different } return result ;}

And just like in your solution, I don't use the "val" parameter passed in. Which in itself is very odd.

I hope you can see how this translates from the way the problem is described into code - each step of the problem is one line inside the loop.

Your solution is modifying the array as it goes, which also may be fine but does change the problem (if you start with 1,2,3 and modify "1" to become "5" (e.g.) then when you test "2" its neighbors will be 5,3 instead of 1,3).

It's hard for me to give you advice since I don't really understand why their test answers are correct. I must be misreading their problem statement somehow.

I'd like to see what other experts think of this and what's going on here :)

So if you take this (their) example, you look for 2, it's not at the end or beginning of the array, so it's a valid candidate, so then you replace it with the larger of 1 or 3, i.e. 3 so now you have 1,3,3 for that part of the array, now look for 2 again, (which is at index pos 3) and replace it with the largest of 3 and 5, which is 5, so now we have 1,3,3,5 and another 5, making 1,3,3,5,5 (after the next search for a 2), and then do the final search for a 2, (which I just mentioned), which is at the end of the array, so it is not replaced this time, making 1,3,3,5,5,2.

So if you take this (their) example, you look for 2

@Doug
you said 'I don't use the "val" parameter passed in.' but exactly this is the crucial point of the challenge.

if you look only for one number, two consecutive items with that number either could be neighbors or one or both are alone (beside of the ends). because of that you don't need to use a separate result array but could replace the 'alone' items inplace since the modification of the current array item doesn't change the problem.

pseudocode:

for index := 1 to (num_items - 2) step 1do previous := item[index-1] current := item[index] next := item[index+1] if current == val and current <> previous and current <> next then item[index] := maximum(previous, next) // skip next step index := index+1 end ifend for

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

You're right that if we assume that you should be modifying the array as you go then we get this case:
notAlone([1, 2, 3, 2, 5, 2], 2) → [1, 3, 3, 5, 5, 2]

But then this one seems wrong:
notAlone([1, 3, 1, 2], 1) → [1, 3, 3, 2]

because when we reach "3" it seems to me this is "alone" with neighbors 1 and 1, so it should become 1, so if we modify as we go, we'd get "1,1,2,2" not "1,3,3,2".

You can re-phrase the challenge, (or, rather, re-phrase the algorithm needed to) :

Whenever you find 'val' in the array - provided it is NOT at the beginning NOR the end of the array - then replace it with the largest of its two immediate neighbours.

public int[] notAlone(int[] nums, int val) { //int val=0; for(int i=1;i<nums.length-1;i++){ if(nums[i]==val&&(nums[i-1]<nums[i+1])){ nums[i]=nums[i+1]; } else if(nums[i]==val&&(nums[i-1]>nums[i+1])){ nums[i]=nums[i-1]; } else if(nums[i]==val&&(nums[i-1]==nums[i+1])){ nums[i]=nums[i]; } } return nums;}//you didn't check for val is equal to current //you didn't check that both previous and next are different to current//you didn't take maximum of previous or next for update

in the original challenge they defined 'an item is alone' as 'current item is different to previous item and is different to next item'. you check whether the previous is less than the next, what is not the same.

if(nums[ i ]==val&&(nums[i-1]<nums[i+1])){
nums[ i ]=nums[i+1];
}
else if(nums[ i ]==val&&(nums[i-1]>nums[i+1])){
nums[ i ]=nums[i-1];
}
else if(nums[ i ]==val&&(nums[i-1]==nums[i+1])){
nums[ i ]=nums[ i ];
}

you check for 'nums[ i ]==val' 3 times in all if and else if conditions. it would be easier if you were nesting the if statements.

// outer conditionif (nums[ i ] == val){ // inner condition if (...) { ...

if the condition is true you would assign the maximum of previous and next to current. you could do that by using a maximum function (see 1st comment of DPearson) or by

if (nums[i-1] < nums[i+1]){ nums[ i ] = nums[i+1];}else{ nums[ i ] = nums[i-1];}

note, if it comes to assignment both nums[i-1] and nums[i+1] are different from val. therefore in the next loop cycle the new current nums[i+1] will not be 'alone' since the outer condition that it is equal to val would not apply.

It's starting to look to me as if the implementation at the Coding bat site is wrong. I would have said that your code should pass all the tests, and the answer to the one that it fails on should be 1,1,2,2, which is what you've got.

Walking through the logic, bareback, I'd say for this case :
* find val (which is 1) starting at index 1
result = found at index 1
* get the largest value from the two adjacent
result = 1
* replace val with result
array snapshot = 1,1,1,2
* find next val
result = found at index 2
* get the largest value from the two adjacent
result = 2
*replace val with result
array snapshot = 1,1,2,2
*find next val
result = no more vals
end.

public int[] notAlone(int[] nums, int val) { //int val=0; for(int i=1;i<nums.length-1;i++){ if(nums[i]!=nums[i-1]&&nums[i]!=nums[i+1]&&nums[i]==val&&(nums[i-1]<nums[i+1])){ nums[i]=nums[i+1]; } else if(nums[i]!=nums[i-1]&&nums[i]!=nums[i+1]&&nums[i]==val&&(nums[i-1]>nums[i+1])){ nums[i]=nums[i-1]; } else if(nums[i]!=nums[i-1]&&nums[i]!=nums[i+1]&&nums[i]==val&&(nums[i-1]==nums[i+1])){ nums[i]=nums[i]; } } return nums;}//you didn't check for val is equal to current //you didn't check that both previous and next are different to current//you didn't take maximum of previous or next for update

import java.util.Arrays;public class NotAlone { public static void main(String[] args) { // TODO Auto-generated method stub int[] ar = { 1, 2, 3 }; System.out.println("value==>" + Arrays.toString(notAlone(ar, 2))); } public static int[] notAlone(int[] nums, int val) { // int val=0; for (int i = 1; i < nums.length - 1; i++) { if (nums[i] != nums[i - 1] && nums[i] != nums[i + 1]) { if (nums[i] == val) { if (nums[i - 1] < nums[i + 1]) { nums[i] = nums[i + 1]; } else if (nums[i - 1] > nums[i + 1]) { nums[i] = nums[i - 1]; } else if (nums[i - 1] == nums[i + 1]) { nums[i] = nums[i]; } } } // return nums; } return nums; // you didn't check for val is equal to current // you didn't check that both previous and next are different to current // you didn't take maximum of previous or next for update }}

public int[] notAlone(int[] nums, int val) { // int val=0; for (int i = 1; i < nums.length - 1; i++) { if (nums[i] != nums[i - 1] && nums[i] != nums[i + 1]) { if (nums[i] == val) { if (nums[i - 1] < nums[i + 1]) { nums[i] = nums[i + 1]; } else if (nums[i - 1] > nums[i + 1]) { nums[i] = nums[i - 1]; } else if (nums[i - 1] == nums[i + 1]) { nums[i] = nums[i]; } } } // return nums; } return nums;} // you didn't check that both previous and next are different to current // you didn't take maximum of previous or next for update

you can drop the last else if block since it doesn't change anything.

as already told you can put the check whether nums[ i ] is equal to val into an outer if block and remove this condition from inner if statements.

then you can add a first inner if block where you check that both neighbors were different. and finally you have a most inner block where you check whether the left or the right neighbor is larger.

finally you have (pseudo code)

for each item from nums[1] to nums[len-2]do if item equals val then if previous not equals item and next not equals item then if previous is less than next then item = next else // if previous is greater or equal to next item = previous end if end if end ifend for

as already told you can put the check whether nums[ i ] is equal to val into an outer if block and remove this condition from inner if statements.

then you can add a first inner if block where you check that both neighbors were different. and finally you have a most inner block where you check whether the left or the right neighbor is larger.

i thought i did the same with 3 nested if loops inside for loop as below?

for (int i = 1; i < nums.length - 1; i++) { if (nums[i] != nums[i - 1] && nums[i] != nums[i + 1]) { if (nums[i] == val) { if (nums[i - 1] < nums[i + 1]) { nums[i] = nums[i + 1];

psedu code of logica approach is:
1.loop through given array.
2.VErify the given array elemnt is not equal to prvious or after value
3. if above step true then verify larger among previous and after value
4. return largest of above 2.
5. if equal return same value

the most outer condition is that current item is equal to val.

you may change the conditions without going wrong but without doubt the first criterium to check is that the current item has the right value since it is the precondition request by the caller.

nevertheless your solution is also valid.

Sara

0

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

It seems to me this should be the solution:

Open in new window

But this code passes all the tests:

Open in new window

And just like in your solution, I don't use the "val" parameter passed in. Which in itself is very odd.

I hope you can see how this translates from the way the problem is described into code - each step of the problem is one line inside the loop.

Your solution is modifying the array as it goes, which also may be fine but does change the problem (if you start with 1,2,3 and modify "1" to become "5" (e.g.) then when you test "2" its neighbors will be 5,3 instead of 1,3).

It's hard for me to give you advice since I don't really understand why their test answers are correct. I must be misreading their problem statement somehow.

I'd like to see what other experts think of this and what's going on here :)

Doug