14,603,271 members
Rate this:
See more:
Write a program to find the siblings of the given integer array. if no siblings found then return -1. input1 refers to the length of the input. input2 is the values and input3 is the value for which we need to find the siblings.

Example1:

input1: 5

input2: 1,2,3,4,5

input3: 1

output: {-1}

Explanation: As 1 is the root element which wont have any siblings so return value is -1.

Example2:

input1: 6

input2: 1,2,3,4,5,6

input3: 5

output: {4,6}

Explanation: {2,3} are the children of 1 and {4,5,6} are the children of {2,3} so the output is {4,6}

What I have tried:

```public class Exam {

public static void main(String[] args) throws IOException {
int input1 = 6;
int[] input2 = new int[] {1, 2, 3, 4, 5, 6};
int input3 = 5;
System.out.println(findSiblings(input1, input2, input3));
}

public static int[] findSiblings(int input1, int[] input2, int input3) {
int[] result = new int[] {-1};
if (input2[0] == input3) {
return result;
}

return result;

}
}```
Posted
Updated 31-Jul-20 7:45am

Rate this:

## Solution 1

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you have no idea how, then start by doing it manually on paper, and work out how you do that yourself.
Then write down those instructions in you native language, and try following them blindly. If they don't work, refine them and try again. When they work, it should be fairly easy to translate them into actual code and start testing.
Rate this:

## Solution 2

Just do it like this:
You have to do a little error handling for non existent elements there:
```public static void main(String[] args) throws IOException {

int[] inputArray = new int[] {1, 2, 3, 4, 5, 6,7};
int siblingsToFindOf = 6;
int[] results = findSiblings(inputArray.length, inputArray, siblingsToFindOf);//lenth as array len

for(int i=0;i<results.length;i++){ //be aware of output arrays item by item. println cant print arrays as well
System.out.print(String.valueOf(results[i]) + ",");
}

}

public static int[] findSiblings(int input1, int[] input2, int input3) {
int[] result = new int[] {-1};
if (input2[0] == input3) {
return result;
}

int siblingFindIndex = -1;
for(int i=0;i<input2.length;i++){
if(input2[i]==input3) { //If element at i position is search element
siblingFindIndex = i;
break; // end loop
}
}
if(siblingFindIndex>-1){ //index was found in integer array
result = new int[]{input2[siblingFindIndex-1],input2[siblingFindIndex+1]}; // add found index+1 as upper sibling and found Index -1 as lower sibling
//to do -> check if both elements exists so there can be no array out of bounds
}

return result;

}```
Comments
Richard MacCutchan 1-Aug-20 3:55am

You do not help people by writing their homework assignments for them.
Pascal Petzoldt 1-Aug-20 5:50am

It does not work well when they dont understand the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard MacCutchan 150 OriginalGriff 150 Member 11819205 75 F-ES Sitecore 65 Rick York 50
 OriginalGriff 1,212 Sandeep Mewara 706 Richard MacCutchan 682 Garth J Lancaster 560 Richard Deeming 430

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100