0

We have a method in which an array and number of rotations are passed as input and we have to return the array after left rotations. Here is my solution.

static int[] rotLeft(int[] a, int d) {
        int i = 0;
        int logicBreak = d;
        int[] copy = new int[a.length];  // SEE HERE GUYS, WHY THIS WORKS
        int[] copy = a;  // AND WHY NOT THIS,

        while(logicBreak < a.length){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }

        logicBreak = 0;
    
        while(logicBreak < d){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }

        return copy;
    }

I hope that there is no confusion until now and if you have then, open this page. So my problem is really basic. Why the answer works when I do this int[] copy = new int[a.length]; but does not work when I do this int[] copy = a;. Can you tell me the difference, because we are changing all the values of copy array, so what is the matter we make it new int[] or same as the array a. I have created an android app but I am still not getting this array concept, if there is a difference then please tell me.

For your ease here is the full code.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the rotLeft function below.
    static int[] rotLeft(int[] a, int d) {
        int i = 0;
        int logicBreak = d;
        int[] copy = new int[a.length];

        while(logicBreak < a.length){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }

        logicBreak = 0;
    
        while(logicBreak < d){
            copy[i] = a[logicBreak];
            i++;
            logicBreak++;
        }

        return copy;
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] nd = scanner.nextLine().split(" ");

        int n = Integer.parseInt(nd[0]);

        int d = Integer.parseInt(nd[1]);

        int[] a = new int[n];

        String[] aItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int aItem = Integer.parseInt(aItems[i]);
            a[i] = aItem;
        }

        int[] result = rotLeft(a, d);

        for (int i = 0; i < result.length; i++) {
            bufferedWriter.write(String.valueOf(result[i]));

            if (i != result.length - 1) {
                bufferedWriter.write(" ");
            }
        }

        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

0

1 Answer 1

2

When you do the following:

int[] copy = a;

You are just assigning the reference of a to copy. They "refer" to the same location in memory.

Here is a demo

int[] a = {1,2,3};
int[] copy  = a;
a[1] = 100;
System.out.println(Arrays.toString(copy));

Prints

[1,100,3]
Sign up to request clarification or add additional context in comments.

1 Comment

oh, I got it now, I solved my doubts in IntelliJ IDE now. Actually it is not copying the array. It is just referencing. If we want to copy then we have to use a loop or a java library. So that's why my answer was wrong in hackerrank because I was copying array an into a and nothing was changing, funny concept I got today. By the way, check the app I developed - PlayStore Link {play.google.com/store/apps/…}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.