Skip to content

Instantly share code, notes, and snippets.

@Brijeshpant
Brijeshpant / nearestLargestInt.md
Created February 17, 2020 18:43
Find nearest largest integer
import java.util.Optional;

/**
 * Given an array of numbers and an index i, return the index of the nearest larger number of the number at index i, where distance is measured in array indices. Assume your array is unsorted and there can be duplicates.
 * E.g.
 * Given int[] array = [4, 1, 3, 5, 6] and index 0, you should return 3.
 * As index array[0] = 4, and the next highest value is at array[3] = 5.
 * If two distances to larger numbers are equal, then return any one of them.
 * If the array at i doesn't have a nearest larger integer, then return null.
@Brijeshpant
Brijeshpant / find_pair_in_an_array.md
Last active February 17, 2020 18:40
Find Pairs in an array

//Write a program that, given an array A[] of n numbers and another number x, determines whether or not there exist two elements in S whose sum is exactly x.

public class PairsInAnArray {
    boolean findPair(List<Integer> list, int x) {
        int count =0;
        
        //{1, 4, 45, 6, 10, -8} and sum to find be 16
        
        HashSet visited = new HashSet();
 for (int i = 0; i &lt; list.size(); i++) {