Wednesday, 29 March 2017

Find Kth Element which has at least one divisor < 50


To find the k'th number whose at least one prime divisor is less than 50.

Prerequisite
Inclusion-Exclusion Principle

Solution
we just need to count how many number having at least one prime divisor less than 50.

We are trying to solve this question for smaller constraints
To find the k'th number who has at least one prime divisor is less than 5.
only prime numbers : {2,3}

counting divisor till X
count_div = X/2 + X/3 - X/(2*3)

So basically we need to add divisor of each element and subtract divisor of both once as they are added twice(once with 2 and once with 3)

So this Idea can be extended to any number of primes using Inclusion-Exclusion Principle.

P = p1*p2*....*pn

if n is odd
count_div += X/P
else
count_div -= X/P

For more clarification see the code.


Problem Link




Ramesh Chandra



View Ramesh Chandra's profile on LinkedIn



Tuesday, 28 March 2017

Find Kth Element In Ascending Order And Delete It, Perform Q-Queries


You are given N numbers and Q queries. There is only one query type, you have to print the Kth number when all the remaining numbers are arranged in ascending order. You have to remove that number from the list after a query.

Prerequisite

Balanced BST

Solution

Basic Idea is in-order traversal of BST gives us sorted array.

So we will traverse Balanced BST in in-order fashion and print the number and mark as deleted(put 0 at that place) after printing.



Now we just need to traverse the tree in in-order traversal based on Kth number and how many elements current node contains in left and right subtree.

what all information is required to store in BST Node.

* value : actual element value
* left_nodes : number of nodes in left
* right_nodes : number of nodes in right



for more clarification, See the code.
If anything is not clear in post, you can ask [me]



Problem Link




View Ramesh Chandra's profile on LinkedIn

Ramesh Chandra

Sunday, 5 March 2017

Younger Brother[CODECHEF] : Grundy Number


GRUNDY NUMBER

The game consists of C boards. Each board i is a grid of dimension ni x mi.

Rules of the game:

- A coin is placed at (1,1) on every board initially.

- Each one takes a turn alternatively.

- In one turn, a player can choose any one board and move a coin from a cell (i,j) to one of the following cells:

(i+1,j) OR (i+2,j) OR (i,j+1) OR (i,j+2) OR (i+1,j+1) OR (i+2,j+2).

- A coin cannot be moved out of the board at any point during the game.

- A coin cannot be moved once it reaches the cell (n,m) where n and m are the dimensions of the board of that coin.

- A player MUST make one valid move.


Problem Link
faster Solution




View Ramesh Chandra's profile on LinkedIn

Ramesh Chandra