Competitive Programming

Preserve indices while sorting array

Introduction This is a request from one of the readers. This is mostly useful in competitive programming where you want to sort an array by the values, but at the same time the requirement is to preserve the index of each element. If I have to explain using an example, then consider the array below. The array is zero indexed. The first two rows represent the input array and indices of each element in the array. The next two rows represent the sorted array, but the moment you sort it, the index for each element changes. Earlier A was at index 4 but in the sorted array it is at index ...
Read More

Fastest Reach in Snakes and Ladders

Introduction For those who are not familiar with the snakes and ladders game. Here is a link for introduction. So the fastest reach in snakes and ladders, is a modified version of the game. So, you are playing the game called snakes and ladders and you have an enchanted dice. You are the master of the dice and you can command it to get any number between 1 and 6 both inclusive. This means that when you throw the dice, the number on the upper face is totally controlled by you. If you ask for a 5, you get a 5 and so on. Problem Statement - Fastest Reach in Snakes and Ladders The problem in ha...
Read More

Single Source Shortest Path For Undirected Graph

Introduction Single source shortest path for undirected graph is basically the breadth first traversal of the graph. Here the graph we consider is unweighted and hence the shortest path would be the number of edges it takes to go from source to destination. You can find posts on the same topic for weighted graphs, and that is solved using Dijkstra's or Bellman Ford algorithms. This post is written from the competitive programming perspective. Here I want to focus on the details of simplified implementations. Problem Statement - Shortest Path for Undirected Graph Most of the times, the probl...
Read More