Code and Why

Photo Credit: Christopher Gower
  • About Me

    Hello and welcome to my blog site! My name is Pranav Patil, and currently, I am a Computer Science and Math student at Rutgers University-New Brunswick. I have always enjoyed learning about technology, specifically how computers do the things they do and how they interact with the user, especially as the world is rapidly transforming to a new age of technology. As of right now, I have been learning the programming language Java for 4 years, as well as 3 years of Python. In the future, I hope to be able to expand my horizons and learn even more coding languages. I hope to spread my own perspectives and ideas upon some programming issues in upcoming posts. Many problems I will post will be from the website LeetCode, which is a popular website that has a plethora of problems with varying difficulty, used by many to prepare for interviews. Anyways, that has been more than enough for you guys to read, so get ready, and have fun! 

    Sidenote: I shall be solving most of the problems in Java, however LeetCode allows for the users to choose which language to submit the response in.

  • LeetCode #13 – Roman to Integer

    For a problem labeled ‘Easy’, this one is quite challenging…

    However, it is also quite simple if you know certain classes, such as the Map class. Using a HashMap, it is quite easy to assign certain values to certain letters. 

    Here is how you initialize them:

    As you can see, each roman numeral is assigned a value, and using these values, you can easily get the value. All you need to do is to check each numeral and the one after it and see whether or not you subtract or add from them. Ex(IV = 4 because 5 – 1 = 4, but VI = 6 because 5 + 1 = 6). After that, you add everything up and you get your final value. Here is my final code:

    Here is the output:

    Here are the logistics of my solution:

    While slightly inefficient, it still works. That is all from me for now. I hope you guys at least learned something today, and that you all have a great day!

  • LeetCode #27 – Remove Element

    This is the next problem:

    Even though this problem is labeled as ‘Easy’, the instructions may be quite overwhelming, and at first glance, this problem seems a lot harder than it actually is. Here are examples of output and explanations for your ease of understanding:

    Despite it seeming complicated, it is actually quite easy to code. This is why it is labeled as an ‘Easy’ problem. As long as you do not get thrown off by the essay of instructions, it should be simple to code. Here is how I did it:

    I’ll break it down for you. The count integer is to count how many indices without val are in the array nums. Thus the for loop and if statement inside it. You also move every value that is not val to the front of the array with the next line, which also increases count by one each time. Here is my output:

    Here are the logistics of my solution:

    I am very happy with my results for this problem, and it confirms that my way was very efficient. If you have any suggestions, be sure to let me know in the comments section below. I hope you guys at least learned something today and that you all have a great day!

  • LeetCode #66 – Plus One

    Here is my next problem:

    Seems pretty easy, right? However, it gets a bit tricky with certain situations. Here are some examples:

    While the first two examples are quite self-explanatory and seemingly easy, where the difficulty increases is with the last example. If digits = [9], then the method would return [1,0], which means you must make a new array and put the right numbers in, rather than simply adding one to the last digit. Here is how I effectively accomplished that:

    This for loop above will start at the very back, and if the last digit is less than 9, it will increment the last element of digits by 1, and subsequently returns digits. However, if the last digit is 9, then it sets it to 0, and adds 1 to the rest of the digits. After that, you need to make sure you make a new array that has a length of digits.length + 1 and make the first digit 1. Here is what that looks:

    Overall, here is what I ended up with:

    Here is my output:

    Here are the logistics for my solution:

    Once again, faster than 100% of other Java solutions feels pretty good, and it is for the fourth time in a row! If you have any suggestions, be sure to let me know in the comments section below. I hope you guys at least learned something today and that you all have a great day!

  • HSCTF 9 – SQueaL

    As you might see from the name of the challenge, this challenge requires knowledge of SQL, a language that is used mainly for managing data. However, all this challenge gives you is a website that has a login screen:

    There is no description for this problem, and all you are given is that screen. This is where you must know some things about SQL, and more specifically, SQL Injection. Without giving a large, boring description about what SQL Injection is, it essentially is a computer attack in which malicious code is inserted into a database in order to gain access to sensitive information. This is illegal, so do not do it on other websites without the owner’s permission. In this case, I do have the permission of the owner, so it is fine. 

    All you need to do for SQL Injection is find the field(Username or Password) which is weak to SQL Injection and insert a certain command. To find which field is sensitive to SQL Injection, simply type a ‘ or “ into the field, and if it says there is a problem after clicking ‘Submit’, the field with the ‘ or “ is weak to SQL Injection. 

    In this case, the password field was weak to SQL Injection, and therefore all you must do to get the flag is type the command:  ‘ or 1=1 –+

    Anyways, that is all from me for now. I hope you guys at least learned something today, and that you all have a great day!

  • HSCTF 9 – Traveling Salesman

    Here is the task:

    Here are the interaction details:

    Here is an example interaction:

    In essence, this challenge was not that tough, and that is why it was one of the most solved challenges in the competition. There are a few things that you can do:

    1. List the order from least to greatest
    2. List the order from greatest to least
    3. List it in any way such that the salesman will go from 1- 99 and back without ever going the opposite direction

    This challenge does not necessarily require any coding, but you could create your own sorter, which makes this challenge much easier, as eventually the array of integers has 30 integers, which could be a hassle to sort through.

    Anyways, that is all from me for now. I hope you guys at least learned something today, and that you all have a great day!

  • HSCTF 9 – Intro

    This might be a bit strange, but this is not from LeetCode, but rather from a Capture the Flag challenge set up by my school’s Computer Science Club called HSCTF 9. There are many challenges, and you need to find out what the flag is from this. Unfortunately, it has been too long since the CTF ended, and therefore the challenges are no longer viewable, however I have some of the files still on my computer, so I can share them with you. The next few entries will be about HSCTF challenges.

  • LeetCode #9 – Palindrome Number

    This is the second question I have completed:

    Here are examples of output for this problem:

    I would also like to note that there is a follow-up question to this question, which is:

    My solution will already be without converting the integer to a string, so this follow-up question will already be addressed. The main reason why I didn’t convert the integer to a string is because I thought it would be too inefficient.

    My logic for this problem was to get the original number, and reverse it. At the end, I would compare the reversed number to the original number. Here is my solution:

    The first 2 lines with the if statement are to make sure that the number is not negative, because if the number is negative, it is not a palindrome.

    The while statement is reversing the original number by taking the last digit of the original number and adding it to the front of the reverse. 

    The final if statement is to just check if the original is the same as the reversed number, if it is, then it returns true, and if it isn’t, it returns false.

    Here is the output:

    Here are the logistics of my solution:

    That is all from me for now. I hope you guys at least learned something today, and that you all have a great day!

  • LeetCode #1 – Two Sum

    This is the very first question that I shall be tackling:

    This question, as it can be seen, is quite simple. Given the array nums, and integer target, the easiest way to solve this problem should be pretty apparent, and that is to just check every combination of indices until you have a sum that adds to target, and that is exactly what I did:

    Note: I made the int j = i +1, and not just 0, because j and i cannot be the same

    When I ran this method, it indeed gave the expected result:

    However, I realized that, while my code works, in the worst case(meaning that the 2 numbers are the last 2 numbers to be checked), the way I did it is very inefficient, with a time complexity of O(n2). This is most likely why there is a follow up to this question:

    This follow-up is also not too difficult either, however more in-depth knowledge of Java is required. There are still a few ways to solve this follow-up. One of them would be to use a Map, which will store the array as keys and the indices of the array as the values associated with them. Each time through the loop, the code will also check to see if the complement has been added to the HashMap. If it does, it will immediately return a new array of the indices. This way, the worst case for this way will only have a time complexity of O(n). The method is displayed here:

    Results:

    That is all from me for now. I hope you guys at least learned something today, and that you all have a great day!