## Find 2 elements in an array that give a target result in a simple equation

In electronics I need a certain resistance, however there are limited standard values on the market. If I make a combination of two resistors in parallel, I could get a very close result to the target. The question is which 2 resistors could give the best result.

So far i have these elements:

- Excel column of 86 standard values from 0.25 to 1000000 in A2:A87 cells
- Target resistance that I enter in cell C3
- R1 and R2 - variables that can get 86 values from A2 to A87 cells
- equation giving the parallel resistance: Rtarget=R1*R2/(R1+R2)

I am a VBA ignorant and find this task as a good opportunity to learn more about VBA operations. Here I started with the variables, but I do not know how to proceed. Your help would be very much appreciated.

Sub ResCom() Dim Rstd As Double Dim Rt As Double, R1 As Double, R2 As Double Rstd = Range("a2:a87").Value . . . End Sub

Thanks in advance!

We can create a tool that can easily be reused.

First enter the resistance values in **A2** through **A87**. Then run:

Sub tablemaker() Dim i As Long, j As Long, K As Long K = 2 For i = 2 To 87 For j = i To 87 Cells(K, 2) = Cells(i, 1) Cells(K, 3) = Cells(j, 1) K = K + 1 Next j Next i Range("D2:D" & K - 1).Formula = "=B2*C2/(C2+B2)" End Sub

This will create a resistance table in **B2** through **D3742**. The table lists combination pairs and the associated resistance of each pair.

*(permutations are not need since the pair 4,2 is really the same as 2,4)*

Then enter the target value in cell **E2**. In **E3** enter the ** array formula**:

=MATCH(MIN(ABS(D2:D3742-E2)),ABS(D2:D3742-E2),0)

**Array formulas** must be entered with Ctrl + Shift + Enter rather than just the Enter key. If this is done correctly, the formula will appear with curly braces around it in the Formula Bar.

This gives the row in the table with the closest match.Finally in **E4** enter:

=INDEX(B2:B3741,$E3)

and copy this to **F4**

To re-run this, just change the target in **E2**

**Given an array A[] and a number x, check for pair in A[] with sum as x ,** Let an array be {1, 4, 45, 6, 10, -8} and sum to find be 16 Initialize two index variables to find the candidate Function to check if array has 2 elements end) if(s.find(target_sum - arr[i]) == s.end) insert(arr[i] into s) else print arr[i], target-arr[i] . Given array of n integers and given a number X, find all the unique pairs of elements (a,b), whose summation is equal to X. The following is my solution, it is O(nLog(n)+n), but I am not sure whether or not it is optimal.

Just to get you started, use this to continue

Option Explicit Sub ResCom() Dim Rstd As Double Dim Rt As Double, R1 As Double, R2 As Double Rstd = Range("a2:a87").Value ' step thru upper triangle of R1/R2 values ' (lower triangle gives same results) Dim r1Row As Long, r2Row As Long For r1Row = LBound(Rstd) To UBound(Rstd) For r2Row = r1Row To UBound(Rstd) ' here is where you solve for Rtarget=R1*R2/(R1+R2) ' R1 = Rstd(r1Row) Next r2Row Next r1Row End Sub

**Dynamic Programming - Subset Sum Problem,** Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 30 Output: False There is no subset that add up to 30. Consider the last element and now the required sum = target sum – value of 'last' element and number of elements Following is the recursive formula for isSubsetSum() problem. printf ( "Found a subset with given sum" );. else. Count of Array elements to be divided by 2 to make at least K elements equal; Value to be subtracted from array elements to make sum of all elements equals K; Construct an Array of size N in which sum of odd elements is equal to sum of even elements; Merge first two minimum elements of the array until all the elements are greater than K

Try this

Sub ResCom() Dim Rt as Double, R1 as Double, R2 as Double Rt = Range("C3").Value For i = 2 to 87 R1 = Range("A" & i).Value if R1 < Rt Then For j = 2 to 87 R2 = Range("A" & j).Value if Round(Rt,2) = Round((R1*R2/(R1+R2)),2) then ' Success you have found a Resistance Combination that matches End If Next j End If Next i End Sub

...

**Array methods,** Here we can see that splice returns the array of removed elements: let arr = [ "I" , "study" , "JavaScript" ] ; // from index 2 // delete 0 // then insert "complex" The method arr.slice is much simpler than similar-looking arr.splice . let result = arr . find ( function ( item , index , array ) { // if true is returned, item is� The result is a list of all even elements in A that are less than 9. The use of the logical NOT operator, ~, converts the matrix mod(A,2) into a logical matrix, with a value of logical 1 (true) located where an element is evenly divisible by 2. Finally, find the elements in A that are less than 9 and even numbered and not equal to 2.

**Find ways to calculate a target from elements of specified array ,** Coding made easy. Twitter � Facebook Find ways to calculate a target from elements of specified array. Write a 2. Consider current element. // 2.1. Subtract current element from the target. // 2.2. base case: if target is found, print result list. Access Array Elements. You can access elements of an array by indices. Suppose you declared an array mark as above. The first element is mark[0], the second element is mark[1] and so on. Few keynotes: Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.

**How to Find all Pairs in Array of Integers Whose sum ,** For example, if the input integer array is {2, 6, 3, 9, 11} and given sum is Sounds simple? maybe, but this exact question has appeared in a Set; /** * Java Program to find two elements in an array that sum to k. new HashSet ( numbers.length); for(int value : numbers){ int target = n Entry entry : result. If we deal with an array that has odd number of elements, it will give us one value that belongs to the array. If we deal with an array that has even number of elements, we should take two from the middle of a sorted one and find the average of those two values, this way we don’t need to get the value that is in the array.

**Find Array Elements That Meet a Condition,** This example shows how to filter the elements of an array by applying For instance, you can examine the even elements in a matrix, find the location of The result, 8, is even, less than 9, and not equal to 2. Use logical indexing with a simple assignment statement to replace the values in an array that meet a condition. Time Complexity: O(sum*n), where sum is the ‘target sum’ and ‘n’ is the size of array. Auxiliary Space: O(sum*n), as the size of 2-D array is sum*n. Subset Sum Problem in O(sum) space Perfect Sum Problem (Print all subsets with given sum)