Friday, December 20, 2013

Roadway to Ubuntu

This post is neither a solution nor a suggestion. This is what I experienced during installation of ubuntu in my system.
    Since the first day I used a computer, I was using MS Windows as default and primary operating system. Though, its important for a student to cope with different operating systems. During this time, I have used different Unix/Linux based live OS. Deciding which OS I should use is pretty tough decision because there are bunch of choices and all of those are free. :P
    My first attempt to install Linux was on a AMD A55 machine. It was a failure because of hardware configuration. ( I have tried it with both 32bit and 64bit different OSs. )
Then I got my laptop, an Intel system with pre-installed Windows 7 in it. Well, I removed it with Windows 8 from DreamSpark account at 0.00$ cost. :D Living together with Windows 8 was quiet fun. xD However, when I took Operating System course at University, it was a necessity having a Linux OS in my system. But, I failed again. Ubuntu live OS ran fine, installation started every time, but it could not modify the karnel about installation! I tried with Fedora, OpenSuse, Ubuntu, BackTrack and LinuxMint. Each time it started fine, but ends with error. Error! Error!! Error!!! ( Now I have figured it why.. Windows hibernate/hybrid-shutdown option was not allowing any other OS to modify the storage. Later I turned it off using the following command. )
powercfg /h off
    Enough installing Linux for me! So, I kept it cool with Windows.
Meanwhile, my pc was attacked by SIREFEF virus. It turned off, turned off(!) Windows Defender permanently and converted system directories into junctions. Thats why I could not turn Windows Defender on. It ate all the website certificates on all browses. Whenever I turned on the pc, a VB script was creating executable file and some null processes on background. I got rid off this virus using HitmanPro.
    Another thing this virus did was making disk partitions undetectable in disk management tool or windows setup. So, I was thinking on a new set up. Why not Linux now? Yes, it was time for Ubuntu to take all the disk spaces. :D This time I was stuck with, UEFI abd Legacy Mode. Certainly I wanted Ubuntu in UEFI mode but did not find any option for Windows but Legacy mode. Oh, I surely wanted a duel boot.
    I created partitions, installed Ubuntu. Then another problem did arise! Windows could not format GPT partitions. I erased the whole disk using diskpart.
diskpart
list disk
select disk *
clean
convert gpt
exit
    Well, windows was installed. Finally, I installed Ubuntu by formatting unallocated spaces. Installation Finish! Restart!! Windows Boot!!!
    Ah, the MBR, windows boot manager. Again I inserted Ubuntu live USB to install GRUB.
sudo add-apt-repository ppa:yannubuntu/boot-repair && sudo apt-get update
sudo apt-get install -y boot-repair
sudo boot-repair
    At last... Got my dual boot system with Ubuntu 12.04.2 and Windows 8. Decided to use Ubuntu as primary and Windows as supporting OS.
Finally, Enjoying Ubuntu!!

Monday, December 16, 2013

Maximum Interval Product

Previously, I have posted about maximum interval sum. Now, unlike summation, product of an interval or segment is bit different.
Well, the problem is, from a given array of integers we have to determine the maximum product which can be found by any segment of the given array of integers.
Example:
Let, we have an array of these integers:
1 -2 4 -3 -1
So, the maximum interval product is 24 from 1 to -3 ( 1*-2*4*-3 ).
1 -2 4 0 -3 -1
Here, the maximum interval product is 4 from only 4.
Iteration:



initial

1

-2

4

-3

-1

mp

inf

1

inf

4

24

12

mn

inf

inf

-2

-8

-12

-24

mxProduct

-1

1

1

4

24

24

Code:
// Maximum Interval Product
// Complexity: O(n)
long long mxProduct  = -1;
// maximum product is stored. if unchanged, no valid product.
long long t1, t2;
long long mp = INF, mn = INF;
// two counters for maxPositiveProduct and maxNegativeProduct
for( i=0; i<N; i++ ) {
    scanf( "%d", &arr[i] );

    if( arr[i] == 0 ) {
        if( mp != INF ) mxProduct = MAX( mxProduct, mp );
        mp = mn = INF;
        continue;
    }

    if( mp==INF && mn==INF ) {
        arr[i] > 0 ? mp = arr[i] : mn = arr[i];
        if( mp != INF ) mxProduct = MAX( mxProduct, mp );
        continue;
    }

    if( mp == INF ) {
        if( arr[i] > 0 ) {
            mp = arr[i];
            mn = arr[i] * mn;
            mxProduct = MAX( mxProduct, mp );
            continue;
        }
        if( arr[i] < 0 ) {
            mp = arr[i] * mn;
            if( arr[i] >= mn ) mn = arr[i];
            else mn = INF;
            mxProduct = MAX( mxProduct, mp );
            continue;
        }
    }

    if( mn == INF ) {
        if( arr[i] < 0 ) {
            mn = arr[i] * mp;
            mp = INF;
            continue;
        }
        if( arr[i] > 0 ) {
            mp = arr[i] * mp;
            mn = INF;
            mxProduct = MAX( mxProduct, mp );
            continue;
        }
    }

    t1 = arr[i] * mp;
    t2 = arr[i] * mn;
    if( arr[i] > 0 ) mp = t1, mn = t2;
    if( arr[i] < 0 ) mp = t2, mn = t1;
    mxProduct = MAX( mxProduct, mp );
}

Source:
http://acm.uva.es/board/viewtopic.php?f=33&t=11466&hilit=11059&sid=6ed77239e43f67e7ca9999e1412b0180
Thanks - Jajabor, 2014