Skip to content

Instantly share code, notes, and snippets.

@aryan-dixit-26
Created January 16, 2021 20:48
Show Gist options
  • Select an option

  • Save aryan-dixit-26/d963df78ac71623810879533fd51595d to your computer and use it in GitHub Desktop.

Select an option

Save aryan-dixit-26/d963df78ac71623810879533fd51595d to your computer and use it in GitHub Desktop.
Answer - QUESTION1 : Pairs
import java.util.*;
class Pair {
int x;
int y;
@Override
public String toString() {
return "{" +
"x=" + x +
", y=" + y +
'}';
}
}
class Question1 {
// Please try not to change anything in this method.
public static void main(String[] args) {
int[] numbers = new int[15];
addRandomNumbers(numbers);
System.out.println(Arrays.toString(numbers));
System.out.println(findPairs(numbers, 10));
}
// Please try not to change anything in this method.
public static void addRandomNumbers(int[] array) {
for (int i = 0; i < array.length; i++) {
array[i] = new Random().nextInt() % 10;
}
}
// Try to complete this method.
public static List<String> findPairs(int[] numbers, int desiredSum) {
Arrays.sort(numbers);
int a = 0;
int b = numbers.length - 1;
HashSet<Pair> pairHashSet = new HashSet<>();
for(int i = 0; i < numbers.length;i++){
if((numbers[a] + numbers[b] == desiredSum) && (a != b))
{
Pair p = new Pair();
p.x = numbers[a];
p.y = numbers[b];
pairHashSet.add(p);
a = a + 1;
b = b - 1;
}
else if (numbers[a] + numbers[b] > desiredSum)
{
b = b - 1;
}
else
{
a = a + 1;
}
}
System.out.println(pairHashSet);
return null;
}
}
//NAME : Aryan Dixit
//SECTION : G
//CLASS ROLL NUMBER : 35
//UNIVERSITY ROLL NUMBER : 191500156
@dbc2201
Copy link

dbc2201 commented Jan 23, 2021

  • Use single-class imports instead of multiple-class imports!
  • Please use ctrl+alt+shift+L to properly format your code in IntelliJ.
  • Try to use IntelliJ code hints (look out for highlights in the code)! It will help simplify your code.
  • Try to use modern Java language syntax in the code such as the var keyword for "Local Variable Type Inferencing".
  • Try to write better names for variables.

Here is a formatted version of your code

import java.util.*;
import java.util.stream.Collectors;

// try to make this class generic!
class Pair {
    int firstNumber;
    int secondNumber;

    public Pair(int firstNumber, int secondNumber) {
        this.firstNumber = firstNumber;
        this.secondNumber = secondNumber;
    }

    @Override
    public String toString() {
        return "{" + firstNumber + ", " + secondNumber + "}";
    }
}

class Question1 {
    // Please try not to change anything in this method.
    public static void main(String[] args) {
        int[] numbers = new int[15];
        addRandomNumbers(numbers);
        System.out.println(Arrays.toString(numbers));
        System.out.println(findPairs(numbers, 10));
    }

    // Please try not to change anything in this method.
    public static void addRandomNumbers(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = new Random().nextInt() % 10;
        }
    }

    // Try to complete this method.
    public static List<String> findPairs(int[] numbers, int desiredSum) {
        Arrays.sort(numbers);
        int leftIndex = 0;
        int rightIndex = numbers.length - 1;
        HashSet<Pair> pairHashSet = new HashSet<>();
        for (int i = 0; i < numbers.length && leftIndex != rightIndex; i++) {
            if ((numbers[leftIndex] + numbers[rightIndex] == desiredSum)) {
                Pair pair = new Pair(numbers[leftIndex], numbers[rightIndex]);
                pairHashSet.add(pair);
                leftIndex = leftIndex + 1;
                rightIndex = rightIndex - 1;
            } else if (numbers[leftIndex] + numbers[rightIndex] > desiredSum) {
                rightIndex = rightIndex - 1;
            } else {
                leftIndex = leftIndex + 1;
            }

        }
        // the method should return the list
        System.out.println(pairHashSet);
        return null;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment