How to make intersection between method - java

The output of my code is:
The fibonacci list that smaller than 40 is:
0 1 1 2 3 5 8 13 21 34
The prime list that smaller than 40 is:
2 3 5 7 11 13 17 19 23 29 31 37
I want to make an intersection between these two list.
To make it becomes: (when I put variable n=40 in fibo() and allPrime() method)
2 3 5 13
But I don't know how to do this. I've searched the forum, and most of the intersection question is between two arraylist or two sets.
I've wonder if it's possible to make intersection between two function like this?
public class FiboAndPrime {
static boolean IsPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
// Find all the prime numbers that are less than or equal to n
static void allPrime(int n) {
for(int i=2; i<=n; i++) {
if(IsPrime(i)) System.out.print(i+ " ");
}
}
//Find the Fibonacci numbers that are less than or equal to n
static void fibo(int n) {
int fibo[] = new int[n];
fibo[0] = 0;
fibo[1] = 1;
System.out.print(fibo[0]+" "+fibo[1]+" ");
for (int i = 2; i <= n; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
if (n >= fibo[i]) {
System.out.print(fibo[i]+ " ");
} else {
break;
}
}
}
public static void main(String[] args) {
int k = 40;
System.out.println("The fibonacci list that smaller than " + k + " is:");
fibo(k);
System.out.println();
System.out.println("The prime list that smaller than " + k + " is:");
allPrime(k);
}
}
I tried to change my code to use ArrayList, but I was stuck at the fibo() method.
The output is:
The final intersection that are both fabonacci and prime is:
0 1true true true true true true true true
The prime list that smaller than 40 is:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
Why does it become a boolean type in my fibo list?
static void allPrime(int n) {
List<Integer> primes = new ArrayList<Integer>(n);
for(int i=2; i<=n; i++) {
if(IsPrime(i)) {
primes.add(i);
}
}
System.out.print(primes);
}
static void fibo(int n) {
List <Integer> fibos = new ArrayList<>(n);
int fibo[] = new int[n];
fibo[0] = 0;
fibo[1] = 1;
System.out.print(fibo[0]+" " + fibo[1]);
for (int i = 2; i <= n; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
if (n >= fibo[i]) {
int in =fibo[i];
System.out.print(fibos.add(in)+ " ");
} else {
break;
}
}
}

You will need to use a data structure like a HashSet or ArrayList to do this, then find the intersection between them.
Solution using an ArrayList:
import java.util.List;
import java.util.ArrayList;
public class FiboAndPrime {
static boolean IsPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
// Find all the prime numbers that are less than or equal to n
static void allPrime(int n, List<Integer> prime_set ) {
for(int i=2; i<=n; i++) {
if(IsPrime(i)) System.out.print(i+ " ");
prime_set.add(i);
}
}
//Find the Fibonacci numbers that are less than or equal to n
static void fibo(int n, List<Integer> fibo_set ) {
int fibo[] = new int[n];
fibo[0] = 0;
fibo[1] = 1;
System.out.print(fibo[0]+" "+fibo[1]+" ");
for (int i = 2; i <= n; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
if (n >= fibo[i]) {
System.out.print(fibo[i]+ " ");
fibo_set.add(fibo[i]);
} else {
break;
}
}
}
public static void main(String[] args) {
int k = 40;
System.out.println("The fibonacci list that smaller than " + k + " is:");
List<Integer> fibo_set = new ArrayList<Integer>();
fibo_set.add(0);
fibo_set.add(1);
List<Integer> prime_set = new ArrayList<Integer>();
fibo(k,fibo_set);
System.out.println();
System.out.println("The prime list that smaller than " + k + " is:");
allPrime(k,prime_set);
fibo_set.retainAll(prime_set); // fibo_set now contains only elements in both sets
System.out.println();
System.out.println("intersection between the fibo and prime set:");
for (Integer intersection : fibo_set) {
System.out.println(intersection);
}
}
}
Solution using a HashSet:
import java.util.Set;
import java.util.HashSet;
public class FiboAndPrime {
static boolean IsPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
// Find all the prime numbers that are less than or equal to n
static void allPrime(int n, Set<Integer> prime_set ) {
for(int i=2; i<=n; i++) {
if(IsPrime(i)) System.out.print(i+ " ");
prime_set.add(i);
}
}
//Find the Fibonacci numbers that are less than or equal to n
static void fibo(int n, Set<Integer> fibo_set ) {
int fibo[] = new int[n];
fibo[0] = 0;
fibo[1] = 1;
System.out.print(fibo[0]+" "+fibo[1]+" ");
for (int i = 2; i <= n; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
if (n >= fibo[i]) {
System.out.print(fibo[i]+ " ");
fibo_set.add(fibo[i]);
} else {
break;
}
}
}
public static void main(String[] args) {
int k = 40;
System.out.println("The fibonacci list that smaller than " + k + " is:");
Set<Integer> fibo_set = new HashSet<Integer>();
fibo_set.add(0);
fibo_set.add(1);
Set<Integer> prime_set = new HashSet<Integer>();
fibo(k,fibo_set);
System.out.println();
System.out.println("The prime list that smaller than " + k + " is:");
allPrime(k,prime_set);
fibo_set.retainAll(prime_set); // fibo_set now contains only elements in both sets
System.out.println();
System.out.println("intersection between the fibo and prime set:");
for (Integer intersection : fibo_set) {
System.out.println(intersection);
}
}
}

It's definitely possible. I am not going to write code for you but i can suggest how you should do it. Rather than printing those fibonacci and primes under 40, you should store them in 2 different arrays and try to find what are common between them.But i think you are not familiar with arraylists and sets, i will suggest you another method.
When you find the fibonacci number under 40, check whether that number is prime or not. If it is, then print it or else you don't.

Why use array instead of list in java. You can simply achieve that by retainAll as follows:
List<Integer> ret = new ArrayList<>(primes);
ret.retainAll(fibos);
For local test:
public static void main(String... args) {
List<Integer> primes = new ArrayList<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37));
List<Integer> fibos = new ArrayList<>(Arrays.asList(0, 1, 1, 2, 3, 5, 8, 13, 21, 34));
List<Integer> ret = new ArrayList<>(primes);
ret.retainAll(fibos);
System.out.println(ret);
}
The output will be:
[2, 3, 5, 13]
UPDATED
As requested by OP, I add this for reference:
static void fibo(int n) {
List <Integer> fibos = new ArrayList<>(n);
fibos.add(0);
fibos.add(1);
System.out.print(fibos.get(0) + "," + fibos.get(1));
for (int i = 2; i <= n; i++) {
int a = fibos.get(i-1) + fibos.get(i-2);
if (a <= n) {
fibos.add(a);
System.out.print("," + a);
} else break;
}
}

Related

Finding largest gap between consecutive numbers in array Java

I'm currently working on a homework assignment and the final task of the assignment is to write a method to find the largest gap between consecutive numbers in an unsorted array. Example: if the array had the values {1,2,3,4,5,20} the gap would be 15. Currently the array is holding 20 values generated at random.
I'm totally lost for how I would make this happen. Initially my idea for how to solve this would be using a for loop which runs through each value of the array with another loop inside to check if the current value is equal to the previous value plus 1. If it is then store that number as the minimum in the range. Another problem I ran into was that I have no idea how to store a second number without overwriting both numbers in the range. Basically nothing i've tried is working and could really use some help or at least a nudge in the right direction.
What the method does right now is only store the value for "a" after it finds a number that isn't consecutive in the array.
Here's the code I have so far
import java.util.Arrays;
class Main {
public static void main(String[] args) {
Main m = new Main();
m.runCode();
}
public void runCode()
{
Calculator calc = new Calculator();
calc.makeList(20);
System.out.println("List:");
calc.showList();
System.out.println("Max is: " + calc.max());
System.out.println("Min is: " + calc.min());
System.out.println("Sum is: " + calc.sum());
System.out.println("Ave is: " + calc.average());
System.out.println("There are " + calc.fiftyLess() + " values in the list that are less than 50");
System.out.println("Even numbers: " + calc.Even());
}
}
class Calculator {
int list[] = new int[20];
public void makeList(int listSize)
{
for (int count = 0; count < list.length; count++) {
list[count] = (int) (Math.random() * 100);
}
}
public void showList()
{
for (int count = 0; count < list.length; count++)
{
System.out.print(list[count] + " ");
}
}
public int max()
{
int max = list[0];
for (int count=0; count<list.length; count++){
if (list[count] > max) {
max = list[count];
}
}
return max;
}
public int min()
{
int min = list[0];
for (int count=0; count<list.length; count++){
if (list[count] < min) {
min = list[count];
}
}
return min;
}
public int sum()
{
int sum = 0;
for (int count=0; count<list.length; count++){
sum = sum + list[count];
}
return sum;
}
public double average()
{
int sum = sum();
double average = sum / list.length;
return average;
}
public int fiftyLess()
{
int lessThan = 0;
for (int count =0; count<list.length;count++)
{
if (list[count] < 50)
{
lessThan++;
}
}
return lessThan;
}
public int Even()
{
int isEven = 0;
for (int count = 0; count<list.length;count++)
{
if (list[count] % 2 == 0)
{
isEven++;
}
}
return isEven;
}
public int Gap()
{
int a = 0;
int b = 0;
int gap = math.abs(a - b);
for (int count = 1; count<list.length;count++)
{
if (list[count] != list[count] + 1)
{
a =list[count];
}
}
}
}
By using the java8 stream library you could achieve this in fewer lines of code.
This code segment iterates the range of the array, and subtracts all consecutive numbers, and returns the max difference between them or -1, in case the array is empty.
import java.util.stream.IntStream;
class Main {
public static void main(String[] args) {
int[] list = {1, 2, 3, 4, 5, 20};
int max_difference =
IntStream.range(0, list.length - 1)
.map(i -> Math.abs(list[i + 1] - list[i]))
.max().orElse(-1);
System.out.println(max_difference);
}
}
Alternatively you could do this with a traditional for loop.
class Main {
public static void main(String[] args) {
int[] list = {1, 2, 3, 4, 5, 20};
int max_difference = -1;
int difference;
for (int i = 0; i < list.length - 1; i++) {
difference = Math.abs(list[i + 1] - list[i]);
if(difference > max_difference)
max_difference = difference;
}
System.out.println(max_difference);
}
}
Output for both code segments:
15

java assign even elements to even index and odd to odd places and if the numbers are not equal add zeros to the places

I am trying to write code to display the even elements to even indexes and odd to odd indexes and if the numbers added numbers are same then add zeros accordingly.
Example:
x = [1,2,3,4]
output: 2 1 4 3
x = [1 1 1 4]
output: 4 1 0 1 0 1
I reached to get even and odd positions but stuck after that.
Below is my code.
import java.util.*;
class ArrayDemo3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter Size of Array :: ");
int size = s.nextInt();
int[] x = new int[size];
System.out.println("Array Created having the size :: " + size);
System.out.println("Enter Elements for Array :: ");
for (int i = 0; i < size; i++) {
System.out.println("Enter element no-" + (i + 1) + " ::");
x[i] = s.nextInt();
}
System.out.println("Contents of Array ::");
for (int i = 0; i < size; i++) {
System.out.print(x[i] + " ");
}
for (int i = 0; i < size; i = i + 1) {
int even = 0;
int odd = 1;
if (i < size && x[i] % 2 == 0) {
System.out.print("even : ");
even = even + i;
System.out.print("position" + i + " " + x[i] + " ");
} else {
System.out.print("odd : ");
odd = odd + i;
System.out.print(i + " " + x[i] + " ");
}
if (even < size && odd < size) {
int temp = x[even];
x[even] = x[odd];
x[odd] = temp;
} else {
}
//System.out.print(x[i] + " ");
}
}
}
You can break up your problem in 3 parts:
First create two lists, one containing in encountered order the even numbers and the other the odd numbers:
private static List<List<Integer>> createOddityLists(int... numbers) {
List<Integer> numsList = Arrays.stream(numbers).boxed().collect(Collectors.toList());
List<List<Integer>> numsByOddity = new ArrayList<List<Integer>>();
numsByOddity.add(new ArrayList<>()); // List of odd numbers
numsByOddity.add(new ArrayList<>()); // List of even numbers
numsList.forEach(num -> numsByOddity.get(num % 2).add(num));
return numsByOddity;
}
Pad the shorter of the two lists with zeros (0s) to make it equal length as the other one:
private static void padShorterList(List<List<Integer>> numsByOddity) {
int sizeDiff = numsByOddity.get(0).size() - numsByOddity.get(1).size();
int listIndexToBePadded = sizeDiff < 0 ? 0 : 1;
List<Integer> padding = Collections.nCopies(Math.abs(sizeDiff), 0);
numsByOddity.get(listIndexToBePadded).addAll(padding);
}
Finally join intertwining both lists:
private static List<Integer> joinLists(List<List<Integer>> numsByOddity) {
List<Integer> resultList = new ArrayList<>(numsByOddity.get(1));
for (int idx = 0; idx < numsByOddity.get(0).size(); idx++)
resultList.add(idx * 2, numsByOddity.get(0).get(idx));
return resultList;
}
The following is the full working example:
public class ArrayRearrangement {
public static void main(String[] args) {
// int[] result = rearrange(1, 2, 3, 4);
int[] result = rearrange(1, 1, 1, 4);
System.out.println(Arrays.stream(result).boxed().collect(Collectors.toList()));
}
private static int[] rearrange(int... numbers) {
List<List<Integer>> numsByOddity = createOddityLists(numbers);
padShorterList(numsByOddity);
return joinLists(numsByOddity).stream().mapToInt(i->i).toArray();
}
private static List<List<Integer>> createOddityLists(int... numbers) {
List<Integer> numsList = Arrays.stream(numbers).boxed().collect(Collectors.toList());
List<List<Integer>> numsByOddity = new ArrayList<List<Integer>>();
numsByOddity.add(new ArrayList<>()); // List of odd numbers
numsByOddity.add(new ArrayList<>()); // List of even numbers
numsList.forEach(num -> numsByOddity.get(num % 2).add(num));
return numsByOddity;
}
private static void padShorterList(List<List<Integer>> numsByOddity) {
int sizeDiff = numsByOddity.get(0).size() - numsByOddity.get(1).size();
int listIndexToBePadded = sizeDiff < 0 ? 0 : 1;
List<Integer> padding = Collections.nCopies(Math.abs(sizeDiff), 0);
numsByOddity.get(listIndexToBePadded).addAll(padding);
}
private static List<Integer> joinLists(List<List<Integer>> numsByOddity) {
List<Integer> resultList = new ArrayList<>(numsByOddity.get(1));
for (int idx = 0; idx < numsByOddity.get(0).size(); idx++)
resultList.add(idx * 2, numsByOddity.get(0).get(idx));
return resultList;
}
}
Complete code on GitHub
Hope this helps.
Using arrays something like this we can do. Code needs to be optimised.
public static int[] arrangeInEvenOddOrder(int[] arr)
{
// Create odd and even arrays
int[] oddArr = new int[arr.length];
int[] evenArr = new int[arr.length];
int oCount = 0, eCount = 0;
// populate arrays even and odd
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0)
evenArr[eCount++] = arr[i];
else
oddArr[oCount++] = arr[i];
}
int[] resArr = new int[oCount >= eCount?
2*oCount : 2*eCount-1];
// populate elements upto min of the
// two arrays
for (int i =0; i < (oCount <= eCount?
2*oCount : 2*eCount ); i++ )
{
if( i%2 == 0)
resArr[i] = evenArr[i/2];
else
resArr[i] = oddArr[i/2];
}
// populate rest of elements of max array
// and add zeroes
if (eCount > oCount)
{
for (int i=2*oCount,j=0;i<2*eCount-1; i++)
{
if (i%2 == 0)
{
resArr[i] = evenArr[oCount+j];
j++;
}
else
resArr[i] = 0;
}
}
else if (eCount < oCount)
{
for (int i=2*eCount,j=0;i<2*oCount; i++)
{
if ( i%2 != 0)
{
resArr[i] = oddArr[eCount+j];
j++;
}
else
resArr[i] = 0;
}
}
return resArr;
}
Sort element based on index i.e if the element is even, it must be at even position and vise-versa
int sortArrayByEvenOddIndex(int arr[]) {
int n = arr.length;
int res[] = new int[n];
int odd = 1;
int even = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
res[even] = arr[i];
even += 2;
} else {
res[odd] = arr[i];
odd += 2;
}
}
return res;
}

Maximum Single Sell Profit algorithm (Java)

I am working on creating an algorithm to maximize profit from a .txt file where each line is the price of a certain stock on a day (Starting with day 0).
The output of my program should be "[day you should buy the stock, day you should sell the stock, profit made]".
For example:
Text file:
12, 45, 3, 15, 60, 23, 4
The output should be [2, 4, 57].
My code returns the actual VALUES and not the index of those values.
My output: [3, 60, 57].
I am a beginner, and I cannot seem to find out what to do to produce the correct output! Help would be very much appreciated!
(Trade is a separate class that returns (in, out, profit)).
[EDIT]: I am supposed to do this recursively, and make sure the the overall time cost of the solution is O(n log n)!
Here is my code:
(Apologies if it is messy/things are in it that aren't needed! :) )
import java.util.*;
import java.lang.Math;
import java.io.*;
public class Test_BestTrading
{
public static void main(String[] args) throws Exception
{
//open file
String fileName = args[0];
File inFile = new File(fileName);
Scanner fin = new Scanner(inFile);
int count = 0;
//find out length of array
while(fin.hasNext())
{
fin.nextLine();
count++;
}
fin.close();
int[]p = new int[count];
fin = new Scanner(inFile);
//read numbers into array
for(int i =0; i < count; i++)
p[i] = Integer.parseInt(fin.nextLine());
Trade trade = BestTrade(p, 0, p.length-1);
System.out.println("[" + trade.in + ", " + trade.out + ", " + trade.profit + "]");
}
public static Trade BestTrade(int[] p, int in, int out)
{
if (p.length <= 1)
return new Trade(in, out, out-in);
//Create two arrays - one is left half of "p", one is right half of "p".
int[] left = Arrays.copyOfRange(p, 0, p.length/2);
int[] right = Arrays.copyOfRange(p, p.length/2, p.length);
// Find best values for buying and selling only in left array or only in right array
Trade best_left = BestTrade(left, 0, left.length-1);
Trade best_right = BestTrade(right, 0, right.length-1);
// Compute the best profit for buying in the left and selling in the right.
Trade best_both = new Trade(min(left), max(right), max(right) - min(left));
if (best_left.profit > best_right.profit && best_left.profit > best_both.profit)
return best_left;
else if (best_right.profit > best_left.profit && best_right.profit > best_both.profit)
return best_right;
else
return best_both;
}
public static int max(int[] A)
{
int max = 0;
for(int i=0; i < A.length; i++)
{
if(A[i] > max)
max = A[i];
}
return max;
}
public static int min(int[] A)
{
int min = 100000;
for(int i=0; i < A.length; i++)
{
if(A[i] < min)
min = A[i];
}
return min;
}
}
Once you have your array of numbers, you could simply run a for loop to detect the lowest value and the greatest value as well as the indices of each number.
int greatestDifference = 0;
int indexLowest = 0;
int indexHighest = 0;
for(int i = 0; i < values.length; i++)
for(int j = i + 1; j < values.length; j++)
if(values[i] - values[j] < greatestDifference){
greatestDifference = values[i] - values[j];
indexLowest = i;
indexHighest = j;
}
System.out.println("Buy value is " + values[indexLowest] + " on day " + (indexLowest + 1) + ".");
System.out.println("Sell value is " + values[indexHighest] + " on day " + (indexHighest + 1) + ".");
System.out.println("Net gain is " + Math.abs(greatestDifference));
Check it -
public class BuySellProfit {
public static void main(String[] args) {
int[] a = { 12, 45, 3, 15, 60, 23, 4 };
int min = a[0];
int max = a[0];
int minIndex=0;
int maxIndex=0;
for (int count = 0; count < a.length; count++) {
if (a[count] > max) {
max = a[count];
maxIndex=count;
}
}
System.out.println("Max = " + max);
for (int count = 0; count < a.length; count++) {
if (a[count] < min) {
min = a[count];
minIndex=count;
}
}
System.out.println("min=" + min);
profit(a, minIndex, maxIndex);
}
private static void profit(int a[], int i, int j) {
int profit = a[j] - a[i];
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(i);
list.add(j);
list.add(profit);
System.out.println(list);
}
}
Output :-
Max = 60
min=3
[2, 4, 57]
You just return the index number instead of Value,
It will work.. BTW your code is OK.
import java.util.Scanner;
public class Example4 {
public static void main(String[] args) {
//System.out.println("input the valuer:");
Scanner x =new Scanner(System.in);
for( int i=1;i<13;i++){
System.out.println("Profit for month" +i);
System.out.println("input the valuer :");
float valuer1 =x.nextFloat();
float result=0;
result+=valuer1;
System.out.println("Total profits for months:"+result);
}
}
}

remove and print prime numbers in an array

I have an assignment due for class and the objective is to take an Array and run it through a method that will print the array out in reverse order, then run it through a second method to test if the number is prime then print the still reversed order array out without the prime numbers. I can get the reverse the order part fine, I am running into trouble with the prime numbers part:
public class Driver {
public static void main(String[] args) {
// CTORs
int[] MyArray = { 10, 8, 7, 14, 22, 11 };
int[] myArray2 = new int[7];
// METHOD CALLING
MyArray = reverseOrder(6, MyArray);
MyArray = primeCheck(MyArray, 6);
for (int i = 0; i < myArray2.length; i++)
System.out.println(myArray2[i] + " ");
}// MAIN
/*--------------------ARRAY PRIME TEST---------------------*/
private static int[] primeCheck(int[] myArray, int num) {
//prime
boolean prime = true;
int[] myArray2 = new int[10];
//test all components of an array
for (int i = num + 1 ; i >= 0; i++) {
if (num % i > 0) {
prime = false;
System.arraycopy(myArray, 0, myArray2, 0, 4);
return myArray2;
}
}
return myArray2;
}// ISPRIME REMOVE
}// CLOSE CLASS
my output is as follows:
11 22 14 7 8 10 0
0
0
0
0
0
I feel really rusty because this is the first assignment back after a long break, so any guidance or help would be greatly appreciated.
myArray2 is defined and never filled with values. So every element is 0.
I think
for (int i = 0; i < myArray2.length; i++)
must be
for (int i = 0; i < myArray.length; i++)
There's definitely something wrong about for (int i = num + 1 ; i >= 0; i++). Below is one way to achieve the objective.
public static void main(String[] args) {
int[] arr = { 10, 8, 7, 14, 22, 11 };
for(int i = 0; i < arr.length; i++) {
if(!isPrime(arr[i])) {
System.out.print(arr[i] + " ");
}
}
}
public static boolean isPrime(int num) {
if(num < 2) return false;
if(num == 2) return true;
int remainder, divisor;
divisor = num - 1;
do {
try {
remainder = num % divisor;
} catch (ArithmeticException e) {
return false;
}
if(remainder == 0) return false;
divisor--;
} while (divisor > 1);
return true;
}

Maximum subsequence sum in an array with no 3 consecutive number

I have to find the maximum sum in an array such that no 3 consecutive number together
for eg
3 4 5 1 2
should return me 11. (4+5+2)
I am getting out as 9.
I am using dynamic programming since I want the running time to be O(N)
The idea is s array will store the max sum and s1 array will store the length of input seen to keep track of consecuent numbers
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class ex {
public static int maxSubArraySum(int a[], int size)
{
int s[]= new int[size+1];
s[0]=0;
int s1[]= new int[size+1];
s1[0]=0;
for (i = 1; i <= size; i++)
{
s1[i]= 1+s1[i-1];
if(s1[i]<3) {
int k=Math.max(s[i-1], a[i-1]+s[i-1]);
s[i]=k;
}
else {
s[i]=Math.max(a[i-1], a[i-1]+a[i]);
s1[i]=0;
}
}
return s[s.length-1];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc= new Scanner(System.in);
int size=sc.nextInt();
int a[]=new int[size];
for(int i=0;i<size;i++) {
a[i]=sc.nextInt();
}
System.out.println(maxSubArraySum(a, a.length));
}
}
I think your code requires a slight tweak, below are the changes you need to make
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class ex
{
public static int maxSubArraySum(int a[], int size)
{
int s[] = new int[size+2];
//they will form your base cases
s[0] = 0;
s[1] = a[0];
s[2] = a[0] + a[1];
for(int i = 2; i < size; i++)
{
s[i+1] = Math.max(a[i] + s[i-1], a[i] + a[i-1] + s[i-2]);
s[i+1] = Math.max(s[i+1], s[i]);
}
return s[size];
}
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
int size=sc.nextInt();
int a[]=new int[size];
for(int i=0;i<size;i++)
{
a[i]=sc.nextInt();
}
System.out.println(maxSubArraySum(a, a.length));
}
}
Hope this helps.
Consider the array starts from index 0...n-1 i.e a[0...n-1],
we create array table[n+1] and table[i] means maximum sum from 1 to i without no three continuous numbers. While computing table[i], we have to choose whether to select i th element or not.
So, three cases are there
Case 1: Since, we can't select three consecutive numbers, but can select two consecutive numbers and skipping third one.
Case 2: select i th element and skip (i-1) th element.
Case 3: If we doesn't select i th element
So, based on above discussion c++ code is given below:
int max_sum_no_three_consecutive(int a[], int n)
{
int table[n+1];
table[0] = 0;
table[1] = a[0];
table[2] = a[1];
for (int i = 3; i <= n; i++) table[i] = max_3(table[i-1], table[i-2] + a[i-1], a[i-1] + a[i-2] + table[i-3]);
return table[n];
}
It's an old post but thought of answering the question.
for(int i = 0; i < input.length; i++) {
if(0 == i) {
sum[i] = input[0];
} else if(1 == i) {
sum[i] = input[0] + input[1];
} else if(2 == i){
sum[i] = getMaxOf3(sum[i - 1], sum[i-2] + input[i], input[i] + input[i - 1]);
} else {
sum[i] = getMaxOf3(sum[i - 1], sum[i - 2] + input[i], sum[i - 3] + input[i] + input[i - 1]);
}
}
int getMaxOf3(int x,
int y,
int z) {
return Math.max(Math.max(x, y), z);
}
Explanation:
Consider the array: {3, 4, 5, 1, 2}
we need to first have some default values for the result array.
sum[0] = array[0];
sum[1] = array[0] + array[1];
sum[2] = maximum of either (arr[0] + arr[1]) or (arr[1] + ar[2]) or (arr[0] + arr[2])
We calculated 3 sums,as we will be using these for furthur sum calculations.
sum[3] = max(sum[2], sum[1] + arr[3], sum[0] + arr[3] + arr[2]);
This reduces to,
sum[i] = max(sum[i - 1], sum[i - 2] + arr[i], sum[i - 3] + arr[i] + arr[i - 1]);

Categories

Resources