I need to find the maximum increasing distance or difference between numbers in a vector. For example, in the vector [10, 5, 20, 45, 5, 5, 7] on first pass it looks that the difference between 10 and 45 is maximum. But on the second iteration the difference between 5 and 45 is larger, so it should be chosen by the algorithm.
I have a solution but it is O(n^2) and for the life of me I can't figure out if there is a solution with lower computational complexity.
public void maxDistance(int inputArray[]) {
int trueMaxValue = 0, trueMinIndex = 0, trueMaxIndex = 0, tempMaxValue;
for (int i = 0; i < inputArray.length - 1; ++i) {
for (int j = i + 1; j < inputArray.length; ++j) {
tempMaxValue = inputArray[j] - inputArray[i];
if (tempMaxValue > trueMaxValue) {
trueMaxValue = tempMaxValue;
trueMinIndex = i;
trueMaxIndex = j;
}
}
}
}
I also need to know which two numbers in the array were used in the max. Can you please help? I just don't know if I should continue to think about it or O(n^2) is the best that you can do?
You can do it in O(n) Stephany. You need to add a few variables to remember what is the current best solution. Using your current naming convention you can do the following:
public void maxDistance(int inputArray[]) {
int currentMin = inputArray[0], currentMinIndex = 0;
int trueMaxValue = 0, trueMinIndex = 0, trueMaxIndex = 0;
int tempMaxValue;
int i = 1;
while (i < inputArray.length) {
tempMaxValue = inputArray[i] - inputArray[currentMinIndex];
if (tempMaxValue > trueMaxValue) {
trueMaxValue = tempMaxValue;
trueMinIndex = currentMinIndex;
trueMaxIndex = i;
}
if (inputArray[i] < currentMin) {
currentMinIndex = i;
currentMin = inputArray[i];
}
++i;
}
Maybe I am missing something but:
iterate the array
determine the smallest entry
determine the biggest entry
compute the delta (max - min)
All of that can be done in one iteration. This would find 5 and 45 and give a difference of 40. And as said - one pass only.
This can be done in linear time.
Here is an outline:
Iterate over the array, keeping track of the smallest number seen
thus far.
Compute the difference between the smallest number seen thus far and the current number, and keep track of the largest such difference.
VoilĂ .
You've definitely got better options. I'm not sure why a nested loop was even necessary here. I'll edit to include the indices, but I'm not sure why that's helpful in the case scenario considering your array can have duplicate numbers.
int[] inputArray = new int[] {10,5,20,5,5,46,7};
int max = 0;
int min = Integer.MAX_VALUE;
for (int i = 0; i < inputArray.length; i++) {
max = inputArray[i] > max ? inputArray[i] : max;
min = inputArray[i] < min ? inputArray[i] : min;
}
int delta = max - min;
I'll edit to include the indices, but I'm not sure why that's helpful in the case scenario considering your array can have duplicate numbers.
int[] inputArray = new int[] {10,5,20,5,5,46,7};
int max = 0;
int min = Integer.MAX_VALUE;
int maxIndex = -1;
int minIndex = -1;
for (int i = 0; i < inputArray.length; i++) {
if (inputArray[i] > max) {
max = inputArray[i];
maxIndex = i;
}
if (inputArray[i] < min) {
min = inputArray[i];
minIndex = i;
}
}
int delta = max - min;
Related
This code is radix sort in Java.
Now I can sort. But I want to reduce its functionality if there is no change in the
array, let it stop the loop and show the value.
Where do I have to fix it? Please guide me, thanks in advance.
public class RadixSort {
void countingSort(int inputArray[], int size, int place) {
//find largest element in input array at 'place'(unit,ten's etc)
int k = ((inputArray[0] / place) % 10);
for (int i = 1; i < size; i++) {
if (k < ((inputArray[i] / place) % 10)) {
k = ((inputArray[i] / place) % 10);
}
}
//initialize the count array of size (k+1) with all elements as 0.
int count[] = new int[k + 1];
for (int i = 0; i <= k; i++) {
count[i] = 0;
}
//Count the occurrence of each element of input array based on place value
//store the count at place value in count array.
for (int i = 0; i < size; i++) {
count[((inputArray[i] / place) % 10)]++;
}
//find cumulative(increased) sum in count array
for (int i = 1; i < (k + 1); i++) {
count[i] += count[i - 1];
}
//Store the elements from input array to output array using count array.
int outputArray[] = new int[size];
for (int j = (size - 1); j >= 0; j--) {
outputArray[count[((inputArray[j] / place) % 10)] - 1] = inputArray[j];
count[(inputArray[j] / place) % 10]--;//decrease count by one.
}
for (int i = 0; i < size; i++) {
inputArray[i] = outputArray[i];//copying output array to input array.
}
System.out.println(Arrays.toString(inputArray));
}
void radixSort(int inputArray[], int size) {
//find max element of inputArray
int max = inputArray[0];
for (int i = 1; i < size; i++) {
if (max < inputArray[i]) {
max = inputArray[i];
}
}
//find number of digits in max element
int d = 0;
while (max > 0) {
d++;
max /= 10;
}
//Use counting cort d no of times
int place = 1;//unit place
for (int i = 0; i < d; i++) {
System.out.print("iteration no = "+(i+1)+" ");
countingSort(inputArray, size, place);
place *= 10;//ten's , hundred's place etc
}
}
1
I'm going to resist typing out some code for you and instead go over the concepts since this looks like homework.
If I'm understanding you correctly, your problem boils down to: "I want to check if two arrays are equivalent and if they are, break out of a loop". Lets tackle the latter part first.
In Java, you can use the keyword"
break;
to break out of a loop.
A guide for checking if two arrays are equivalent in java can be found here:
https://www.geeksforgeeks.org/compare-two-arrays-java/
Sorry if this doesnt answer your question. Im just gonna suggest a faster way to find the digits of each element. Take the log base 10 of the element and add 1.
Like this : int digits = (int) Math.log10(i)+1;
I recently had to solve a coding challenge for an interview, I was able to get the proper solution, but my code didn't execute in the time constraints for very large datasets. I've been wracking my brain trying to think of how I could have optimized it further and so far haven't been able to think of a solution.
Here's the problem: Given an array list of integers, find the number of possible contiguous segments such that the difference between the maximum and minimum values in the segment doesn't exceed k.
Example: array = [1, 2, 7], k = 3
[1] = max(1) - min(1) = 0 < k = good
[1,2] = max(1,2) - min(1,2) = 1 < k = good
[1,2,7] = max(1,2,7) - min(1,2,7) = 6 > k = bad
[2] = max(2) - min(2) = 0 < k = good
[2,7] = max(2,7) - min(2,7) = 5 > k = bad
[7] = max(7) - min(7) = 0 < k = good
there are 4 possible segments that meet the criteria.
Here's my solution in Java that gives the right answer, but doesn't calculate fast enough on large datasets. I should mention I'm not allowed to change the method params. How else could this be optimized further?
public static int possibleSegments(int k, List<Integer> weights) {
int possibleSegments = weights.size();
for (int i = 0; i < weights.size(); i++) {
int max = weights.get(i);
int min = weights.get(i);
for (int j = i + 1; j < weights.size(); j++) {
int nextSegmentWeight = weights.get(j);
if (nextSegmentWeight > max) {
max = nextSegmentWeight;
} else if (nextSegmentWeight < min) {
min = nextSegmentWeight;
}
if (max - min <= k) {
possibleSegments++;
} else {
break;
}
}
}
return possibleSegments;
}
The top voted answer in the link provided by Alexander did indeed improve the execution time, but significantly increased the number of loop iterations. I modified that top answer a little bit and got a solution that has the same number of loop iterations as my original question, but significantly reduced execution time.
public static int possibleSegments2(int k, List<Integer> weights) {
int possibleSegments = weights.size();
for (int i = 0; i < weights.size(); i++) {
int max = weights.get(i);
int min = weights.get(i);
int j;
for (j = i + 1; j < weights.size(); j++) {
int nextSegmentWeight = weights.get(j);
max = Math.max(max, nextSegmentWeight);
min = Math.min(min, nextSegmentWeight);
if (max - min > k) {
break;
}
}
possibleSegments += (j - i) - 1;
}
return possibleSegments;
}
I am trying to print the minimum number of coins to make the change, if not possible print -1
In this code variable int[] c (coins array) has denominations I can use to come up with Total sum.
int total has total sum I need to come up with using coins (Unlimited supply)
public static int mincoinDP(int[] c, int total) {
int[][] a = new int[c.length + 1][total + 1];
for (int i = 0; i <= c.length; i++) {
a[i][0] = 0;
}
for (int j = 1; j <= total; j++) {
a[0][j] = Integer.MAX_VALUE - total;
}
for (int i = 1; i <= c.length; i++) {
for (int j = 1; j <= total; j++) {
if (c[i - 1] > j) {
a[i][j] = Integer.MAX_VALUE - total;
} else {
a[i][j] = Math.min(a[i - 1][j], 1 + a[i][j - c[i - 1]]);
}
}
}
return a[c.length][total];
}
For Sum : 4759 and Array: {31 90 8 36} Correct output is: 59
My output is: 60
What is wrong in code ?
Below is my recursive solution, trying to apply same logic in DP solution. Something seems to be wrong in logic here as well. For same input it prints -2147483595
public static void main(String[] args) {
int[] array = new int[] {31, 90, 8, 36};
System.out.println(mincoin(array, 4759, 0));
}
public static int mincoin(int[] c, int total, int i) {
if (total == 0) return 0;
if (i >= c.length) return Integer.MAX_VALUE;
int x = Integer.MAX_VALUE, y = Integer.MAX_VALUE;
if (total - c[i] >= 0) {
x = 1 + mincoin(c, total - c[i], i);
}
y = mincoin(c, total, i + 1);
return Math.min(x, y);
}
Edit: Problems in code were:
DP version: if (c[i -1] > j) , It is case when solution is not
possible choosing this coin: Here we should accept solution without
this coin which is a[i-1][j]
Recursive version: if(i >= c.length),
it is terminating condition when we dont any coin at this position,
here we should return infinity (Integer.MAX_VALUE) and
to avoid integer overflow return Integer.MAX_VALUE - total.
Though I dont like this version of infinity, but dont see any nice way other than this here.
It looks like you're using dynamic programming, with a[i][j] intended to represent the minimum number of coins (using the first i denominations) that sum to j. But I think your recurrence relations are off. They should be:
a[0][j] = 0 if j==0, otherwise infinity
a[i][j] = a[i-1][j] if c[i-1] > j
a[i][j] = min(a[i-1][j], 1 + a[i][j-c[i-1]]) if c[i-1] <= j
The main mistake is the if c[i-1] > j case in your code. You set the value to infinity (or your variant of infinity), but you should just copy the minimum number of coins from the previous row since you may be able to construct the total using the smaller number of coins.
By the way, there is a neater way to write this code. In pseudocode:
a = new int[total+1]
for int j = 1 to total+1 {
a[j] = infinity
}
for int coin in coins {
for j = coin to total+1 {
a[j] = min(a[j], a[j-coin]+1)
}
}
It's essentially the same algorithm, but it uses a smaller one-dimensional array which it modifies in-place.
Just in case someone looking for solution
public int coinChange(int[] coins, int amount) {
int dp[][] = new int[coins.length+1][amount+1];
Arrays.sort(coins);
// First column of every row
for (int i = 0; i < coins.length; ++i) {
dp[i][0] = 0;
}
/*
Setting this so that this is default max value. We always
want our dp[i][j] better than this
*/
for (int j = 0; j <= amount; ++j) {
dp[0][j] = amount+1;
}
for (int i = 1; i <= coins.length; ++i) {
for (int j = 1; j <= amount; ++j) {
if (coins[i-1] > j) {
dp[i][j] = dp[i-1][j]; // Take the already best value in above row
} else {
dp[i][j] = Math.min(dp[i-1][j], 1 + dp[i][j-coins[i-1]]); // Take the best of above row and using current coin
}
}
}
if (dp[coins.length][amount] > amount) { // it means we cannot find any coin
return -1;
} else {
return dp[coins.length][amount];
}
}
Recently I've been assigned a task which asks to me to "calculate the maximum difference between two adjacent numbers in the array that is passed to it". I'm fairly new to Java (I have only done VB in the past) and since this topic was not well explained to me, I'm not quite sure how to go about it.
Here is some additional information about the task itself:
The function has to pass the following test. The function maxDiff should calculate the maximum difference between two adjacent numbers in the array that is passed to it.
#Test
public void assessmentTest() {
int [] numbers = {12, 8, 34, 10, 59};
assertEquals(49, maxDiff(numbers));
int [] numbers2 = {-50, 100, 20, -40};
assertEquals(150, maxDiff(numbers2));
}
You must assure to take the absolute difference, don't forget it. That's why I used the Math.abs() function.
public static int maxDiff(int[] numbers) {
int diff = Math.abs(numbers[1] - numbers[0]);
for(int i = 1; i < numbers.length-1; i++)
if(Math.abs(numbers[i+1]-numbers[i]) > diff)
diff = Math.abs(numbers[i+1] - numbers[i]);
return diff;
}
Something like this should do the trick:
public static int maxDiff(int[] numbers) {
if (numbers.length < 2) {
return 0;
}
if (numbers.length == 2) {
return Math.abs(numbers[1] - numbers[0]);
}
int max = Math.abs(numbers[1] - numbers[0]);
for (int i = 2; i < numbers.length; i++) {
int diff = Math.abs(numbers[i-1] - numbers[i]);
if (diff > max) {
max = diff;
}
}
return max;
}
For the specific question you asked:
public static int maxDiff(int[] arr) {
if(arr.length < 2)
return -1; // error condition: throw exception?
int maxdiff = Integer.MIN_VALUE;
for(int i = 1; i < arr.length; ++i) {
int diff = Math.abs(arr[i] - arr[i-1]);
if(diff > maxdiff)
maxdiff = diff;
}
return maxdiff;
}
If you want the max difference across all numbers in the array (not just adjacent ones) the most efficient way to do it would be to iterate the array just once to find the minimum and maximum, then return the absolute value of the two values subtracted from each other.
public static int maxDiff(int[] arr) {
if(arr.length < 2)
return -1; // error condition: throw exception?
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i = 0; i < arr.length; ++i) {
if(arr[i] < min)
min = arr[i];
if(arr[i] > max)
max = arr[i];
}
return Math.abs(max - min);
}
This piece of code can help you:
int[] numbers = {12, 8, 34, 10, 59};
int diff = 0;
int previous = 0;
for (int n : numbers) {
diff = Math.max(diff, Math.abs(n - previous));
previous = n;
}
Variable "diff" will contain the value you look for.
You can use this for Java;
int arrayMaximalAdjacentDifference(int[] inputArray) {
int max=0;
for( int i = 1 ; i < inputArray.length ; i++ ){
max = Math.max(max,Math.abs(inputArray[i] - inputArray[i-1]));
}
return max;
}
How can I find the smallest value in a int array without changing the array order?
code snippet:
int[] tenIntArray = new int [10];
int i, userIn;
Scanner KyBdIn = new Scanner(System.in);
System.out.println("Please enter 10 integer numbers ");
for(i = 0; i < tenIntArray.length; i++){
System.out.println("Please enter integer " + i);
userIn = KyBdIn.nextInt();
tenIntArray[i] = userIn;
}
I am not sure how I can find the smallest array value in the tenIntArray and display the position
For example the array holds - [50, 8, 2, 3, 1, 9, 8, 7 ,54, 10]
The output should say "The smallest value is 1 at position 5 in array"
This figure should be helpful :
Then to answer your question, what would you do on paper ?
Create and initialize the min value at tenIntArray[0]
Create a variable to hold the index of the min value in the array and initialize it to 0 (because we said in 1. to initialize the min at tenIntArray[0])
Loop through the elements of your array
If you find an element inferior than the current min, update the minimum value with this element and update the index with the corresponding index of this element
You're done
Writing the algorithm should be straightforward now.
Try this:
//Let arr be your array of integers
if (arr.length == 0)
return;
int small = arr[0];
int index = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < small) {
small = arr[i];
index = i;
}
}
Using Java 8 Streams you can create a Binary operator which compares two integers and returns smallest among them.
Let arr is your array
int[] arr = new int[]{54,234,1,45,14,54};
int small = Arrays.stream(arr).reduce((x, y) -> x < y ? x : y).getAsInt();
The method I am proposing will find both min and max.
public static void main(String[] args) {
findMinMax(new int[] {10,40,50,20,69,37});
}
public static void findMinMax(int[] array) {
if (array == null || array.length < 1)
return;
int min = array[0];
int max = array[0];
for (int i = 1; i <= array.length - 1; i++) {
if (max < array[i]) {
max = array[i];
}
if (min > array[i]) {
min = array[i];
}
}
System.out.println("min: " + min + "\nmax: " + max);
}
Obviously this is not going to one of the most optimized solution but it will work for you. It uses simple comparison to track min and max values. Output is:
min: 10
max: 69
int[] input = {12,9,33,14,5,4};
int max = 0;
int index = 0;
int indexOne = 0;
int min = input[0];
for(int i = 0;i<input.length;i++)
{
if(max<input[i])
{
max = input[i];
indexOne = i;
}
if(min>input[i])
{
min = input[i];
index = i;
}
}
System.out.println(max);
System.out.println(indexOne);
System.out.println(min);
System.out.println(index);
Here is the function
public int getIndexOfMin(ArrayList<Integer> arr){
int minVal = arr.get(0); // take first as minVal
int indexOfMin = -1; //returns -1 if all elements are equal
for (int i = 0; i < arr.size(); i++) {
//if current is less then minVal
if(arr.get(i) < minVal ){
minVal = arr.get(i); // put it in minVal
indexOfMin = i; // put index of current min
}
}
return indexOfMin;
}
the first index of a array is zero. not one.
for(i = 0; i < tenIntArray.length; i++)
so correct this.
the code that you asked is :
int small = Integer.MAX_VALUE;
int i = 0;
int index = 0;
for(int j : tenIntArray){
if(j < small){
small = j;
i++;
index = i;
}
}
System.out.print("The smallest value is"+small+"at position"+ index +"in array");