Find Pair of Elements in Unsorted Array Whose Sum is Equal to Required Number

package org.wesome.dsalgo;

import java.util.Arrays;
import java.util.List;

public class PairSum {
    static boolean pairSum(Integer arr[], int sum) {
        List<Integer> list = Arrays.asList(arr);
        int temp;
        for (Integer integer : arr) {
            temp = sum - integer;
            if (list.contains(temp)) {
                System.out.println("sum required is = " + sum + " can be obtained by addition of " + integer + " and " + temp);
                return true;
            }
        }
        return false;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class PairSumTest {
    @Test
    void PairSumTest1() {
        Integer arr[] = {-8, 1, 4, 6, 10, 45};
        int sum = 16;
        Assertions.assertTrue(PairSum.pairSum(arr, sum));
    }

    @Test
    void PairSumTest2() {
        Integer arr[] = {-5, -1, 2, 3, 4};
        int sum = -6;
        Assertions.assertTrue(PairSum.pairSum(arr, sum));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on