https://en.m.wikipedia.org/wiki/Sieve_of_Eratosthenes

Go through the Wikipedia article for sieve of eratosthenes for better understanding of computational and algorithmic complexity.

Active In

Competitive Programming

International Olympiad in Informatics

Web Development

UI/UX Design

Sphere Online Judge (SPOJ)

Featured Contributions

comment in this discussion

Ashish SBLearning. · 2y

https://en.m.wikipedia.org/wiki/Sieve_of_Eratosthenes

Go through the Wikipedia article for sieve of eratosthenes for better understanding of computational and algorithmic complexity.

Read more… (18 words)

comment in this discussion

Ashish SBLearning. · 2y

Be careful of mod of negative values.

F(n) is always >= F(m)

But F(n)mod(10^9+7) is not always >= F(m)mod(10^9+7).

Read more… (19 words)

comment in this discussion

Ashish SBLearning. · 3y

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)

comment in this discussion

Ashish SBLearning. · 3y

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)

comment in this discussion

Ashish SBLearning. · 3y

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)

comment in this discussion

Ashish SBLearning. · 3y

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)

comment in this discussion

Ashish SBLearning. · 3y

@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)

comment in this discussion

Ashish SBLearning. · 3y

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)

comment in this discussion

Ashish SBLearning. · 3y

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

Read more… (20 words)

comment in this discussion

Ashish SBLearning. · 3y

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)