0

Circle Summation:

There are N children sitting along a circle, numbered 1,2,...,N clockwise. The ith child has a piece of paper with number ai written on it. They play the following game:

In the first round, the child numbered x adds to his number the sum of the numbers of his neighbors. In the second round, the child next in clockwise order adds to his number the sum of the numbers of his neighbors, and so on. The game ends after M rounds have been played.

Input:

The first line contains T, the number of test cases. T cases follow. The first line for a test case contains two space seperated integers N and M. The next line contains N integers, the ith number being ai.

Output:

For each test case, output N lines each having N integers. The jth integer on the ith line contains the number that the jth child ends up with if the game starts with child i playing the first round. Output a blank line after each test case except the last one. Since the numbers can be really huge, output them modulo 1000000007.

Constraints:

1 <= T <= 15

3 <= N <= 50

1 <= M <= 10^9

1 <= ai <= 10^9

Sample Input:

2

5 1

10 20 30 40 50

3 4

1 2 1

Sample Output:

80 20 30 40 50

10 60 30 40 50

10 20 90 40 50

10 20 30 120 50

10 20 30 40 100

23 7 12

11 21 6

7 13 24

This is a problem in INterviewStreet. I wrote a logic and worked successfully when keeping that entire logic in One method. But when i try to do it by using Two methods, Array 'b' in "main" method getting over written by Array 'a' in "mtd" method. I tried my best and couldn't find the what causing it.

import java.util.Scanner;
public class Solution{ 
    public static void main(String[] args) {
    Solution obj = new Solution();
        Scanner sc = new Scanner(System.in);
        int n,k; // Variables
    long m;
    int t = sc.nextInt();
           for(int rot=0;rot<t;rot++) {
            n = sc.nextInt(); // reading input
            m = sc.nextLong(); // reading input
            long[] b = new long[n]; 
            for(int i =0; i< n; i++)
               b[i] = sc.nextLong(); // Reading values into array
                for(int i=0; i< n;i++ ){  // Loop
                obj.mtd(b,i,n,m); // calling Method "mtd"
            System.out.println();
            }
            System.out.println();
        }
    }
         void mtd(long[] a, int j,int n, long m) // "mtd" method
    {

            int p=0;        
                for(; p< m && j<n; j++,p++){     // logic
                    if(j==0){
                        a[0]= a[0]+a[1]+a[n-1];
                    }else if(j==n-1){
                        a[n-1]= a[0]+a[n-2]+a[n-1];
                    }else{
                        a[j]= a[j-1]+a[j]+a[j+1];
                    }
               }
                do{
                    if(j == n){                 // 
                    for(j=0; p< m && j< n; j++,p++){
                         if(j==0){
                                a[0]= a[0]+a[1]+a[n-1];
                        }else if(j==n-1){
                                a[n-1]= a[0]+a[n-2]+a[n-1];
                        }else{
                                a[j]= a[j-1]+a[j]+a[j+1];
                        }
                   }
            }
            }while(p!=m);
                long z;
             for(int k =0; k< n; k++){
                z=a[k]%1000000007;
                 System.out.print(z+" ");    // printing array after all operations
             }
    }
}

The Output i am getting is :

80 20 30 40 50

80 130 30 40 50

80 130 200 40 50

80 130 200 290 50

80 130 200 290 420

23 7 12

142 261 77

883 1624 2987

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Theja
  • 744
  • 1
  • 7
  • 24

2 Answers2

2

Java is pass by value, but when you pass an Object, the value of the reference to the object is passed on. Using this reference if you make any changes to the contents of the Object, the changes are reflected back.

And Arrays are treated as Objects in Java.

If you intend to pass a copy of your Array and work with it, first create a copy using System.arraycopy() method, and then pass the copy of the Array. Here's an example.

Community
  • 1
  • 1
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
1

Copy your array, then pass the copy as your parameter:

int[] ints = new int[x];
...
int[] intsCopy = new int[ints.length];
System.arrayCopy(ints, 0, intsCopy 0, ints.length);
someMethod(intsCopy);

This is necessary because of Java's behavior when dealing with objects described by @KazekageGaara in his answer.

Kallja
  • 5,362
  • 3
  • 23
  • 33