Getting middle value of array to create balanced BST - java

I'm trying to find the middle element or index of an array then get the middle of each half, so on...
so let say we have [0,1,2,3,4,5,6,7,8,9,10,11,12,13]
what i'm expecting 7, 3, 1, 0, 2, 5, 4, 6 ...
That way when I add elements from the new array, I end up with a balanced BST
start: starting index (0)
end: length - 1
nums: list of numbers to add
b: the tree that will insert to
Code:
public static BST fillBST(BST b, List<Integer> nums, int start, int end) {
int mid = (start + end) / 2;
if (start > end)
b.insertt(nums.get(mid));
else {
fillBST(b, nums, start, mid - 1);
fillBST(b, nums, mid + 1, end);
}
return b;
}
output I get using list [0,31]: 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31

Your recursion is not properly written that's what the problem is. You should always add the mid element and then move to the left and right part if you need so.
So let's do it like that:
public static void main(String[] args) {
List<Integer> list = Arrays.asList(new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 });
List<Integer> res =new ArrayList<>();
fillBST(res, list, 0, list.size() - 1);
System.out.println(res);
}
public static List<Integer> fillBST(List<Integer> b, List<Integer> nums, int start, int end) {
int mid = (int)Math.round((1.0 * start + end) / 2);
b.add(nums.get(mid));
if (start <= mid - 1)
fillBST(b, nums, start, mid - 1);
if (end >= mid + 1)
fillBST(b, nums, mid + 1, end);
return b;
}
This prints
[7, 3, 1, 0, 2, 5, 4, 6, 11, 9, 8, 10, 13, 12]
Other than the recursion condition you can see the different way I calculate mid. By calculating it int mid = (start + end) / 2; you don't round the value but truncate it. So the medium element becomes 6 in your case instead of 7.

Related

Sum of all elements of each subarray

I was solving Kadane's algorithm , a very weird approach came to my mind while solving it. What if we find a way to find out the sum of all the elements of all possible subarrays forming from an array and store it in an arraylist. I've been thinking about it for a long time now, but I'm unable to solve it. It would be great if I can get some assistance.
`
import java.util.*;
import java.lang.*;
import java.io.*;
class Sample
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
ArrayList<Integer> list = new ArrayList<>();
int sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
}
list.add(sum);
for(int i=0;i<n;i++){
list.add(arr[i]);
}
for(int i=0;i<n;i++){
//sum-=arr[i];
if(!list.contains(sum-arr[i]) && (sum-arr[i])>0){
list.add(sum-arr[i]);
}
sum=sum-arr[i];
}
System.out.println(list);
}
}
`
This is what I've done till now. There's a very big flaw in the logic and I know it but I just can't seem to solve it.
This doesn't use Kadanes algorithm, but it does sum the sub arrays. It is facilitated by using the subList method of the List interface.
for (int i = 1; i < 8; i++) {
List<Integer> list =
IntStream.range(1, i+1).boxed().toList();
List<Integer> sums = new ArrayList<>();
findsubs(list, 0, list.size(), sums);
System.out.println(sums);
sums.clear();
}
prints
[1]
[3, 2, 1]
[6, 5, 3, 3, 2, 1]
[10, 9, 7, 4, 6, 5, 3, 3, 2, 1]
[15, 14, 12, 9, 5, 10, 9, 7, 4, 6, 5, 3, 3, 2, 1]
[21, 20, 18, 15, 11, 6, 15, 14, 12, 9, 5, 10, 9, 7, 4, 6, 5, 3, 3, 2, 1]
[28, 27, 25, 22, 18, 13, 7, 21, 20, 18, 15, 11, 6, 15, 14, 12, 9, 5, 10, 9, 7, 4, 6, 5, 3, 3, 2, 1]
The method
public static void findsubs(List<Integer> list, int s, int e,
List<Integer> sums) {
if (s >= e) {
return;
}
for (int i = s; i < e; i++) {
sums.add(list.subList(i, e).stream()
.mapToInt(Integer::intValue).sum());
}
findsubs(list, s, e - 1, sums);
}
long subarraySum(vector<long> &A) {
long result = 0;
int n = A.size();
for (int i=0; i <n; ++i)
{
result +=(A[i]*(i+1)*(n-i));
}
return result;
}
Sum of all sub array means,
Actually each element how many time contribute himself in each sub array
lets take : [1 2 3 4][0,0][0,1][1,1][0,2][1,2][2,2][0,3][1,3][2,3][3,3]
contribution of ith element is A[i](i+1)(n-i)

Quicksort went wrong (added some duplicates)

I'm new to sorting algorithms and can't seem to figure out what went wrong. With an unsorted array of int [] uArr = {5,2, 10, 7, 6, 0, 8, 1, 9}, i'm given 0,1,2,5,5,6,8,10 (the nine in the org. array was replaced with a duplicate 5 and the 7 was lost). If I add duplicates to the array, I get more duplicates after the array is sorted and a few more missing numbers.
public static void main(String args[]){
int [] uArr = {5,2, 10, 7, 6, 0, 8, 1, 9};
qSort(uArr, 0, 8);
}
public static void qSort(int [] A, int low, int high){
if (low < high){
int pivotL = partition(A, low, high);
qSort(A, low, pivotL);
qSort(A, pivotL+1, high);
}
}
public static int partition(int [] arr, int low, int high){
int pivot = arr[low];
int leftwall = low;
for (int i = low + 1; i < high; i++){
if (arr[i] < pivot){
int temp = arr[i];
arr[i] = arr[leftwall];
arr[leftwall] = temp;
leftwall += 1;
}
}
int temp2 = pivot;
pivot = arr[leftwall];
arr[leftwall] = temp2;
return leftwall;
}
}
So one thing you should seriously do is start writing documentation. Even though this is a small program, you seem to have forgotten what you were doing as you were writing the code.
For example, there are 9 elements in the array, and you pass in the offsets to sort, inclusive, as 0 through 8:
qSort( uArr, 0, 8 );
But then in the partition routine you only sort elements less than the high value:
for( int i = low + 1; i < high; i++ ) {
And unlike you the last value in the array, 9, is never touched for me and never sorted. So that's an issue. Figure out if you want your indexes to be inclusive or not. For me writing documentation as I go helps me keep these ideas straight.
I'm still looking for other problems like the duplication.
Update: I'm going to just post these as I figure them out, as a sort of stream-of-consciousness of how I look for problems. One thing I was taught early on is that adding print statements to your code can be faster than using a debugger. So (after re-formatting your code, because the formatting you posted was frankly crap) I added this:
public static void qSort( int[] A, int low, int high ) {
System.out.println( Arrays.toString( A ) + " low=" + low + " high=" + high );
And got this output:
run:
[5, 2, 10, 7, 6, 0, 8, 1, 9] low=0 high=8
[2, 0, 1, 5, 6, 5, 8, 10, 9] low=0 high=3
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=0 high=2
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=0 high=0
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=1 high=2
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=1 high=1
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=2 high=2
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=3 high=3
[0, 1, 2, 5, 6, 5, 8, 10, 9] low=4 high=8
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=4 high=5
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=4 high=4
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=5 high=5
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=6 high=8
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=6 high=6
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=7 high=8
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=7 high=7
[0, 1, 2, 5, 5, 6, 8, 10, 9] low=8 high=8
[0, 1, 2, 5, 5, 6, 8, 10, 9]
BUILD SUCCESSFUL (total time: 0 seconds)
I did have to stare at the output for a while, but what I noticed next was two things. The 5 is duplicated almost immediately, and the 5 is also the first element, which means it's going to be chosen by your partition code as the pivot. So it looks as though you have problems re-merging the pivot with the array.
Update 2: OK found another problem. This one I found by writing your array out by hand and walking through each value of i and leftwall as it makes the first partition. Problem here:
When the pivot, 5, encounters the element 0 in the array, the rest of the array has not been sorted. The 10, 7, 6, etc. are not less than the pivot and have not be touched. So when you make the swap, you sawp this:
[2, 5, 10, 7, 6, 0, 8, 1, 9]
for this:
[2, 5, 0, 7, 6, 5, 8, 1, 9]
This is because leftwall was 1 (it had been swapped with the 2 but not any other number, so it only had been incremented once) and there's the duplication and losing numbers too. I'm going to stop there because you have some pretty big problems.
What you need to do in this case is swap the 10, not the pivot, with the 0. This is going to require one additional pointer at least. Quicksort algorithms need to find the lowest and highest in the array and have two loops inside the outer for loop. What you have here is a kind of weird recursive insert sort. You'll need to think a bit more how to do this, but two more loops, nest inside the first, will be required.

How could we split an array into two new ones which their weight differences are minimal? [duplicate]

This question already has answers here:
Minimum sum partition of an array
(3 answers)
Closed 2 years ago.
I am doing the following programming exercise: Stone pile. The statement is:
You are given pile of stones with different weights.
Your task is to divide this stone pile into two parts, so the weight
difference between the piles will be minimal. You can't break one
stone into smaller ones.
For example if we have stones:
[1, 2, 3, 4]
We could divide them into:
[2, 3], [1, 4]
So the difference will be 0
If you are given empty stone pile, you can still divide it into two
piles with no stones.
The pile will contain maximum 22 stones. 🗿
Following the examples I have thought:
Sort stones in ascending order
for each stone
if length is even
if movement is even
add min and max to right list
remove them
if movement is odd
add min and max to left list
remove them
if length is odd
if movement is even
add max to left
remove it
if movement is odd
add min to right
remove it
if there are more items
add next min
remove it
So in code would be:
import java.util.*;
import java.util.stream.*;
public class Pile {
public static int minDiff(int[] stones) {
System.out.println("stones: "+Arrays.toString(stones));
Arrays.sort(stones);
System.out.println("stones: "+Arrays.toString(stones));
List<Integer> allStones = Arrays.stream(stones).boxed().collect(Collectors.toList());
System.out.println("allStones: "+Arrays.toString(allStones.toArray()));
ArrayList<Integer> left = new ArrayList<>();
ArrayList<Integer> right = new ArrayList<>();
for(int i = 0; allStones.size()>0; i++){
if(stones.length%2==0){
if(i%2==0){
right.add(allStones.get(0));
right.add(allStones.get(allStones.size()-1));
allStones.remove(0);
allStones.remove(allStones.size()-1);
}else{
left.add(allStones.get(0));
left.add(allStones.get(allStones.size()-1));
allStones.remove(0);
allStones.remove(allStones.size()-1);
}
}else{
if(i%2==0){
left.add(allStones.get(allStones.size()-1));
allStones.remove(allStones.size()-1);
}else{
right.add(allStones.get(0));
allStones.remove(0);
if(allStones.size()>0){
right.add(allStones.get(0));
allStones.remove(0);
}
}
}
}
System.out.println("left: "+Arrays.toString(left.toArray()));
System.out.println("right: "+Arrays.toString(right.toArray()));
return left.stream().mapToInt(Integer::intValue).sum()-right.stream().mapToInt(Integer::intValue).sum();
}
}
Currently it is just working when we test it with inputs where the difference is zero. However if we test it with other inputs, it will fail.
Given the following tests, the first suite will pass, the others fail:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class PersonTest {
#Test
public void testDifferenceShouldBeZero(){
assertEquals(0, Pile.minDiff(new int[]{1, 2, 3}));
assertEquals(0, Pile.minDiff(new int[]{1, 2, 3, 4}));
assertEquals(0, Pile.minDiff(new int[]{5,5,4,3,3}));
}
#Test
public void testDifferenceShouldBeOne(){
assertEquals(1, Pile.minDiff(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}));
}
#Test
public void testDifferenceShouldBeTwo(){
assertEquals(2, Pile.minDiff(new int[]{89409, 34351, 3065, 10128, 27694, 27585, 87350, 33875, 2658, 41606, 57512, 73368, 83345, 37048, 31827, 94644, 15972, 74813, 31441, 70395}));
}
}
For example, when stones are: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
Trace is:
stones: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
stones: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
allStones: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
left: [2, 21, 4, 19, 6, 17, 8, 15, 10, 13]
right: [1, 22, 3, 20, 5, 18, 7, 16, 9, 14, 11, 12]
Being the result:
expected:<1> but was:<-23>
How could we split the stone pile in two chunks where the difference is the minimum, for the general case?
We have read:
Make copy of an array
How to convert int[] into List<Integer> in Java?
How to get the last value of an ArrayList
Is there possibility of sum of ArrayList without looping
I would use bit combination to divide the stones into two groups and sum up every possible combination, comparing them to choose the one with lowest difference:
import java.util.Arrays;
import java.util.LinkedList;
public class Pile {
/**
* return solution as int, if bit is set use the "bitNumber" as index for stone
* for one side, otherwise for the other
**/
private static int divide(int[] stones) {
int solution = -1;
long minDifference = Long.MAX_VALUE;
for (int n = 0; n < (1 << (stones.length + 1)); n++) {
long sumLeft = 0;
long sumRight = 0;
for (int bit = 0; bit < stones.length; bit++) {
boolean isSet = (n & (1 << bit)) > 0;
if (isSet) {
sumLeft += stones[bit];
} else {
sumRight += stones[bit];
}
}
long diff = Math.abs(sumLeft - sumRight);
if (diff < minDifference) {
solution = n;
minDifference = diff;
}
}
return solution;
}
public static long minDiff(int[] stones) {
int solution = divide(stones);
long sumLeft = 0;
long sumRight = 0;
LinkedList<Integer> left = new LinkedList<>();
LinkedList<Integer> right = new LinkedList<>();
for (int bit = 0; bit < stones.length; bit++) {
boolean isSet = (solution & (1 << bit)) > 0;
if (isSet) {
sumLeft += stones[bit];
left.add(stones[bit]);
} else {
sumRight += stones[bit];
right.add(stones[bit]);
}
}
System.out.println("left: " + Arrays.toString(left.toArray()));
System.out.println("right: " + Arrays.toString(right.toArray()));
return Math.abs(sumRight - sumLeft);
}
}

How to use binary search when the array has odd value?

I'm using binarySearch and it's worked well with arrays that has even index, but when the array has odd index (length) it's give wrong result.
What i'v done so fare:
public static int binarySearch(int[] list, int key) {
int low = 0;
int high = list.length - 1;
while (high >= low) {
int mid = (low + high) / 2;
if (key < list[mid])
high = mid - 1;
else if (key == list[mid])
return mid;
else
low = mid + 1;
}
return - 1;
}
Input:
int[] arr1 = {5, 6, 8, 9, 11, 12, 11, 50, 1, 3, 15, 121, 33, 16, 17, 18, 19};
int[] arr2 = {5, 6, 8, 9, 11, 12, 11, 50, 1, 3, 15, 121, 33, 16, 17, 18};
Case:
System.out.println(binarySearch(arr1, 12));
System.out.println(binarySearch(arr2, 12));
OutPut:
-1
5
How i can get the right outPut in the both situation?
Binary search only works on sorted array
Solution : add Arrays.sort(list)
public static int binarySearch(int[] list, int key) {
Arrays.sort(list);
int low = 0;
int high = list.length - 1;
while (high >= low) {
int mid = (low + high) / 2;
if (key < list[mid]) high = mid - 1;
else if (key == list[mid]) return mid;
else low = mid + 1;
} return - 1;
}
You must sort the arrays before doing binary search operation.
In your question you are unable to search for the odd array length.
In java its very easy.
You can use the below code.
import java.util.Arrays;
class BS
{
public static void main(String args[])
{
int[] arr1 = {5, 6, 8, 9, 11, 12, 11, 50, 1, 3, 15, 121, 33, 16, 17, 18, 19};
System.out.println(Arrays.binarySearch(arr1 , '12'));
}
}

Rotate array by arbitrary step size without creating second array

So for a step size of 1, I want the array:
{1, 2, 3, 4}
To become:
{4, 1, 2, 3}
And for a step of size 2 the result will be:
{3, 4, 1, 2}
This is the code I'm using now:
private static int[] shiftArray(int[] array, int stepSize) {
if (stepSize == 0)
return array;
int shiftStep = (stepSize > array.length ? stepSize % array.length : stepSize);
int[] array2 = new int[array.length];
boolean safe = false;
for (int i = 0; i < array.length; i++) {
if (safe) {
array2[i] = array[i - shiftStep];
}
else {
array2[i] = array[array.length - shiftStep + i];
safe = (i+1) - shiftStep >= 0;
}
}
return array2;
}
The code is working great, but is it possible to achieve this without creating a helper array (which is array2 in the code above)?
Thanks!
You can do it without creating as big an array:
// void return type as it shifts in-place
private static void shiftArray(int[] array, int stepSize) {
// TODO: Cope with negative step sizes etc
int[] tmp = new int[stepSize];
System.arraycopy(array, array.length - stepSize, tmp, 0, stepSize);
System.arraycopy(array, 0, array, stepSize, array.Length - stepSize);
System.arraycopy(tmp, 0, array, 0, stepSize);
}
So for a 100,000 array and a step size of 10, it creates a 10-element array, copies the last 10 elements into it, copies the first 999,990 elements to be later, then copies from the temporary array back to the start of the array.
Use not the i++, but i += shiftSize and several loops (amount of them would be equal to gcd of array.length and shifSize).
Then you'll need only one int as buffer and execution time will be almost the same.
You could do it with a couple of loops, but its not easy. Using recursion is simpler in this case.
public static void main(String... args) {
for (int i = 0; i < 12; i++) {
int[] ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
rotateLeft(ints, i);
System.out.println(Arrays.toString(ints));
}
}
public static void rotateLeft(int[] array, int num) {
rotateLeft(array, num, 0);
}
private static void rotateLeft(int[] array, int num, int index) {
if (index >= array.length) return;
int tmp = array[(index + num) % array.length];
rotateLeft(array, num, index + 1);
array[index] = tmp;
}
prints
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1]
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3]
[5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4]
[6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5]
[7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
[8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7]
[9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8]
[10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Yes it's possible, you'd only need to temporary store one element additional to the array.
Basically what you want to do is to:
store last element in tmp var
shift all elements to the right by one starting with the second to last element
sotre tmp var as first element
repeat from step 1 depending on your stepsize
This is not tested ...
public void rotateByStep(int[] array, int step) {
step = step % array.length;
if (step == 0) {
return;
}
int pos = step;
int tmp = array[0];
boolean inc = array.length % step == 0;
for (int i = 0; i < array.length; i++) {
int tmp2 = array[pos];
array[pos] = tmp;
tmp = tmp2;
pos = (pos + step) % array.length;
if (inc && pos < step) {
array[pos] = tmp;
pos++;
tmp = array[pos];
}
}
}
The idea I'm trying to implement is as follows:
If step isn't a factor of length, then incrementing an index (pos) by step modulo length starting from zero will visit every array element once after length iterations.
If step is a factor of length, then index (incremented as above) will get back to its starting point after length / step iterations. But if you then increment by one, you can process the cycle starting at 1, and then at 2, and so on. After length iterations, we'll have visited every array element once.
The rest is just rippling the element values as we cycle through the element indexes ... with some adjustment when we increment to the next cycle.
The other complete solutions have the advantage that they are much easier to understand, but this one requires no extra heap storage (i.e. no temporary array), and does the job in array.length loop iterations.
In n- 1 iterations
#include <stdio.h>
int main(int argc, char **argv) {
int k = 0, x = 0;
int a[] = {-5,-4,-1,0,1,2,30,43,52,68,700,800,9999};
int N = 0, R = 57; /*R = No of rotations*/
int temp = 0, temp2 = 0, start = 0, iter = 0;
x = 0;
temp2 = a[x];
N = sizeof(a) / sizeof(a[0]);
for ( k = 0; k < N - 1; k++) {
x = x + R;
while ( x >= N ) {
x = x - N;
}
temp = a[x];
a[x] = temp2;
temp2 = temp;
if ( x == start ) {
start = start + 1;
x = x + 1;
temp2 = a[x];
}
iter++;
}
a[start] = temp2;
for ( k = 0; k < N; k++) {
printf(" %d", a[k]);
}
printf("\n");
printf("Done in %d iteration\n", iter);
return 0;
}

Categories