Home > Enterprise >  C# locate value in a array and move it to right
C# locate value in a array and move it to right

Time:10-03

My task is to find a element in the array that is the same as given value, and then take it to right while maintaining the order of other elements. An example (Test Case):

{1, 2, 0, 1, 0, 1, 0, 3, 0, 1} for value = 0 => {1, 2, 1, 1, 3, 1, 0, 0, 0, 0}

While my code could do that above example, what it could not do is a very specific case: if the element in array equals value and the next element also equals value it will not shift the element. Again a example:

{ 1, int.MinValue, int.MinValue, int.MaxValue, int.MinValue, -1, -3, -9, 1 }, value = int.MinValue

Expected result: { 1, int.MaxValue, -1, -3, -9, 1, int.MinValue, int.MinValue, int.MinValue }

Result with my code: { 1, int.MinValue ,int.MaxValue, -1, -3, -9, 1, int.MinValue, int.MinValue }

I thought shifting is the one solution, is it? I am having a lot of problems with it, I also tried Array.Copy but there were problems the result was always out of range.

How can I make it so that it will shifts/rotates correctly in all cases?

Code:

        static void Main(string[] args)
        {
            int[] source = new int[] { 1, int.MinValue, int.MinValue, int.MaxValue, int.MinValue, -1, -3, -9, 1 };
            int value = int.MinValue;

            for (int i = 0; i < source.Length; i  )
            {
                if (source[i] == value)
                {
                    LeftShiftArray(source, i);
                }
            }

            for (int i = 0; i < source.Length; i  )
            {
                Console.WriteLine(source[i]);
            }
        }

        public static void LeftShiftArray(int[] source, int i)
        {
            var temp1 = source[i];
            for (var j = i; j < source.Length - 1; j  )
            {
                source[j] = source[j   1];
            }

            source[source.Length - 1] = temp1;

        }

Now this

CodePudding user response:

I have a simple approach to solve this problem. Run a loop, You keep on counting the numbers which are not equal to your number. And keep assigning to arr[count]. Then increment the count. And then finally you will be left to assign all the remaining numbers with the given number.

static void MoveToEnd(int []arr, int n)
{
    int count = 0; 

    for (int i = 0; i < arr.Length; i  )
        if (arr[i] != n)
            arr[count  ] = arr[i]; 

    while (count < arr.Length)
        arr[count  ] = n;
}

Please note I have typed this answer from phone, so please avoid typing mistakes.

CodePudding user response:

This is a classic off by one error. Think about it like this:

Let's say you are moving all 0s to the back (value = 0). When you find a zero at some position, let's say source[2], you turn your array from this:

      1 1 0 0 1 1    
source[2] ^

To this:

      1 1 0 1 1 0
source[2] ^

Now that you've shifted the array, the next step in your code is to increase i by 1. That means the next comparison you make will be with source[3]. In the above array, that looks like this:

      1 1 0 1 1 0
  source[3] ^

Do you see the problem? Let me know if not and I can explain further.

PS. There are a couple of issues with the other code that was posted that will probably stop you from getting full points if you were to turn it in for an assignment :)

  • Related