Skip to main content
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
added 17 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I came up with 2 different solutions in Java. 

First one is:

public static void main(String[] args) {


    int[] array = new int[]{10,3,4,6,1,9,10,0};
    int sum=0;

    for (int i=0;i<array.length;i++)
    {
      for (int j=i ; j<array.length;j++){


        sum = array[i] + array[j];
        if (sum==10){

          System.out.println(array[i] + " " + array[j]);

        }


      }

    }

I believe this one works O(N^2)in \$O(N^2)\$.

My second solution is:

  public static void main(String[] args) {
    int[] nums = new int[] {10, 3, 4, 6, 1, 9, 10, 0};
    Hashtable<Integer, Integer> reqNoList = new Hashtable<Integer, Integer>();
    int sum = 10;
    int i = 0, count = 0;

    while (i < nums.length && i < sum) {
      int key = sum - nums[i];
      if (reqNoList.containsKey(nums[i])) {
        Integer temp = reqNoList.get(nums[i]);
        System.out.println(key + " " + nums[i]);
        count++;
      } else {
        reqNoList.put(key, i);
      }
      i++;
    }
  }
}

I tried to do it O(N)in \$O(N)\$ but I have no idea.

I came up with 2 different solutions in Java. First one is:

public static void main(String[] args) {


    int[] array = new int[]{10,3,4,6,1,9,10,0};
    int sum=0;

    for (int i=0;i<array.length;i++)
    {
      for (int j=i ; j<array.length;j++){


        sum = array[i] + array[j];
        if (sum==10){

          System.out.println(array[i] + " " + array[j]);

        }


      }

    }

I believe this one works O(N^2).

My second solution is:

  public static void main(String[] args) {
    int[] nums = new int[] {10, 3, 4, 6, 1, 9, 10, 0};
    Hashtable<Integer, Integer> reqNoList = new Hashtable<Integer, Integer>();
    int sum = 10;
    int i = 0, count = 0;

    while (i < nums.length && i < sum) {
      int key = sum - nums[i];
      if (reqNoList.containsKey(nums[i])) {
        Integer temp = reqNoList.get(nums[i]);
        System.out.println(key + " " + nums[i]);
        count++;
      } else {
        reqNoList.put(key, i);
      }
      i++;
    }
  }
}

I tried to do it O(N) but I have no idea.

I came up with 2 different solutions in Java. 

First one is:

public static void main(String[] args) {


    int[] array = new int[]{10,3,4,6,1,9,10,0};
    int sum=0;

    for (int i=0;i<array.length;i++)
    {
      for (int j=i ; j<array.length;j++){


        sum = array[i] + array[j];
        if (sum==10){

          System.out.println(array[i] + " " + array[j]);

        }


      }

    }

I believe this one works in \$O(N^2)\$.

My second solution is:

  public static void main(String[] args) {
    int[] nums = new int[] {10, 3, 4, 6, 1, 9, 10, 0};
    Hashtable<Integer, Integer> reqNoList = new Hashtable<Integer, Integer>();
    int sum = 10;
    int i = 0, count = 0;

    while (i < nums.length && i < sum) {
      int key = sum - nums[i];
      if (reqNoList.containsKey(nums[i])) {
        Integer temp = reqNoList.get(nums[i]);
        System.out.println(key + " " + nums[i]);
        count++;
      } else {
        reqNoList.put(key, i);
      }
      i++;
    }
  }
}

I tried to do it in \$O(N)\$ but I have no idea.

Source Link
user127004
user127004

Find pairs in an integer array whose sum is equal to 10

I came up with 2 different solutions in Java. First one is:

public static void main(String[] args) {


    int[] array = new int[]{10,3,4,6,1,9,10,0};
    int sum=0;

    for (int i=0;i<array.length;i++)
    {
      for (int j=i ; j<array.length;j++){


        sum = array[i] + array[j];
        if (sum==10){

          System.out.println(array[i] + " " + array[j]);

        }


      }

    }

I believe this one works O(N^2).

My second solution is:

  public static void main(String[] args) {
    int[] nums = new int[] {10, 3, 4, 6, 1, 9, 10, 0};
    Hashtable<Integer, Integer> reqNoList = new Hashtable<Integer, Integer>();
    int sum = 10;
    int i = 0, count = 0;

    while (i < nums.length && i < sum) {
      int key = sum - nums[i];
      if (reqNoList.containsKey(nums[i])) {
        Integer temp = reqNoList.get(nums[i]);
        System.out.println(key + " " + nums[i]);
        count++;
      } else {
        reqNoList.put(key, i);
      }
      i++;
    }
  }
}

I tried to do it O(N) but I have no idea.