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.
BOOOOM! That was challenging and fun. I wish the language of the challenge were easier to understand. For example, the lastAn should have been named "lastAnswer" so as to be easier to understand. I thought lastAn was some kind of mathematical term. I would recommend the person responsible for making these challenges read "Clean Code" by Uncle Bob Martin!
I completely agree, the wording of the question is terrible. I thought 'lastans' would be assigned the value of 'size'.... Your comment put me back on the right track. Thanks.
Agree the wording is not goode enough. I was not quite understand his explaination why he came up to the output initially. Got it and solved the problemm finally. Thanks.
Agreed, very confusing wording of the question. For me, the use of curly braces in the example made me think, wait, should seqList be a set? But he said it was a list? Does the word sequence suggest it should be ordered? I guess this question reflects real life, when the client cannot express accurately what they want!
Thank you! Thank you! After banging my head on the wall for an hour trying to understand their modulus statement, this cleared it up. Such a poorly written set of instructions.
The values of x and y are the items at queries[i][1] and queries[i][2].
let x = queries[i][1];
let y = queries[i][2];
I had to read the instruction 10 times to understand. What it means by (1 or 2) x y is that in the Ith index of queries, the first value will be 1 or 2, the second value will be what x's value will be and the third will be y's value.
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int N= sc.nextInt();
int q=sc.nextInt();
int lastAnswer=0;
int x=1;
int a[] [] = new int[q][3];
List> seqList = new ArrayList>();
for(int i=0;i
}
x=(a[i][1]^lastAnswer%N);
if(a[i][0]==1)
{
seqList.add(new ArrayList<Integer>());
seqList.get(x).add(a[i][2]);
}
else
{
int s = seqList.get(x).size() - 1;
lastAnswer =seqList.get(x).get(s);
if(lastAnswer!=0)
System.out.println(lastAnswer);
}
}
}
}
what is wrong with this code as it is giving only one positive test case?????
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int q = sc.nextInt();
int inc=0,inb=0;
int last = 0,x;
int a[][] = new int[q][3];
int b[] = new int[100000];
int c[] = new int[100000];
for(int i = 0; i<q ; i++)
{
for(int j=0;j<3;j++)
a[i][j] = sc.nextInt();
x = ((a[i][1]^last)%n); //sequence s1 or s0
if(a[i][0] == 1) // query 1 or 0
{
if(x == 0)
b[inb++] = a[i][2];
else
c[inc++] = a[i][2];
}
else{
if(x == 0)
last = b[a[i][2]%(inb)];
else
last = c[a[i][2]%(inc)];
System.out.println(last);
}
}
}
}
WOULD ANYONE MIND HELPING ME TO FIND WHATS WRONG WITH THIS PROGRAM?
n is number of sequences so ther might possible more then 2 sequences. In that case you need more array like b[],c[].Max possible size of these arrays are q so there you can also save some memory.
yes you are right , you have to do this creating dyanmically allocated array of structures and each structure would contain and array and a variable which will always point to the end of array
Many, many users have repeatedly asked that others not post their code in the discussions. If you wish help with your code, simply say so, and if another user responds yes, then send them a private message. Please.
Is this really true? I've never seen others comment on this, and it's seems to be a function of the discussion board to post your (working) result and compare to others' solutions.
I find it much better to just submit your code, then copy the link and posting it on here. It seems to be a way to deter people from just copying the code and not learning anything. The submission link method requires them to forgo any points if they have not completed the problem.
I wrote this and i am getting Run time error. ALmost same code.
Can anyone tell me why this isn't working.
#!/bin/python3importmathimportosimportrandomimportreimportsys# Complete the dynamicArray function below.defdynamicArray(n,queries):lastNumber=0seqList=[];foriinrange(n):seqList.append([])res=[];fork,x,yinqueries:index=(x^lastNumber)%nifk==1:seqList[index].append(y)print(seqList)else:size=len(seqList[index])print(seqList)print(size)lastNumber=seqList[index][y%size]print(lastNumber)res.append(lastNumber)returnresif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')nq=input().rstrip().split()n=int(nq[0])q=int(nq[1])queries=[]for_inrange(q):queries.append(list(map(int,input().rstrip().split())))result=dynamicArray(n,queries)fptr.write('\n'.join(map(str,result)))fptr.write('\n')fptr.close()
I have a small problem:
when we generate the dynamic array, i used
seqList= [[]] * n
instead of your approach,
but we i used seqList.append(y),
every list in seqList add the element of y.
Do you know why it happened like this?
becuase all the empty sequences youre creating referring to the same object, they all will simultaneously when you make inplace changes to one of the "[]".
My eyes are bleeding here.
Why did you need the "i" for ? If you need each element in a list just ask "for element in list".
If you need to name each element in list just ask for "element1, element2, element3 = list"
And why would you write something twice ?
def dynamicArray(n, queries):
# Write your code here
la=0
a=[]
for i in range(n):
a.append([])
for i in queries:
t=(i[1]^la)%n
if i[0]==1:
a[t].append(i[2])
if i[0]==2:
la=a[t][i[2]%len(a[t])]
print(la)
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
q = int(first_multiple_input[1])
queries = []
for _ in range(q):
queries.append(list(map(int, input().rstrip().split())))
After reading the description several times, trying to map the output to the input, then reading the comments, reading the description again, taking a ten minute break, reading the description again and only after that realizing what the types of query actually means, I have to agree that it was fun too solve. Seriously is there any way to obscure the intention any more? The description is way to hard for the work that has to be done.
When you first read the question you look over at the difficulty level and wonder if "Easy" is relative to a genius. The person who wrote this question definitely needs to rewrite it so it's more clear.
3 years later, and it's still worded in a confusing way. Also, the mathematics appear to use non-standard symbols.
The example gives (1 XOR 7) % 2 = 3. So 1 XOR 7 is 6, but 6 mod 2 gives 0. So apparently they mean integer division when using the % sign??? Who does that??
Edit: Oops, never mind. Re-read it once again, and they do go with 6 mod 2 = 0. But the zero is then used as an index into array1 (3,5), and LastAnswer is set to that. That's where the 3 comes from. Okay, I think I understand it well enough to start programming finally.
I am still wondering if the difficulty I found to understand the challenge was because I need to study more or was because the question was made in a confusing way?
Hello, I'm Fakhruddin. I am new to HackerRank. Sorry for this silly question but I can not understand this specific question at all. If you could please help me understand what the problem wants to convey. It would be really great.
most welcome.
If you find my tutorials helpful, please provide your feedback like, dislike , comment etc. on my video. It motivate me to do more for you all
Very true.. Can't understand what he is trying to explained. I also have sevaral of doubts. i would skip and jump to other challenges rather spending time to understand this. It would help if he could map this to practicle situation if it is not possible to expalin at least.
Challenging, and due to the very obtuse language, not fun. Correct me if I'm wrong, but it's making a simple hashmap using a specific/custom hashing algorithm, right?
If it weren't for previous examples to follow along with, I would not have solved this, and if this were a real world coding challenge to whiteboard, there would be a lot of questions, enough that I assume the interviewer would be graded more poorly than the code. /rant
Dynamic Array
You are viewing a single comment's thread. Return to all comments →
BOOOOM! That was challenging and fun. I wish the language of the challenge were easier to understand. For example, the lastAn should have been named "lastAnswer" so as to be easier to understand. I thought lastAn was some kind of mathematical term. I would recommend the person responsible for making these challenges read "Clean Code" by Uncle Bob Martin!
I completely agree, the wording of the question is terrible. I thought 'lastans' would be assigned the value of 'size'.... Your comment put me back on the right track. Thanks.
Agree the wording is not goode enough. I was not quite understand his explaination why he came up to the output initially. Got it and solved the problemm finally. Thanks.
here is problem solution in python java c++ and c programming. https://programs.programmingoneonone.com/2021/05/hackerrank-dynamic-array-solution.html
Find Solution in Python :) https://github.com/alibaba/nacos/compare/develop...SourabhMishra048:patch-1#diff-7ba6d8d49d41a083c560e01c1a23eb836ddfaebb38a8ea39a28fcaa0e7286ae9
@yashpalsinghdeo1 if you add explanation of your solution and explanation of the question it will be helpfull to us.
your code is almost the same with mine.. but why is it my code having rubtime error in most of the test cases?
Agreed, very confusing wording of the question. For me, the use of curly braces in the example made me think, wait, should seqList be a set? But he said it was a list? Does the word sequence suggest it should be ordered? I guess this question reflects real life, when the client cannot express accurately what they want!
completely agree.. question is not explained well enough to write code..
yeah I too agree to the fact.It became cryptic for me
Also, they say "size", which is non-specific, when they mean "length". Size could be size in memory.
Agreed that the wording was a bit odd.
The usage of "size" instead of length is consistent the Java list interface, but not so much in general.
Just adding a comment because I understood this incorrectly. Size is the y % length of seqList[index].
So it is the length of the sub-array in the seqList array with an index ( seq ) calcuated from the original XOR ( ( x ^ lastAnswer ) % N ).
Size = y % len( seqList[seq] )
Thank you! Thank you! After banging my head on the wall for an hour trying to understand their modulus statement, this cleared it up. Such a poorly written set of instructions.
Took an hour on understand. very confusing problem
I think you guys interpreted problem wrongly for query type 2
Hope its clear now :)
what are the values of x and y ?
The values of x and y are the items at queries[i][1] and queries[i][2].
let x = queries[i][1]; let y = queries[i][2];
I had to read the instruction 10 times to understand. What it means by (1 or 2) x y is that in the Ith index of queries, the first value will be 1 or 2, the second value will be what x's value will be and the third will be y's value.
Hope this hlps.
import java.io.; import java.util.;
public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); int N= sc.nextInt(); int q=sc.nextInt(); int lastAnswer=0; int x=1; int a[] [] = new int[q][3]; List> seqList = new ArrayList>(); for(int i=0;i
Why don't you print lastAnswer when it's 0?
I have the same behaviour in javascript. My success test is the 10th one.
The problem is seq calculation returns a value much bigger than N, the actual length of seqList.
The solution is to add the paranthesis on xor calculation ( a ^ b) % c
There are 2 queries , when to use which query?Like in the example they are executing 3 type 1 query and then type 2. When to switch from 1 to 2?
I had the same doubt. The query formats are: Query: 1 x y(for query 1) & Query: 2 x y(for query 2).
Do we take the input of q (query) or is it given?
it is already given..check the input format section
queries[i][0] represents query type either 1 or 2 queries[i][1] denotes x value queries[i][2] denotes y value
When query is 1 0 1, that is at 0th index if it is 1, it refers to query 1 similarly if query is 2 0 1 that is it refers to query 2
No in real life, you ask the client clarifying questions. If you build using unclear requirements, you will fail.
so true
I found that the example at the bottom contradicted what the explanation of the different queries did. They actually detail 2 different operations.
true. I find it unnecessarily hard to understand. The example is organized in a bad and confusing way.
import java.io.; import java.util.;
public class Solution {
}
WOULD ANYONE MIND HELPING ME TO FIND WHATS WRONG WITH THIS PROGRAM?
n is number of sequences so ther might possible more then 2 sequences. In that case you need more array like b[],c[].Max possible size of these arrays are q so there you can also save some memory.
Thanks a lot friend!I got my mistake.
I have the same problem can you explain??
n = number of sequence lastAns=0 q = number of query sequences S0={} S1={} S2={} . . . Sn={}
for queries type (1) 1 x y index of sequence id = (x^lastAns)%n append y in Si-th sequence
(2) 2 x y index of sequence id = (x^lastAns)%n lastAns = valueAt(y%(size of Si-th sequence)) and pint lastAns
after every query value of lastAns will be updated according to query type.
what is y and x
y and x are integer inputs (given in question)
what is the size of every sequence??
according to me it must be n, but i'm guessing that size of each sequence should be exactly the amount of elements that are present at that moment.
Yes, the sequences start of as size 0 and grow as you add elements in.
so what should we take the size initially
Yeah. That sentence has terrible phrasing.
Sequence is a dynamic list. in Java Arraylist or in C# List is ok to use i think?
Can also use 2D Vector in Cpp
yes i did it with vectors
watch each input line after the first one.
1 0 5
this means, query type 1, x is 0 and y is 5
Thank you so much, understood this now
Thanks! It was unclear.
Thank You So much. Really it was unclear.
oh, without your explanation I've could not figure it out. Thank you!
tnks bro.... i was missing that....
Thanks a lot! the problem statement is clear now.
Thank you! I was soo lost. Thanks for explaining.
hi how to store all those sequences. Because then I need nearly 100 arrays to store.
yes you are right , you have to do this creating dyanmically allocated array of structures and each structure would contain and array and a variable which will always point to the end of array
thanks
Use ArrayList,it will be easier
how to create N number of arraylist dynamically ??
where length is the first int read from the input.
Or you can just create and ArrayList of ArrayLists.
ArrayList> arr=new ArrayList<>();
each element i in the list arr will be another ArrayList.
To avoid errors such as "Unsafe operations",Use the following:-
And to later on perform the operations, use expressions like:
You're wonderful for this man
Many, many users have repeatedly asked that others not post their code in the discussions. If you wish help with your code, simply say so, and if another user responds yes, then send them a private message. Please.
Is this really true? I've never seen others comment on this, and it's seems to be a function of the discussion board to post your (working) result and compare to others' solutions.
I find it much better to just submit your code, then copy the link and posting it on here. It seems to be a way to deter people from just copying the code and not learning anything. The submission link method requires them to forgo any points if they have not completed the problem.
the function dynamicArray is returning a List which is not specified properly what list is get to return?
In c++:
Here why vector > s(n,vector ());
what is used here vector>
it's a vector of vector
what is T,X,y Here
Why did you do lastAnswer=s[(x^lastAnswer)%n][y%s[(x^lastAnswer)%n].size()]; in the column sec for s?
great approach!
we can make it a bit more simple by replacing vector decl. by
what will that do ? will it declare 2d vector of n rows ?
no.
I wrote this and i am getting Run time error. ALmost same code. Can anyone tell me why this isn't working.
Hi ! Just remove your "print" instructions because they perturb the output...
Edit: Why minus votes? Could someone explain?
Bro youre correct, those who downvoted thoughts that adding print in between makes no negative effect on grading.
Thanks! Really wasted a lot of time because of this.
I have a small problem: when we generate the dynamic array, i used seqList= [[]] * n instead of your approach, but we i used seqList.append(y), every list in seqList add the element of y. Do you know why it happened like this?
Thanks verymuch
Use [[] for i in range(n)] instead.
thanks dude!!
Can you please explain why seqList= [[]*n] * n is not working
becuase all the empty sequences youre creating referring to the same object, they all will simultaneously when you make inplace changes to one of the "[]".
if you append a digit to one sublist then the number appends to all the sublists...so its not a good idea d = [[]]*n d[0].append(1)
[[1],[1],...]
lastNumber=seqList[index][y%len(seqList[index])]
It's XOR operation not Addition.
bro how r we taking x and y as given by u
My eyes are bleeding here. Why did you need the "i" for ? If you need each element in a list just ask "for element in list". If you need to name each element in list just ask for "element1, element2, element3 = list" And why would you write something twice ?
Didn't you get a runtime error, cause i've written the same code and got runtime error
this is the solution in python
def dynamicArray(n, queries): # Write your code here la=0 a=[] for i in range(n): a.append([]) for i in queries: t=(i[1]^la)%n if i[0]==1: a[t].append(i[2]) if i[0]==2: la=a[t][i[2]%len(a[t])] print(la)
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
q = int(first_multiple_input[1])
queries = []
for _ in range(q): queries.append(list(map(int, input().rstrip().split())))
dynamicArray(n, queries)
You can also do this to initiate the 2D vector
why did you use y%s[(x^lastAnswer)%n].size()? in here: -
lastAnswer=s[(x^lastAnswer)%n][y%s[(x^lastAnswer)%n].size()];
cause, acc to que, it should be just this much: -
lastAnswer=s[(x^lastAnswer)%n][y];
I also had used vector only but, i am getting segementation fault though, i dont know what is wrong!?
vector>s(n);
Bro your code passes only sample test case . Other test cases gives segmentation fault.
thank you so much... i didn't know that how to declare vector vector > s(n,vector ()); this really helped
it works only for two sequences if there are more than 2 sequences then error
Funny thing is it's a simple piece of code, just 11 lines in python, shame the question is so poorly framed.
After reading the description several times, trying to map the output to the input, then reading the comments, reading the description again, taking a ten minute break, reading the description again and only after that realizing what the types of query actually means, I have to agree that it was fun too solve. Seriously is there any way to obscure the intention any more? The description is way to hard for the work that has to be done.
hey can u plz help me in understanding the question
im still not undestand the question :/ can you please describe in easy way? :)
Agreed - I read the question start to finish twice and still don't completely understand what the question is asking.
When you first read the question you look over at the difficulty level and wonder if "Easy" is relative to a genius. The person who wrote this question definitely needs to rewrite it so it's more clear.
No, the sign of a genius is to be able to explain a subject matter to a child. This guy couldn't explain to me how to work a faucet if he wanted to.
Boy, I had no clue what he was asking about. I did not understand what the problem is.
So true. The question was really abstruse. Kind of infuriated me.
can u pls explain me this question
Agreed! Problem discription is way too confusing.
can u please explain the question?
Hard to read, it made me want to avoid that challenge.
I completely agree, the wording of the question is terrible.
The style of writing used was very much the kind we find in technical books or some textbooks. It is closer to "mathematical language".
3 years later, and it's still worded in a confusing way. Also, the mathematics appear to use non-standard symbols.
The example gives (1 XOR 7) % 2 = 3. So 1 XOR 7 is 6, but 6 mod 2 gives 0. So apparently they mean integer division when using the % sign??? Who does that??
Edit: Oops, never mind. Re-read it once again, and they do go with 6 mod 2 = 0. But the zero is then used as an index into array1 (3,5), and LastAnswer is set to that. That's where the 3 comes from. Okay, I think I understand it well enough to start programming finally.
I am still wondering if the difficulty I found to understand the challenge was because I need to study more or was because the question was made in a confusing way?
hi,
I have uploaded a video tutorial on the same.
I hope this comment will help you to understand the problem.
https://www.hackerrank.com/challenges/dynamic-array/forum/comments/591510
Hello, I'm Fakhruddin. I am new to HackerRank. Sorry for this silly question but I can not understand this specific question at all. If you could please help me understand what the problem wants to convey. It would be really great.
hi,
I hope this comment will help you to understand the problem.
https://www.hackerrank.com/challenges/dynamic-array/forum/comments/591510
thanks!!
most welcome. If you find my tutorials helpful, please provide your feedback like, dislike , comment etc. on my video. It motivate me to do more for you all
thanks~ I'll do it ~
agree, challenge should invoke logical thought process in our mind not lost in understanding the english statments
The description of what they want is almost incomprehensible.
Very true.. Can't understand what he is trying to explained. I also have sevaral of doubts. i would skip and jump to other challenges rather spending time to understand this. It would help if he could map this to practicle situation if it is not possible to expalin at least.
Agreed. For me to understand language of query took time.
The question is very poorly written. For all those who are having doubt, the following should be done for "2 x y" type queries
Agreed, wordings of question is very difficult to understand. It took more time to understand rather than solving this.
Challenging, and due to the very obtuse language, not fun. Correct me if I'm wrong, but it's making a simple hashmap using a specific/custom hashing algorithm, right? If it weren't for previous examples to follow along with, I would not have solved this, and if this were a real world coding challenge to whiteboard, there would be a lot of questions, enough that I assume the interviewer would be graded more poorly than the code. /rant
new easy python solution https://hacker-rank-dsa-python.blogspot.com/2022/05/dynamic-array.html