Find Pairs in Array with Given Sum | Programming Tutorials

Поделиться
HTML-код
  • Опубликовано: 7 фев 2025
  • In this tutorial, I have explained how to find pairs in array with given sum. Write a Java Program to find pairs with given sum in a sorted array.
    Find pairs with given sum in an array Java Code - webrewrite.com...
    Interview Questions on Binary Search - • Binary Search | Interv...
    LeetCode Solutions Java Code - • LeetCode Solutions | Java
    Linked List Interview Questions - • Linked List | Intervie...
    Binary Tree Interview Questions - • Binary Tree | Intervie...
    Stack Interview Questions - • Stack | Interview Ques...
    Coding Interview Books -
    Cracking the coding interview - amzn.to/3h70jXy
    Data structure and algorithms made easy - amzn.to/3jgMl7U
    Contact Me through this Form - webrewrite.com...
    Website - webrewrite.com/
    Join Membership - / @programmingtutorials1m

Комментарии • 78

  • @kingnani1557
    @kingnani1557 5 лет назад +9

    Super explanation brother... thanks

  • @hackernight5110
    @hackernight5110 4 года назад +9

    for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr.length; j++) {
    if (arr[i]+arr[j]==sum)
    System.out.println("Pair "+arr[i]+" , "+arr[j]);
    }
    }
    With O(n^2) time complexity

    • @charankumarreddy9495
      @charankumarreddy9495 4 года назад

      brooo thank u!! :')

    • @tusharprajapati2574
      @tusharprajapati2574 4 месяца назад +1

      pairs repeat hoga

    • @syedalluddin3977
      @syedalluddin3977 2 месяца назад

      This will give duplicate pairs like for given sum 4 pairs will be -> 1+3 and 3+1 , I got rejected in the interview because I wrote the code in this approach , interviewer just said thanks and left the meeting

  • @WeREmotional
    @WeREmotional Год назад +2

    I had an interview earlier this week they asked me solve it without using loops but only using streams api.can you pleasehelp me.

  • @garimanagarwal1571
    @garimanagarwal1571 Год назад +3

    this one is for sorted array
    please upload for unsorted array also

  • @ProgrammingTutorials1M
    @ProgrammingTutorials1M  6 лет назад +1

    Code link is present in the description box.

  • @xristosmestviridis3708
    @xristosmestviridis3708 5 лет назад +3

    great job man. Congratulations

  • @susongbrit
    @susongbrit 4 года назад +1

    Sir i subscribed ur channel and i will share it to my other frnds also ..awesome explantaion thank you sir

  • @chandanagoshika2454
    @chandanagoshika2454 3 месяца назад

    Super explanation..

  • @rohitkushwaha6125
    @rohitkushwaha6125 Год назад

    Nice explanation. Sir sometimes it happens they ask to return array of such pairs how it could be done.

  • @pritamagarwala9415
    @pritamagarwala9415 2 года назад

    time complexity will be O(log n)...right ?

  • @TheEngineerFarm
    @TheEngineerFarm Год назад

    //for unsorted Arrays
    // k mean sum and n length of arr
    HashMapmap=new HashMap();
    for(int i=0;i

  • @abduchadili5691
    @abduchadili5691 Год назад

    What if you have repeating pairs how would you code it ?

    • @sdetbyraviteja568
      @sdetbyraviteja568 11 месяцев назад

      For that again we have to create one hash set and by using if condition by putting distinct pairs we can person the sum operation.

  • @sarah-fu6hw
    @sarah-fu6hw 3 года назад

    thank you soo much your videos are soo helpful

  • @togashiyuuta-s1p
    @togashiyuuta-s1p Год назад

    What if I want to find 6 variables not just 2 for the sum?

  • @ndmotivations
    @ndmotivations 4 года назад

    thanks a lot for geat explanation Sir.

  • @rohitshirodkar1129
    @rohitshirodkar1129 3 года назад +2

    what if array has duplicate elements? then low++, high-- (on sum =9) wont work?

    • @merxxibeaucoup9093
      @merxxibeaucoup9093 3 года назад

      I solved it using a hashset

    • @simon-yi6jv
      @simon-yi6jv 2 года назад

      @@merxxibeaucoup9093 only if array in sorted it will work

  • @Hemavani-lx6zb
    @Hemavani-lx6zb 9 месяцев назад

    awesome explanation, need program for : Write a java program for getting the sum of 30 by adding min 3 numbers from count[]={1, 15, 10, 5}. pleas help

  • @abduchadili5691
    @abduchadili5691 Год назад

    you don't need if equal test, just use else which is the only case left

  • @lokeshborole8997
    @lokeshborole8997 Год назад

    for Unsorted ?

  • @pradyumna668
    @pradyumna668 4 года назад +1

    what if duplicate are present or array with same elements like {2,2,2,2} and the required sum is 4

  • @vipulraj_1193
    @vipulraj_1193 3 года назад

    what if there are multiple numbers like example array 122344 and we have sum equal to 5 then how to do in this way

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  3 года назад

      Are you talking about duplicates in an array and in that case we have to find the pair?

  • @sankarpatra2788
    @sankarpatra2788 2 года назад

    Sir please do some questions related to list stack and que

  • @pratuu_s
    @pratuu_s 3 года назад +1

    This program asked on technical round.

  • @ashwinishete511
    @ashwinishete511 2 года назад +1

    Very nice ...plz make seris of interview prog...in this way

  • @siddhigolatkar8558
    @siddhigolatkar8558 Год назад

    Thank you

  • @amrish2181
    @amrish2181 5 лет назад

    does it only work with sorted array?

  • @adhvikworld1743
    @adhvikworld1743 4 года назад

    In case of unsorted array?

  • @arijitmohapatra147
    @arijitmohapatra147 3 года назад

    What if the numbers in random order?

    • @iamnew3353
      @iamnew3353 3 года назад +1

      I think you have to sort the array before finding the pairs.

    • @mujtabahussain7015
      @mujtabahussain7015 3 года назад

      @@iamnew3353 but in that case using hashset is more efficient i think.

  • @jackelynmonteclaro2679
    @jackelynmonteclaro2679 2 года назад

    How about unsorted array sir?

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  2 года назад

      For unsorted array either you can use HashMap to find the pairs or you can first sort the array and then use two pointers approach to solve.

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  2 года назад

      You can also check this tutorial for unsorted array - ruclips.net/video/MAjWstEJ8rM/видео.html

  • @ketanmorey1953
    @ketanmorey1953 5 лет назад

    Having array element like{123,51,33,678,67} want such elements in output having digits sum is 6 ... Find such elements and put it into output array....so code please sir...

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  5 лет назад

      Hi Ketan, here is the code

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  5 лет назад +4

      public static void main(String[] args) {
      int[] arr = {123,51,33,678,67};
      int digitSum = 6;

      for(int elem : arr) {
      int sum = getSum(elem);
      if(sum == digitSum) {
      System.out.println(elem);
      }
      }
      }
      public static int getSum(int num) {
      int sum = 0;
      while(num > 0) {
      int digit = num%10;
      sum = sum + digit;
      num = num/10;
      }
      return sum;
      }

    • @ketanmorey1953
      @ketanmorey1953 5 лет назад +2

      Thank u sir

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  5 лет назад +3

      @@ketanmorey1953 Please share this channel with your friends and colleagues.

  • @yadnyeshrane7448
    @yadnyeshrane7448 4 года назад

    Sir can i make use of 2 for loops..plz check whether the code is right
    int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
    int k = 9;
    for (int i = 0; i < arr.length; i++) {
    for (int j = i + 1; j < arr.length; j++) {
    if (arr[i] + arr[j] == k) {
    System.out.println("pair is (" + arr[i] +" " +arr[j] + ")");
    }
    }
    }

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  4 года назад +2

      But the time complexity in this case will be O(n^2). Your code is correct only small modification is needed
      int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
      int k = 9;
      for (int i = 0; i < arr.length-1; i++) {
      for (int j = i + 1; j < arr.length; j++) {
      if (arr[i] + arr[j] == k) {
      System.out.println("pair is (" + arr[i] +" " +arr[j] + ")");
      }
      }
      }

    • @merxxibeaucoup9093
      @merxxibeaucoup9093 3 года назад

      yes but what if there are duplicates numbers in the array??

  • @technosys9060
    @technosys9060 2 года назад +1

    work only with sorted array

  • @start1learn-n171
    @start1learn-n171 8 месяцев назад

    Tq

  • @AlokBehera-n3k
    @AlokBehera-n3k Год назад +1

    HI I need this probem fixed in java 8 .Could you please help me for this probem Given array : 1,3,5,7,9,11,16,20
    Input value : 16
    Output [2,5][3,4]
    Write the logic to identify the pairs of index in given array which value sum equal to input value.I have pasted here the problem statement I need solution by using java 8 concepts.

  • @prateekagrawal8166
    @prateekagrawal8166 4 года назад

    This won't work in test acse- a[]={ 1, 5, 7, -1, 5}, sum=6. its o/p should be 3 but this won't come with ur logic

  • @jayugjklb9466
    @jayugjklb9466 5 лет назад

    Sprb

  • @thorunitha7755
    @thorunitha7755 4 года назад +1

    import java.io.*;
    import java.util.*;
    class Pair
    {
    public static void main(String args[])
    {
    int arr[]={30,5,10,20,35,40,45,25,15};
    int sum=50;
    for(int i=0;i

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  4 года назад

      It's O(n^2). You can but it's not an efficient way.

    • @thorunitha7755
      @thorunitha7755 4 года назад

      @@ProgrammingTutorials1M Thank you sir! This tutorial is really helpful for me.

  • @sriracha9729
    @sriracha9729 4 года назад +2

    Didnt consider duplicates

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  4 года назад

      I have already mentioned in problem statement that array elements are distinct and in sorted order.

  • @prashantsingh456
    @prashantsingh456 2 года назад

    your voice is low

  • @Biharibabu135
    @Biharibabu135 5 лет назад

    but 1+2+5=8 so it should print 1 2 5