Max Points On a Line (Asked in Google, Apple, LinkedIn, Amazon) - Codexplained

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ม.ค. 2025
  • On Episode 17 of Codexplained, we're going to be tackling the coding problem, "Max Points On a Line". Points scattered everywhere! Tis' confusion! It's time to connect the dots!
    Want to code the problem for yourself? Visit this link - p.hck.re/3fnt
    At Codexplained, we aim to break those difficult coding questions down into small and easily digestible chunks. Interested to know more? • HackerEarth's CODEXPLA...
    Learn the basics of programming with easy-to-learn lessons at CodeMonk from HackerEarth. Click here - p.hck.re/ixea
    Want to follow the journeys of influential programmers, listen in as they share their inspiring tales? If you do, make sure to listen to Codecast, HackerEarth's epic podcast, at spoti.fi/2NCV2vW

ความคิดเห็น • 22

  • @acceleratorlevel645
    @acceleratorlevel645 2 ปีที่แล้ว +1

    alright, you earned a sub, beautiful explanation of the problem before providing solution , that was enough for me to come up with the answer, this is what i wanted, amazing!

  • @aryanyadav3926
    @aryanyadav3926 2 ปีที่แล้ว +3

    Thanks for the solution and explanation!

  • @kewtomrao
    @kewtomrao 2 ปีที่แล้ว +2

    Those hints were enough to solve the problem. Thanks! Subscribed!

  • @nani4027
    @nani4027 3 ปีที่แล้ว +1

    Where are you checking the common points in the code?

    • @HackerEarthInc
      @HackerEarthInc  3 ปีที่แล้ว

      All lines passing through one point and having the same slope, lie on the same line (1:50).
      Lines 12 and 14 in the code are used to calculate the number of points passing through point 'i', with slope 'slope'. In other words, the number of points that are colinear, with i as a common point.
      Once the i iteration ends, the next i value will be different. Hence, we clear the dictionary in line 16.

  • @sandeshsonawane4988
    @sandeshsonawane4988 3 ปีที่แล้ว +3

    why are we clearing hashmap each time

    • @HackerEarthInc
      @HackerEarthInc  3 ปีที่แล้ว +8

      Good question - it's because of parallel lines. Say the points are:
      (0,0), (1,1), (0,1), (1,2), (2.3)
      If you plot them out on a graph, you will find that (0,0) and (1,1) lie on one line, and (0,1), (1,2) and (2,3) lie on another parallel line. Both have a slope of 1. Here, the answer must be 3.
      When we complete i=0, the hashmap will say that slope 1 has 1 point.
      If we do not clear the hashmap, when all iterations are complete, the hashmap will say that slope 1 has 4 points (1 from i=0 + 2 from i=2 +1 from i=3). That's because the slope is the same, and it will give us an incorrect answer of 5 (1 gets added at the end).
      dict.clear ensures that the slope is reset after each point. Now, before we enter i=2, the value is reset, and when i=2 is complete, the hasmap will say that slope 1 has 2 points, which becomes 3 when we add 1 at the end.
      To identify colinear points, the slopes have to be the same, and they have to pass through at least one common point (1:50). Since the dictionary/hashmap is only tracking the slope, it must be reset whenever we change that common point after each i iteration.

    • @sandeshsonawane4988
      @sandeshsonawane4988 3 ปีที่แล้ว

      @@HackerEarthInc Great Explanation Thank you!!

    • @me.deepaksharma
      @me.deepaksharma 3 ปีที่แล้ว

      @@HackerEarthInc Thanks.

  • @two_wheel_tales_jsr
    @two_wheel_tales_jsr 2 ปีที่แล้ว

    Osm Explanation love it bro

  • @satyamjha6268
    @satyamjha6268 2 ปีที่แล้ว

    Nyc explanation

  • @meetshah2288
    @meetshah2288 2 ปีที่แล้ว

    No need for max variable in the i loop. Changing max doesn't make a difference to the i value since range is precalculated taking max as 0 in the begining of the for loop.
    So you are not doing any optimization by adding max in the range() of i loop

    • @sunilpanchal1498
      @sunilpanchal1498 2 ปีที่แล้ว

      You can add one condition inside of for loop if(n-max>=i) break then it will save your iteration

  • @adyantgupta4661
    @adyantgupta4661 2 ปีที่แล้ว

    j should loop from 0 not from i+1

  • @saiudaydasari
    @saiudaydasari 3 ปีที่แล้ว +1

    parallel lines have same slope then ur adding all parallel line points to one point then ur code will fail

    • @HackerEarthInc
      @HackerEarthInc  3 ปีที่แล้ว +6

      We are not counting the parallel line points, but rather the colinear points. The way to check if many points are colinear is if they have the same slope and pass through at least 1 common point.
      In each j loop, we are considering the slopes of the lines passing through one common point. Parallel lines never intersect, so if they have the same slope and pass through a common point, it means that it must be same line!
      After each j loop, we are clearing the dictionary using dict.clear. We are starting fresh, with another point. The counter gets reset to 0, so if there is another parallel line, it will be counted separately.

  • @yogeshyts
    @yogeshyts 2 ปีที่แล้ว +2

    hindi bolne m sharm aati h ky

    • @henrypaul5676
      @henrypaul5676 2 ปีที่แล้ว +1

      angrezi seekhne me aalas aata hai kya? nhk bhai??

    • @yogeshyts
      @yogeshyts 2 ปีที่แล้ว +2

      @@henrypaul5676 h

    • @henrypaul5676
      @henrypaul5676 2 ปีที่แล้ว

      To seekh le bhai. Dawai ki dukan pr to nahi bolta ki angreji naam wli dawa mt de wrna nahi khaoonga😂😂

    • @yogeshyts
      @yogeshyts 2 ปีที่แล้ว +1

      @@henrypaul5676 English psnd ni