Array - 16: Check if there exists a pair which matches given Sum

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • Code: thecodingsimpl...
    Solution:
    Brute Force:
    - In this, each element will add to other element in array & check if target sum matches
    - Take two loop & add each element with every other & check the target sum.
    - Complexity: O(n^2)
    Sorting:
    - Sort the array
    - Take two variable, one from start & other from end.
    - If a[start] + a[end] = target_sum, it means there exists a sum
    - If target_sum is greater than a[start] + a[end], then decrease end, else increase start.
    - Complexity: O(nlog(n))
    Hashing:
    - Take one Set & iterate each element of array
    - If target_sum - arr[i] is in set, we got the solution, else keep adding element in array.
    Please check video for more info:
    This problem is similar to:
    array check there is a pair for given sum,
    array check if pair exists for sum,
    array if sum adds ti two values,
    pair,
    sum,
    array,
    java tutorial,
    coding simplified,
    java
    CHECK OUT CODING SIMPLIFIED
    / codingsimplified
    ★☆★ VIEW THE BLOG POST: ★☆★
    thecodingsimpli...
    I started my CZcams channel, Coding Simplified, during Dec of 2015.
    Since then, I've published over 400+ videos.
    ★☆★ SUBSCRIBE TO ME ON CZcams: ★☆★
    www.youtube.co...
    ★☆★ Send us mail at: ★☆★
    Email: thecodingsimplified@gmail.com

Komentáře • 35

  • @ManishaKumari-yv3nf
    @ManishaKumari-yv3nf Před 3 lety +3

    You r the only one who explained it in the👍💯 best way

  • @rasid_khan
    @rasid_khan Před 3 lety +5

    Im doing this playlist by using c++
    Now im going to learn java coz you playlists are very good🔥💓💓

    • @CodingSimplified
      @CodingSimplified  Před 3 lety

      Thanks for your nice feedback. Yeah, only syntax is different otherwise logic would be same.

    • @Robert-ob8bu
      @Robert-ob8bu Před 3 lety

      should i learn java ...i know cpp and i entered 3rd yr now

  • @thebot4346
    @thebot4346 Před 5 lety +2

    Sir iam a huge fan of your content ..plz make videos regularly

  • @clasherrony6526
    @clasherrony6526 Před 4 měsíci

    amazing explaination

  • @03ajaychauhan68
    @03ajaychauhan68 Před rokem

    while(start < end) not while(start

  • @AnkitRege
    @AnkitRege Před rokem

    Hi as you were saying we will increase the start or decrease the end, However I observed that the elements are getting increased or decreased not array element.
    example
    end = 12
    end -- = 11 -- not in the array
    can you please answer this, is this the desired outcome?

  • @explore_with_shanu
    @explore_with_shanu Před 2 lety +1

    Sir , for sorting approach there should be ->
    While (start < end )
    Not
    While (start

    • @tarundhouni2869
      @tarundhouni2869 Před rokem

      it will depend on the size of the array if it is even then 1 one if it is odd than 2 one

  • @vipulraj_1193
    @vipulraj_1193 Před 2 lety

    what if there are multiple numbers how to find pair sum in sorting method

  • @sunnykakrani7830
    @sunnykakrani7830 Před 4 lety

    plz make correction when u r using the concept of sorting the while condition should be like dis while(start

    • @CodingSimplified
      @CodingSimplified  Před 4 lety +1

      Nice Catch Sunny. For this case, Sorting case will give true. Updated the condition to while(start < end) rather than while(start

  • @HarshGupta-if2xg
    @HarshGupta-if2xg Před 5 lety +4

    Plzz post videos on Amazon interview questions

  • @neelpatel8821
    @neelpatel8821 Před 4 lety +1

    Can we get the index of those two elements using hashset

    • @sivaganesh4489
      @sivaganesh4489 Před 4 lety +1

      No you can get it by element only

    • @CodingSimplified
      @CodingSimplified  Před 4 lety +1

      Hi Neel, though it's late reply, but answering if it helps you. Using current Hashing solution, we can't get index. But if you want to get index. While insertion, store index as well. For this, you can take another structure like Node & store in hashSet.
      - Hope it helps you. Thanks.

  • @armanalirahul
    @armanalirahul Před 2 lety

    well explained than.

  • @ooomr.253
    @ooomr.253 Před 3 lety +1

    sir ekdum mast

  • @anishbishnoi29xD
    @anishbishnoi29xD Před 2 lety

    OP ,♥️

  • @hamsithachallagundla
    @hamsithachallagundla Před 4 lety

    Does this also work for duplicate elements?

    • @CodingSimplified
      @CodingSimplified  Před 4 lety

      If you've duplicate i.e 2, 3, 2, 3 & given sum.is 5 then it'll work. For case 2, 3, 2 & given sum 5. It'll given answer 2 using hashing. Now it all depends on requirements. If you're ok with 2 set (2, 3) & (2, 3) then it'll work. So as per your question, you need to change little bit in code. Thanks.

    • @deepak9365
      @deepak9365 Před 3 lety

      We can do using sorting we duplicate contain also

  • @theexpo7659
    @theexpo7659 Před rokem

    Bestttttt

  • @preetiipriya
    @preetiipriya Před 3 lety

    two pointer algorithm

  • @deepak9365
    @deepak9365 Před 3 lety

    If array contains duplicate elements then your code fails

    • @CodingSimplified
      @CodingSimplified  Před 3 lety +2

      For duplicate, we need little bit modify the code. Try to do it by yourself. If you need any help, let me know with your input.