aman_naughty's blog

By aman_naughty, history, 3 days ago, In English,

Problem link

I could not think of any approach but just am getting an intuition of using DSU in reverse or querying offline but I can't think of anything. Someone please help.

I just need an approach not complete code.:)

Read more »

 
 
 
 
  • Vote: I like it
  • -4
  • Vote: I do not like it

By aman_naughty, history, 2 weeks ago, In English,

Since most of the engineering colleges are having placement tests in India, I believe all those students can use this blog for discussion of questions being asked in different colleges. Feel free to post questions in the comment section with relevant descriptions and examples and someone from the community will surely help I believe.

Those who are not interested/Indians please ignore the blog.

Read more »

 
 
 
 
  • Vote: I like it
  • -19
  • Vote: I do not like it

By aman_naughty, history, 5 weeks ago, In English,

I have a problem: Given two numbers in l and r where l<=r and can be as large as 1e18, I want to count the number of palindrome numbers in the range l to r such that they are a square of some palindrome number.

For example, I have L=4 and R=1000

ans = 4 , numbers are 4, 9, 121, 464.

My code:

code

I am getting the wrong answer on this test case

L=92904622 R=232747148

My answer is 3 but the correct answer is 6.

My approach is to generate all palindromes of length at most 10 and then check if their square is also a palindrome.

I am generating palindromes as follows,

For length 1 I have 10 palindromes from 0 to 9 and for length 2 I have 9 palindromes of the form {11, 22,.., 99}.

Now for forming l length palindrome I simply append digits 1 to 9 at the beginning and the end to each of the generated palindromes of length l-2

Can someone point out what am I doing wrong??

Any help is appreciated.

Read more »

 
 
 
 
  • Vote: I like it
  • +9
  • Vote: I do not like it

By aman_naughty, history, 6 weeks ago, In English,

Problem link : link

code : link

I am considering each element as a maximum element and then determining the size of the subarray in which that element can be maximum. If x is my maximum element and y is my minimum element in the optimal subarray, then we have (x-y)<B which implies that my y should be -> x-B< y <= x . Now I store the elements in a vector of pair where the first element is the element at index i and the second element is the index i. Then I sort it based on the first element. After that, I build a segment tree on the modified index array to get the maximum and minimum index of y which will then determine our ans.

In other words, if v[i] is my maximum element of the optimal subarray I need to find the maximum and minimum index of an element in the entire array which has a value y satisfying v[i]-B< y <=v[i]. For this purpose, I am using a segment tree to store the maximum and minimum index in a range and that range I am finding with the help of lower_bound and upper_bound.

If you look at the code maybe you can understand what I am trying to say.

This is giving wrong answer.

Can someone please help me, whether my approach is wrong or am I doing some implementation mistake.

Sorry for poor explanation of my code .

Read more »

 
 
 
 
  • Vote: I like it
  • -6
  • Vote: I do not like it

By aman_naughty, history, 7 weeks ago, In English,

I was wondering if we can find the mother vertex in a directed connected graph by topological sorting. The last finished vertex will be the mother vertex.

Is this approach correct or am I thinking wrong?

Read more »

 
 
 
 
  • Vote: I like it
  • +3
  • Vote: I do not like it

By aman_naughty, history, 7 weeks ago, In English,

Hi, I was going through this point of bfs application from cpalgorithms,

Find the shortest path of even length from a source vertex s to a target vertex t in an unweighted graph: For this, we must construct an auxiliary graph, whose vertices are the state (v,c), where v — the current node, c=0 or c=1 — the current parity. Any edge (a,b) of the original graph in this new column will turn into two edges ((u,0),(v,1)) and ((u,1),(v,0)). After that we run a BFS to find the shortest path from the starting vertex (s,0) to the end vertex (t,0).

I was wondering if this problem can be solved as follows:

Run bfs from src and store the shortest distance from src in an array a.
Run bfs from dest and store the shortest distance from dest in an array b.
Now iterate over all the vertices u and check if a[u]+b[u] is even and if so then get the minimum distance.
Is this approach correct or am I thinking wrong.

Read more »

 
 
 
 
  • Vote: I like it
  • +10
  • Vote: I do not like it

By aman_naughty, history, 2 months ago, In English,

Can someone suggest some method to count the number of prefixes of a given string which are also palindromes? It would be even helpful if I can get a boolean array of the length of string where a true denotes that a prefix up to this index is a palindrome.

Right now I have a simple 2d dp approach where dp[i][j] is true if string i to j is a palindrome, but this is O(n2). Is there some O(n) or O(nlog(n)) method thankyou.

Read more »

 
 
 
 
  • Vote: I like it
  • -3
  • Vote: I do not like it

By aman_naughty, history, 3 months ago, In English,

problem : link

code : link

My approach is also the same as mentioned in the editorial but I am getting wrong answer.

Instead of using two separate memoisation table for true and false I am using only one dp

dp[i][j][w] = the number of ways to put parenthesis such that the result is w

where w is 1 for true and 0 for false;

Can someone tell me where am I wrong

Read more »

 
 
 
 
  • Vote: I like it
  • -7
  • Vote: I do not like it

By aman_naughty, history, 3 months ago, In English,

code

I cannot estimate the time complexity of this code? This code is for optimal binary search tree dp problem.

Is it O(n3) or O(n4) or O(n2)

Someone please help.

Also if someone could tell me some tutorial on how to find time complexity of recursion.

Read more »

 
 
 
 
  • Vote: I like it
  • -12
  • Vote: I do not like it

By aman_naughty, history, 3 months ago, In English,

Problem : link

Code : code

My approach is : For any {i,j} I maintain 4 colors dp[i][j][0],dp[i][j][1],dp[i][j][2] and dp[i][j][3];

dp[i][j][k] denotes the number of ways to fill the board[0..i][0..j] using the color k at position i,j.

To fill dp[i][j][k] we need only summation of (dp[i-1][j][l]+dp[i][j-1][l]) where l varies from 0 to 3 and l is not equal to k.

In simple words to fill i,j with color k number of ways would be we can fill i-1,j with color other than k and i,j-1 with color other than k.

In the end my answer would be summation of dp[3][n][k] where 0<=k && k<=3;

Why is this approach wrong as I am getting wrong answer for n=2 test case?

Any help is appreciated.

Read more »

 
 
 
 
  • Vote: I like it
  • -12
  • Vote: I do not like it

By aman_naughty, history, 3 months ago, In English,

Can someone please help me in binary search. I am confused whether to use

--> l=0 , h = arr.size()-1 and m = (l+h)/2 and while(l<h)

--> l=0 , h = arr.size() and m = (l+h+1)/2 and while(l<h)

--> l=0 , h = arr.size()-1 and m = l+(h-l)/2 and while(l<h)

--> l=0 , h = arr.size()-1 and m = (l+h)/2 and while(l<=h)

It seems that in some cases some of the above give TLE. Can some experienced community member help me which I should use .

I think many of the beginners face this issue while implementing binary search . A little help would be appreciated

So basically there are confusion regarding

while(l<h) or while(l<=h) or while(h-l>1)

m=(l+h+1)/2 or m=l+(h-l)/2 or m=(l+h)/2

h=arr.size() or h=arr.size()-1

Read more »

 
 
 
 
  • Vote: I like it
  • +8
  • Vote: I do not like it

By aman_naughty, history, 3 months ago, In English,

Problem link : link

code :

Spoiler

This gives only 23 points on interviewbit and says that the code might be failing for larger test cases. Can someone give a test case where my code might fail. I have tried a lot of test cases and all those cases match the expected output.

Read more »

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

By aman_naughty, history, 3 months ago, In English,

Problem link: link

My code

Spoiler

We have 2 choices for each element either + or — I am considering both the choices and then picking the optimal one. In the code the dp[i].first = the minimum number of sign inversions dp[i].second = the optimal sum we achieve by either inverting this one or not;

It fails on array 10 4 3 2 1

Can someone point out why my aaproach is wrong?

Read more »

 
 
 
 
  • Vote: I like it
  • +1
  • Vote: I do not like it

By aman_naughty, history, 3 months ago, In English,

I have solved the maximum product subarray problem using divide and conquer. Can someone help me with the time complexity of my solution . Whether it is O(log(n)) or O(nlog(n)) . I am confused please help.

code

My approach is either the max product subarray lie in left half or right half or be passing through the middle. In the third case the answer can be formed from max of following:

left part's maximum suffix product * right part's maximum prefix product

left part's minimum suffix product * right part's minimum prefix product

as elements can be negative

Read more »

 
 
 
 
  • Vote: I like it
  • -20
  • Vote: I do not like it

By aman_naughty, history, 3 months ago, In English,

Problem Link — problem

code —

code

Why is this giving TLE? I am only visiting each array index only once.I am considering this as a graph problem where the neighbors of a node are all the indices reachable from that index.Then BFS will clearly result in the shortest path to reach index n-1.Can someone help me please..

Read more »

 
 
 
 
  • Vote: I like it
  • +2
  • Vote: I do not like it

By aman_naughty, history, 4 months ago, In English,

Your title here...I am following the editorial for this problem 489D - Unbearable Controversy of Being and am getting TLE on test case 7. 55729287

Can someone point out what is the difference between my implementation and Mike's.

Your text to link here...

I am also using brute force of selecting the two end nodes and then counting the number of intermediate nodes which provide path length of 2 between the selected nodes.

Anyone out there???

Read more »

 
 
 
 
  • Vote: I like it
  • -16
  • Vote: I do not like it

By aman_naughty, history, 4 months ago, In English,

Why does this code get accepted 55539853 and this does not 55539543?

I believe the time complexity of 1st submission is O(k(n+m)) for k bfs and then O(nklog(k)) for sorting each node.

Can someone tell the complexity of the second code because of which it is failing.

For getting s smallest element I am using slog(s) complexity in bfs in 2nd submission.

Read more »

 
 
 
 
  • Vote: I like it
  • -12
  • Vote: I do not like it

By aman_naughty, history, 7 months ago, In English,

I want help please don't down-vote.

I want to print the cycle in an undirected graph. I know how to detect cycle in an undirected graph but can't determine how to find the vertices involved in the cycle. Here is the code to find cycle.

code

Can someone please tell me how to modify this so that I can get the vertices in the cycle as well.

I have seen geeksforgeeks article on printing all cycles but I can't understand it. Is there some simpler approach ?

Read more »

 
 
 
 
  • Vote: I like it
  • +3
  • Vote: I do not like it

By aman_naughty, history, 7 months ago, In English,

Is this code to detect cycle in an undirected graph correct. I have run it on a few test cases and it seems good . Can someone from the community verify it.

code:

code

Read more »

 
 
 
 
  • Vote: I like it
  • -35
  • Vote: I do not like it

By aman_naughty, history, 7 months ago, In English,

Problem Link : link

code link : code

In the bfs part I am extracting nodes at a given level and then in help function I am greedily sorting the CT and TT by CT time in acsending order as we want the developer to finish the module code asap so that the tester can attend to it. Also I believe that it doesn't matter if we process level 1 first and then subsequent levels as I am adding the time of each level to my answer. Why is this code giving WA. Any help is appreciated.

P.S- No one was able to solve this problem in the contest and thus it seems an interesting problem.

Read more »

 
 
 
 
  • Vote: I like it
  • -16
  • Vote: I do not like it

By aman_naughty, history, 7 months ago, In English,

Problem : problem

Code :

code

. I want to know the time complexity of my algorithm. Although it runs within 16 ms on leetcode but is it really O(n) time and O(1) space??

Read more »

 
 
 
 
  • Vote: I like it
  • -3
  • Vote: I do not like it

By aman_naughty, history, 7 months ago, In English,

Why does this solution fail at test 21 ?? code : 51444038. and this solution gives AC though code : 51444907 . Aren't the two doing the same thing??

Read more »

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

By aman_naughty, history, 7 months ago, In English,

Problem : problem

My code : code

Why is my DSU solution failing on test 14. Am I applying wrong logic Please help

Read more »

 
 
 
 
  • Vote: I like it
  • -7
  • Vote: I do not like it

By aman_naughty, history, 8 months ago, In English,

I want to know what is the minimum contribution one can have on codeforces. I am currently at -68 , is there anyone with less contribution than this? Also does having negative contribution affect the accessibility of features on codeforces?? Can the community help me beat r_pi in negative contribution. Come on it's time for a change. bring those downvotes assholes xD

Read more »

 
 
 
 
  • Vote: I like it
  • -16
  • Vote: I do not like it

By aman_naughty, history, 9 months ago, In English,

problem link : LINK

submission link : code

I am adding words to the trie and while adding i also store at each node the lexicographically largest encrypted code encountered till now in it. As the encrypted code is unique for all the strings I maintain a hashmap with key=encrypted string and value = original string Then for any query my answer is simply the number of vowels in the original string found after searching the prefix in trie. Why is my code giving wrong answer. Can someone provide some test cases where this fails??

P.S. why are people downvoting this

Read more »

 
 
 
 
  • Vote: I like it
  • -18
  • Vote: I do not like it