Saturday, 19 September 2015

Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed

Given an array, only rotation operation is allowed on array. We can rotate the array as many times as we want. Return the maximum possbile of summation of i*arr[i].

Example:
Input: arr[] = {1, 20, 2, 10}
Output: 72
We can 72 by rotating array twice.
{2, 10, 1, 20}
20*3 + 1*2 + 10*1 + 2*0 = 72

Input: arr[] = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Output: 330
We can 330 by rotating array 9 times.
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
0*1 + 1*2 + 2*3 ... 9*10 = 330

Solution:

Maintain the sum (lets say, B) of the full array except the last and first element for the current rotation.
After each clockwise rotation, the new sum to calculated(lets say, A) increases by this B & the current first element, and reduces by the (n-1) * last element.

Wednesday, 14 January 2015

Checking symbols/functions in *nix binary library so/a files

What the man page says - "Nm  displays  the  name  list (symbol table) of each object file in the argument list."

If you want to list the symbols or function names in the binary library you can see their names using this command.

Example:
nm  opt/hadoop-2.4.0/lib/native/libhadoop.so

If you have to check whether there is support for snappy in your hadoop binary do this:
nm  opt/hadoop-2.4.0/lib/native/libhadoop.so | grep -i snappy

And you should see something like this:
Java_org_apache_hadoop_io_compress_snappy_SnappyCompressor_compressBytesDirect0000000000003960 T Java_org_apache_hadoop_io_compress_snappy_SnappyCompressor_initIDs0000000000003bb0 T Java_org_apache_hadoop_io_compress_snappy_SnappyDecompressor_decompressBytesDirect0000000000003f60 T Java_org_apache_hadoop_io_compress_snappy_SnappyDecompressor_initIDs0000000000206cf0 b dlsym_snappy_compress0000000000206d20 b dlsym_snappy_uncompress

Without these Java native methods being compiled and available in the libhadoop.so, MR runtime will also complain that "native snappy library not available".