Tag Archives: array

Check for Same BSTs from Arrays: GeeksforGeeks

Problem link: http://www.geeksforgeeks.org/check-for-identical-bsts-without-building-the-trees/

Problem Statement:
Given two arrays which represent a sequence of keys. Imagine we make a Binary Search Tree (BST) from each array. We need to tell whether two BSTs will be identical or not without actually constructing the tree.
For example, the input arrays are {2, 4, 3, 1} and {2, 1, 4, 3} will construct the same tree
Continue reading


Move All Zeroes to End of Array : GeeksforGeeks

Problem link: http://www.geeksforgeeks.org/move-zeroes-end-array/

Problem Statement:
Given an array of random numbers, Push all the zero’s of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity is O(n) and extra space is O(1).
Continue reading

FoxAndClassroom: TopCoder Problem 12811

[The problem appeared in TopCoder SRM 594 (Div-2, Level-1)]
Problem link: http://community.topcoder.com/stat?c=problem_statement&pm=12811

Problem Statement:
Fox Ciel is now in high school. The seats in her classroom are arranged into an n by m matrix. The rows are numbered from 0 to n-1 (front to back) and the columns from 0 to m-1 (left to right).
At the beginning, Ciel can choose any of the seats. Then, at the end of each week Ciel will shift one row to the back and one column to the right, wrapping around whenever necessary. Formally, if her current seat is in row r and column c, then her seat next week will be the one in row ((r+1) modulo n) and column ((c+1) modulo m).
Fox Ciel now wonders whether she can sit in all the seats in the classroom if she follows the above procedure. As we already mentioned, she can start in any of the seats. Also, she can attend the school for as many weeks as she wants to. Return “Possible” if she can sit in all the seats and “Impossible” otherwise.
Continue reading

LittleElephantAndBooks: TopCoder Problem 12112

[The problem appeared in TopCoder SRM 592 (Div-2, Level-1)]

Problem link: http://community.topcoder.com/stat?c=problem_statement&pm=12112

Problem Statement:
Little Elephant from the Zoo of Lviv has a bunch of books. You are given a int[] pages. Each element of pages is the number of pages in one of those books. There are no two books with the same number of pages.

You are also given a int number. As a homework from the teacher, Little Elephant must read exactly number of his books during the summer. (Little Elephant has strictly more than number books.)

All other elephants in the school also got the exact same homework. Little Elephant knows that the other elephants are lazy: they will simply pick the shortest number books, so that they have to read the smallest possible total number of pages. Little Elephant wants to be a good student and read a bit more than the other elephants. He wants to pick the subset of books with the second smallest number of pages. In other words, he wants to pick a subset of books with the following properties:

1. There are exactly number books in the chosen subset.
2. The total number of pages of those books is greater than the smallest possible total number of pages.
3. The total number of pages of those books is as small as possible (given the above conditions).

Return the total number of pages Little Elephant will have to read.
Continue reading

Utopian Identification Number: Hackerrank

Problem link: https://www.hackerrank.com/challenges/utopian-identification-number

Problem Statement:
A new identification number is given for every Citizen of the Country Utopia and it has the following format.

1. The string must begin with between 0 and 3 (inclusive) lowercase letters.
2. Immediately following the letters, there must be a sequence of digits. The length of this segment must be between 2 and 8, both inclusive.
3. Immediately following the numbers, there must be at least 3 uppercase letters.

Your task is to find out if a given identification number is valid or not.
Continue reading

Detect HTML Tags: Hackerrank

Problem link: https://www.hackerrank.com/challenges/detect-html-tags

Problem Statement:
In this problem you will use regular expressions to help you detect the various Tags used in an HTML document.

Here are a few examples of tags:

The “p” tag for paragraphs:

<p>This is a paragraph</p>

It is also okay to have one or more spaces before or after the tag name:

<  p >This is also a paragraph</p>

Then, there is also something called a void or an empty tag such as:


Some tags can also have attributes. For example, here we see the “a” tag which is used for adding links to a document.

<a href="http://www.google.com">Google</a> 

There are also tags such as this which haven’t been split into an opening and closing tag:

In the above case, “a” is the tag and “href” is an attribute, the value of which is “http://www.google.com”. Ignore any attributes. Your task is to find out all the tags present in the given document.
Continue reading

String Similarity: Hackerrank

Problem link: https://www.hackerrank.com/challenges/string-similarity

Problem Statement:
For two strings A and B, we define the similarity of the strings to be the length of the longest prefix common to both strings. For example, the similarity of strings “abc” and “abd” is 2, while the similarity of strings “aaa” and “aaab” is 3.

Calculate the sum of similarities of a string S with each of it’s suffixes.
Continue reading