<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Emdad Hossain on Medium]]></title>
        <description><![CDATA[Stories by Emdad Hossain on Medium]]></description>
        <link>https://medium.com/@Emdad?source=rss-bad745323808------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Iyn40E2jiWxh6EtWqtveqg.jpeg</url>
            <title>Stories by Emdad Hossain on Medium</title>
            <link>https://medium.com/@Emdad?source=rss-bad745323808------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 19 May 2026 11:56:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Emdad/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Leetcode 1629 Problem- Slowest Key]]></title>
            <link>https://medium.com/@Emdad/leetcode-1629-problem-slowest-key-581bbb92aaec?source=rss-bad745323808------2</link>
            <guid isPermaLink="false">https://medium.com/p/581bbb92aaec</guid>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[blog]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[unittest]]></category>
            <dc:creator><![CDATA[Emdad Hossain]]></dc:creator>
            <pubDate>Sat, 03 Jun 2023 08:39:39 GMT</pubDate>
            <atom:updated>2023-06-03T08:39:39.025Z</atom:updated>
            <content:encoded><![CDATA[<p>A newly designed keypad was tested, where a tester pressed a sequence of n keys, one at a time.</p><p>You are given a string keysPressed of length n, where keysPressed[i] was the ith key pressed in the testing sequence, and a sorted list releaseTimes, where releaseTimes[i] was the time the ith key was released. Both arrays are <strong>0-indexed</strong>. The 0th key was pressed at the time 0, and every subsequent key was pressed at the <strong>exact</strong> time the previous key was released.</p><p>The tester wants to know the key of the keypress that had the <strong>longest duration</strong>. The ith keypress had a <strong>duration</strong> of releaseTimes[i] - releaseTimes[i - 1], and the 0th keypress had a duration of releaseTimes[0].</p><p>Note that the same key could have been pressed multiple times during the test, and these multiple presses of the same key <strong>may not</strong> have had the same <strong>duration</strong>.</p><p><em>Return the key of the keypress that had the </em><strong><em>longest duration</em></strong><em>. If there are multiple such keypresses, return the lexicographically largest key of the keypresses.</em></p><p><strong>Example 1:</strong></p><pre>Input: releaseTimes = [9,29,49,50], keysPressed = &quot;cbcd&quot;<br>Output: &quot;c&quot;<br>Explanation: The keypresses were as follows:<br>Keypress for &#39;c&#39; had a duration of 9 (pressed at time 0 and released at time 9).<br>Keypress for &#39;b&#39; had a duration of 29 - 9 = 20 (pressed at time 9 right after the release of the previous character and released at time 29).<br>Keypress for &#39;c&#39; had a duration of 49 - 29 = 20 (pressed at time 29 right after the release of the previous character and released at time 49).<br>Keypress for &#39;d&#39; had a duration of 50 - 49 = 1 (pressed at time 49 right after the release of the previous character and released at time 50).<br>The longest of these was the keypress for &#39;b&#39; and the second keypress for &#39;c&#39;, both with duration 20.<br>&#39;c&#39; is lexicographically larger than &#39;b&#39;, so the answer is &#39;c&#39;.</pre><p><strong>Example 2:</strong></p><pre>Input: releaseTimes = [12,23,36,46,62], keysPressed = &quot;spuda&quot;<br>Output: &quot;a&quot;<br>Explanation: The keypresses were as follows:<br>Keypress for &#39;s&#39; had a duration of 12.<br>Keypress for &#39;p&#39; had a duration of 23 - 12 = 11.<br>Keypress for &#39;u&#39; had a duration of 36 - 23 = 13.<br>Keypress for &#39;d&#39; had a duration of 46 - 36 = 10.<br>Keypress for &#39;a&#39; had a duration of 62 - 46 = 16.<br>The longest of these was the keypress for &#39;a&#39; with duration 16.</pre><p><strong>Constraints:</strong></p><ul><li>releaseTimes.length == n</li><li>keysPressed.length == n</li><li>2 &lt;= n &lt;= 1000</li><li>1 &lt;= releaseTimes[i] &lt;= 109</li><li>releaseTimes[i] &lt; releaseTimes[i+1]</li><li>keysPressed contains only lowercase English letters.</li></ul><h4>Solution</h4><pre>package array;<br><br>public class Leetcode_1629_Slowest_key {<br><br>    public char slowestKey(int[] releaseTimes, String keyPressed) {<br>        int currentReleaseTime =0;<br>        int previousReleaseTime =0;<br>        int maxReleaseTime=0;<br>        char c = &#39;a&#39;;<br>        // iterate over previous time<br>        for (int i =0;i&lt;releaseTimes.length;i++){<br><br>            // get current time difference from current and previous time<br>            currentReleaseTime=releaseTimes[i]-previousReleaseTime;<br><br>            // check if time is maximum if not update max time and character at that index<br>            if(maxReleaseTime&lt;currentReleaseTime){<br>                maxReleaseTime=currentReleaseTime;<br>                c=keyPressed.charAt(i);<br>            }<br>            // if max time is equal current time check if character stored is lesser if so update character<br>            else if(maxReleaseTime==currentReleaseTime){<br>                if(c&lt;keyPressed.charAt(i)) c = keyPressed.charAt(i);<br>            }<br><br>            // update the previous time<br>            previousReleaseTime=releaseTimes[i];<br>        }<br><br>        return c;<br>    }<br>}</pre><h4>Unit Testing</h4><pre>package array;<br><br>import com.google.common.truth.Truth;<br>import org.junit.Before;<br>import org.junit.Test;<br><br>import static org.junit.Assert.*;<br><br>public class Leetcode_1629_Slowest_keyTest {<br>    Leetcode_1629_Slowest_key sut;<br><br>    @Before<br>    public void setUp() throws Exception {<br>        sut = new Leetcode_1629_Slowest_key();<br>    }<br><br>    @Test<br>    public void testSlowestKey() {<br>        int[] releaseTimes = {9,29,49,50};<br>        String keyPressed = &quot;cbcd&quot;;<br>        char result = &#39;c&#39;;<br>        Truth.assertThat(sut.slowestKey(releaseTimes, keyPressed))<br>                .isEqualTo(result);<br>    }<br><br>    @Test<br>    public void testSlowestKey2() {<br>        int[] releaseTimes = {12,23,36,46,62};<br>        String keyPressed = &quot;spuda&quot;;<br>        char result = &#39;a&#39;;<br>        Truth.assertThat(sut.slowestKey(releaseTimes, keyPressed))<br>                .isEqualTo(result);<br>    }<br>    @Test<br>    public void testSlowestKey3() {<br>        int[] releaseTimes = {23,34,43,59,62,80,83,92,97};<br>        String keyPressed = &quot;qgkzzihfc&quot;;<br>        char result = &#39;q&#39;;<br>        Truth.assertThat(sut.slowestKey(releaseTimes, keyPressed))<br>                .isEqualTo(result);<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=581bbb92aaec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 167 Two Sum — II Input Array is sorted]]></title>
            <link>https://medium.com/@Emdad/leetcode-167-two-sum-ii-input-array-is-sorted-2412cf969aa2?source=rss-bad745323808------2</link>
            <guid isPermaLink="false">https://medium.com/p/2412cf969aa2</guid>
            <category><![CDATA[two-sum-ii]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[unit-testing]]></category>
            <dc:creator><![CDATA[Emdad Hossain]]></dc:creator>
            <pubDate>Thu, 01 Jun 2023 09:28:18 GMT</pubDate>
            <atom:updated>2023-06-01T09:28:18.326Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leetcode 167 Two Sum — II Input Array is sorted</h3><p>Given a <strong>1-indexed</strong> array of integers numbers that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 &lt;= index1 &lt; index2 &lt; numbers.length.</p><p>Return<em> the indices of the two numbers, </em>index1<em> and </em>index2<em>, </em><strong><em>added by one</em></strong><em> as an integer array </em>[index1, index2]<em> of length 2.</em></p><p>The tests are generated such that there is <strong>exactly one solution</strong>. You <strong>may not</strong> use the same element twice.</p><p>Your solution must use only constant extra space.</p><p><strong>Example 1:</strong></p><pre>Input: numbers = [2,7,11,15], target = 9<br>Output: [1,2]<br>Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].</pre><p><strong>Example 2:</strong></p><pre>Input: numbers = [2,3,4], target = 6<br>Output: [1,3]<br>Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].</pre><p><strong>Example 3:</strong></p><pre>Input: numbers = [-1,0], target = -1<br>Output: [1,2]<br>Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].</pre><p><strong>Constraints:</strong></p><ul><li>2 &lt;= numbers.length &lt;= 3 * 104</li><li>-1000 &lt;= numbers[i] &lt;= 1000</li><li>numbers is sorted in <strong>non-decreasing order</strong>.</li><li>-1000 &lt;= target &lt;= 1000</li><li>The tests are generated such that there is <strong>exactly one solution</strong>.</li></ul><h4>Solution</h4><pre>import java.util.HashMap;<br>public class Leetcode_167_Two_Sum_1_indexed_One_Pass {<br><br>    public int[] twoSum(int[] nums, int target) {<br>        HashMap&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();<br>        for (int i = 0; i &lt; nums.length; i++) {<br>            int complement = target - nums[i];<br>            if (map.containsKey(complement)) {<br>                return new int[]{map.get(complement) + 1, i + 1};<br>            } else {<br><br>                map.put(nums[i], i);<br>            }<br>        }<br>        return null;<br>    }<br>}</pre><h4>Unit Tests</h4><pre>import com.google.common.truth.Truth;<br>import org.junit.After;<br>import org.junit.Before;<br>import org.junit.Test;<br><br>public class Leetcode_167_Two_Sum_1_indexed_One_PassTest {<br><br>    Leetcode_167_Two_Sum_1_indexed_One_Pass sut;<br><br>    @Before<br>    public void setUp() throws Exception {<br>        sut = new Leetcode_167_Two_Sum_1_indexed_One_Pass();<br><br>    }<br><br>    @Test<br>    public void resultCorrectFor3_3() {<br>        int[] result = {1, 2};<br>        int[] nums = {3, 3};<br>        int target = 6;<br>        int[] resultArray = sut.twoSum(nums, target);<br>        Truth.assertThat(resultArray).isEqualTo(result);<br>    }<br><br>    @Test<br>    public void resultCorrectFor2719() {<br>        int[] result = {1, 2};<br>        int[] nums = {2, 7, 11, 15};<br>        int target = 9;<br>        int[] resultArray = sut.twoSum(nums, target);<br>        Truth.assertThat(resultArray).isEqualTo(result);<br>    }<br><br>    @Test<br>    public void resultNotFound3_3() {<br>        int[] result = null;<br>        int[] nums = {3, 3};<br>        int target = 7;<br>        int[] resultArray = sut.twoSum(nums, target);<br>        Truth.assertThat(resultArray).isEqualTo(result);<br>    }<br><br>    @Test<br>    public void resultNotFound324() {<br>        int[] result = {2,3};<br>        int[] nums = {3, 2,4};<br>        int target = 6;<br>        int[] resultArray = sut.twoSum(nums, target);<br>        Truth.assertThat(resultArray).isEqualTo(result);<br>    }<br><br>    @After<br>    public void tearDown() throws Exception {<br>    }<br>}</pre><p>With unit testing we can be sured our code is working for various inputs</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2412cf969aa2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[2D Array — DS Hackerrank Problem Solution (C++)]]></title>
            <link>https://medium.com/@Emdad/2d-array-ds-hackerrank-problem-solution-c-1f512a756559?source=rss-bad745323808------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f512a756559</guid>
            <category><![CDATA[hackerrank]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[hourglass]]></category>
            <category><![CDATA[max]]></category>
            <category><![CDATA[2d-arrays]]></category>
            <dc:creator><![CDATA[Emdad Hossain]]></dc:creator>
            <pubDate>Thu, 11 May 2023 12:43:00 GMT</pubDate>
            <atom:updated>2023-05-11T12:43:00.337Z</atom:updated>
            <content:encoded><![CDATA[<h3>2D Array — DS Hackerrank Problem Solution (C++)</h3><p><a href="https://www.hackerrank.com/challenges/2d-array/problem?isFullScreen=true">2D Array - DS | HackerRank</a></p><p>Given a 2D Array, :</p><pre>1 1 1 0 0 0<br>0 1 0 0 0 0<br>1 1 1 0 0 0<br>0 0 0 0 0 0<br>0 0 0 0 0 0<br>0 0 0 0 0 0</pre><p>An hourglass in is a subset of values with indices falling in this pattern in ‘s graphical representation:</p><pre>a b c<br>  d<br>e f g</pre><p>There are hourglasses in . An hourglass sum is the sum of an hourglass’ values. Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum. The array will always be .</p><p><strong>Example</strong></p><pre>-9 -9 -9  1 1 1 <br> 0 -9  0  4 3 2<br>-9 -9 -9  1 2 3<br> 0  0  8  6 6 0<br> 0  0  0 -2 0 0<br> 0  0  1  2 4 0</pre><p>The hourglass sums are:</p><pre>-63, -34, -9, 12, <br>-10,   0, 28, 23, <br>-27, -11, -2, 10, <br>  9,  17, 25, 18</pre><p>The highest hourglass sum is from the hourglass beginning at row , column :</p><pre>0 4 3<br>  1<br>8 6 6</pre><p><strong>Note:</strong> If you have already solved the Java domain’s Java 2D Array challenge, you may wish to skip this challenge.</p><p><strong>Function Description</strong></p><p>Complete the function hourglassSum in the editor below.</p><p>hourglassSum has the following parameter(s):</p><ul><li>int arr[6][6]: an array of integers</li></ul><p><strong>Returns</strong></p><ul><li>int: the maximum hourglass sum</li></ul><p><strong>Input Format</strong></p><p>Each of the lines of inputs contains space-separated integers .</p><p><strong>Constraints</strong></p><p><strong>Output Format</strong></p><p>Print the largest (maximum) hourglass sum found in .</p><p><strong>Sample Input</strong></p><pre>1 1 1 0 0 0<br>0 1 0 0 0 0<br>1 1 1 0 0 0<br>0 0 2 4 4 0<br>0 0 0 2 0 0<br>0 0 1 2 4 0</pre><p><strong>Sample Output</strong></p><pre>19</pre><p><strong>Explanation</strong></p><p>contains the following hourglasses:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/0*87gfUjxBEEbGKeLe.png" /></figure><p>The hourglass with the maximum sum (19) is:</p><pre>2 4 4<br>  2<br>1 2 4</pre><h4>Solution (Using Brute Force)</h4><pre>#include &lt;bits/stdc++.h&gt;<br><br>using namespace std;<br><br>string ltrim(const string &amp;);<br>string rtrim(const string &amp;);<br>vector&lt;string&gt; split(const string &amp;);<br><br>/*<br> * Complete the &#39;hourglassSum&#39; function below.<br> *<br> * The function is expected to return an INTEGER.<br> * The function accepts 2D_INTEGER_ARRAY arr as parameter.<br> */<br><br>int hourglassSum(vector&lt;vector&lt;int&gt;&gt; a) {<br>    int i = 0, j = 0;<br>    int sum , result = -999;<br>  while (i &lt;= 3)<br>  {<br>    j = 0;<br>    while (j &lt;= 3)<br>    {<br>      for (int r = i; r &lt; i + 3; r++)<br>      {<br><br>        for (int c = j; c &lt; j + 3; c++)<br>        {<br>          if (r == i + 1 &amp;&amp; ((c == j) || (c == j + 2)))<br>          {<br>          }<br>          else<br>          {<br>            sum += a[r][c];<br>          }<br>        }<br>      }<br>      result=max(sum,result);<br>    <br>      sum = 0;<br><br>      j++;<br>    }<br>    i++;<br>  }<br>  cout &lt;&lt; result &lt;&lt; endl;<br><br>  return result;<br>}<br><br>int main()<br>{<br>    ofstream fout(getenv(&quot;OUTPUT_PATH&quot;));<br><br>    vector&lt;vector&lt;int&gt;&gt; arr(6);<br><br>    for (int i = 0; i &lt; 6; i++) {<br>        arr[i].resize(6);<br><br>        string arr_row_temp_temp;<br>        getline(cin, arr_row_temp_temp);<br><br>        vector&lt;string&gt; arr_row_temp = split(rtrim(arr_row_temp_temp));<br><br>        for (int j = 0; j &lt; 6; j++) {<br>            int arr_row_item = stoi(arr_row_temp[j]);<br><br>            arr[i][j] = arr_row_item;<br>        }<br>    }<br><br>    int result = hourglassSum(arr);<br><br>    fout &lt;&lt; result &lt;&lt; &quot;\n&quot;;<br><br>    fout.close();<br><br>    return 0;<br>}<br><br>string ltrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        s.begin(),<br>        find_if(s.begin(), s.end(), not1(ptr_fun&lt;int, int&gt;(isspace)))<br>    );<br><br>    return s;<br>}<br><br>string rtrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        find_if(s.rbegin(), s.rend(), not1(ptr_fun&lt;int, int&gt;(isspace))).base(),<br>        s.end()<br>    );<br><br>    return s;<br>}<br><br>vector&lt;string&gt; split(const string &amp;str) {<br>    vector&lt;string&gt; tokens;<br><br>    string::size_type start = 0;<br>    string::size_type end = 0;<br><br>    while ((end = str.find(&quot; &quot;, start)) != string::npos) {<br>        tokens.push_back(str.substr(start, end - start));<br><br>        start = end + 1;<br>    }<br><br>    tokens.push_back(str.substr(start));<br><br>    return tokens;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f512a756559" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codeforces problem — Finding Max sum of Sub Array in a given array (C++) Kadane’s Algorithm]]></title>
            <link>https://medium.com/@Emdad/codeforces-problem-finding-max-sum-of-sub-array-in-a-given-array-c-kadanes-algorithm-993ba8118bb2?source=rss-bad745323808------2</link>
            <guid isPermaLink="false">https://medium.com/p/993ba8118bb2</guid>
            <category><![CDATA[subarray]]></category>
            <category><![CDATA[blog]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[codeforces]]></category>
            <category><![CDATA[sum]]></category>
            <dc:creator><![CDATA[Emdad Hossain]]></dc:creator>
            <pubDate>Fri, 05 May 2023 19:07:45 GMT</pubDate>
            <atom:updated>2023-05-05T19:50:56.042Z</atom:updated>
            <content:encoded><![CDATA[<h3>LeetCode problem — Finding Max sum of Sub Array in a given array (C++) Kadane’s Algorithm</h3><blockquote>Given an integer array nums, find the subarray with the largest sum, and return <em>its sum</em>.</blockquote><blockquote><strong>Example 1:</strong></blockquote><pre>Input: nums = [-2,1,-3,4,-1,2,1,-5,4]<br>Output: 6<br>Explanation: The subarray [4,-1,2,1] has the largest sum 6.</pre><p><strong>Example 2:</strong></p><pre>Input: nums = [1]<br>Output: 1<br>Explanation: The subarray [1] has the largest sum 1.</pre><p><strong>Example 3:</strong></p><pre>Input: nums = [5,4,-1,7,8]<br>Output: 23<br>Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= nums.length &lt;= 105</li><li>-104 &lt;= nums[i] &lt;= 104</li></ul><p><strong>Follow up:</strong> If you have figured out the O(n) solution, try coding another solution using the <strong>divide and conquer</strong> approach, which is more subtle.</p><blockquote>Solution</blockquote><pre>class Solution {<br>public:<br>    int sum=0,best=INT_MIN;<br>    int maxSubArray(vector&lt;int&gt;&amp; v) {<br>      <br>     <br>for (int k = 0; k &lt; v.size(); k++)<br>  {<br>    sum = max(v[k], sum + v[k]);<br>    best = max(sum, best);<br>  }<br> return best;<br>      <br>  <br>        <br>    }<br>};</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=993ba8118bb2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Connecting Towns — Hackerrank Poblem (C++)]]></title>
            <link>https://medium.com/@Emdad/connecting-towns-hackerrank-poblem-c-6fedb84c3fa7?source=rss-bad745323808------2</link>
            <guid isPermaLink="false">https://medium.com/p/6fedb84c3fa7</guid>
            <category><![CDATA[hackerrank]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Emdad Hossain]]></dc:creator>
            <pubDate>Wed, 03 May 2023 10:53:42 GMT</pubDate>
            <atom:updated>2023-05-03T10:53:42.365Z</atom:updated>
            <content:encoded><![CDATA[<h3>Connecting Towns — Hackerrank Poblem (C++)</h3><p><a href="https://www.hackerrank.com/challenges/connecting-towns/problem">https://www.hackerrank.com/challenges/connecting-towns/problem</a></p><p>Cities on a map are connected by a number of roads. The number of roads between each city is in an array and city is the starting location. The number of roads from city to city is the first value in the array, from city to city is the second, and so on.</p><p>How many paths are there from city to the last city in the list, modulo ?</p><p><strong>Example</strong></p><p>There are roads to city , roads to city and roads to city . The total number of roads is .</p><p><strong>Note</strong><br>Pass all the towns Ti for i=1 to n-1 in numerical order to reach Tn.</p><p><strong>Function Description</strong></p><p>Complete the connectingTowns function in the editor below.</p><p>connectingTowns has the following parameters:</p><ul><li>int n: the number of towns</li><li>int routes[n-1]: the number of routes between towns</li></ul><p><strong>Returns</strong></p><ul><li>int: the total number of routes, modulo 1234567.</li></ul><p><strong>Input Format</strong><br>The first line contains an integer T, T test-cases follow.</p><p>Each test-case has 2 lines.<br>The first line contains an integer N (the number of towns).<br>The second line contains N — 1 space separated integers where the ith integer denotes the number of routes, Ni, from the town Ti to Ti+1</p><p><strong>Constraints</strong><br>1 &lt;= T&lt;=1000<br>2&lt; N &lt;=100<br>1 &lt;= routes[i] &lt;=1000</p><p><strong>Sample Input</strong></p><pre>2<br>3<br>1 3<br>4<br>2 2 2</pre><p><strong>Sample Output</strong></p><pre>3<br>8</pre><p><strong>Explanation</strong><br>Case 1: 1 route from T1 to T2, 3 routes from T2 to T3, hence only 3 routes.<br>Case 2: There are 2 routes from each city to the next, hence 2 * 2 * 2 = 8.</p><h4>Solution</h4><pre>#include &lt;bits/stdc++.h&gt;<br><br>using namespace std;<br><br>string ltrim(const string &amp;);<br>string rtrim(const string &amp;);<br>vector&lt;string&gt; split(const string &amp;);<br><br>/*<br> * Complete the &#39;connectingTowns&#39; function below.<br> *<br> * The function is expected to return an INTEGER.<br> * The function accepts following parameters:<br> *  1. INTEGER n<br> *  2. INTEGER_ARRAY routes<br> */<br><br>int connectingTowns(int n, vector&lt;int&gt; routes) {<br> unsigned long long result=1;<br> for(int route:routes){<br>    <br>    result= (result*route)%1234567;<br>   <br> }<br>      <br>      return result; <br>}<br><br>int main()<br>{<br>    ofstream fout(getenv(&quot;OUTPUT_PATH&quot;));<br><br>    string t_temp;<br>    getline(cin, t_temp);<br><br>    int t = stoi(ltrim(rtrim(t_temp)));<br><br>    for (int t_itr = 0; t_itr &lt; t; t_itr++) {<br>        string n_temp;<br>        getline(cin, n_temp);<br><br>        int n = stoi(ltrim(rtrim(n_temp)));<br><br>        string routes_temp_temp;<br>        getline(cin, routes_temp_temp);<br><br>        vector&lt;string&gt; routes_temp = split(rtrim(routes_temp_temp));<br><br>        vector&lt;int&gt; routes(n - 1);<br><br>        for (int i = 0; i &lt; n - 1; i++) {<br>            int routes_item = stoi(routes_temp[i]);<br><br>            routes[i] = routes_item;<br>        }<br><br>        int result = connectingTowns(n, routes);<br><br>        fout &lt;&lt; result &lt;&lt; &quot;\n&quot;;<br>    }<br><br>    fout.close();<br><br>    return 0;<br>}<br><br>string ltrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        s.begin(),<br>        find_if(s.begin(), s.end(), not1(ptr_fun&lt;int, int&gt;(isspace)))<br>    );<br><br>    return s;<br>}<br><br>string rtrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        find_if(s.rbegin(), s.rend(), not1(ptr_fun&lt;int, int&gt;(isspace))).base(),<br>        s.end()<br>    );<br><br>    return s;<br>}<br><br>vector&lt;string&gt; split(const string &amp;str) {<br>    vector&lt;string&gt; tokens;<br><br>    string::size_type start = 0;<br>    string::size_type end = 0;<br><br>    while ((end = str.find(&quot; &quot;, start)) != string::npos) {<br>        tokens.push_back(str.substr(start, end - start));<br><br>        start = end + 1;<br>    }<br><br>    tokens.push_back(str.substr(start));<br><br>    return tokens;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6fedb84c3fa7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leonardo’s Prime Factors- Hackerrank Problem (C++)]]></title>
            <link>https://medium.com/@Emdad/leonardos-prime-factors-hackerrank-problem-c-49bc8e7f0b8a?source=rss-bad745323808------2</link>
            <guid isPermaLink="false">https://medium.com/p/49bc8e7f0b8a</guid>
            <category><![CDATA[prime-factor]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[hackerrank]]></category>
            <category><![CDATA[prime-numbers]]></category>
            <dc:creator><![CDATA[Emdad Hossain]]></dc:creator>
            <pubDate>Tue, 02 May 2023 16:08:47 GMT</pubDate>
            <atom:updated>2023-05-02T16:08:47.349Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://www.hackerrank.com/challenges/leonardo-and-prime/problem?isFullScreen=true">https://www.hackerrank.com/challenges/leonardo-and-prime/problem?isFullScreen=true</a></p><p>Leonardo loves primes and created queries where each query takes the form of an integer, . For each , count the maximum number of distinct prime factors of any number in the inclusive range .</p><p><strong>Note:</strong> Recall that a prime number is only divisible by and itself, and is not a prime number.</p><p><strong>Example</strong></p><p>The maximum number of distinct prime factors for values less than or equal to is . One value with distinct prime factors is . Another is .</p><p><strong>Function Description</strong></p><p>Complete the primeCount function in the editor below.</p><p>primeCount has the following parameters:</p><ul><li>int n: the inclusive limit of the range to check</li></ul><p><strong>Returns</strong></p><ul><li>int: the maximum number of distinct prime factors of any number in the inclusive range .</li></ul><p><strong>Input Format</strong></p><p>The first line contains an integer, , the number of queries.<br>Each of the next lines contains a single integer, .</p><p><strong>Constraints</strong></p><p><strong>Sample Input</strong></p><pre>6<br>1<br>2<br>3<br>500<br>5000<br>10000000000</pre><p><strong>Sample Output</strong></p><pre>0<br>1<br>1<br>4<br>5<br>10</pre><p><strong>Explanation</strong></p><ol><li>is not prime and its only factor is itself.</li><li>has prime factor, .</li><li>The number has prime factor, , has and has prime factors.</li><li>The product of the first four primes is . While higher value primes may be a factor of some numbers, there will never be more than distinct prime factors for a number in this range.</li></ol><h3>Solution</h3><pre>#include &lt;bits/stdc++.h&gt;<br><br>using namespace std;<br><br>string ltrim(const string &amp;);<br>string rtrim(const string &amp;);<br><br>/*<br> * Complete the &#39;primeCount&#39; function below.<br> *<br> * The function is expected to return an INTEGER.<br> * The function accepts LONG_INTEGER n as parameter.<br> */<br><br>int primeCount(long n) {<br>vector&lt;int&gt; primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};<br><br><br>  unsigned long long product = 1;<br>  int count = 0;<br><br>  if (n == 1)<br>  {<br>    return count;<br>  }<br><br>  for (int prime : primes)<br>  {<br>// product of primes <br>    product *= (unsigned long long)prime;<br><br>// exit conditions<br>    if (product &gt; n)<br>      break;<br>    if (product == n)<br>    {<br>      count++;<br>      break;<br>    }<br><br>    count++;<br>  }<br>  return count;<br>}<br><br>int main()<br>{<br>    ofstream fout(getenv(&quot;OUTPUT_PATH&quot;));<br><br>    string q_temp;<br>    getline(cin, q_temp);<br><br>    int q = stoi(ltrim(rtrim(q_temp)));<br><br>    for (int q_itr = 0; q_itr &lt; q; q_itr++) {<br>        string n_temp;<br>        getline(cin, n_temp);<br><br>        long n = stol(ltrim(rtrim(n_temp)));<br><br>        int result = primeCount(n);<br><br>        fout &lt;&lt; result &lt;&lt; &quot;\n&quot;;<br>    }<br><br>    fout.close();<br><br>    return 0;<br>}<br><br>string ltrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        s.begin(),<br>        find_if(s.begin(), s.end(), not1(ptr_fun&lt;int, int&gt;(isspace)))<br>    );<br><br>    return s;<br>}<br><br>string rtrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        find_if(s.rbegin(), s.rend(), not1(ptr_fun&lt;int, int&gt;(isspace))).base(),<br>        s.end()<br>    );<br><br>    return s;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49bc8e7f0b8a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Army Game — Hackerrank Problem (C++)]]></title>
            <link>https://medium.com/@Emdad/army-game-hackerrank-problem-c-da072053e38f?source=rss-bad745323808------2</link>
            <guid isPermaLink="false">https://medium.com/p/da072053e38f</guid>
            <category><![CDATA[graph]]></category>
            <category><![CDATA[plot]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[hackerrank]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[Emdad Hossain]]></dc:creator>
            <pubDate>Sun, 30 Apr 2023 18:27:37 GMT</pubDate>
            <atom:updated>2023-04-30T18:27:37.276Z</atom:updated>
            <content:encoded><![CDATA[<h3>Army Game — Hackerrank Problem (C++)</h3><p>Luke is daydreaming in Math class. He has a sheet of graph paper with rows and columns, and he imagines that there is an army base in each cell for a total of bases. He wants to drop supplies at strategic points on the sheet, marking each drop point with a red dot. If a base contains at least one package inside or on top of its border fence, then it’s considered to be supplied. For example:</p><p>Given and , what’s the minimum number of packages that Luke must drop to supply all of his bases?</p><p><strong>Example</strong></p><p>Packages can be dropped at the corner between cells (0, 0), (0, 1), (1, 0) and (1, 1) to supply bases. Another package can be dropped at a border between (0, 2) and (1, 2). This supplies all bases using packages.</p><p><strong>Function Description</strong></p><p>Complete the gameWithCells function in the editor below.</p><p>gameWithCells has the following parameters:</p><ul><li>int n: the number of rows in the game</li><li>int m: the number of columns in the game</li></ul><p><strong>Returns</strong></p><ul><li>int: the minimum number of packages required</li></ul><p><strong>Input Format</strong></p><p>Two space-separated integers describing the respective values of and .</p><p><strong>Constraints</strong></p><p><strong>Sample Input 0</strong></p><pre>2 2</pre><p><strong>Sample Output 0</strong></p><pre>1</pre><p><strong>Explanation 0</strong></p><p>Luke has four bases in a grid. If he drops a single package where the walls of all four bases intersect, then those four cells can access the package:</p><p>Because he managed to supply all four bases with a single supply drop, we print as our answer.</p><h4>Solution</h4><pre>#include &lt;bits/stdc++.h&gt;<br><br>using namespace std;<br><br>string ltrim(const string &amp;);<br>string rtrim(const string &amp;);<br>vector&lt;string&gt; split(const string &amp;);<br><br>/*<br> * Complete the &#39;gameWithCells&#39; function below.<br> *<br> * The function is expected to return an INTEGER.<br> * The function accepts following parameters:<br> *  1. INTEGER n<br> *  2. INTEGER m<br> */<br><br>int gameWithCells(int n, int m) {<br>    // consider edges and corners of a cell,<br>    // if we put drops in corner it will be available to 4 adjacent cells<br>    // if we put in th edge and drag it to corner same case will occur<br>    // so the intersecting point covers 2 cells horizontally and 2 cells vertically<br>    // using the unit law we will<br>    <br>    return ((n/2)+bool(n%2)) * ((m/2)+bool(m%2)) ;<br>    // here each droping covers 2 rows and 2 columns as per the above explanation<br>    // <br>}<br><br>int main()<br>{<br>    ofstream fout(getenv(&quot;OUTPUT_PATH&quot;));<br><br>    string first_multiple_input_temp;<br>    getline(cin, first_multiple_input_temp);<br><br>    vector&lt;string&gt; first_multiple_input = split(rtrim(first_multiple_input_temp));<br><br>    int n = stoi(first_multiple_input[0]);<br><br>    int m = stoi(first_multiple_input[1]);<br><br>    int result = gameWithCells(n, m);<br><br>    fout &lt;&lt; result &lt;&lt; &quot;\n&quot;;<br><br>    fout.close();<br><br>    return 0;<br>}<br><br>string ltrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        s.begin(),<br>        find_if(s.begin(), s.end(), not1(ptr_fun&lt;int, int&gt;(isspace)))<br>    );<br><br>    return s;<br>}<br><br>string rtrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        find_if(s.rbegin(), s.rend(), not1(ptr_fun&lt;int, int&gt;(isspace))).base(),<br>        s.end()<br>    );<br><br>    return s;<br>}<br><br>vector&lt;string&gt; split(const string &amp;str) {<br>    vector&lt;string&gt; tokens;<br><br>    string::size_type start = 0;<br>    string::size_type end = 0;<br><br>    while ((end = str.find(&quot; &quot;, start)) != string::npos) {<br>        tokens.push_back(str.substr(start, end - start));<br><br>        start = end + 1;<br>    }<br><br>    tokens.push_back(str.substr(start));<br><br>    return tokens;<br>}</pre><p>Happy Coding~</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da072053e38f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Minimum Height Triangle — Hackerrank (C++) problem]]></title>
            <link>https://medium.com/@Emdad/minimum-height-triangle-hackerrank-c-problem-a2f1f50d4b66?source=rss-bad745323808------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2f1f50d4b66</guid>
            <category><![CDATA[triangle]]></category>
            <category><![CDATA[hackerrank]]></category>
            <category><![CDATA[area]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[mathematics]]></category>
            <dc:creator><![CDATA[Emdad Hossain]]></dc:creator>
            <pubDate>Sun, 30 Apr 2023 15:11:10 GMT</pubDate>
            <atom:updated>2023-04-30T15:11:10.165Z</atom:updated>
            <content:encoded><![CDATA[<h3>Minimum Height Triangle — Hackerrank (C++) problem</h3><p><a href="https://www.hackerrank.com/challenges/lowest-triangle/problem?isFullScreen=true">https://www.hackerrank.com/challenges/lowest-triangle/problem?isFullScreen=true</a></p><p>Given integers and , find the smallest integer , such that there exists a triangle of height , base , having an area of at least .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/251/0*y7-VvqSniXlWSi_-.jpg" /></figure><p><strong>Example</strong></p><p>The minimum height is . One example is a triangle formed at points (0, 0), (4, 0), (2, 3).</p><p><strong>Function Description</strong></p><p>Complete the lowestTriangle function in the editor below.</p><p>lowestTriangle has the following parameters:</p><ul><li>int b: the base of the triangle</li><li>int a: the minimum area of the triangle</li></ul><p><strong>Returns</strong></p><ul><li>int: the minimum integer height to form a triangle with an area of at least</li></ul><p><strong>Input Format</strong></p><p>There are two space-separated integers and , on a single line.</p><p><strong>Constraints</strong></p><p><strong>Sample Input 0</strong></p><pre>2 2</pre><p><strong>Sample Output 0</strong></p><pre>2</pre><p><strong>Explanation 0</strong></p><p>The task is to find the smallest integer height of the triangle with base and area at least . It turns out, that there are triangles with height 2, base 2 and area 2, for example a triangle with corners in the following points: :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/0*n2uIHKXMcWxjpyaH.png" /></figure><p>It can be proved that there is no triangle with integer height smaller than , base and area at least .</p><p><strong>Sample Input 1</strong></p><pre>17 100</pre><p><strong>Sample Output 1</strong></p><pre>12</pre><p><strong>Explanation 1</strong></p><p>The task is to find the smallest integer height of the triangle with base 17 and area at least 100 . It turns out, that there are triangles with height 12, base 17and area 102, for example a triangle with corners in the following points: (2,2), (19,2), (16,14).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/0*G1VUOPg-6WZBkuJE.png" /></figure><p>It can be proved that there is no triangle with integer height smaller than 12, base 17 and area 100 at least .</p><h4>Solution</h4><pre>#include &lt;bits/stdc++.h&gt;<br><br>using namespace std;<br><br>string ltrim(const string &amp;);<br>string rtrim(const string &amp;);<br>vector&lt;string&gt; split(const string &amp;);<br><br>/*<br> * Complete the &#39;lowestTriangle&#39; function below.<br> *<br> * The function is expected to return an INTEGER.<br> * The function accepts following parameters:<br> *  1. INTEGER trianglebase<br> *  2. INTEGER area<br> */<br><br>int lowestTriangle(int trianglebase, int area) {<br>    /*<br>     here is <br>    - formula for triangle area= 1/2 * base * height<br>    - bool (2*area%trianglebase) this part is for rounding up;<br>    */<br>    return (2*area/trianglebase)+bool(2*area%trianglebase);<br><br>}<br><br>int main()<br>{<br>    ofstream fout(getenv(&quot;OUTPUT_PATH&quot;));<br><br>    string first_multiple_input_temp;<br>    getline(cin, first_multiple_input_temp);<br><br>    vector&lt;string&gt; first_multiple_input = split(rtrim(first_multiple_input_temp));<br><br>    int trianglebase = stoi(first_multiple_input[0]);<br><br>    int area = stoi(first_multiple_input[1]);<br><br>    int height = lowestTriangle(trianglebase, area);<br><br>    fout &lt;&lt; height &lt;&lt; &quot;\n&quot;;<br><br>    fout.close();<br><br>    return 0;<br>}<br><br>string ltrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        s.begin(),<br>        find_if(s.begin(), s.end(), not1(ptr_fun&lt;int, int&gt;(isspace)))<br>    );<br><br>    return s;<br>}<br><br>string rtrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        find_if(s.rbegin(), s.rend(), not1(ptr_fun&lt;int, int&gt;(isspace))).base(),<br>        s.end()<br>    );<br><br>    return s;<br>}<br><br>vector&lt;string&gt; split(const string &amp;str) {<br>    vector&lt;string&gt; tokens;<br><br>    string::size_type start = 0;<br>    string::size_type end = 0;<br><br>    while ((end = str.find(&quot; &quot;, start)) != string::npos) {<br>        tokens.push_back(str.substr(start, end - start));<br><br>        start = end + 1;<br>    }<br><br>    tokens.push_back(str.substr(start));<br><br>    return tokens;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2f1f50d4b66" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handshake — Hackerran Problem about combinatorics (C++)]]></title>
            <link>https://medium.com/@Emdad/handshake-hackerran-problem-about-combinatorics-c-7a5d71b740ec?source=rss-bad745323808------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a5d71b740ec</guid>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[combination]]></category>
            <category><![CDATA[hackerrank]]></category>
            <category><![CDATA[blog]]></category>
            <category><![CDATA[combinatorics]]></category>
            <dc:creator><![CDATA[Emdad Hossain]]></dc:creator>
            <pubDate>Sun, 30 Apr 2023 13:04:19 GMT</pubDate>
            <atom:updated>2023-04-30T13:07:28.064Z</atom:updated>
            <content:encoded><![CDATA[<h3>Handshake — Hackerrank Problem about combinatorics (C++)</h3><p>At the annual meeting of Board of Directors of Acme Inc. If everyone attending shakes hands exactly one time with every other attendee, how many handshakes are there?</p><p><strong>Example</strong></p><p>n=3;</p><p>There are attendees 3, p1, p2 and p3. p1 shakes hands with p2 and p3 , and p2 shakes hands with p3 . Now they have all shaken hands after 3 handshakes.</p><p><strong>Function Description</strong></p><p>Complete the handshakes function in the editor below.</p><p>handshakes has the following parameter:</p><ul><li>int n: the number of attendees</li></ul><p><strong>Returns</strong></p><ul><li>int: the number of handshakes</li></ul><p><strong>Input Format</strong><br>The first line contains the number of test cases .<br>Each of the following lines contains an integer, .</p><p><strong>Constraints</strong></p><p>1 ≤ t ≤ 1000</p><p>0 &lt; n &lt; 10e6</p><p><strong>Sample Input</strong></p><pre>2<br>1<br>2</pre><p><strong>Sample Output</strong></p><pre>0<br>1</pre><p><strong>Explanation</strong></p><p>Case 1 : The lonely board member shakes no hands, hence 0.<br>Case 2 : There are 2 board members, so 1 handshake takes place.</p><h4><strong>Solution</strong></h4><pre>#include &lt;bits/stdc++.h&gt;<br><br>using namespace std;<br><br>string ltrim(const string &amp;);<br>string rtrim(const string &amp;);<br><br>/*<br> * Complete the &#39;handshake&#39; function below.<br> *<br> * The function is expected to return an INTEGER.<br> * The function accepts INTEGER n as parameter.<br> */<br><br>int handshake(int n) {<br>    /* n!/r!(n-r)! finding ncr (combinations) if normalized<br>     <br>    n!/2!(n-2)!<br>    n(n-1)(n-2)!/(n-2)!2!<br>    n(n-1)/2!<br>    n(n-1)/2 (as factorial of 2 is 2*1==2)<br>    */<br>return n*(n-1)/2;<br>}<br><br>int main()<br>{<br>    ofstream fout(getenv(&quot;OUTPUT_PATH&quot;));<br><br>    string t_temp;<br>    getline(cin, t_temp);<br><br>    int t = stoi(ltrim(rtrim(t_temp)));<br><br>    for (int t_itr = 0; t_itr &lt; t; t_itr++) {<br>        string n_temp;<br>        getline(cin, n_temp);<br><br>        int n = stoi(ltrim(rtrim(n_temp)));<br><br>        int result = handshake(n);<br><br>        fout &lt;&lt; result &lt;&lt; &quot;\n&quot;;<br>    }<br><br>    fout.close();<br><br>    return 0;<br>}<br><br>string ltrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        s.begin(),<br>        find_if(s.begin(), s.end(), not1(ptr_fun&lt;int, int&gt;(isspace)))<br>    );<br><br>    return s;<br>}<br><br>string rtrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        find_if(s.rbegin(), s.rend(), not1(ptr_fun&lt;int, int&gt;(isspace))).base(),<br>        s.end()<br>    );<br><br>    return s;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a5d71b740ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Find The Point — Hackerrank Problem (C++)]]></title>
            <link>https://medium.com/@Emdad/find-the-point-hackerrank-problem-c-bf6c41ba7955?source=rss-bad745323808------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf6c41ba7955</guid>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[hackerrank]]></category>
            <category><![CDATA[vector]]></category>
            <dc:creator><![CDATA[Emdad Hossain]]></dc:creator>
            <pubDate>Sat, 29 Apr 2023 13:20:56 GMT</pubDate>
            <atom:updated>2023-04-29T13:20:56.862Z</atom:updated>
            <content:encoded><![CDATA[<h3>Find The Point — Hackerrank Problem (C++)</h3><p><a href="https://www.hackerrank.com/challenges/find-point/problem?isFullScreen=true">https://www.hackerrank.com/challenges/find-point/problem?isFullScreen=true</a></p><p>Consider two points , and . We consider the inversion or <a href="https://en.wikipedia.org/wiki/Point_reflection">point reflection</a>, , of point across point to be a rotation of point around .</p><p>Given sets of points and , find for each pair of points and print two space-separated integers denoting the respective values of and on a new line.</p><p><strong>Function Description</strong></p><p>Complete the findPoint function in the editor below.</p><p>findPoint has the following parameters:</p><ul><li>int px, py, qx, qy: x and y coordinates for points and</li></ul><p><strong>Returns</strong></p><ul><li>int[2]: x and y coordinates of the reflected point</li></ul><p><strong>Input Format</strong></p><p>The first line contains an integer, , denoting the number of sets of points.<br>Each of the subsequent lines contains four space-separated integers that describe the respective values of , , , and defining points and .</p><p><strong>Constraints</strong></p><p><strong>Sample Input</strong></p><pre>2<br>0 0 1 1<br>1 1 2 2</pre><p><strong>Sample Output</strong></p><pre>2 2<br>3 3</pre><p><strong>Explanation</strong></p><p>The graphs below depict points , , and for the points given as Sample Input:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/255/0*0gVu4LmC_nhn4DUH.png" /></figure><h3>Solution</h3><pre>#include &lt;bits/stdc++.h&gt;<br><br>using namespace std;<br><br>string ltrim(const string &amp;);<br>string rtrim(const string &amp;);<br>vector&lt;string&gt; split(const string &amp;);<br><br>/*<br> * Complete the &#39;findPoint&#39; function below.<br> *<br> * The function is expected to return an INTEGER_ARRAY.<br> * The function accepts following parameters:<br> *  1. INTEGER px<br> *  2. INTEGER py<br> *  3. INTEGER qx<br> *  4. INTEGER qy<br> */<br><br>/*<br>*    explanation<br>*    p`x-qx = qx-px; // 180 degree to the point of px to qx is p`x<br>*    p`y-qy = qy-py; // 180 degree to the point of py to qy is p`y<br>*/<br>vector&lt;int&gt; findPoint(int px, int py, int qx, int qy) {<br>vector&lt;int&gt; r;<br>int value1,value2;<br>value1= 2*qx-px;<br>value2= 2*qy-py;<br><br>r.push_back(value1);<br>r.push_back(value2);<br><br>return r;<br>}<br><br>int main()<br>{<br>    ofstream fout(getenv(&quot;OUTPUT_PATH&quot;));<br><br>    string n_temp;<br>    getline(cin, n_temp);<br><br>    int n = stoi(ltrim(rtrim(n_temp)));<br><br>    for (int n_itr = 0; n_itr &lt; n; n_itr++) {<br>        string first_multiple_input_temp;<br>        getline(cin, first_multiple_input_temp);<br><br>        vector&lt;string&gt; first_multiple_input = split(rtrim(first_multiple_input_temp));<br><br>        int px = stoi(first_multiple_input[0]);<br><br>        int py = stoi(first_multiple_input[1]);<br><br>        int qx = stoi(first_multiple_input[2]);<br><br>        int qy = stoi(first_multiple_input[3]);<br><br>        vector&lt;int&gt; result = findPoint(px, py, qx, qy);<br><br>        for (size_t i = 0; i &lt; result.size(); i++) {<br>            fout &lt;&lt; result[i];<br><br>            if (i != result.size() - 1) {<br>                fout &lt;&lt; &quot; &quot;;<br>            }<br>        }<br><br>        fout &lt;&lt; &quot;\n&quot;;<br>    }<br><br>    fout.close();<br><br>    return 0;<br>}<br><br>string ltrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        s.begin(),<br>        find_if(s.begin(), s.end(), not1(ptr_fun&lt;int, int&gt;(isspace)))<br>    );<br><br>    return s;<br>}<br><br>string rtrim(const string &amp;str) {<br>    string s(str);<br><br>    s.erase(<br>        find_if(s.rbegin(), s.rend(), not1(ptr_fun&lt;int, int&gt;(isspace))).base(),<br>        s.end()<br>    );<br><br>    return s;<br>}<br><br>vector&lt;string&gt; split(const string &amp;str) {<br>    vector&lt;string&gt; tokens;<br><br>    string::size_type start = 0;<br>    string::size_type end = 0;<br><br>    while ((end = str.find(&quot; &quot;, start)) != string::npos) {<br>        tokens.push_back(str.substr(start, end - start));<br><br>        start = end + 1;<br>    }<br><br>    tokens.push_back(str.substr(start));<br><br>    return tokens;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf6c41ba7955" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>