With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.
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;
}
Expected Run
notAlone([1, 2, 3], 2) → [1, 3, 3] [1, 3, 3] OK
notAlone([1, 2, 3, 2, 5, 2], 2) → [1, 3, 3, 5, 5, 2] [1, 3, 3, 5, 5, 2] OK
notAlone([3, 4], 3) → [3, 4] [3, 4] OK
notAlone([3, 3], 3) → [3, 3] [3, 3] OK
notAlone([1, 3, 1, 2], 1) → [1, 3, 3, 2] [1, 1, 2, 2] X
notAlone([3], 3) → [3] [3] OK
notAlone([], 3) → [] [] OK
notAlone([7, 1, 6], 1) → [7, 7, 6] [7, 7, 6] OK
notAlone([1, 1, 1], 1) → [1, 1, 1] [1, 1, 1] OK
notAlone([1, 1, 1, 2], 1) → [1, 1, 1, 2] [1, 1, 2, 2] X
other tests
X
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 ;
}
So if you take this (their) example, you look for 2
for index := 1 to (num_items - 2) step 1
do
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 if
end for
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;
}
because when we reach "3"
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
Expected Runi passed one other test but failing last one. please advise
notAlone([1, 2, 3], 2) → [1, 3, 3] [1, 3, 3] OK
notAlone([1, 2, 3, 2, 5, 2], 2) → [1, 3, 3, 5, 5, 2] [1, 3, 3, 5, 5, 2] OK
notAlone([3, 4], 3) → [3, 4] [3, 4] OK
notAlone([3, 3], 3) → [3, 3] [3, 3] OK
notAlone([1, 3, 1, 2], 1) → [1, 3, 3, 2] [1, 3, 3, 2] OK
notAlone([3], 3) → [3] [3] OK
notAlone([], 3) → [] [] OK
notAlone([7, 1, 6], 1) → [7, 7, 6] [7, 7, 6] OK
notAlone([1, 1, 1], 1) → [1, 1, 1] [1, 1, 1] OK
notAlone([1, 1, 1, 2], 1) → [1, 1, 1, 2] [1, 1, 2, 2] X
other tests
OK
&&(nums[i-1]<nums[i+1])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 ];
}
// outer condition
if (nums[ i ] == val)
{
// inner condition
if (...)
{
...
if (nums[ i ] != nums[i-1] && nums [ i ] != nums[i+1])
if (nums[i-1] < nums[i+1])
{
nums[ i ] = nums[i+1];
}
else
{
nums[ i ] = nums[i-1];
}
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
Expected Run
notAlone([1, 2, 3], 2) → [1, 3, 3] [1, 3, 3] OK
notAlone([1, 2, 3, 2, 5, 2], 2) → [1, 3, 3, 5, 5, 2] [1, 3, 3, 5, 5, 2] OK
notAlone([3, 4], 3) → [3, 4] [3, 4] OK
notAlone([3, 3], 3) → [3, 3] [3, 3] OK
notAlone([1, 3, 1, 2], 1) → [1, 3, 3, 2] [1, 3, 3, 2] OK
notAlone([3], 3) → [3] [3] OK
notAlone([], 3) → [] [] OK
notAlone([7, 1, 6], 1) → [7, 7, 6] [7, 7, 6] OK
notAlone([1, 1, 1], 1) → [1, 1, 1] [1, 1, 1] OK
notAlone([1, 1, 1, 2], 1) → [1, 1, 1, 2] [1, 1, 1, 2] OK
other tests
OK
* get the largest value from the two adjacentthat is the 3rd condition. the 2nd condition was that the current item is 'alone' what is not the case as the previous item is 1.
if there are values before and after it,
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
}
}
i refactoredpublic 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
Expected Run
evenOdd([1, 0, 1, 0, 0, 1, 1]) → [0, 0, 0, 1, 1, 1, 1] [1, 0, 1, 0, 0, 1, 1] X
evenOdd([3, 3, 2]) → [2, 3, 3] [3, 3, 2] X
evenOdd([2, 2, 2]) → [2, 2, 2] [2, 2, 2] OK
evenOdd([3, 2, 2]) → [2, 2, 3] [3, 2, 2] X
evenOdd([1, 1, 0, 1, 0]) → [0, 0, 1, 1, 1] [1, 1, 0, 1, 0] X
evenOdd([1]) → [1] [1] OK
evenOdd([1, 2]) → [2, 1] [1, 2] X
evenOdd([2, 1]) → [2, 1] [2, 1] OK
evenOdd([]) → [] [] OK
other tests
X
Your progress graph for this problem
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 if
end for
This part is very badly phrased : unless the array is short, there are going to always be values "before and after it".
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.
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];
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.