package december;
import java.util.ArrayList;
import java.util.Arrays;
public class EmployeeTest {
//this function will return an integer array, which
will be a set of values
public static int[] subsetA(int arr[]) {
//sorting the original array
Arrays.sort(arr);
//calculating sum of all elements
in the array
int sum = 0;
for(int a: arr)
sum += a;
int len = arr.length;
//creating a array list that will
store all the value of the set
ArrayList<Integer> subsetA =
new ArrayList<Integer>();
//first step to add the last
element of the array, which will be the largest of the array
//since we need to keep the size
minimum and sum of the subset maximum, so we will use a sorted
values
//and add values in the subset in
decreasing order,
//first we will check for the
largerst then second largest and so on.
subsetA.add(arr[len-1]);
//this variable will store the sum
of all the values in the subset
int sumSubsetA =
subsetA.get(0);
//here, we will store the sum of
all the values in array, considering we remove the values from the
original array
//and add them in the subset.
int sumLeftInArray = sum -
subsetA.get(0);
//starting tghe loop from the
second largest element of the array
int i = len-2;
//j will store the count of values
in the subset
int j = 1;
while(sumSubsetA <
sumLeftInArray) {
//checking if
the current element is same as last element entered in the
subset
//since in set
all the elements are unique.
if( arr[i] ==
subsetA.get(j-1)) {
i--;
continue;
}
subsetA.add(arr[i]);
sumLeftInArray -= arr[i];
sumSubsetA += arr[i];
i--;
j++;
}
int arrToRet[] =
subsetA.stream().mapToInt(Integer:: intValue).toArray();
return arrToRet;
}
public static void main(String[] args) {
int arr[] = {3, 7, 5, 6, 2,
7};
int []subset = subsetA(arr);
System.out.println("Original Array:
" + Arrays.toString(arr));
System.out.println("Subset A: " +
Arrays.toString(subset));
}
}
Java 8 9m left Jav 27 28 ALL 29 0 Given an integer array, separate the...
Given an array of integer, please complete a function multiply(). The function accepts the first memory address of an array and the size of an array as parameters and returns the multiplication of all the elements. In order to get a full score, the function must use a loop to iterate through each element of an array. Pseudo code example: multiply([ 1, 2, 3], 3) → 6 multiply([1, 1, 4 ,2], 4) → 8 multiply([7, 0, 0, 7, 0, 7],...
IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...
Java 8 Braces You are designing a compiler for a C++ program and need to check that braces in any given file are balanced Braces in a string are considered to be balanced if the following criteria are met: All braces must be closed. Braces come in pairs of the form 0.0andl1. The left brace opens the pair, and the right one closes it In any set of nested braces, the braces between any pair must be closed For example,...
#include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...
java estion7 For this question, assume all input comes from the keyboard, and al output goes to the screen. Include method prototypes and comments. The array should have room for 100 integers Write a complete Java program, including at least one comment in the main propram and one in e to do the following: Write a main program which will call the methods described below (a) First the main program will read an integer (this integer is a parameter or...
Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...
Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...
ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...
In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....
static public int[] Question() // retrieve an integer 'n' from the console // the first input WILL be a valid integer, for 'n' only. // then read in a further 'n' integers into an array. // in the case of bad values, ignore them, and continue reading // values until enough integers have been read. // this question should never print "Bad Input" like in lab // hint: make subfunctions to reduce your code complexity return null; static public int...