There seems to be some issue with input cases in this problem, so don't generalise this to other problems. Best of luck!

Active In

Competitive Programming

International Olympiad in Informatics

Web Development

UI/UX Design

Sphere Online Judge (SPOJ)

Featured Contributions

reply in this discussion

Ashish SBLearning. · 6w

There seems to be some issue with input cases in this problem, so don't generalise this to other problems. Best of luck!

Read more… (22 words)

reply in this discussion

Ashish SBLearning. · 36w

Since the id numbers are not duplicate one could , pre sort and then maintain 2 pointers to find the number of similar numbers in the id in O(n) time.

Read more… (30 words)

reply in this discussion

Ashish SBLearning. · 36w

Beautiful problem :>

Apart from the nice hints given by Tanavya Dimri and Kuntal Majumder , here's my way of putting it :-

Root the tree at a singe point ,consider 1 for simplicity.

Now the tree can be divided into one of the following 3 sub trees:

- The Whole Tree rooted at 1 - one of its's adjacent connected subtree AND the subtree in-turn cut into two .
- The Whole Tree rooted at 1 - one of the vertex weight in the path to its connected subtree - one of the other adjacent connected subtree, the one of the other adjacent connected and the subtree rooted at one of the vertex weight in the path from 1 .

Read more… (119 words)

reply in this discussion

Ashish SBLearning. · 36w

According to your implementation you have considered paths that lead to a[1] and a[2], since you have started your initial paths either from n or n-1, if minimum of sum1=min(min_ar[1],min_ar[2]) comes to be min_ar[1] then this would be the solution since it will satisfy the condition that at least one of the adjacent knights get their dishes, now when min_ar[2] is lesser we have to check for the condition that in you paths we have to choose a[n] cause we are not considering a[1] (since a[1] and a[n] are adjacent , because of circular condition) and also path for a[n-2] should come from a[n] and a[n-3] will be

min_ar[n-3]=ar[n-3]+min(min_ar[n-2],ar[n]+ar[n-1]);

hope this helped:)

Read more… (111 words)

reply in this discussion

Ashish SBLearning. · 36w

@Sushrut you are almost there , your logic seems to be almost correct.

I have made as few a changes in your code so that your idea remains the same and handled a boundary case.

Please look into the code with comments for where i have made the changes.

#include<bits/stdc++.h>using namespace std;int main(int argc , char **argv){int n , sum1 = 0,sum2 = 0 ;cin >> n;int ar[n+1] , min_ar[n+1],path[n+1] ;for(int i = 1 ; i <= n ; i++){cin >> ar[i];}if(n == 1){cout << ar[1] ;return 0 ;}if(n == 2){cout << min(ar[1 ] , ar[2]);return 0;}if(n==3){

Read more… (53 words)

reply in this discussion

Ashish SBLearning. · 36w

Call a dfs for all the employees of the boss , track the maximum wealth up to a level and maintain a max answer , max(ans,maximum_till_now-wealth[i])

#include<iostream>#include<bits/stdc++.h>using namespace std;#define ll long long intint w[100001];vector<int> adj[100001];int ans=0;void dfs(int s, vector<int> adj[],int mx){ans=max(ans,mx-w[s]);mx=max(mx,w[s]);vector<int>::iterator it;for(it=adj[s].begin();it!=adj[s].end();it++){

Read more… (26 words)

reply in this discussion

Ashish SBLearning. · 36w

BFS would be the obvious choice(though one could also implement using dfs). The last test case gives segment fault :/

Read more… (20 words)

reply in this discussion

Ashish SBLearning. · 36w

What you could do is maintain a vector of cycles and push elements to it in your first loop itself where you are counting the number of cycles.

cycles[counts].push_back(j);

Read more… (28 words)

reply in this discussion

Ashish SBLearning. · 36w

If anyone's looking for a graphical representation and implementation of heaps Heaps and Priority Queues

Read more… (15 words)

reply in this discussion

Ashish SBLearning. · 37w

@Sushrut No particular resource on dynamic programming will cover all possible problems on dynamic programming, what you need to do is for a given problem try to solve the problem by breaking it into a smaller problem, look for recurrence relations, look for finding dynamic states which derive their solution from a smaller set of problems, you may find some problems to be efficiently and clearly coded through bottom up approach and others which have nice recurrsive nature which can use memoization to store values for some state.

Dynamic Programming is about finding the states that can be expressed in terms of smaller instances of the problem.

Try hard , to come up with the dp states yourself before you look for hints!

Read more… (123 words)