Alice and his candy

Alice is a kindergarten teacher. She wants to give some candies to children in her class. All the children sit in a line and each of them has a rating score according to his or her performance in the class. Alice wants to give at least 1 candy to each child. If two children sit next to…

Read more...

Trailing Zeroes in Factorial of a Number – FCTRL – SPOJ

Now we can approach it in 2 ways – the first approach is to calculate factorial of a number and then calculate the number of trailing zeros from the result. But for any value greater than 12 we will see integer exceed. So let’s go with the second approach and Find a relation between number and…

Read more...

SPOJ – FCTRL – Factorial

The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little-simplified view). Of course, BTSes need some attention and technicians need…

Read more...

Reverse ArrayList Between 2 Given Indexes

Given an ArrayList and we need to reverse list from given startIndex to endIndex. example – List – [1,2,3,4], startIndex = 1, endIndex = 3 result = [1,4,3,2] List – [1,2,3,4,5], startIndex = 1, endIndex = 4 result = [1,5,4,3,2] Approach 1 – This approach starts swapping elements from startIndex and endIndex until startIndex is greater…

Read more...

HashMap in JAVA

Consider a scenario where we need to store data in the form of key value pairs. Which data structure will we use? Well, The obvious answer which comes in our mind is Map. HashMap is Hash table based implementation of the Map interface which stores data in the form of key value pairs. This class provides…

Read more...

SPOJ: PT07Y – Is it a tree

PT07Y – Is it a tree #dfs   You are given an unweighted, undirected graph. Write a program to check if it’s a tree topology. Input The first line of the input file contains two integers N and M — number of nodes and number of edges in the graph (0 < N <= 10000,…

Read more...

Convert int to String and String to int

This is one of the most occurred scenario where we need to convert a Integer to String or a String to Integer. We can attain that by using methods of wrapper classes. Here we are discussing about Integer -> String or String ->Integer. String to int To convert String to Integer we can use parseInt()….

Read more...