We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Data Structures
- Arrays
- Dynamic Array
- Discussions
Dynamic Array
Dynamic Array
+ 0 comments Java:
//Write your code here public static List dynamicArray(int n, List> queries) { // Write your code here
List<List<Integer>> temp = new ArrayList<>(); List<Integer> sol = new ArrayList<Integer>(); for(int i=0; i<n; i++){ temp.add(new ArrayList<>()); } int lastAnswer = 0; for(List<Integer> query: queries){ int type = query.get(0); int x = query.get(1); int y = query.get(2); int idx = (x ^ lastAnswer) % n; if(type == 1){ temp.get(idx).add(y); } else if(type == 2){ lastAnswer = temp.get(idx).get( y % temp.get(idx).size() ); sol.add(lastAnswer); } } return sol;
+ 0 comments need sujestion about dynamic errors created
+ 2 comments C#
Here you should do exactly what they have explained at the top with the variable decleration. You can get some idea if you refer the given example properly.
public static List<int> dynamicArray(int n, List<List<int>> queries) { var len = queries.Count(); var arr = new List<List<int>>(); for (int i = 0; i < n; ++i) arr.Add(new List<int>()); var lastAnswer = 0; var answers = new List<int>(); for (int i = 0; i < len; ++i) { var query = queries[i]; var type = query[0]; var x = query[1]; var y = query[2]; var idx = (x^lastAnswer)%n; if (type == 1) arr[idx].Add(y); else { lastAnswer = arr[idx][y % arr[idx].Count()]; answers.Add(lastAnswer); } } return answers; }
+ 0 comments Great stuff, Could you please have a look on my site and let me know what is the big problem you are facing in it.
+ 0 comments Hi, Do anyone know why my output is working properly on vs code and other editor but here output in stdout is empty.`vector dynamicArray(int n, vector> queries) { vector> arr(n,vector()); int lastAnswer = 0; int q_no = queries.at(0).at(1); vector res;
for(int i=1; i<=q_no; i++){ int query_typ = queries.at(i).at(0); int x = queries[i][1]; int y = queries[i][2]; int idx = (x^lastAnswer)%2; if(query_typ==1){ arr[idx].push_back(y); } else if(query_typ==2) { lastAnswer = arr[idx][y]; res.push_back(lastAnswer); } } return res;
}`
Load more conversations
Sort 1931 Discussions, By:
Please Login in order to post a comment