Add to function to
This is a simple example of a function with complexity O (n!) That has an int array in the parameter and an integer k. it returns true if there are two elements from the array x + y = k, for example: if tab was [1, 2, 3, 4] and k = 6, the return value will be true, because 2 + 4 = 6
public boolean addToUpK(int[] tab, int k) { boolean response = false; for(int i=0; i<tab.length; i++) { for(int j=i+1; j<tab.length; j++) { if(tab[i]+tab[j]==k) { return true; } } } return response; }
As a bonus, this is a unit test with jUnit, works great
@Test public void testAddToUpK() { DailyCodingProblem daProblem = new DailyCodingProblemImpl(); int tab[] = {10, 15, 3, 7}; int k = 17; boolean result = true;
Achraf Bellaali Jun 04 '19 at 14:50 2019-06-04 14:50
source share