# L22 Arrays – Problem-solving – 2

## L22 Arrays Problem solving – 2

Q1. Write a class ElementCheckInEveryPair with a public method checkElement that takes two parameters one is arr of type int[] and second one is arg of type int and returns true if every pair of arr contains at least one arg.

Assumptions:

1. arr is never null

These are examples for your understanding:

Enter no of elements in the array:
6
Enter elements in the array seperated by space:
52 2 3 2 65 2
Enter the search element:
2
true

Enter no of elements in the array:
6
Enter elements in the array seperated by space:
4 5 4 1 1 4
Enter the search element:
4
false

Q2. Write a class CompareArrays with a public method compare that takes two parameters arr1 and arr2 of type int[] find the difference between the corresponding arr1 and arr2 elements and returns the count of elements whose difference is less than 2 and greater than -2. The return type of compare should be int.

Assumptions:

1. arr1 and arr2 never will null
2. arr1 and arr2 have same length

Here is an example:

Enter no of elements in the arr1:
3
Enter elements in the arr1 seperated by space:
1 2 3
Enter no of elements in the arr2:
3
Enter elements in the arr2 seperated by space:
2 3 4
3

Q3. Write a class CheckSurroundedElement with a public method checkElement that takes one parameter arr of type int[] and print all the elements in the arr that are surrounded by left and right elements and not equal to the left and right elements.

Here is an example:

Enter·no·of·elements·in·the·arr1:
5
Enter·elements·in·the·arr1·seperated·by·space:
1 2 1 1 3
2

Q4. Write a class ReorderArray with a public method reorder that takes one parameter arr of type int[] and returns the arr such that all zeros should come in front of the arr.

Assumptions:

1. arr is never null

Here is an example:

Enter no of elements in the arr:
8
Enter elements in the arr seperated by space:
55 0 21 0 63 0 45 0
0
0
0
55
21
63
45

Q5. Write a class ReorderArray with a public method reorder that takes one parameter arr of type int[] and returns the arr such that all even numbers in the array come to the front of the arr.

Assumptions:

1. arr is never null

Here is an example:

Enter no of elements in the array:
6
Enter elements in the array seperated by space:
3 5 6 4 2 4
6
4
2
4
3
5

Q6. Write a class MultiplesInArray with a public method findMultiples that takes three parameters arr of type int[] and other two are m1 and m2 are of type int. Print all the elements in the array, but if any element in the array is a multiple of m1, print multiple of (actual value of m1 should be printed instead of ). If it is a multiple of m2, print multiple of . If it is a multiple of both m1 and m2, print multiple of m1 and m2.

For example:

Enter no of elements in the array:
6
Enter elements in the array separated by space:
1 2 34 5 6 7
Enter the first multiple element:
2
Enter the second multiple element:
3
1
2 is multiple of 2
34 is multiple of 2
5
6 is multiple of 2 and 3
7

Q7. Write a class FindCenteredAverage with a public method findCenteredAverage that takes one parameter arr of type int[] and returns the centered average of the elements in the arr

Hint: exclude the biggest and smallest numbers from the array and compute the average of the remaining numbers. If there is more than one smallest value excludes only one of those. Similarly for biggest also.

Here is an example:

Enter no of elements in the array:
9

Enter elements in the array separated by space:
1 5 1 1 9 9 1 9 2
4

Q8. Write a class FindSumIgnoringSection with a public method findSum that takes three parameters one is arr of type int[] and other two are ignore1 and ignore2 are of type int and returns the sum of all the elements in the array, if the numbers ignore1 and ignore2, both appear in the array, ignore all the elements between them, including these two numbers.

Assumptions:

1. arr is never null
2. arr will not contain duplicate elements

Here is an example:

Enter no of elements in the array:
5
Enter elements in the array separated by space:
1 3 6 9 5
Enter the first element:
3
Enter the second element:
9
Sum of remaining elements is:
6

Q9. Write a class EitherOfASequence with a public method checkSequences that takes one parameter arr of type int[] and returns true only if one of these two sequences is present in the array: 18, 28 and 33, 36, returns false if none of these sequences are present or both are present. The return type of checkSequences is boolean.

Assumptions:

1. arr is never null

These are examples for understanding:

Enter no of elements in the array:
5
Enter elements in the array seperated by space:
18 28 36 4 2
true

Enter no of elements in the array:
6
Enter elements in the array seperated by space:
18 28 5 6 33 36
false

Q10. Write a class SequenceOfEvens with a public method checkEvenSequence that takes one parameter arr of type int[] and returns true if three consecutive even numbers are present in the arr. The return type of checkEvenSequence is boolean.

Assumptions:

1. arr is never null

Here is an example:

Enter no of elements in the array:
4
Enter elements in the array seperated by space:
2 4 6 5
true

Q11. Write a class SymmetricalArrayCheck with a public method checkSymmetry that takes two parameters one is arr of type int[] and second one is n of type int and returns true if the first n numbers are same as the last n numbers in the arr.

Assumptions:

1. arr is never null

Here is an example:

Enter no of elements in the array:
8
Enter elements in the array seperated by space:
1 2 3 5 6 1 2 3
Enter the search number you want to search:
3
true

Q12. Write a class SequenceCheck with a public method checkSequence that takes one parameter arr of type int[] and returns true if any three consecutive elements in arr are in incremental order.

Assumptions:

1. arr is never null

Here is an example:

Enter no of elements in the array:
6
Enter elements in the array seperated by space:
1 2 3 7 4 6
true

Candidates having any kind of query so he/she can ask in our comment section. Our Panel will b reverting back as soon as possible. Thank You. For more details visit our website (https://speedjobalert.com/)