import java.util.*;
import java.lang.*;
import java.io.*;
/*In this question i am modifying the array in every step
and adding them to hashmap.
But after debugging i found out that hashmap is
containing only the last modified array. */
class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt(); /*Testcases*/
while(t-->0) /*Iterating t times */
{
/*Size of array*/
int n=sc.nextInt();
long k=sc.nextLong();
/*Creating a hashmap*/
Integer arr[]=new Integer[n]; //Initialising the array arr
HashMap<Integer,List<Integer>> map=new HashMap<>();
/*Input array elements*/
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
/*Converting array to list*/
List<Integer> list=Arrays.asList(arr);
/*Inserting values to hashmap*/
map.put(0,list);
/*for debugging purpose*/
System.out.println(map);
/*mid value of array*/
int midvalue=arr[(0+(n/2))];
/*In this step i am modifying the array in every step
and adding them to hashmap.
But after debugging i found out that hashmap is
containing only the last modified array. */
for(int i=0;i<n*3-1;i++)
{
/*Performing some operations on array*/
int a=arr[(i%n)];
int b=arr[(n-(i%n)-1)];
int c=a^b;
arr[i%n]=c;
/*When array is changed,we convert that array to list and store it into map*/
List<Integer> k1=Arrays.asList(arr);
System.out.println(k1); /*for debugging*/
map.put(i+1,k1); //storing the new modified array to map
}
/*debugging*/
System.out.println(map);
}
}
}
Я тоже пытался использовать метод Collections.addAll. Хотя этот метод работает, но он дает мне TLE в моем коде. Поэтому я не хочу использовать этот метод
for(int i=0;i<n*3-1;i++)
{
int a=arr[((i%n))];
int b=arr[(n-(i%n)-1)];
int c=a^b;
arr[i%n]=c;
List<Integer> k1=new ArrayList<>();
Collections.addAll(k1,arr);
map.put(i+1,k1);
}
ожидаемый результат: {0 = [3, 7], 1 = [4,7], 2 = [4,3], 3 = [7,3], 4 = [7, 4], 5 = [3, 4]}
фактический результат: {0 = [3, 4], 1 = [3, 4], 2 = [3,4], 3 = [3, 4], 4 = [3, 4], 5 = [3, 4]}