Showing posts with label codeforces. Show all posts
Showing posts with label codeforces. Show all posts

Wednesday, November 29, 2017

CodeForces: Xenia and Ringroad(339B) Solution in Java for Beginners

  Xenia and Ringroad(339B) Problem Link:

Since this is a beginner's problem, I suggest you try it out yourself first.

Java programming topics you need to have knowledge of ->

  1. Loops from geeksforgeeks
  2. Conditional operators in Java from w3resource 
.
.
.
.
.

Explanation of Solution:

After taking the necessary input in an array (the house numbers), we assign the variable result (the total time) a[0] - 1: this is the time to go to the 1st house (from house 1 to that house).

We enter a for loop now to traverse all the houses/array elements one by one. In each iteration, if the current element/house number under consideration is greater than the element before, it is in front of the previous house. In this case, we just add the difference of the house numbers.
In the 'else' case, we see the house number is less than the previous one meaning we go to that house after completing the rotation. So we apply the rotational equation n - (a[i-1] - a[i]). 
Accumulating the results like this, we get our final solution.

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.

Monday, June 19, 2017

Beginner's guide: Best Programming Websites for a Novice

There are tons of websites that cater to learning and practicing programming. While some are suitable for beginners, many others are for a bit mature programmers. Here is a list that I use. I recommend these websites to beginner programmers. These are fun, good-looking websites and It won't disappoint you when it comes to guiding you to learn to programme better.


Hackerrank.com :    I have already written a review about this great and fun website in another blog post. Check it out   http://aprogrammersexperience.blogspot.com/2017/05/review-hackerrank-1-great-site-for.html

codeforces.com: It regularly arranges contests where the problems are set by gradual difficulty levels. The problems are later stored in an archive along with their discussions. This is a great website for those who have already learnt the syntax of C, C++ and Java.

codechef.com :   It is also like codeforces with a cute looking interface. But here, the problems come along with subtasks. It means you will receive partial points if the solution passes some of the test cases. This feature is absent in codeforces.com. So I recommend trying out codechef first before proceeding to codeforces.

devskill.com :   This is similar to codeforces but the problems are a little bit easier.

hackerearth.com : This is an interesting website. It has the features of codechef. It also has a fun challenging arena where you duel with another programmer to solve problems as quickly as possible. It regularly arranges various hackathons and idea competitions too. Competing in these hackathons/idea competitions will really broaden your coding horizon.

geeksforgeeks.com: This is not a website for competitions. It is about teaching you about algorithms and their solutions. It is a great guide for your algorithm courses. It explains the solutions of many important, easy and hard algorithms.

tutorialspoint.com: This is a great website to learn a language quickly. They provide numerous examples when explaining an aspect of a programming language. It is your one-stop whenever you need to know something about a programming language. Whenever I find myself confused about syntax or can't remember the syntax I just search through the extensive examples of this website and I am sure to find my answer explained in an easy way.




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...