Tuesday, December 4, 2012

Reverse Words In A Char Array - Java

I'm back again with a Java post in my blog :-)

Recently I came across an interview question - "Given a statement in char[], reverse the order of words in that statement without using any extra temporary char[] or String functions". Here is my solution for this question.

There will be N - 1 Runs for reversing a statement which has N words in it.

1st Run will move the First word in statement to make it as Last Word and move the remaining space from first position before the word just moved.

2nd Run will move the now First word in statement to make it as 2nd last word and the remaining space from first position before the word just moved.

So, (N-1)th Run will move the now First word in statement to make it as Nth last word (i.e., Second Word) and the space before it from first position. 

public class ReverseStatement {


private static final char WORD_DELIMITER = ' ';

public static void main(String[] args) {
char[] input = "One Two Three Four".toCharArray();
System.out.println(input);
new ReverseStatement().reverse(input);
System.out.println(input);
}

int wordsLength[];

public synchronized void reverse(char[] input) {

if (input == null) {
throw new NullPointerException("input");
}

if (input.length == 0) {
throw new IllegalArgumentException("input is empty");
}

if (input[0] == WORD_DELIMITER || input[input.length - 1] == WORD_DELIMITER) {
throw new IllegalArgumentException(
"input must not start or end with SPACE");
}

countAndUpdateWordsLength(input);

int newPosition = input.length;
for (int run = 1; run < (wordsLength.length); run++) {
newPosition = newPosition - wordsLength[run - 1];
shiftFirstNCharsTo(input, wordsLength[run - 1], newPosition);
shiftFirstNCharsTo(input, 1, --newPosition);
}
}

private void countAndUpdateWordsLength(final char[] input) {
int wordCount = 0;

// Loop to identify the total number of words.
int lastSpaceAt = 0;
for (int i = 0; i < input.length; i++) {

if (input[i] == WORD_DELIMITER) {
wordCount++;
if (lastSpaceAt == (i - 1)) {
throw new IllegalArgumentException(
"Continuous Spaces Detected " + i + ", " + lastSpaceAt);
}
lastSpaceAt = i;
}
}

// Incrementing by 1 for last word in input
wordCount++;

wordsLength = new int[wordCount];
int wordNumber = 0;
int eachWordLength = 0;

// Loop to count each word's length
for (int i = 0; i < input.length; i++) {
if (input[i] == WORD_DELIMITER) {
wordsLength[wordNumber] = eachWordLength;
eachWordLength = 0;
wordNumber++;
continue;
}
eachWordLength++;
}
wordsLength[wordNumber] = eachWordLength; // For the last word
}

private static void shiftFirstNCharsTo(char[] input, int n, int newPosition) {
if (newPosition + n > input.length) {
throw new RuntimeException("Cannot move to " + newPosition);
}
for (int x = n - 1; x >= 0; x--) {
int targetPosition = newPosition + n - 1;
for (int y = x; y < targetPosition; y++) {
char t = input[y];
input[y] = input[y + 1];
input[y + 1] = t;
}
newPosition--;
}
}
}

Output:
One Two Three Four
Four Three Two One

If you find any bug or improvement area, please comment.

Friday, August 17, 2012

Indian History - British Strategy to Keep India Divided

Read a good book and wanted to share a page from it... (In Tamil)

வரலாறு அபாயகரமானது. பிசாசாக உருமாறி மக்களை ஆட்கொண்டு ஆட்டிப்படைப்பது. நம் நாட்டின் வரலாறு என்னும் பெயரில் எழுதப்பட்டிருப்பது, பரப்பப்படுவது பெரும்பாலும்கடந்த காலத்தின் உண்மையான சித்தரிப்பு அல்ல. எழுதுபவர்களின் கண்ணோட்டத்திற்கு ஏற்ப, அவர்களது குறுகிய சாதி, சமய, அரசியல் தேவைகளுக்கு ஏற்றவண்ணம் திரித்து, மறைத்து, கூட்டிக்கழித்து எழுதப்பட்டிருப்பது. சமுதாயப் பொறுப்புணர்வு இல்லாமல், தங்கள் துவேஷங்களையே வண்ணக் கலவையாக்கி வரலாற்றைத் தீட்டியிருக்கிறார்கள் பல மேதாவிகள்.

‘நம் நாட்டில் இந்துக்களும் முஸ்லிம்களும் இருவேறுபட்ட இனங்கள், வேறுபட்ட கலாச்சாரங்களைச் சேர்ந்தவர்கள்… அவர்களிடையே பாலமிட முடியாத பிளவுகள் காலங்காலமாக இருந்திருக்கின்றன…’ என்பவை போன்ற மதவாதக் கருத்துகள் இத்தகைய வக்கரித்த வரலாற்றின் வார்ப்புகள் தாம்.

ஆங்கிலேயர் ஆட்சிக் காலத்தில் நம் நாட்டின் வரலாறு சொல்லொணாச் சிதைவுக்கு உள்ளாயிற்று. தங்கள் ஆதிக்கத்துக்கு உட்பட்ட மக்களைப் பிரித்தாளும் ராஜதந்திரத்தின் அற்புதக் கருவியாக வரலாற்றை ஆங்கிலேயர் பயன்படுத்தினர். பிரிட்டிஷ் சாம்ராஜ்யத்தின் மகோன்னதத்தை வலியுறுத்த, அதற்கு முன்னர் முஸ்லிம்கள் ஆண்ட காலத்தை இருண்டகாலமாகச் சித்தரித்தனர். முஸ்லிம் மன்னர்களைக் கொடுங்கோலர்கள் என்றும் கொள்ளைக்காரர்கள் என்றும் மதவெறியர்கள் என்றும் இந்தியக் கலாச்சாரத்தைச் சீரழித்தவர்களென்றும் காட்டி, அவர்களது கொடிய ஆதிக்கத்திலிருந்து இந்துக்களை மீட்கவந்த ரட்சகர்களாகவும் கருணா மூர்த்திகளாகவும் தங்களைச் சித்தரித்துத் தங்கள் ஆதிக்கத்தை நியாயப் படுத்தினர். திட்டமிட்டு, நாசூக்காக வரலாற்றில் விஷத்தைக் கலந்தனர்.

இப்படி விஷமத்தில் பிறந்த வரலாறு, நம் நாட்டின் சில தலைவர்களுக்கும் மதவாதப் பிரச்சாரகர்களுக்கும் மிகச்சிறந்த கருவியாகப் பயன்பட்டது. இன்று நம் நாட்டில் தலை விரித்தாடிக்கொண்டிருக்கும் மதத் துவேஷமும் நூற்றுக்கணக்கான உயிர்களைக் குடித்துள்ள மதக் கலவரங்களும் இத்தகைய வரலாற்றுத் தத்துவம் மக்களிடையே பரப்பப்பட்டதன்விபரீத விளைவுகள்தாம்.

முஸ்லிம் மன்னர்கள் மதவெறியின் காரணமாக ஆயிரக்கணக்கான இந்துக் கோயில்களை இடித்தார்கள் என்பது இன்று அனைத்து மக்களாலும் நம்பப்படும் ‘உண்மை’யாகிவிட்டது. இதில் ஓரளவு உண்மை இருந்தாலும், அதைச் சரியான கண்ணோட்டத்தில் புரிந்துகொள்ள வேண்டும். பல முஸ்லிம் மன்னர்கள் இந்துக் கோயில்களைக் கட்டியும் உள்ளனர். மகதராஷ்டிரத்தின் மராத்துவாடா இந்து, முஸ்லிம் கலாச்சாரங்கள் பெருமளவு ஒன்றாகக் கலந்த பகுதி. இப்பகுதியில் மிகப் பெரிய மதவெறியராகச் சித்தரிக்கப்பட்டிருக்கும் ஓளரங்கசீப் எண்பதுக்கும் மேற்பட்ட இந்துக் கோயில்களைக் கட்டியுள்ளார். அவர் இப்பகுதியில் கட்டிய மசூதிகளைவிடக் கோயில்கள் தான் அதிகம்.

ஓளரங்கசீப் சதாராவிலிருந்த இந்துக் கோயிலை இடித்தார் என்பது உண்மை. அதற்குக் காரணம் மதவெறியல்ல. அக்கோயிலில்பெருமளவு நிலைபெற்றிருந்த தேவதாசி முறைதான்  காரணம் என டாக்டர். பி.வி. ரானடே என்னும் வரலாற்று அறிஞர் குறிப்பிடுகிறார். ஜெய்னா என்னும் ஊரில் உள்ள கணபதி கோயிலுக்கு ஓளரங்கசீப் தானப் பத்திரம் எழுதி அளித்தார். புனே மாவட்டத்தில் சின்சுவாட் என்னும் இடத்தில் உள்ள கணபதி கோயிலை 16ஆம் நூற்றாண்டில் கட்டியவர் பிஜாப்பூர் சுல்தான் இப்ராஹிம் ஆதில்ஷா. தௌலதாபாத்தில் உள்ள சரஸ்வதி பௌதி என்னும் புண்ணியக் கிணற்றை கி.பி.1335ஆம் ஆண்டு கட்டுவித்தவர் முகமது பின் துக்ளக். இதில் விடுவதற்கான தண்ணீரை  துக்ளக் பிரயாகையிலுள்ள திரிவேணி சங்கமத்திலிருந்து வருவித்தார். டெல்லி சுல்தானிய அரசை நிறுவிய முகமது கோரி வெளியிட்ட ஒரு நாணயத்தில் லக்ஷ்மி உருவமும் மற்றொன்றில் சிவனின் ரிஷப வாகனமும் பொறிக்கப்பட்டிருந்தன.

முஸ்லிம் மன்னர்கள் பலர் இந்துப் பெண்களை – குறிப்பாக ராஜபுதனத்து அரச குடும்பத்துப் பெண்களை – மணந்து கொண்டனர் என்பது நமது பள்ளிகளில் கூடக் கற்பிக்கும் வரலாற்றுப் பாடம். ஆகவே முஸ்லிம் மன்னர் பரம்பரையிலேயே இந்து ரத்தம் கலந்திருந்தது.
இதே போன்று இந்து மன்னர்களும் மசூதிகளை ஆதரித்தனர்.
சிவாஜியின் பேரன் சத்ரபதி ஷாஹூ குல்தாபாத்தில் இருக்கும் ஔரங்கசீபின் சமாதிக்குப் பெருமளவு தானம் அளித்தார் என்பதைப் பத்திரங்களின் ஆதாரத்துடன் ரானடே கூட்டிக்காட்டுகிறார்.
நாம் நினைவில்கொள்ள வேண்டியது இன்னொன்று.
கோயில்கள் இடிக்கப்பட்டதன் காரணம் மதவெறியல்ல. ஆக்கிரமிக்க முற்படும் மன்னர்கள், தங்கள் அதிகாரத்தை நிலைநிறுத்தவும் எதிரியை அவமானப்படுத்தவும் எதிரிநாட்டின் முக்கிய ஸ்தலங்களை நாசப்படுத்துவதைப் பழக்கமாகக் கொண்டிருந்தார்கள். மன்னர்களின் ஆளுமையை நிலைநாட்டும் முக்கியச் சின்னங்களாகக் கோயில்கள் திகழ்ந்தன. ஆகவே தான் தங்கள் பெருமையைப் பறைசாற்ற அரசர்கள் கோயில்கள் கட்டினர். இதன் காரணமாகவே கோயில்கள் அந்நியத் தாக்குதல்களுக்கு முக்கிய இலக்குகளாயின. கோயில்களில் குவித்து வைக்கப்பட்டிருந்த செல்வத்தைக் கொள்ளையடிப்பதும் இதன் நோக்கம். இந்துக் கோயில்களை இடித்துத் தகர்த்த இந்து மன்னர்களும் உண்டு! எதிரி நாட்டு மசூதிகளை இடித்த முஸ்லிம் மன்னர்களும் உண்டு!

நம் நாட்டில் வரலாற்றுக்கான இலக்கணத்துடன் எழுதப்பட்ட முதல் வரலாற்று நூல் என்று கருதப்படுவது கல்ஹனா எழுதிய ராஜதரங்கிணி என்னும்  காஷ்மீரத்தின் வரலாறு.அதில் ஹர்ஷர் என்ற இந்து மன்னர் காஷ்மீரத்தில் உள்ள அனைத்துக் கோயில்களிலும் இருந்த எல்லாத் தெய்வச்சிலைகளையும் நொறுக்கி நாசம் செய்ததாகக் குறிப்பிடப்பட்டுள்ளது. இந்த மன்னரின் அமைச்சரவையில் சிலைகளை உடைப்பதற்காகவும் அவற்றை உருக்கி, விலையுயர்ந்த ஊலோகத்தைச் சேகரிப்பதற்காகவும் தனிப்பொறுப்பு வகித்த இந்து அமைச்சர் ‘தேவோத்பாதநாயகா’ (தெய்வத்திற்குத் துன்பம் விளைவிக்கும் அதிகாரி) எனப் பெயர் கொண்ட ஒருவர் இருந்தார் என்று புகழ்பெற்ற வரலாற்று அறிஞர் கோசாம்பி குறிப்பிடுகிறார்.

மற்றொரு வரலாற்று அறிஞர் ஆர்.எஸ்.சர்மா இன்னொரு உண்மையைச் சுட்டிக்காட்டுகிறார். கஜினி முகமது, கோரி முகமது, தைமூர் ஆகிய முஸ்லிம் மன்னர்கள் இந்நாட்டின் மீது படையெடுத்து வந்து, அக்கிரமங்கள் புரிந்ததைப் பற்றிச் சொல்லும்போது, இதே மன்னர்கள் மத்திய ஆசியாவிலிருந்த முஸ்லிம் ராஜ்யங்களின் மேல் படையெடுத்துச் சென்று, அங்கிருந்த மசூதிகளை நாசம் செய்ததையும் அந்நாடுகளின் முஸ்லிம் மக்களுக்குச் சொல்லொணாத் துன்பங்களை விளைவித்ததையும் மறந்துவிடக் கூடாது எனக் கூறுகிறார். இதே போன்று, இந்து மன்னர்களால் அழிக்கப்பட்ட இந்துக் கோயில்கள், புத்த, ஜைனக் கோயில்கள், குறிப்பாகத் தமிழ் நாட்டிலும் கர்நாடகாவிலும் பல உண்டு. இவர்கள் எல்லோரையும் ஊக்குவித்தது மதவெறியல்ல. அதிகார, ஆக்கிரமிப்பு வெறியேயாகும். அரசியல் வரலாற்றின் இத்தகைய வேட்டைகளுக்கும் கொடுமைகளுக்கும் சுரண்டல்களுக்கும் மதச் சாயம் பூச முயல்வது மக்களைப் பிரித்தாளும் சில்லறைத்தனமாகும்.
நமது கல்லூரிகளிலும் பள்ளிகளிலும் ஆராய்ச்சி நிறுவனங்களிலும் வரலாறு இகழ்ந்து ஒதுக்கப்பட்ட பாடமாகிவிட்டது. ஆனால் வரலாறு இன்றி எந்தச் சமுதாயமும் வாழ இயலாது. கல்வி நிறுவனங்கள் விஞ்ஞானரீதியான வரலாற்றைப் புறக்கணித்ததன் விளைவு மதவாதிகள் இன்று அதை ‘ஹைஜாக்’ செய்யும் நிலை உருவாகிவிட்டது.

- முனைவர் வசந்திதேவி

From Book (சக்தி பிறக்கும் கல்வி - Page 111)

Sunday, July 29, 2012

Start Giving, Start Living

"Indeed, in the creation of the heavens and the earth and the alternation of the night and the day are signs for those of understanding" (Al-Qur'an, Chapter 3 [Ale Imran], Verse 190)

A few years ago a motor manufacturer's advertising line read

'life's a journey, enjoy the ride'.

For many of us though it seems that

'life's a destiny, miss the ride'

Our lives have become hectic and frantic characterized by a continuous race against time. In so doing, we have opted for the 'highways' instead of the 'byways' little realizing the beauty of the byways. We enjoy the selfish exhilaration of speeding down this 'highway' called life, with the wind blowing through our hair and our fancy shades, little realizing that these shades have obstructed our view of the scenery the 'byways' of life have to offer; the mountains, rivers and the colorful array of flora and fauna. Our 'speeding', our focus on racing against time - this deadline, that deal, the end result, the 'destination' - has caused us to focus only on finding the 'shortest road', the 'highway', thus preventing us to sit back and take in the beauty that the 'byways' have to offer. The irony is that our 'speeding' is in pursuit of that happiness and contentment which, strangely enough, lies in 'slowing down' and 'taking' in the scenery.

In the context of life what is this scenery?

It is the people that we come into contact with or are in contact with in our daily lives. They are those who we know, family and friends, as well as those that we do not, strangers.

Given the 'speed' at which we are traveling we often take for granted those that care for us and our well being and who go out of their way to make our lives comfortable, to make us smile and strive to make us happy. They are those whom we 'receive' from. If, for a moment, we ponder and acknowledge them, then we would appreciate their act of giving. When doing so we would realize how much joy they bring to our lives because of their 'giving'. This, would in turn, spur us on to give.

Know that the joy of giving supersedes that of receiving and accumulating because giving brings an inner contentment while accumulating only provides an apparent contentment beneath which lingers the fear of loss. Knowing that some seemingly insignificant word or action has brought happiness and joy to the heart of someone provides more satisfaction than money and wealth can ever bring. It is, in fact, the very essence of kindness to spread goodwill and uplift the heart.

What do we give?

A starting point would be acknowledgment in word, deed (if and where possible) and in prayer (Dua) for those that make some sort of difference to our lives and give meaning. Thereafter we should strive to make a positive difference and impression in the lives of all others that we come into contact with.   

More often than not we have programmed our minds to understand that only that which is 'big, substantial, financial and material' would be considered an act of giving and, in doing do deprive ourselves of the great joy and satisfaction that even a small act of kindness can provide. As a matter of fact The Messenger of Allah (peace be upon him) has mentioned that,

'Do not belittle any good deed, even if it be meeting your brother with a cheerful face'[Reported by Abu Dharr (May Allah be pleased with him) - Sahih Muslim, Book 1, Hadith 121]

There is also an incident mentioned in the seerah where Sayyiduna Usama bin Zayd (RA) on one occasion, did not have anything material and financial to contribute in the path of Allah. At the time of Tahajjud he awoke and beseeching and crying unto Allah (SWT) he said,

‘Oh my Lord, I do not have any financial means to contribute for your sake. Instead, Oh my Lord, I forgive and overlook all those upon whom I have a right/s due’

The next morning The Messenger of Allah (peace be upon him) gave him glad tidings and said,
    
‘Oh Usamah! Rejoice for verily Allah (SWT) has accepted your charity of the night’

From the above it is evident that Islam, while encouraging the giving of material, has not limited the act of 'giving' to that which is material. So start giving today, whether it is a kind word to someone, an acknowledgment of a favor to a benefactor, a little prayer (Dua) for a friend or loved one, giving a loaf of bread to a hungry one or just a smile, for it could be to some the only sunshine that they enjoy today!

There are so much positives in our lives due, directly or indirectly, to those who care and have concern for us. Let it not be that, when we start to care we realize that those who have done so all along, have stopped.

Before lamenting and complaining of having no time... Make the time!

We need to decide... Are we driving? or Are we driven?

Stop existing and start living!

Thursday, October 20, 2011

Jesus (pbuh) is a Muslim

God clarified in the Quran that Jesus (pbuh) was not crucified; rather, it was made to seem that way to the Jews, and that God raised him to the Heavens. The Quran does not explain, though, who was the person crucified instead of Jesus (pbuh), may the mercy and blessings of God be upon him.

“…They did not kill him, nor did they crucify him, but (another) was made to resemble him to them…” (Quran 4:157)

“God lifted him up to His presence. God is Almighty, All-Wise.” (Quran 4:158)

As such, Islam denies that Jesus (pbuh) came to this earth with the purpose of sacrificing himself for the sin of Adam, Eve, and the rest of humanity, freeing them from its burden. Islam strictly rejects the notion that any person bears the sin of another. God says:

“No bearer of burdens shall bear the burden of another.” (Quran 39:7)

Also, Islam stresses the notion that God will forgive sins (If He wishes), if a person truly repents and then refrains from repeating it. God does not need any blood sacrifice for that, let alone descend in the form of man himself and die for every man's sins. Rather, God’s mercy extends to all creatures, believers and disbelievers alike. The door to forgiveness is open to anyone who seeks it.

The Second Coming of Christ

As Christians do, Muslims also believe in the return of Jesus (pbuh) the Messiah to earth, although his role and reason for his return does differ from what the Christians propose. He will return to earth first and foremost to prove his mortality and refute the false beliefs people held about him. He will live a normal life, marry, and also die as any other human. At that point, the matter will be clear concerning him, and all people will have believed that he was truly mortal.

“There is not one of the People of the Scripture but will believe in him (Jesus (pbuh)) before his death, and on the Day of Resurrection he will be a witness against them.” (Quran 4: 159)

Jesus (pbuh) will also fight the false Christ, who will call people to the belief that he is God, and who will appear just before he returns. Jesus (pbuh) will defeat the antichrist, and all people will accept the true religion of God. The world will see a type of peace and serenity unfelt in history, all worshipping the same God, subservient to Him alone, and at peace with one another.

Conclusion

What has preceded was just a brief introduction to the truth which God revealed about Jesus (pbuh) in His final Book, the Quran, which he revealed to Muhammad. There is only One God who created one race of human beings and communicated to them one message: submission to the worship of God and God alone and following His instruction – known in Arabic as Islam. That message was conveyed to the first human beings on this earth, and reaffirmed by all of the prophets of God who came after them throughout all the ages.

Jesus (pbuh) Christ, born of the Virgin Mary, performed miracles and invited the Israelites to the same message of submission (Islam) as did all of the prophets who preceded him. He was not God, nor was he the ‘Son of God’, but was the Messiah, an illustrious prophet of God. Jesus (pbuh) did not invite people to worship himself; rather, he called them to worship God, Whom he himself worshipped. He confirmed the laws of the Torah which Prophet Moses taught; he lived by them, and instructed his disciples to follow them to the finest detail, after allowing those things which the Jews made illegal upon themselves. Before his departure, he informed his followers of the last prophet, Muhammad of Arabia, who would come after him, and instructed them to observe his teachings.

In the generations after Jesus (pbuh)’ departure from this world, his teachings were distorted and he was elevated to the status of God. Six centuries later, with the coming of Prophet Muhammad, the truth about Jesus (pbuh) Christ was finally retold and preserved eternally in the last book of divine revelation, the Quran. Furthermore, many of the laws of Moses, which Jesus (pbuh) followed, were revived in their pure and unadulterated form and implemented in the divinely prescribed way of life known as Islam, while many other aspects and injunctions of the laws which had been brought by the earlier prophets were lightened or dropped altogether.

Consequently, the reality of the prophets, their uniform message, and the way of life which they followed, can only be found preserved in the religion of Islam, the only religion prescribed by God for man. Furthermore, only Muslims today actually follow Jesus (pbuh) and his true teachings. Their way of life is much more in tune with the way of life Jesus (pbuh) practiced than any of the modern day “Christians.” Love and respect of Jesus (pbuh) Christ is an article of faith in Islam, and God stressed the importance of belief in Jesus (pbuh) in numerous places in the Quran.

It is clear now that Jesus (pbuh) is a Muslim followed Islam by his actions

1. Submitting himself to The God
2. Worshipping The God
3. Calling People towards Worship of The God.