Posts

HackerRank: Find Maximum Index Product ( Stack, DS, Histogram Logic )

HackerRank: Find Maximum Index Product ( Stack, DS ) (Find me on HackerRank @ Handle: BishalG ) Problem Summary: According to problem, you are given an array A of length n . For each index - i in the array, calculate IndexProduct(i) which is defined as: IndexProduct(i) = Left(i) * Right(i).  Again, Left(i) and Right(i) has following definition: Left(i) = Index on the left side of the array such that j < i and arr[j] > arr[i] Right(i) = Index on the right side of the array such that j > i and arr[j] > arr[i] If such index does not exist then it would be 0 in both cases. Our task is to find maximum of IndexProduct(i) among all values of i. Solution Idea: I'm explaining here the idea for generating values for Left array. Similar concept can be applied to find the values for Right array. After finding both arrays, we can easily calculate IndexProduct for every i and find the maximum value among them which will be our answer. Calculating Left a...

HackerRank: Poisonous Plants ( Stacks, DS )

HackerRank: Poisonous Plants ( Stack, DS ) (Find me on HackerRank @ Handle: BishalG ) Problem Summary: In this problem, we are given some value , say pesticide levels of each plants. A plant will dies if all plants to its left side have smaller such value than that of its own. We have to calculate the number of days after which no plants will die!! Solution Idea: For every position find out: "How many times it will be hitted from right side", say hits(i) maximum among all hits(i) is the desired answer!! To find this, keep track of values in stack and try to eliminate all values in current stack which are greater than p(i) from each points !! At certain point, if we find element which is to be counted as kth hit counter but that element itself require more than k hits to be stable then, we count that element as hit source of maximum value. In this way, we will not repetitive actions and one element is added and popped from stack at most one time which leads to th...

Problem : Codeforces Round #406 (Div. 1) B [ Legacy ]( Dijakstra, Segment tree)

You are given graph N (10^5) nodes and starting node (S), You have to find min distance needed to go to all other nodes. Connections among nodes are a little bit different in the form of range type queries, which are: (1) you can open a portal from planet u to planet v with cost z. (2) you can open a portal from planet u to any planet with index in range [l, r] with cost z. (3) you can open a portal from any planet with index in range [l, r] to planet u with cost z. Idea: If we tried to make a adjacency matrix normally by traversing from l to r in (2) and (3) type of connection queries, it will surely time out. So, what we have to do is to think little bit about segmenttree representation of array. This representation allow us to make connection matrix only with LOGN adjacency nodes. So that we may proceed further. Lets build two types of segment-tree nodes , say UP segment tree , which will be used for (2) type of query and DOWN segmenttree which will be used for (3)...

CF: 381Div1 ( Sack, DSU on Tree )

CF: 381Div1 ( Sack, DSU on Tree ) ///==================================================/// /// HELLO WORLD !! /// /// IT'S ME /// /// BISHAL GAUTAM /// /// [ bsal.gautam16@gmail.com ] /// ///==================================================/// #include<bits/stdc++.h> # define X first # define Y second # define mpp make_pair # define nl printf("\n") # define SZ ( x ) (int)(x.size()) # define pb ( x ) push_back(x) # define pii pair<int,int> # define pll pair<ll,ll> # define OnBit ( i ) __builtin_popcount(i) ///----------------------------------- # define S ( a ) scanf("%d",&a) # define P ( a ) printf("%d",a) # define SL ( a ) scanf("%lld",&a) # define S2 ( a,b ) scanf("%d%d",&a,&b) # define SL2 ( a,b ) scanf("%lld%lld",...

CF: 138-D World of Darkraft ( Game Theory )

CF : 138 - D World of Darkraft ( Game Theory ) /// Given Matrix of 'L', 'R' or 'X' characters /// Move is: /// 1.) if choose 'L' remove containing second diagnonal /// 2.) if choose 'R' remove containing main diagonal /// 3.) if choose 'X' remove containing both diagonals /// We have to print, If you move first, will you WIN or LOSE? /// Idea: is of Representing both diagonals in DP /// then give all possible move and partision grid /// onto diagnonals & finally find out MEX of all /// dp returns calls; ///==================================================/// /// HELLO WORLD !! /// /// IT'S ME /// /// BISHAL GAUTAM /// /// [ bsal.gautam16@gmail.com ] /// ///==================================================/// #include <bits / stdc + + .h> #define X first #...

CF Div2-88E Interesting Game( Game theory )

CF div2-88E Interesting Game ( Game theory ) ///==================================================/// /// HELLO WORLD !! /// /// IT'S ME /// /// BISHAL GAUTAM /// /// [ bsal.gautam16@gmail.com ] /// ///==================================================/// #include<bits/stdc++.h> # define X first # define Y second # define mpp make_pair # define nl printf("\n") # define SZ ( x ) (int)(x.size()) # define pb ( x ) push_back(x) # define pii pair<int,int> # define pll pair<ll,ll> ///--------------------- # define S ( a ) scanf("%d",&a) # define P ( a ) printf("%d",a) # define SL ( a ) scanf("%lld",&a) # define S2 ( a,b ) scanf("%d%d",&a,&b) # define SL2 ( a,b ) scanf("%lld%lld",&a,&b) ///-----------------------------...

13154 - Extreme XOR Sum (Observation, Precal )

UVA  13154 - Extreme XOR Sum (Observation, Precal ) /// Story Behind This Problem!! /// As problem pattern was similar to another problem which I had faced /// recently on HackerRank( XOR MATRIX ) /// I easily figured out the pattern of NcR. Main Problem was with complexity!! /// But, this problem luckily passed within 2 sec, as there was not strong pretests. /// I heard after contest that complexity can be reduced to O( q*sqrt(n) ). I have no /// proper idea of this Sqrt decomposition solution. /// My solution is based on following idea: /// 1. Observe the patten for different length. /// 2. If ncr[ len ][ j ] is odd then include jth item of that len otherwise exclude. /// 3. Now, As XOR follows cummulative property answer each xor sum of subsegment /// containing odd values only on that len. ///==================================================/// /// HELLO WORLD !! /// /// IT'S ME ...