Question: Problem 1 Difficulty: HARD Problem Statement: Create a class named Main with a main ( ) method to take positive integers as user input and

Problem 1
Difficulty: HARD
Problem Statement:
Create a class named Main with a main() method to take positive integers as user
input and store in an integer array named nums.
Subsequently, create a class named Solution with a method named divide(), which
accepts nums as its parameter and returns an int value.
The functionality of the divide() method is as follows: Each element within nums must
be allocated into either of two arrays, First or Second, ensuring that both First and
Second contain at least one element, and that the averages of elements within First
and Second are equal.
The method should return the average as integer (floor value in case of floating point)
if achieving such an allocation is possible, and -1 otherwise.
Display the returned value from the divide() method in the main() method.
Please note that for an array, the average is determined by the summation of all its
elements divided by the length of the array.
Constraints:
1<= nums.length <=30
0<= nums[i]<=10^3
Code Structure
Use this syntax for writing your solution. You may define any other method(s) in
Solution class to make your code read
import java.util.Scanner;
class Main {
public static void main(String[] args){
// Take input for integer array nums
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); //first integer is the size of the
array
int[] nums = new int[n];
for (int i =0; i < n; i++){
nums[i]= scanner.nextInt();
}
scanner.close();
Solution solution = new Solution();
int result = solution.divide(nums);
System.out.println(result);
}
}
class Solution {
public int divide(int[] nums){
// Method implementation as described:
return -1; // Place
holder return value
}
}
NOTE
Only use array data structure as taught in labs and class for this problem.
You should only make use of Scanner package wherever necessary, no other
packages are allowed.
Input format
First line takes the length of the array. Second line contains the elements of the array
separated by space.
8
12345678
Output format
Single interger
4
Please solve this dont use plagarism. should with output also. please give full code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!