Maximum subsequence sum in an array with no 3 consecutive number - java

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]);

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

Multiply numbers represented as arrays in Java?

I have to write a function that multiplies two numbers represented by two int arrays (so I can't use ArrayLists or something).
Each digit of a number is represented by an int between 0 and 9 in the array, no element should be greater than that.
The first element of the array represents the last digit of the number and so on, therefore the number 1234 would be {4,3,2,1} as an array in this function.
I thought multiplying those arrays that way would be similar to long multiplication, so I tried to implement it in a similar way: You multiply every digit of the first array with every digit of the second one and store the rest if the result is equal or greater to 10 and then add it to the next digit. However, I seem to have done something wrong in the code (maybe the calculation of the rest??) because the result of my function is not correct: I tested it with 190 times 86 (represented by the arrays {0,9,1} and {6,8}) and get 15342 ({2,4,3,5,1}) instead of the actual result 16340 (which would be {0,4,3,6,1}).
Can somebody here help me out with this please? This is my code:
import java.util.Arrays;
public class MultiplyArrays {
static int[ ] times(int[ ] a, int[ ] b) {
int[] arr = new int[a.length + b.length - 1];//arr should be the result of a*b. The result shouldn't be shorter than that
int tmp = 0;//stores the rest of two-digit numbers
for(int i = b.length - 1; i >= 0; i--){
for(int j = 0; j < a.length; j++){//should multiply all digits of a with the current index of b
arr[i + j] = (arr[i + j] + (b[i] * a[j] + tmp)) % 10;//sets the value of the (i+j)th index in arr to the multiplication of two numbers from a and b adding the rest tmp.
if((arr[i + j] + b[i] * a[j] + tmp) < 10){//if this number is less than 10, there is no rest
tmp = 0;
}
else{//otherwise, the rest should be the second digit
tmp = (((arr[i + j] + (b[i] * a[j] + tmp))) - ((arr[i + j] + (b[i] * a[j] + tmp)) % 10)) / 10;//something in the formula for the rest is wrong, I guess
}
}
}
if(tmp != 0){//if the last number of the array containing the result is calculated and there still is a rest, a new array with one more digit is created
int[] arr2 = new int[arr.length + 1];
for(int i = arr.length - 1; i >= 0; i--){//the new array copies all numbers from the old array
arr2[i] = arr[i];
arr2[arr2.length - 1] = tmp;//the last space is the rest now
}
return arr2;
}
else{//if there is no rest after calculating the last number of arr, a new array isn't needed
return arr;
}
}
public static void main(String[] args) {//test the function with 190 * 86
int[] a = {0,9,1};
int[] b = {6,8};
System.out.println(Arrays.toString(times(a,b)));
}
}
Maybe this comes from the fact that your indices in the for-loops of the times()-method are incrementing AND decrementing.
The i is going down and the j is going up.
Also, in the second for loop, you should only increment to 'a.length - 1', not to 'a.length'.
Arbitrary precision multiplication is more complex than it seems, and contains corner cases (like one and zero). Fortunately, Java has an arbitrary precision type; BigInteger. In order to use it here, you would need to create two additional methods; one for converting an int[] to a BigInteger, and the second the convert a BigInteger to an int[].
The first can be done with a single loop adding each digit at index i (multiplied by 10i) to a running total. Like,
private static BigInteger fromArray(int[] arr) {
BigInteger bi = BigInteger.ZERO;
for (int i = 0, pow = 1; i < arr.length; pow *= 10, i++) {
bi = bi.add(BigInteger.valueOf(arr[i] * pow));
}
return bi;
}
And the second can be done a number of ways, but the easiest is simply to convert the BigInteger to a String to get the length() - once you've done that, you know the length of the output array - and can populate the digits in it. Like,
private static int[] toArray(BigInteger bi) {
String s = bi.toString();
int len = s.length();
int[] r = new int[len];
for (int i = 0; i < len; i++) {
r[i] = s.charAt(len - i - 1) - '0';
}
return r;
}
Finally, call those two methods and let BigInteger perform the multiplication. Like,
static int[] times(int[] a, int[] b) {
BigInteger ba = fromArray(a), bb = fromArray(b);
return toArray(ba.multiply(bb));
}
Running your original main with those changes outputs (as expected)
[0, 4, 3, 6, 1]
Well, your thought would work with addition, but on multiplication you multiply each digit of one with the whole number of the other and step one digit to the left (*10) each time you change the multiplication digit of the first number.
So you might brought something into confusion.
I just solved it in a more structured way, running the debugger will hopefully explain the process. In the solutions you can remove the trailing / leading zero by checking the digit if 0 and replace the array with one of length - 1.
The solutions are:
With conditions mentioned (numbers in reverse order):
public static void main(String[] args) {
int[] a = {3,2,1};
int[] b = {9,8};
System.out.println("Result is: " + Arrays.toString(calculate(a, b)));
}
private static int[] calculate(int[] a, int[] b) {
// final result will be never longer than sum of number lengths + 1
int[] finalResult = new int[a.length + b.length + 1];
int position = 0;
for(int i = 0; i < a.length; i++) {
int[] tempResult = multiplyWithOther(a[i], b);
addToFinalResult(finalResult, tempResult, position);
position++;
}
return finalResult;
}
private static int[] multiplyWithOther(int number, int[] otherArray) {
// The number cannot be more digits than otherArray.length + 1, so create a temp array with size +1
int[] temp = new int[otherArray.length + 1];
// Iterate through the seconds array and multiply with current number from first
int remainder = 0;
for(int i = 0; i < otherArray.length; i++) {
int result = number * otherArray[i];
result += remainder;
remainder = result / 10;
temp[i] = result % 10;
}
// Add remainder (even if 0) to start
temp[temp.length - 1] = remainder;
return temp;
}
private static void addToFinalResult(int[] finalResult, int[] tempResult, int position) {
int remainder = 0;
for(int i = 0; i < tempResult.length; i++) {
int currentValue = tempResult[i];
int storedValue = finalResult[i + position];
int sum = storedValue + currentValue + remainder;
remainder = sum / 10;
finalResult[i + position] = sum % 10;
}
finalResult[position + tempResult.length] = remainder;
}
And with numbers in normal order in array:
public static void main(String[] args) {
int[] a = {1,2,3,6};
int[] b = {8, 9, 1};
System.out.println("Result is: " + Arrays.toString(calculate(a, b)));
}
private static int[] calculate(int[] a, int[] b) {
// final result will be never longer than sum of number lengths + 1
int[] finalResult = new int[a.length + b.length + 1];
int positionFromEnd = 0;
for(int i = 1; i <= a.length; i++) {
int[] tempResult = multiplyWithOther(a[a.length-i], b);
addToFinalResult(finalResult, tempResult, positionFromEnd);
positionFromEnd++;
}
return finalResult;
}
private static int[] multiplyWithOther(int number, int[] otherArray) {
// The number cannot be more digits than otherArray.length + 1, so create a temp array with size +1
int[] temp = new int[otherArray.length + 1];
// Iterate through the seconds array and multiply with current number from first
int remainder = 0;
for(int i = 1; i <= otherArray.length; i++) {
int result = number * otherArray[otherArray.length - i];
result += remainder;
remainder = result / 10;
temp[otherArray.length - i +1] = result % 10;
}
// Add remainder (even if 0) to start
temp[0] = remainder;
return temp;
}
private static void addToFinalResult(int[] finalResult, int[] tempResult, int positionFromEnd) {
int remainder = 0;
for(int i = 1; i <= tempResult.length; i++) {
int currentValue = tempResult[tempResult.length - i];
int storedValue = finalResult[finalResult.length - positionFromEnd - i];
int sum = storedValue + currentValue + remainder;
remainder = sum / 10;
finalResult[finalResult.length - positionFromEnd - i] = sum % 10;
}
finalResult[finalResult.length - positionFromEnd - tempResult.length - 1] = remainder;
}

How to make intersection between method

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;
}
}

Java - Recursion to find the 2 smallest nums in array

So I have this problem of finding the 2 smallest numbers in array but I have to use recursion (the recursion part is a part of this exercise).
I start thinking how to do it and 'the plan' that I trying to do is like this, first of all find the smallest in the array, then mark where I found it and then to split the array in to 2 groups and search in this 2 group an eventuality take the minimum from them..
this is the code that I got this far:
public static int MinRecu2(int[] a) {
if (a.length == 1)
return a[0];
int min = a[0];
int min2;
int mark = 0;
// Find first min
for (int i = 1; i < a.length; i++) {
if (a[i] < min) {
min = a[i];
mark = i;
}
}
int [] help1= new int [mark+1];
int [] help2= new int [a.length-mark];
help1=Arrays.copyOfRange(a, 0, mark);
help2=Arrays.copyOfRange(a, mark+1, a.length-1);
// Find second min
min2 = Math.min(MinRecu2(help1), MinRecu2(help2));
return min;
}
I know the code not working and that the 'recursion' I was trying to do is bad, but that how I was trying to make my thinking to code
This is an approach to your question.
public static int[] twoSmallest(int[] arr, int s, int e) {
int[] list;
if (e - s <= 3) { // maybe 2 or 3
list = new int[e - s];
for (int i = s; i < e; i++)
list[i - s] = arr[i];
} else {
int[] left = twoSmallest(arr, s, (s + e) / 2);
int[] right = twoSmallest(arr, (s + e) / 2, e);
list = new int[4];
list[0] = left[0];
list[1] = left[1];
list[2] = right[0];
list[3] = right[1];
}
Arrays.sort(list);
return new int[] {list[0], list[1]};
}
This function means find two smallest numbers in arr in [s, e). You can call like this:
int[] result = twoSmallest(arr, 0, arr.length);
private static int findMinFromArray(int[] iArray, int index, int min) {
if(index <= (iArray.length - 1)){
if(iArray[index] < min){
min = iArray[index];
}
System.out.println("Before: " + "Index = " + index + " | Min = " + min);
findMinFromArray(iArray, index + 1, min);
}
System.out.println("After: " + "Index = " + index + " | Min = " + min);
return min;
}
I'm not sure how to get the program to stop, rather than going back again like it's doing.This finds only smallest.

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);
}
}
}

Categories

Resources