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
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers. Write a program in java which has an array...
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared 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,...
Write a program in C++ to read 10 numbers from keyboard and find their sum, average,...
Write a program in C++ to read 10 numbers from keyboard and find their sum, average, maximum and minimum
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.
use java Write a program that, given two binary numbers represented as strings, prints their sum...
use java Write a program that, given two binary numbers represented as strings, prints their sum in binary. The binary strings are comma separated, two per line. The final answer should not have any leading zeroes. In case the answer is zero, just print one zero i.e. 0 Input: Your program should read lines from standard input. Each line contains two binary strings, separated by a comma and no spaces. Output: For each pair of binary numbers print to standard...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT