So this is an unique question and I would appreciate any help.
The objective:
To count and return an integer of 'distance' from min value to max value.
The list is not sorted and should not be sorted.
Min value might be before max or vise-verse.
In a list of { 2, -5, -7, 8, 22, -10 } answer = 1 (the distance from -10 to 22)
In a list of { 2, -5, -7, 8, 22, -6 } answer = 2 (the distance from -7 to 22)
Thank you for any help.
Just keep track of the corresponding index:
double min = list[0];
int minIndex = 0;
double max = list[0];
int maxIndex = 0;
for (int i =1 ; i < list.length ; i++) {
if(min > list[i]){
min = list[i];
minIndex = i;
}
if(max < list[i]){
max = list[i];
maxIndex = i;
}
}
int res = Math.abs(minIndex - maxIndex);
Keep the indice while finding max and min :
int indMin = 0;
int indMax = 0;
double min = list[0];
double max = list[0];
for (int i =0; i<list.length;i++){
if(min>list[i]){
min=list[i];
indMin = i;
}
if(max<list[i]){
max = list[i];
indMax = i;
}
}
int distance = Math.abs(indMax - indMin);
Related
Given an array and an integer k, find the maximum for each and every contiguous subarray of size >=2.
INPUT:
int[] array = new int[]{-5,-2,-3,-1,-1};
int[] array1 = new int[]{5,2,3,-3,1,1};
OUTPUT:
-1,-1 //Since the maximum sum with maximum contiguous subarray can be of size 2 or greater than 2 and all are negative, so we taking the subarray = -1 -1
5,2,3 // Because we need to find the maximum sum and maximum subarray with limitation that array can be >= 2
I have solved this but my solution is not working when all the integers are negative. Also, it is not working when input is -5,-2,-3,-3,1,1. The output should be 1 and 1 but it is coming as -3,-3,1 and 1.
I need an optimized solution. Please find my code below:
public class Pro {
static int[] maxSubarray(int[] a) {
int max_sum = a[0];
int curr_sum = a[0];
int max_start_index = 0;
int startIndex = 0;
int max_end_index = 1;
for (int i = 1; i < a.length; i++) {
if (max_sum > curr_sum + a[i]) {
startIndex = i-1;
curr_sum = a[i];
} else {
curr_sum += a[i];
}
if (curr_sum > max_sum) {
max_sum = curr_sum;
max_start_index = startIndex;
max_end_index = i;
}
}
if (max_start_index <= max_end_index) {
return Arrays.copyOfRange(a, max_start_index, max_end_index + 1);
}
return null;
}
public static void main(String[] args) {
int[] array = new int[]{-5,-2,-3,-1,-1};
int[] array1 = new int[]{5,2,-3,1,1};
int[] out = maxSubarray(array1);
for(int a : out)
{
System.out.println(a);
}
}
}
There is one more code snippet which can be helpful. It finds the maximum subarray. See code below:
public int maxSubArray(int[] A) {
int max = A[0];
int[] sum = new int[A.length];
sum[0] = A[0];
for (int i = 1; i < A.length; i++) {
sum[i] = Math.max(A[i], sum[i - 1] + A[i]);
max = Math.max(max, sum[i]);
}
return max;
}
The algorithm you are using is called Kardane's algorith and cannot be used when all the numbers are negative. At least one number has to be positive for the algorithm to work. To make it work for arrays with negative numbers but also at least one positive number, curr_sum should be set to 0 whenever you come across a negative number in your array. One way to achieve this is to use a custom max-function
private int max(int first, int second) {
if (first < second) return first;
else return second;
}
Replace:
if (max_sum > curr_sum + a[i]) {
startIndex = i-1;
curr_sum = a[i];
} else {
curr_sum += a[i];
}
with:
curr_sum = max(curr_sum + a[i], 0);
if (curr_sum == 0) startIndex = i + 1;
Full code:
public int max(int[] a) {
int max_sum = a[0];
int curr_sum = a[0];
int startIndex = 0;
int max_start_index = 0;
int max_end_index = 1;
for (int i = 1; i < a.length; i++) {
max_sum = max(max_sum + a[i], 0);
if (max_sum == 0) startIndex = i + 1;
curr_sum = max(curr_sum, max_sum);
if (curr_sum == max_sum) {
max_start_index = startIndex;
max_end_index = i;
}
}
System.out.println("Start index: " + max_start_index);
System.out.println("End index: " + max_end_index);
return curr_sum;
}
Input: -5, -2, -3, -3, 1, 1
Output:
Start index: 4
End index: 5
2
So for any given odd length array, I need to look at the first, middle and last value, and return an array with only hose 3 values but in ascending order.This is what I have, but it's only working with arrays of 3 elements, it's not working for other odd length arrays:
public int[] maxTriple(int[] nums) {
int toSwap, indexOfSmallest = 0;
int i, j, smallest;
for( i = 0; i < nums.length; i ++ )
{
smallest = Integer.MAX_VALUE;
for( j = i; j < nums.length; j ++ )
{
if( nums[ j ] < smallest )
{
smallest = nums[ j ];
indexOfSmallest = j;
}
}
toSwap = nums[ i ];
nums[ i ] = smallest;
nums[ indexOfSmallest ] = toSwap;
}
return nums;
}
You're getting the sorted array, so if you need just the first, middle and last values you can try it like this:
public int[] maxTriple(int[] nums) {
int toSwap, indexOfSmallest = 0;
int i, j, smallest;
for( i = 0; i < nums.length; i ++ )
{
smallest = Integer.MAX_VALUE;
for( j = i; j < nums.length; j ++ )
{
if( nums[ j ] < smallest )
{
smallest = nums[ j ];
indexOfSmallest = j;
}
}
toSwap = nums[ i ];
nums[ i ] = smallest;
nums[ indexOfSmallest ] = toSwap;
}
nums=new int[]{nums[0],nums[(nums.length/2)],nums[nums.length-1]};
return nums;
}
You're thinking about this too hard. If the length of your array is less than 3 or even, return null.
Otherwise, get the first element, the last element, and the middle element and store them in a new array. Sort the array with Arrays.sort() and return the new array.
public static void main(String[] args) throws Exception {
System.out.println(Arrays.toString(maxTriple(new int[] {1, 4, 2, 4})));
System.out.println(Arrays.toString(maxTriple(new int[] {1, 4, 2, 4, 5})));
System.out.println(Arrays.toString(maxTriple(new int[] {75, 99, 4, 999, 4, 65, 23})));
}
public static int[] maxTriple(int[] nums) {
if (nums.length < 3 || nums.length % 2 == 0) {
return null;
}
int[] result = new int[3];
result[0] = nums[0]; // First
result[1] = nums[nums.length - 1]; // Last
result[2] = nums[nums.length / 2]; // Middle
Arrays.sort(result);
return result;
}
Results:
null
[1, 2, 5]
[23, 75, 999]
I used another approach with ternary operators. I hope it can be useful...
public int maxTriple(int[] nums) {
return (nums[0] > nums[nums.length/2]) ? (nums[0] > nums[nums.length-1] ?
nums[0] : nums[nums.length-1]) : (nums[nums.length/2] > nums[nums.length-1]
? nums[nums.length/2] : nums[nums.length-1]);
}
This code works fine:
public int[] maxTriple(int[] nums) {
int[] myArray = new int[3];
int length = nums.length;
int middle = nums[(length + 1) / 2 - 1];
int last = nums[length - 1];
int first = nums[0];
myArray[0] = nums[0];
myArray[1] = nums[middle];
myArray[2] = nums[last];
return myArray;
}
What you are tyring to do is called selection sort. Here is the code for that:
int[] maxTriple(int[] nums, int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i; j < size; j++) {
if (nums[j] < nums[i]) {
int toSwap = nums[i];
nums[i] = nums[j];
nums[j] = toSwap;
}
}
}
return nums;
}
I have some code that I've compiled and ran. It is suppose to sort the values from smallest to greatest. Can someone help me find what is going on in the code and not making it sort correctly? I get these numbers
-9 -3 -1 1 6 7 83 19 2 6 4 6 32 66
Can someone help me and tell me what is wrong with the code? Thank you!
int myArray[] = {1, 6, -1, 7, 83, 19, -3, 6, 2, 4, 6, 32, 66, -9};
int n = myArray.length;
myArray = doop(myArray);
for (int i = 0; i < n; i++) {
System.out.println(myArray[i]);
}
private static int[] doop(int[] myArray) {
int n = myArray.length;
int swap;
for (int i = n - 1; i >= 0; i--) {
int j = i;
int min = myArray[i];
while ((j > 0) && (myArray[j - 1] < min)) {
myArray[j] = myArray[j - 1];
j = j - 1;
}
myArray[j] = min;
}
return myArray;
}
In bubble sort you have to compare the only the adjacent elements and traverse the array. Repeating this n-1 times , your array gets sorted and so correct code is:
private static int[] doop(int[] myArray)
{
int n = myArray.length;
for (int i = n - 1; i >= 0; i--)
{
for(int j=n-1;j>0;j--)
{
if(myArray[j]<myArray[j-1])
{
//swapping the elements
myArray[j]=myArray[j]^myArray[j-1];
myArray[j-1]=myArray[j]^myArray[j-1];
myArray[j]=myArray[j]^myArray[j-1];
}
}
}
return myArray;
}
int myArray[] = {1, 6, -1, 7, 83, 19, -3, 6, 2, 4, 6, 32, 66, -9};
int n = myArray.length;
myArray = doop(myArray);
for (int i = 0; i < n; i++) {
System.out.println(myArray[i]);
}
}
private static int[] doop(int[] myArray) {
int n = myArray.length;
int swap;
for (int i = n - 1; i >= 0; i--) {
int j = i;
int min = myArray[i];
while ((j > 0) && (myArray[j - 1] < min)) {
myArray[j] = myArray[j - 1];
j = j - 1;
myArray[j] = min;
}
}
return myArray;
}
I am a beginner. I am unable to figure out how to write a function which will return 1, if have this property:
arr[0] = arr[1] + arr[2] = arr[3] + arr[4] + arr[5] = arr[6] + arr[7] + arr[8] + arr[9] = ...
else returns 0. The length of an array must be n*(n+1)/2 for some n.
For example, if input array is {2, 1, 1, 4, -1, -1}, it returns 1 because 2 = 1 + 1, 2 = 4 + -1 + -1
I have tried this:
public static int myArray(int[] a) {
int len = a.length;
if (checkLenght(len)) {
int firstElem = a[0];
int value = 1;
int sum = 0;
for (int i = 1; i <= a.length; i++) {
for (int j = value; j < value + 1; j++) {
sum += a[j];
value++;
}
}
}
return 0;
}
public static boolean checkLenght(int len) {
for (int i = 0; i <= 100; i++) {
if ((i * (i + 1) / 2) == len) {
return true;
}
}
return false;
}
Thanks in advance.
I try to partition input in sets of two , three , .... elements. for that I use a pointer to show how many elements are in this partition. firs it is 2. then it is three , ... . and I use a temp number to count if in this partition I have enough element or not. after I have enough element in each partition I just check sum of the element of that partition.
This should do the work:
public static int myArray(int[] a) {
int len = a.length;
if (checkLenght(len)) {
int firstElem = a[0];
int pointer = 2; // pointer that will be 2, 3, 4 , ...
int sum = 0; // sum of element in each partition
int temp = 0; // a temp value to check if I reach desirable number of element in this partition or not.
for (int i = 1; i < a.length; i++) { // i<=a.length give you exception.
temp++;
sum += a[i];
if (temp == pointer) { // check if I have enough element.
pointer++; // plus pointer by one
temp = 0; // reset temp
if (sum != firstElem) // if in any of those partitions my needs doesnt meet I return zero.
return 0;
sum = 0; // reset sum
}
}
return 1;
}
return 0;
}
I would like to write a function in java that receives a parameter and returns 1 or 0, in the following conditions:
If the length of the array is odd, return 1 if the sum of the odd numbers is greater than the sum of the even numbers, and 0 otherwise.
If the length of the array is even, check the largest number less or equal then 10. If it is odd, return 1. If it is even, return 0.
For example, using the following arrays:
array1 = {13, 4, 7, 2, 8}
array2 = {11, 7, 4, 2, 3, 10}
The first array returns 1, because there 13(odd) + 7(odd) = 20 is greater then 4 + 2 + 8 = 14.
The second array returns 0, because there 11, 7 are odd but, 10 is greater then 7.
What I already tried, please check below:
public static int isOddHeavy(int[] arr) {
int summationOd = 0;
int summationEv = 0;
for (int i = 0; i < arr.length; i++) {
if (i % 2 != 0) {
summationOd = sumOddEven(arr);
} else {
summationEv = sumOddEven(arr);
}
}
if(summationOd > summationEv) {
return 1;
} else {
return 0;
}
}
public static int sumOddEven(int[] arr) {
int total = 0;
for (int i = 1; i < arr.length; i++) {
total += arr[i];
}
return total;
}
Here is a Java function that does what you want. Just iterate through the array, updating the variables and check your conditions in the end.
public static int yourFunction(int[] arr) {
int sumOdd = 0;
int sumEven = 0;
int maxOdd = 0;
int maxEven = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
sumEven += arr[i];
if (maxEven < arr[i] && arr[i] <= 10)
maxEven = arr[i];
}
else {
sumOdd += arr[i];
if (maxOdd < arr[i] && arr[i] <= 10)
maxOdd = arr[i];
}
}
if (arr.length%2 != 0)
return (sumOdd > sumEven) ? 1 : 0;
return (maxOdd > maxEven) ? 1 : 0;
}