We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
First, two variables are declared, i and j, and assigned values 0 and c.length - 1 respectively.
for(
int i = 0 // declares variable i and assigns it the value 0
, j = c.length - 1; // declares variable j and assigns it the value c.length - 1
c.length is the length of the character array. You don't have to declare 'int' again to declare variable j if you just use a comma in Java.
Then comes the statement that will be evaluated before each loop. If true, the iteration continues, if not, stop.
i < c.length/2; // At the beginning of each loop, check if variable i is less than the length of the character arry divided by two (the middle* of the array)
Next are the commands that are to be executed at the end of each loop, separated by commas.
i++, j--) // Increment i, decrement j
Inside each loop, Math.abs(c[i]-c[j]) is added to the value of the variable 'count'.
Math.abs is a method of the Java language that returns the absolute value of a number, i.e. if negative, return the positive counterpart, else return the number itself.
So what is being calculated is the absolute value of the difference between the numerical value of the characters in the position i and j of the array.
count+= Math.abs(c[i]-c[j]); // Add to 'count' the absolute value of the difference between the numerical value of the characters in the position i and j of the array.
the 'middle' of the array is actually between two positions if the size is even. In this case, c.length / 2 returns the highest of those two positions
The same effect can be achieved if you just use i < j instead, without having to work out rounding mechanics and if you need to use < or <=, or if you have to subtract one or whatever.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Love-Letter Mystery
You are viewing a single comment's thread. Return to all comments →
First, two variables are declared, i and j, and assigned values 0 and c.length - 1 respectively. for( int i = 0 // declares variable i and assigns it the value 0 , j = c.length - 1; // declares variable j and assigns it the value c.length - 1
c.length is the length of the character array. You don't have to declare 'int' again to declare variable j if you just use a comma in Java. Then comes the statement that will be evaluated before each loop. If true, the iteration continues, if not, stop. i < c.length/2; // At the beginning of each loop, check if variable i is less than the length of the character arry divided by two (the middle* of the array)
Next are the commands that are to be executed at the end of each loop, separated by commas. i++, j--) // Increment i, decrement j
Inside each loop, Math.abs(c[i]-c[j]) is added to the value of the variable 'count'. Math.abs is a method of the Java language that returns the absolute value of a number, i.e. if negative, return the positive counterpart, else return the number itself. So what is being calculated is the absolute value of the difference between the numerical value of the characters in the position i and j of the array.
count+= Math.abs(c[i]-c[j]); // Add to 'count' the absolute value of the difference between the numerical value of the characters in the position i and j of the array.