Maximum Value But Limited Neighbors
You are given an array a[1..n] of positive numbers and an integer
k. You have to produce an array b[1..n], such that: (i) For each j,
b[j] is 0 or 1, (ii) Array b has adjacent 1s at most k times, and
(iii) sum_{j=1 to n} a[j]*b[j] is maximized. For example, given an
array [100, 300, 400, 50] and integer k = 1, the array b can be: [0
1 1 0], which maximizes the sum to be 700. Or, given an array [10,
100, 300, 400, 50, 4500, 200, 30, 90] and k = 2, the array b can be
[1, 0, 1, 1, 0, 1, 1, 0, 1] which maximizes the sum to 5500.
To be precise about the definition of adjacency: sequence [0, 1, 0,
1, 0, 1, 1, 1] has two adjacent 1s. Sequence [0, 1, 0, 0, 1, 1, 1,
1] has 3 adjacent 1s. Sequence [1, 0, 1, 1, 0, 1, 1, 1] also has 3
adjacent 1s.
MVBLN(i,m): Maximum value sum that can be achieved from the array A[1..i], using adjacent 1s at most m times.
Recurrence Relation: MVBLN(i,m) = max{MVBLN(i-1,m), MVBLN(i-1,m-1) + A[i], MVBLN(i-2,m) + A[i]}
Please use Java for programming.
The required JAVA code
import java.util.*;
import java.lang.*;
import java.io.*;
class Chegg{
public static void main (String[] args){
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int m = Integer.parseInt(sc.nextLine());
String line = sc.nextLine();
String[] in = line.split(" ");
int[] a = new int[n];
for(int i=0;i<n;i++)
a[i] = Integer.parseInt(in[i]);
int ans = MVBLN(a,n-1,m);
System.out.println(ans);
}
public static int MVBLN(int[] a,int i,int m){
if(i < 0 || m < 0)
return 0;
else{
return Math.max(Math.max(MVBLN(a,i-1,m), MVBLN(a,i-1,m-1) +
a[i]),MVBLN(a,i-2,m) + a[i]);
}
}
}
Thank you, please upvote.
Maximum Value But Limited Neighbors You are given an array a[1..n] of positive numbers and an...
Given an array A[1..n] of positive integers and given a number x, find if any two numbers in this array sum upto x. That is, are there i,j such that A[i]+A[j] = x? Give an O(nlogn) algorithm for this. Suppose now that A was already sorted, can you obtain O(n) algorithm?
Coded in Java. Also advised to use class java.util.HashMap
Problem 1 You are given an array A of integers and an integer k. Implement an algorithm that determines, in linear time, the smallest integer that appears at least k times in A. Problem 2 You are given an array A of integers and an integer k. Implement an algorithm that determines in linear time whether there are two distinct indices i and j in the array such that A[i] =...
We are given a color picture consisting of an m?n array AŒ1::m;1::n? of pixels, where each pixel specifies a triple of red, green, and blue (RGB) intensities. Sup- pose that we wish to compress this picture slightly. Specifically, we wish to remove one pixel from each of the m rows, so that the whole picture becomes one pixel narrower. To avoid disturbing visual effects, however, we require that the pixels removed in two adjacent rows be in the same or...
(5 +2 points) You are given an array A[1..n] of positive numbers where Ai] is the stock price on day i. You are allowed to buy the stock once and sell it at some point later. For each day you own the stock you pay S1 fee. Design a divide-and-conquer algorithm that will return a pair (i,j) such that buying the stock on day i and selling it on day j will maximize your gain The complexity of the algorithm...
need help in this algorithm question
Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...
An m×n
array A
of real numbers is a Monge array if for all i,j,k,
and l
such that 1≤i<k≤m
and 1≤j<l≤n
, we have
>A[i,j]+a[k,l]≤A[i,l]+A[k,j]>
In other words, whenever we pick two rows and two columns of a
Monge array and consider the four elements at the intersections of
the rows and columns, the sum of the upper-left and lower-right
elements is less than or equal to the sum of the lower-left and
upper-right elements. For example, the following...
6. Consider the following basic problem. You're given an array A consisting of n integers A[1], A[2], , Aln]. You'd like to output a two-dimensional n-by-n array B in which B[i, j] (for i <j) contains the sum of array entries Ali] through Aj]-that is, the sum A[i] Ai 1]+ .. +Alj]. (The value of array entry B[i. Λ is left unspecified whenever i >j, so it doesn't matter what is output for these values.) Here's a simple algorithm to...
Question 6: Let n 2 1 be an integer and let A[1...n] be an array that stores a permutation of the set { 1, 2, . .. , n). If the array A s sorted. then Ak] = k for k = 1.2. .., n and, thus. TL k-1 If the array A is not sorted and Ak-i, where iメk, then Ak-서 is equal to the "distance" that the valuei must move in order to make the array sorted. Thus,...
Make a sorted integer array a[i]=i, i=0,...,n-1. Let bs(a,n,x) be a binary search program that returns the index i of array a[0..n-1] where a[i]=x. Obviously, the result is bs(a,n,x)=x, and the binary search function can be tested using the loop for(j=0; j<K; j++) for(i=0; i<n; i++) if(bs(a,n,i) != i) cout << “\nERROR”; Select the largest n your software can support and then K so that this loop with an iterative version of bs runs 3 seconds or more. Then measure...
Given an unsorted array of distinct positive integers A [ 1......n ] in the range between 1 and 10000 and an integer i in the sane range. Here n can be arbitrary large You want to find out whether there are 2 elements of the array that add up to i. Give an algorithm that runs in time (O(n).