patternjavaMinor
Given an integer array of size n, find any three numbers
Viewed 0 times
threearraysizeanynumbersfindgiveninteger
Problem
Return any three descending numbers that appear sequentially in the input such that A[i] > A[j] > A[k] and i < j < k. Since problem description contains 'any', I could not unit test using JUnit.
I'm looking for request code review, optimizations and best practices.
```
final class DescendingTriplets {
private final int high;
private final int mid;
private final int low;
DescendingTriplets(int high, int mid, int low) {
this.high = high;
this.mid = mid;
this.low = low;
}
public int getHigh() {
return high;
}
public int getMid() {
return mid;
}
public int getLow() {
return low;
}
@Override
public String toString() {
return "high: " + high + " mid: " + mid + " low: " + low;
}
}
public final class ThreeDescending {
private ThreeDescending() {}
/**
* This class is used to save high and mid values.
*/
private static class State {
int mid;
int high;
State(int low, int high) {
this.mid = low;
this.high = high;
}
}
/**
* Returns any three descending numbers in an array such that they appear one after other in the original sequence.
* If such numbers dont exist then null is returned
*
* @param a the input array
* @return object with three descending numbers in squence
*/
public static DescendingTriplets getDescendingTriplets(int[] a) {
if (a.length a[i + 1]) {
stateCurrent = new State(a[i + 1], a[i]);
k = i + 1;
break;
}
}
for (int j = k + 1; j stateInProgress.high) {
stateInProgress.high = a[j];
} else {
stateInProgress.mid = a[j];
stateCurrent = stateInProgress;
}
} else {
stateInP
I'm looking for request code review, optimizations and best practices.
```
final class DescendingTriplets {
private final int high;
private final int mid;
private final int low;
DescendingTriplets(int high, int mid, int low) {
this.high = high;
this.mid = mid;
this.low = low;
}
public int getHigh() {
return high;
}
public int getMid() {
return mid;
}
public int getLow() {
return low;
}
@Override
public String toString() {
return "high: " + high + " mid: " + mid + " low: " + low;
}
}
public final class ThreeDescending {
private ThreeDescending() {}
/**
* This class is used to save high and mid values.
*/
private static class State {
int mid;
int high;
State(int low, int high) {
this.mid = low;
this.high = high;
}
}
/**
* Returns any three descending numbers in an array such that they appear one after other in the original sequence.
* If such numbers dont exist then null is returned
*
* @param a the input array
* @return object with three descending numbers in squence
*/
public static DescendingTriplets getDescendingTriplets(int[] a) {
if (a.length a[i + 1]) {
stateCurrent = new State(a[i + 1], a[i]);
k = i + 1;
break;
}
}
for (int j = k + 1; j stateInProgress.high) {
stateInProgress.high = a[j];
} else {
stateInProgress.mid = a[j];
stateCurrent = stateInProgress;
}
} else {
stateInP
Solution
Return any three descending numbers that appear sequentially in the input such that A[i] > A[j] > A[k] and i
- there are only a couple of possible sequences and check that the returned one whether exists in the possible return values list or not,
- there isn't any valid sequence.
Context
StackExchange Code Review Q#47126, answer score: 2
Revisions (0)
No revisions yet.