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
I need to write a c/c++ code to find the maximum sum in those possible combinations...
I need to write a c/c++ code to find the maximum sum in those possible combinations of arrays. There are N arrays with S elements. ex2: 4 arrays with 3 elements.We want to find the greater number in the same column of two arrays(may possible be three or four...arrays), and sum those greater number to find the greatest sum in all of the combinations in those 4 arrays, like: A: [50, 60, 70] B: [80, 40, 20] C: [30, 100,...
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?
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers....
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers. Start your code by copying/pasting this information into an editor like notepad, notepad++, or IDLE: public class Main { public static void main(String[] args) { // Write your code here } } Sample input: Input first number: 125 Input second number: 24 Sample Output: 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5...
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,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT