-1

Here i try to swap position of '9', so it comes right after a '4'.

Input : int[] nums1 = { 1, 4, 1, 9 };

Output: [1, 4, 9, 1]

And the code:

int loc4 = 0;
int temp = 0;

for (int i = 0; i < arr.length; i++) {
    if (arr[i] == 4) {
        loc4 = i;   
    }

    if (arr[i] == 9) {
        for (int j = i; j > 0; j--) {
            temp = arr[i];
            arr[i] = arr[loc4 + 1];
            arr[loc4 + 1] = temp;
        }
    }
}

The problem with this code is that once there's multiple 4s and 9s, it ignores the duplicates.

So i tried adding a repetition count and a continue statement, for when rep count is more than 1, but that doesn't seem to work?

int loc4 = 0;
int temp = 0;
int rep = 0;
for (int i = 0; i < arr.length; i++) {
    if (arr[i] == 4) {
        loc4 = i;
        rep++;
    }

    if (arr[i] == 9) {
        for (int j = i; j > 0; j--) {
            temp = arr[i];
            arr[i] = arr[loc4 + 1];
            arr[loc4 + 1] = temp;

            if (rep > 1)
                continue;
        }   
    }
}

So when my input is: int[] nums2 = { 1, 4, 1, 9, 9, 4, 1 };

My output should be: [1,4,9,1,1,4,9]

And what i'm getting is: [1, 4, 9, 1, 9, 4, 1]

Note that when there're singular 4s and 9s or when they're already in order, nothing will take place.

Belphegor
  • 4,456
  • 11
  • 34
  • 59
cosmo
  • 83
  • 1
  • 1
  • 7

1 Answers1

0

First, you should check what continue does: What is the "continue" keyword and how does it work in Java?

Here is a solution that is kind of a different algorithm than your's. (I hope i didn't leave any edge cases open)

public static void foo(int[] arr)
    {
        int i = 0;
        while(i != arr.Length)
        {
            if(arr[i] == 4 || arr[i] == 9)
            {
                if(arr[i] == 4)//find the next 9 and swap with arr[i+1]
                {
                    int j = i+1;
                    while(arr[j] != 9 && j < arr.Length)
                    {
                        j++;//inc j while arr[j] is not 9
                    }
                    if(arr[j] == 9)
                    {
                        int temp = arr[i + 1];
                        arr[i + 1] = arr[j];
                        arr[j] = temp;
                        i += 2; //skip over '9' that just replaced
                    }
                    else
                    {
                        i++;// didn't find '9' so move on to next index 
                    }
                }
                else if(arr[i] == 9) //find the next '4' and swap with the number that comes after it, and don't increament i if found..
                {
                    int j = i + 1;
                    while (arr[j] != 4 && j < arr.Length)
                    {
                        j++;//inc j while arr[j] is not '4'
                    }
                    if(arr[j] == 4 && j < arr.Length)
                    {
                        int temp = arr[i];
                        arr[i] = arr[j+1];
                        arr[j+1] = temp;
                    }
                    else
                    {             
                        i++;  // didn't find '4' so move on to next index             
                    }
                }
            }
            else // not 4 or 9
            {
                i++; //move on to next index
            }
        }
    }
Community
  • 1
  • 1
Aviv
  • 58
  • 5