Wednesday, August 30, 2017

CodeForces: New Year Transportation(500 A) Solution in Java for Beginners

 New Year Transportation(500 A) Problem Link



The solution is found in the problem, so I suggest you try it by yourself.
If you need a quick reminder for while loop, check out this youtube video of newBoston.
You can find a tutorial for if-else, on this tutorialspoint page about if-else.

.
.
.
.
.

New Year Transportation(500 A) Solution Link

If you are facing problems solving this,  you can come here for the solution. So after taking the necessary input, you begin from the first index (currentIndex - set to 1). Then you enter a while loop. Here on every iteration, you check whether you've reached your destination, on which case done variable will be true. If currentIndex > destination, you'll never get to destination since you can't travel backward. In this case, you just break out of the loop. The loop will terminate once you've traversed the whole array.

Finally, you check whether done is true (you've reached your destination) or false(otherwise).


Check out my other codeforces solutions under the codeforces label.



Tuesday, August 22, 2017

Codeforces: Pangrams 520A Solution in Java

Problem link of 520A

Solution link in github

The problem is a simple one. If you learn Java Strings, it'll be a piece of cake. I suggest you try the problem yourself before proceeding. As a quick reminder, check out this short java string tutorial from tutorialspoint  as a guide to this problem.

When taking input, in line 13 I've written an extra sc.nextline(), because when taking a string input before an integer input, omitting line 13 would cause erroneous input. So line 13 works as buffer to correctly take a string input after a numerical input.

The 'frequency' integer array will count the occurrence of each character in the input string. frequency[0] refers to  the count of 'a', frequency[1] refers to  the count of 'b' and so on. For convenience I've converted the whole string into lowercase. To determine index into this array for a particular character just subtract 'a'. For example index for count of 'c' is 2. So if we encounter 'c' just subtract 'a' from 'c' to get the index 2.

Next we check for the number of elements in the frequency array that have non-zero values. If it's 26 we output 'YES', since all the characters in the alphabet are present. Otherwise print "No".

Hope you find this useful. Check out my Hackerrank Solutions in Java. Don't forget to share and follow :) 

Monday, August 21, 2017

CodeForces: Ultra Fast Mathematician-61A Solution in Java

Ultra Fast Mathematician-61A Problem Link

Solution Link in Java

The problem is a simple one. If you learn Java Strings, it'll be a piece of cake. I suggest you try the problem yourself before proceeding. As a quick reminder, check out this short java string tutorial from tutorialspoint  as a guide to this problem.

Now let's come to our solution. After taking the input, we enter a for loop with range from 0 to n-1 , where n is the length of the input string(both strings have the same length). We extract one character from each string and put them in two character variable a and b.

We now check whether they are the same character or not. If they are the same, we output 0. Otherwise, we print out 1 since a and b are not same characters.

After the loop is finished, we get the desired output binary string.

If you face any problem or have any other suggestion, feel free to post a comment.

Don't forget to share and subscribe.

Interview Questions at Enosis(Part 3)

In Part 2 , I have discussed 3 coding problems out of 6. Here we will talk about the next 3 coding problems. Problem 4: Write a function...