Missing HDD space after diskutil command I wanted to merge 2 partitions disk0s2(unused space) and disk0s4(OS volume)

My disk looked like this:

/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                  Apple_HFS Untitled                500.6 GB   disk0s2
   3:                 Apple_APFS Container disk2         199.3 GB   disk0s4
   4:       Microsoft Basic Data BOOTCAMP                300.0 GB   disk0s5


Somewhere here I found a solution so I ran the command


  sudo diskutil erasevolume "Free Space" %noformat% /dev/disk0s2


But then the next command


  sudo diskutil apfs resizeContainer disk0s4 0


Gave error:


  Error: -69519: The target disk is too small for this operation, or a gap is required in your partition map which is missing or too small, which is often caused by an attempt to grow a partition beyond the beginning of another partition or beyond the end of partition map usable space


So now it looks like this:

/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                 Apple_APFS Container disk2         199.3 GB   disk0s4
   3:       Microsoft Basic Data BOOTCAMP                300.0 GB   disk0s5



  gpt show disk0


       start        size  index  contents
           0           1         PMBR
           1           1         Pri GPT header
           2          32         Pri GPT table
          34           6         
          40      409600      1  GPT part - C12A7328-F81F-11D2-BA4B-00A0C93EC93B
      409640   977979728         
   978389368   389234312      2  GPT part - 7C3457EF-0000-11AA-AA11-00306543ECAC
  1367623680   585901455      3  GPT part - EBD0A0A2-B9E5-4433-87C0-68B6B72699C7
  1953525135          32         Sec GPT table
  1953525167           1         Sec GPT header




The best would be if I could make the main volume to the full space available, but if it's  too difficult it would be enough to have that 500Gb as a separate volume as it was.
Your free space is before the container, not after it. In order to expand it, you need free physical space after the container. You could try using a live CD (such as GNU Parted) to physically relocate the blocks of the APFS container to start at 409640, but this is perilous and should not be done without a backup.