https://ide.geeksforgeeks.org/u20ZmQpun2-------What’s the error in the code?
Only one test case is passing upon submission and the compiler is showing ‘Sucess’ in message upon submission and also output of sample input is also correct.
Mike and HashTrick problem
Yeah it is not passing all test cases as the logic is not correct.
This is what you have written:
for(int i = 0;i<arr.length;i++){
if(!hmap.containsKey(arr[i])){
hmap.put(arr[i], count);
al.add(arr[i]);
}else{
hmap.replace(arr[i], count);
}
count++;
}
This is what Mike did:
for(int i=0;i<n;i++) // Loop through elements of array
{
if(hash[A[i]] == -1) // A[i] was not assigned any hash before
{
hash[A[i]] = count; // Assigning hash to A[i]
count++;
continue;
}
for(int j = 0;j<i;j++)
{
if(hash[A[j]] > hash[A[i]]) // All the hashes greater than previous hash of A[i] are decremented.
hash[A[j]]--;
}
hash[A[i]] = count - 1; // Assigning a new hash to A[i]
}
see that’s what i m asking what is wrong in logic i have applied exact same logic as described in hint video but whatever are the error in the logic please make corrections ans point them out
You are not correctly applying this step in your code it is missing
for(int j = 0;j<i;j++)
{
if(hash[A[j]] > hash[A[i]]) // All the hashes greater than previous hash of A[i] are decremented.
hash[A[j]]--;
}
hash[A[i]] = count - 1; // Assigning a new hash to A[i]
}
ok but in video they told to build hashmap of indicies if its a new number than its index is current value of count varible and it is inserted in arrayList and if it is a repeating number than its index is updated in hashmap with count present value and it is not added in the arraylist now the arraylist thus formed only contain unique numbers and now we have to sort the arraylist according to the indices in the hashmap… i have also sent screen shot of the hind video for your reference in chat with TA option … the code u are showing is just and algo how to do this work in the question we have to implement it… according to this hint video… please reconsider my doubt and if possible make correction and check the ss in the chat
https://ide.geeksforgeeks.org/u20ZmQpun2-------What’s the error in the code? Only one test case is passing upon submission and the compiler is showing ‘Sucess’ in message upon submission and also output of sample input is also correct.---------it had been 2 days since i posted the doubt but it is not resolved please resolve the doubt
call me on 7838702561 so that I can easily resolve your doubt