0

I'm trying to solve a simple array problem and I have encountered some bug.

I input {1, 2, 3, 4, 5, 6} and rotate left by 2, it should give me {3, 4, 5, 6, 1, 2}

I cannot find a reason why it gives me {0, 0, 0, 0, 1, 2} I think my code is correct. I do not like looking at answers but I looked it up but then I still don't see why it is giving me this wrong answer.

I've looked it through more than 1 hour and I still cannot figure it out so I joined this community. Please help me out...

public int[] rotate(int[] arr, int shift, int size) {
    int[] output = new int[size];
    for(int i = 0; i < output.length;i++) {
        int newIndex = (arr.length-shift+i) % arr.length;
        output[newIndex] = arr[i]; 
        }
    return output;
}

My output:

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 2
    I ran your method (`System.out.println (Arrays.toString (rotate (new int[] {1,2,3,4,5,6}, 2 , 6)));`) and got the desired output (`[3, 4, 5, 6, 1, 2]`). – Eran Feb 08 '22 at 08:04
  • 3
    Note that you are already on the right path. Your screenshot shows that you started to PRINT stuff to the console. But: A) the code you are showing to us ... does not have any print statements. Always give us the exact code that matches the output you want to show us. B) your output ... is TEXT, too. This place has excellent markdown support, so: you can show us text as text, you should NOT give us images (unless there is no other way). – GhostCat Feb 08 '22 at 08:04
  • And then: in the end, it is about diligence. Every character you put in your source matters. It matters when you run the compiler, and maybe you compiled your code, then run it ... but later you forgot to rerun the compiler. All such things matter. So: every time, make sure you saved your input, and that what you are running is your latest code, etc. And yes, the key is: you have to learn how to debug things. Writing down the code is just the first step, then it is about figuring "why doesnt it do what I expect it to do". and there, printing ALL important stuff comes in. Or using a debugger. – GhostCat Feb 08 '22 at 08:06
  • 1
    I'm very ashamed... Thank you so much guys. I used print statement and it kept giving me 0s and I was not doing it right at the main itself. Thank you so much, It feels amazing to solve a puzzle and get the right answer as a beginner coder. Hope you guys all have a great day – dabin choi Feb 08 '22 at 08:12
  • 1
    No need to be ashamed. You obviously tried to convey your problem, you had your code there. All fine ... diligence needs to be practiced, too. It is up to you. You can improve your question, and show the "failing" code, that gave you the wrong output. Or ... you might decide to delete your question. As it is not clear what your problem *actually* is. – GhostCat Feb 08 '22 at 08:19

4 Answers4

1

Welcome to Stackoverflow and I think most likely there is something wrong with your output code.

Below is my test code:

public class Test {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6};
        int[] rotate = rotate(a, 2, 6);
        System.out.println(Arrays.toString(rotate));
    }

    public static int[] rotate(int[] arr, int shift, int size) {
        int[] output = new int[size];
        for (int i = 0; i < output.length; i++) {
            int newIndex = (arr.length - shift + i) % arr.length;
            output[newIndex] = arr[i];
        }
        return output;
    }
}

Console output:

[3, 4, 5, 6, 1, 2]
Yawei Xi
  • 11
  • 3
0

You can find the shifted index by adding the shift value with modulo length of array. Now you can add this value to the current index.

int newIndex = (shift+i) % arr.length;
output[i] = arr[newIndex];
DeGo
  • 783
  • 6
  • 14
0

Although facing some list-conversion you could use the standard-library function Collections.rotate(List<?> list, int distance):

public static int[] rotate(int[] arr, int shift, int size) {
    //List<Integer> list = Arrays.asList(arr);  // only works with Wrapper-types not primitive
    var list = Arrays.stream(arr).boxed().collect(Collectors.toList());  // since Java 9: .toList();
    
    Collections.rotate(list, shift);
    
    int[] array = new int[size];  // can also be original: list.size() or arr.length 
    for(int i = 0; i < size; i++) array[i] = list.get(i);
    
    return array;
}

Try the demo on IDEone.

For input {1, 2, 3, 4, 5, 6}; and a shift of 2 (to the right) it results in output:

[5, 6, 1, 2, 3, 4]

for a shift of -2 (to the left) it results in:

[3, 4, 5, 6, 1, 2]

See also:

hc_dev
  • 8,389
  • 1
  • 26
  • 38
0

Your code works well and I see that the output array has the right result, however I suspect that you are not printing/processing this output array in the right direction. See this answer on how to print the array output.

Adding code here for your understanding.

import java.util.Arrays;
public class ProcessArrays{

 public static void main(String []args){
    int[] arr = {1,2,3,4,5,6};
    int[] output = rotate(arr, 2 , 6);
    System.out.println(Arrays.toString(output));
 }
 
public static int[] rotate(int[] arr, int shift, int size) {
    int[] output = new int[size];
    for(int i = 0; i < output.length;i++) {
        int newIndex = (arr.length-shift+i) % arr.length;
        output[newIndex] = arr[i]; 
        }
    return output;
}
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
ScrapCode
  • 2,109
  • 5
  • 24
  • 44