Question

In: Computer Science

write a program to find the maximum possible sum such that no two chosen numbers are...

write a program to find the maximum possible sum such that no two chosen numbers are adjacent either vertically, horizontally, or diagonally. code in java

Solutions

Expert Solution

Solution

Source code

Main.java

class Main
{
  
int maximumsum(int array[], int n)
{
int include = array[0];
int exclude = 0;
int exclude_new;
int i;
  
for (i = 1; i < n; i++)
{

exclude_new = (include > exclude) ? include : exclude;
  

include = exclude + array[i];
exclude = exclude_new;
}
  
  
return ((include > exclude) ? include : exclude);
}
  
  
public static void main(String[] args)
{
Main sum = new Main();
int array[] = new int[]{1,21,3};
System.out.println(sum.maximumsum(array, array.length));
}
}

Screenshot

Output

Explanation

array[] = {5, 5, 10, 40, 50, 35}

include = 5
exclude = 0

For i = 1 (current element is 5)
include = (exclude + array[i]) = 5
exclude = max(5, 0) = 5

For i = 2 (current element is 10)
include = (exclude + array[i]) = 15
exclude = max(5, 5) = 5

For i = 3 (current element is 40)
include = (exclude + array[i]) = 45
exclude = max(5, 15) = 15

For i = 4 (current element is 50)
include = (exclude + array[i]) = 65
exclude = max(45, 15) = 45

For i = 5 (current element is 35)
include = (exclude + array[i]) = 80
exclude = max(65, 45) = 65

And 35 is the last element. So, answer is max(include, exclude) = 80


Related Solutions

The sum of two numbers is 34. a)Find the largest possible product of these numbers.
  1-The sum of two numbers is 34.    a)Find the largest possible product of these numbers.    b)What would be the largest possible product if the sum if the two numbers were "k"? 2-Sixty meters of fencing are used to fence a rectangular garden.    a)Find the dimensions that will give that maximum area.    b)What would be the maximum area if "k" feet of fencing were used in terms of "k"? THANK YOU
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
The sum of two numbers is 35 and their difference is 13. Find the numbers?
The sum  of two numbers is 35 and their difference is 13. Find the numbers?
a. Two dice are rolled; find the probability that the sum of the two numbers is...
a. Two dice are rolled; find the probability that the sum of the two numbers is 7. b. If one card is drawn from a standard deck, find the probability of getting a spade card or a Queen. c. A couple has 3 children, find the probability that exactly one are girls.
Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the 2 numbers. The difference between the 2 numbers (num1-num2) and (num2-num1) The value that is 305 more than the 1st number. The value that is 305 less than the 2nd number
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
Find three positive numbers whose sum is 400 and whose product is a maximum. show all...
Find three positive numbers whose sum is 400 and whose product is a maximum. show all steps or don't solve please
Use calculus to find three positive numbers that sum to 150 with the greatest possible product.
Use calculus to find three positive numbers that sum to 150 with the greatest possible product.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT