Posts

Jishan's Log 17: Minimum Absolute Difference in an Array

 Hackerrank Greedy So this problem is your basic problem. The Problem: We are given an array and we are to find the minimum absolute difference. The Solution: We will sort the array Create a storage variable to keep track of minimum difference We will take the initial abs difference between the first two pairs if it's 0, we return it else we iterate through the loop taking two pairs  if we find their difference is 0, return it else compare if it is smaller than what we had stored Finally, we return that storage variable The Code: #include   < bits/stdc++.h >   using   namespace   std ; int   min ( int   a ,   int   b )   {      return   a   <   b   ?   a   :   b ; } int   minimumAbsoluteDifference ( vector < int >   arr )   {      sort ( arr . begin (),   arr . end ());           int ...

Jishan's Log 16: Opinion on Digital Security

  Digital Security The 21st Century. An era filled with the inception of world-changing innovations and the creation of technology whose sciences were just a fiction for movies in the 80’s and 90’s. More and more we are abandoning our native ways of doing a simple task and simplifying it through the use of modern technology. The individual cellphone replacing the family telephone, e-mails replacing the personal feel of letters and so on. As we incorporate these inventions more and more into our lives and into our world, we are also welcoming the dangers that are, well, ‘already made’ when those technologies were invented. We are already trusting these technologies with our personal (sometimes even too personal) information about us and we are already seeing big corporations abuse these kinds of privileges that we ourselves, by our own decisions and by our own hands, freely give it to them with the assumption that they can keep it safe and use that data to help us more in our daily ...

Jishan's Log 14: Codeforces 69A solution

Codeforces 69A: Young Physicist The Problem: A boy is given a boy with a set of forces acting on it in x, y and z space. He needs to find out if the body is in equilibrium or not. For a body to be in equilibrium, all forces on it must be balanced ( Σ(F) == 0 ). So, we need to find out if the forces in x, y, z plane individually are 0. The Solution: The Algorithm: 1) Take n input. Declare sums of x, y, z and set them to 0. 2) Take a loop and make it run n times. 3) Take the x, y, z inputs and sum them up with their respective sum variables. 4) Check to see if all of them are zero. If true, print YES. Else print NO The Code: #include <bits/stdc++.h> #define   fastio   ios :: sync_with_stdio ( 0 );  cin . tie ( 0 );  cout . tie ( 0 ); using   namespace   std ; int   main () {      fastio ;      int   n ;  cin   >>   n ;      int   x_sum ,  y_sum ,...

Jishan's Log 13: Codeforces 96A solution

Codeforces 96A: Football The Problem: A boy watching football keeps noting down the number of players in both teams with 0 being the players from one team and 1 being the players from another team. He deduces that if minimum 7 players of the same team stand side by side, the situation would become dangerous. Ex: 01001. Situation is not dangerous because no 7 players of the same team are standing side by side. Ex: 1000000001. Situation is dangerous because 7 players of the same team are side by side. Ex: 1011011111111011. Situation is dangerous because 7 players of the same team are side by side. The Solution:  The Algorithm: 1) take a string input 2) start counting the string from 1 until strings length (we start from 1 because we would want to check the chars of current index string with the previous index string) using a for loop. 3) check if current index char matches with last index char. 4) if it does then we increment the counter and check if it's 7 or not. If it is then we r...

Jishan's Log 12: Codeforces 236A solution

Codeforces 236A: Boy or Girl The Problem: A person tries to figure out how to distinct a boy and girl in an online chat platform. A boys account will have odd number of distinct characters. Here, distinct characters mean the total number of the type of characters.  Ex: string: AB distinct chars: 2 (there are two types: A and B) string: ABBAACCD distinct chars: 4 (there are four types: A, B, C and D) The Solution: The Algorithm: 1) take a string 2) take a set of chars and insert each char in string into set. 3) count the number of chars in the set 4) if odd print "IGNORE HIM", else "CHAT WITH HER!". NOTE: We used set here because it does not take ANY DUPLICATE VALUES. If a value is already in the set, it will ignore it. The Code: #include <bits/stdc++.h> #define   fastio   ios :: sync_with_stdio ( 0 );  cin . tie ( 0 );  cout . tie ( 0 ); using   namespace   std ; int   main () {      string   str ;  cin   ...

Jishan's Log 11: Codeforces 112A solution

Codeforces 112A: Petya & Strings What does the problem want? Petya is given two strings. Petya now needs to make sure (regardless of case) the strings are of same length. How to solve it? The Algorithm: 1) Take 2 strings. 2) convert them to lower case 3) compare them  The Code: #include <bits/stdc++.h> using   namespace   std ; #define   fastio   ios :: sync_with_stdio ( 0 );  cin . tie ( 0 );  cout . tie ( 0 ); int   main () {      fastio ;      string   s1 ;  cin   >>   s1 ;      string   s2 ;  cin   >>   s2 ;           for_each ( s1 . begin (),  s1 . end (), []( char   &   c ) {          c   =   tolower ( c );     });      for_each ( s2 . begin (),  s2 . end (),...

Jishan's Log 10: Codeforces 281A solution

Codeforces 281A: Word Capitalization What does the problem want? This problem is simple. Just capitalize the first word of the sentence. That's it! How to solve it? The Algorithm: 1) Take a string 2) Convert the first word to upper. The Code: #include <bits/stdc++.h> using   namespace   std ; #define   fastio   ios :: sync_with_stdio ( 0 );  cin . tie ( 0 );  cout . tie ( 0 ); int   main () {      fastio ;      string   s ;  cin   >>   s ;      s [ 0 ]   =   toupper ( s [ 0 ] );      cout   <<   s   <<   " \n " ;      }