Topcoder SRM 553 Div-II-500 tutorial

The problem

Problem statement (requires login)

Summary of the statement is:

Given an array of non-negative integers program[], an integer wantedResult, and pseudocode of a peculiar program. This program takes program[] as argument, and reads it through. There’s also a stack initially filled with infinite number of 0’s. If program[i] = 0 it pops topmost two elements from the stack, adds them up, and pushes the sum into the stack, otherwise it pushes program[i] into stack. At the end of execution, it pops and prints the top element of the stack.

The trouble is exactly one of program[]s elements is -1. But the program operates only on non-negative integers. Return the smallest non-negative integer with which -1 can be replaced such that the program’s output is wantedResult. If no such non-negative integer exists, return -1 instead.

Continue reading

Posted in programming | Tagged , , | Leave a comment

Topcoder SRM 551 Div-II-500 tutorial

I didn’t participate in the SRM, but solved this problem later. What follows is strictly a personal account of my attempt to this. I tried to be elaborate to make this accessible to newcomers. Hoping people facing difficulty in solving this problem may find this post helpful. My blog revives with this post!

The problem

Problem statement (requires login)

Excluding the unnecessary details, the problem statement reduces to:

Given a string chocolates[] and an integer maxSwapsand you are allowed to swap any two adjacent elements of the string. Return length of the longest substring of identical characters achievable by no more than maxSwaps swaps.

First thoughts

Observing the sample cases, I devised a trivial one: AAABCAA. In this case BC is sandwiched between two segments of A’s. With 4 swaps, we can move BC to the right, and get the string AAAAABC. Also, 4 is the optimal number of swaps required to get a longer substring of identical characters from the given string.

Up to this point observation confirmed that, joining segments of identical characters is the only way to produce longer segments. But, how to join them with minimum possible swaps? For trivial cases like AAABCAA, solution is quite obvious—moving all sandwiched characters to either left or right whose length is shorter, a greedy approach. But, what about cases with more than two segments of identical characters, like AAABCABBACAA? What kind of complication do they introduce? Still no idea. Once I am with an idea how to do this, the rest is simple—for all possible O(n^2) substrings of chocolates[] flanked by segments of identical characters, find the minimum number of swaps (swap) needed to take all characters not identical to flanks’ out, if swap \leq minSwaps take the maximum of the lengths among all such substrings  of identical characters obtained.

Continue reading

Posted in programming | Tagged , , , | Leave a comment

CSE 141: Modular Arithmatic and Congruences


1. If m|a-b then a \equiv b \pmod{m}.

Where a and b are integers and m is a positive integer.

2. If a\,\bmod\,m=r

and b\,\bmod\,m=r

then a \equiv b \pmod{m}.

Where a and b are integers and m is a positive integer.

3. If a=b+km

then, a \equiv b \pmod{m}.

Where a, b and k are integers and m is a positive integer.




So 12 \equiv 5 \pmod{7}

Algebra of Congruence:

If a \equiv b \pmod{m}

and c \equiv d \pmod{m}

then a+c \equiv b+d \pmod{m}

also ac \equiv bd \pmod{m}.

Posted in computer science, discrete mathematics, number theory | Tagged , , , | Leave a comment

The Princeton Companion to Mathematics

Princeton University Press has recently published a book called The Princeton Companion to Mathematics. The chief editor of the book is Professor Timothy Gowers, who is a Fields Medal winner. It is a reference for every mathematician or anyone interested in Mathematics. Although world leaders in Mathematics have contributed to the book but, it will attract a wide range of audience.

Princeton Companion to Mathematics

Princeton Companion to Mathematics

It discusses almost all of the branches of present day mathematics, fundamental concepts of mathematics, biographical essays on famous mathematicians,influence of mathematics on other branches of knowledge etc. I believe it will provide a good introduction the subject to anyone.

I have tried some sample chapters from the book. Among them I found the Advice to a Young Mathematician very useful and stimulating. This chapter is contributed by world leading mathematicians like Michael Atiyah (Fields Medalist) and others.

Anyone interested may try some free chapters here.

WordPress posts on Princeton Companion may found here.

Posted in books on mathematics, mathematics | Tagged , | Leave a comment

Solution to STEP problem-I

I have solved a STEP problem from Stephen Siklos’ book Advanced Problems in Core Mathematics. The problem and the solution follows:

Problem: Use the substitution x=2-\cos\theta to evaluate

\int_{3/2}^{2} (\frac {x-1}{3-x})^{1/2}\, dx

Hence or otherwise show that for a<b

\int_{p}^{q} (\frac {x-a}{b-x})^{1/2}\, dx=\frac {(b-a)(\pi+3\sqrt{3}-6)}{12}

Where p=\frac {3a+b}{4} and q=\frac {a+b}{2}

Read the rest of this entry »

Posted in calculus, mathematics | Tagged , , | 2 Comments

Galilean and Lorentz transformation

Suppose two inertial reference frames S and S’ are moving with velocity v with respect to each other. Also suppose that an observer at reference frame S measures an event as (x, y, z, t) and another observer at frame S’ measures the same event as (x', y', z', t'). Now if we want to relate the measurements of S frame with S’ frame we have to use transformation equations.

If we take Newtonian mechanics granted then we use the Galilean transformation. Galilean or Classical transformation equations are:





Notice that the time co-ordinates are measured the same by both observer—that is t'=t.

But if we take Special Relativity instead of Newtonian Mechanics than we use Lorentz transformation. Lorentz transformation equations are:

x'=\frac {x-vt} {\sqrt{1-v^2/c^2}}



t'=\frac {t-vx/c^2}{\sqrt {1-v^2/c^2}}

The fraction \frac {1}{\sqrt {1-v^2/c^2}} is also called relativistic gamma (\gamma).

Posted in physics | Tagged , | 1 Comment

Hello world!

Welcome to This is your first post. Edit or delete it and start blogging!

Posted in Uncategorized | 1 Comment