Блог пользователя KiZamaDo17

Автор KiZamaDo17, история, 9 месяцев назад, По-английски

I attempted the D problem in JAVA 21 64bit with the simple logic of storing the remainder of a[i] in pairs along with their frequency in HashMap and check if (x-(a[i]%x)),(a[i]%y)) is already present, adding 1 to answer if it happens. I also make the remainder equal to x if it comes out to be zero.

Heres my solution

My Answer

but for some reason it fails the 12th test. I don't know what on God's Earth it is, but I then just convert the same logic to C++ and its accepted.

Where am I going wrong? What seems to be the problem here? Is there an alternative? A friend of mine suggested using a 2D array instead of a map an hence saving the trouble of creating pair class in JAVA but I could not fully grasp his concept. Please help me out here.

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
9 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by KiZamaDo17 (previous revision, new revision, compare).

»
9 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by KiZamaDo17 (previous revision, new revision, compare).

»
9 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I don't know much about Java but it looks like your "pair" implementation such return a 64-bit integer since to hash it in a * M + b then M should be greater than b.

In addition, the answer may get larger than 32-bit integers so consider using 64-bit integers in this case.

  • »
    »
    9 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Hey thank you for your reply. I am already using long data type. It's a 64 bit integer data type. For your hashing suggestion, let me try it real quick.

  • »
    »
    9 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I implemented all your suggestions and this is what is happening. When I use M in hashing function anything less than 64, it gives me a wrong asnwer at test case 12

    Using M=63 here

    and when I use M=64, it gives me a TLE at M=64 at test case 4

    When M is 64

    What to do now? Vladosiya any clues you can give mate?

    • »
      »
      »
      9 месяцев назад, # ^ |
        Проголосовать: нравится +12 Проголосовать: не нравится

      Line 16: int ans = 0 ;

      it should be long ans = 0;

      • »
        »
        »
        »
        9 месяцев назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        If that would actually be my only fault I swear to god I should quit. I will check it once CF allows me to. PS:- I saw your solution man. You actually just used a string instead of a pair. Why can't I think like you? That was so clever. Thank you for the help.

      • »
        »
        »
        »
        9 месяцев назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        I just realized the maximum no. Of pairs can be n(n-1)/2 and not n/2 which I was previously thinking so this just might be it.