Array - 36: Find all Triplets for given Sum | Check if Triplet exists for given Sum

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ม.ค. 2025

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

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

    It does not work if array is arr = [5, 5, 4, 4, 5, 4] even after sorting when triplet sum is 13 and size is explicitly 6. plz correct and help us!

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

      Yes

    • @jatinsharma3792
      @jatinsharma3792 11 หลายเดือนก่อน

      Buddy This will work.
      Bhai 2 wala solve kro fir 3 walo pakro...Is ka toh samjh ni aaya But 2 lagaya simple wala toh 3 wala clear ho gya
      int[] ar = {1,2,4,5,6};
      int target = 6;
      for (int i =0; i < ar.length; i++) {
      for ( int j = i+1; j < ar.length; j++) {
      if((ar[i] + ar[j]) == 6) {
      System.out.println(ar[i] + " + " + ar[j] + " = " + (ar[i] + ar[j]) );
      }
      }
      }
      ----------------------------------------------------------------------------------------------------------------
      int[] ar = {1, -2, 0, 5, -1, -4};
      int target = 2;
      for (int i =0; i < ar.length; i++) {
      for ( int j = i+1; j < ar.length; j++) {
      for ( int k = j+1; k < ar.length; k++) {
      if(( (ar[i]) + (ar[j]) + (ar[k]) ) == 2) {
      System.out.println(ar[i] + " + " + ar[j] + " + " + ar[k] + " = " + ( (ar[i]) + (ar[j]) + (ar[k]) ));
      }
      }
      }
      }

  • @suprabhatkumar151
    @suprabhatkumar151 4 ปีที่แล้ว +4

    Your way of explaining questions are damn good. I have watched many videos of urs and every time u earn my respect,Sir. Thank you so much.

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว

      Thanks for your nice feedback. Keep Watching.

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

    Bro your channel is best on youtube for such type of questions which are frequently asked in the interviews. Your content is just crisp and to the point. Same solution is explained by other youtuber in a 30 min video. you managed in 10 min saving our crutial time. I have followed all your array and tree series and tree series is just awesome. In my college days I wish the channel existed my DSA would have been at other level. Anyhow I am learning now. Keep up the good work.

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

    Bro can you make a video of a roadmap of becoming a developer in the various fields of computer science. eg: a roadmap of how to become an android developer or path of a java developer, what one must learn or do after learning the core concepts of Java. It would be very helpful if you did so because there are so many technologies out there and it really confuses me. Please make a video if you can. I have been following your channel for some time now. thank you.

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

    Thank you for the video!!

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

      Thanks for your nice feedback. Keep Watching.

  • @saurabhvishwakarma9616
    @saurabhvishwakarma9616 4 ปีที่แล้ว +6

    what if array is 2 2 2 2 2 2 2 2 2 2 and sum is 6 will it work??

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

      I'm late to answer this question but, the answer is that it won't! This algorithm only works if the list has all unique elements.

    • @deviprasanna2551
      @deviprasanna2551 ปีที่แล้ว

      Yeah it will

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

    sir pls make solution using hash also.....

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว

      yeah, we can make video using Hash also. Thanks for your suggestion. Will try to make on it.

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

    one of best approach ive ever seen good and i subscribed

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

      Thanks for your nice feedback. Keep Watching.

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

    this code is not working for finding sum of all triplet present in the array.

  • @ripudamansingh5866
    @ripudamansingh5866 4 ปีที่แล้ว +1

    why
    start = i+1
    not
    start = 0, pls help in understanding, triplets can also be formed with the element before i index

    • @ripudamansingh5866
      @ripudamansingh5866 4 ปีที่แล้ว

      pls reply anyone?

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว +1

      For triplet, we need three numbers,
      - Value at index i is 1st value
      - Value at index start is 2nd value
      - Value at index end is 3rd value
      if index is i is 1st value, 2nd value at start can't start from 0, it'll be always i + 1.
      Hope it helps you. Thanks.

    • @amaljohns8236
      @amaljohns8236 ปีที่แล้ว

      @@CodingSimplifiedwhy

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

    cpp code
    vector findTriplets(vectorarr, int n, int K) {
    vector final_ans;
    set initial_ans;
    sort(arr.begin(),arr.end());

    for(int i=0;i

  • @nikhilpatil6328
    @nikhilpatil6328 ปีที่แล้ว

    I will not work for duplicate elements ,in your example if we one more element 4 then it will get wrong

  • @6365bharath
    @6365bharath 4 ปีที่แล้ว

    Can we use Radix sort to get O(n) instead of O(nlogn) ?

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว

      Whatever is best for Sorting, we can us that. But Radix sort is not so famous for sorting. Please follow this article or some other: stackoverflow.com/questions/3539265/why-quicksort-is-more-popular-than-radix-sort#:~:text=Quicksort%20saves%20space%2C%20and%20will,arises%20that%20it%20is%20slower.&text=Radix%20sort's%20efficiency%20%3D%20O(c.n,keys%20in%20input%20key%20set.&text=Radix%20sort%20%3D%2016%20*%206%20%3D%2096%20time%20units.

  • @suprabhatkumar151
    @suprabhatkumar151 4 ปีที่แล้ว +1

    Can't we get better complexity than O(N^2)??

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว

      For this question, this is the best solution I can think & which is of O(N^2).

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

      @@CodingSimplified exactly

  • @Shreya-kd8th
    @Shreya-kd8th 3 ปีที่แล้ว +1

    best explanation

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

      Thanks for your nice feedback. Keep watching.

  • @punitsharma584
    @punitsharma584 8 หลายเดือนก่อน

    if you are taking -2 as second case then -3 also will be in arraay whyyou skip those cases that"s why many of us getting error in duplicate element cases ..

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

    nice explanation

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

      Thanks for your nice feedback. Keep Watching.

  • @punitsharma584
    @punitsharma584 8 หลายเดือนก่อน

    #include
    using namespace std;
    bool Triplet(int arr[], int arr_size, int sum)
    {
    int l, r, m; // for left ,right and middle
    sort(arr, arr + arr_size);
    for (int i = 0; i < arr_size - 2; i++)
    {
    l = 0;
    r = arr_size - 1;
    m = (l + r) / 2;
    while (l < r)
    {
    if (arr[l] + arr[m] + arr[r] == sum)
    {
    cout

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

    👏

  • @jatinsharma3792
    @jatinsharma3792 11 หลายเดือนก่อน

    int[] ar = {1, -2, 0, 5, -1, -4};
    int target = 2;
    for (int i =0; i < ar.length; i++) {
    for ( int j = i+1; j < ar.length; j++) {
    for ( int k = j+1; k < ar.length; k++) {
    if(( (ar[i]) + (ar[j]) + (ar[k]) ) == 2) {
    System.out.println(ar[i] + " + " + ar[j] + " + " + ar[k] + " = " + ( (ar[i]) + (ar[j]) + (ar[k]) ));
    }
    }
    }
    }

  • @saranshdhyani3164
    @saranshdhyani3164 3 หลายเดือนก่อน

    Aby hindi m hi samjha le bhai kuch bi bolra hai