Its when I try to do stuff like this I realise I really need to go to university!
Anyway I have an array of strings (275) I need to loop through them and create strings of all the possible pairs, in Java.
I've been learning about recursion but I cant find the answer for this.
In order to find all the possible pairs from the array, we need to traverse the array and select the first element of the pair. Then we need to pair this element with all the elements in the array from index 0 to N-1.
Follow the steps below to solve the given problem: Initialize the count variable with 0 which stores the result. Iterate arr and if the sum of ith and jth [i + 1…..n – 1] element is equal to sum i.e. arr[i] + arr[j] == sum, then increment the count variable. Return the count.
There can be a total of n(n - 1)/2 number of total pairs from the given array of numbers.
In case pairs ab
and ba
are different, do:
for i=0 to array.length
for j=0 to array.length
if i == j skip
else construct pair array[i], array[j]
and if not, do something like this:
for i=0 to array.length-1
for j=i+1 to array.length
construct pair array[i], array[j]
Note that I am assuming the array holds unique strings!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With