Skip to main content
added 4 characters in body
Source Link
Sai
  • 161
  • 3

(1) You are sorting the vector so duplicate values will be consecutively present in the vector.

(2) Now we come to the logic behind that condition:

  • As the vector can contain the duplicates we should make sure to not theto repeat the same sequence by starting with the same element again(duplicate)
    • example :
      • 1,1,2 (starting with first one) and 1,1,2 (starting with second one) => both are same (we should make sure this doesn't happen)

Example: In [1, 1, 2]

  • If the first element in the sequence is chosen as 1, then we should make sure we are not going to create another sequence starting with another 1 (duplicate of the element).

  • So for that, we need to skip the loop for all the consecutive duplicate elements.

  • In youyour case, after creating a sequence staring with first 1, when we enter the loop for the second time we check whether the current element is a duplicate nums[i] == nums[i - 1] (this is sufficient as you have already sorted the vector) and whether the current element is the first element of the sequence !used[i - 1].

    • In your case, due to this condition, the second one cannot be the first element of the sequence.

(1) You are sorting the vector so duplicate values will be consecutively present in the vector.

(2) Now we come to the logic behind that condition:

  • As the vector can contain the duplicates we should make sure to not the repeat the same sequence by starting with the same element again(duplicate)
    • example :
      • 1,1,2 (starting with first one) and 1,1,2 (starting with second one) => both are same (we should make sure this doesn't happen)

Example: In [1, 1, 2]

  • If the first element in the sequence is chosen as 1, then we should make sure we are not going to create another sequence starting with another 1 (duplicate of the element).

  • So for that we to skip the loop for all the consecutive duplicate elements.

  • In you case after creating a sequence staring with first 1, when we enter the loop for the second time we check whether the current element is a duplicate nums[i] == nums[i - 1] (this is sufficient as you have already sorted the vector) and whether the current element is the first element of the sequence !used[i - 1].

    • In your case, due to this condition, the second one cannot be the first element of the sequence.

(1) You are sorting the vector so duplicate values will be consecutively present in the vector.

(2) Now we come to the logic behind that condition:

  • As the vector can contain the duplicates we should make sure not to repeat the same sequence by starting with the same element again(duplicate)
    • example :
      • 1,1,2 (starting with first one) and 1,1,2 (starting with second one) => both are same (we should make sure this doesn't happen)

Example: In [1, 1, 2]

  • If the first element in the sequence is chosen as 1, then we should make sure we are not going to create another sequence starting with another 1 (duplicate of the element).

  • So for that, we need to skip the loop for all the consecutive duplicate elements.

  • In your case, after creating a sequence staring with first 1, when we enter the loop for the second time we check whether the current element is a duplicate nums[i] == nums[i - 1] (this is sufficient as you have already sorted the vector) and whether the current element is the first element of the sequence !used[i - 1].

    • In your case, due to this condition, the second one cannot be the first element of the sequence.
Source Link
Sai
  • 161
  • 3

(1) You are sorting the vector so duplicate values will be consecutively present in the vector.

(2) Now we come to the logic behind that condition:

  • As the vector can contain the duplicates we should make sure to not the repeat the same sequence by starting with the same element again(duplicate)
    • example :
      • 1,1,2 (starting with first one) and 1,1,2 (starting with second one) => both are same (we should make sure this doesn't happen)

Example: In [1, 1, 2]

  • If the first element in the sequence is chosen as 1, then we should make sure we are not going to create another sequence starting with another 1 (duplicate of the element).

  • So for that we to skip the loop for all the consecutive duplicate elements.

  • In you case after creating a sequence staring with first 1, when we enter the loop for the second time we check whether the current element is a duplicate nums[i] == nums[i - 1] (this is sufficient as you have already sorted the vector) and whether the current element is the first element of the sequence !used[i - 1].

    • In your case, due to this condition, the second one cannot be the first element of the sequence.