Java firstPosition Algorithm - java

int firstPosition(int x, int [] a) {
int lower = 0;
int upper = a.length;
while (lower != upper) {
int mid = (lower + upper) / 2;
**if (x <= a[mid]) {** // the line I don't understand
upper = mid;
} else {
lower = mid + 1;
}
return (lower);
}
If a = {4, 4, 5, 5, 6, 7, 8, 9, 9, 9} what will the algorithm return for the following choices of x?
i) x = 3
ii) x = 4
iii) x = 5
iv) x = 9
v) x = 11
I have tried stepping through this program, for example x = 3, a.length returns 10, so upper is always equal to 10.
while ( 3 ! = 0 ) { // execute line
int mid = lower + upper / 2 - which is (0 + 10)/2 = 5
if ( x <= a[mid]) // I assume that means if 3 is less than or equal to 5? 5 then replace mid with 5 and then...
lower = mid + 1 // 5+1 = 6, return 6 as lower?

This is a basic binary search algorithm, implemented iteratively instead of recursively.
The line you don't understand checks to see if x (the search value) might lie in the lower half or upper half of the array. This works because the array is sorted. We can divide any sorted array into two halves, and look at the value in the middle to determine which half the value we're looking for might be in.
Say that the array looks like this:
+---+---+---+---+---+----+
| 1 | 3 | 5 | 7 | 9 | 11 |
+---+---+---+---+---+----+
^ ^
| |
lower upper
and we're trying to figure out which slot the number 9 is in. Since the array is sorted, we can immediately discard half of the array.
How?
Look at the value in the "center" of the array: it's 5, and 9 is larger than 5, so we know that 9 must be in the upper half of the array. Algorithmically speaking, this would be the else case of the if statement in your code.
So we repeat the same process, but only looking at the upper half of the array this time:
+---+---+---+---+---+----+
| 1 | 3 | 5 | 7 | 9 | 11 |
+---+---+---+---+---+----+
^ ^
| |
lower upper

Looks to me like a binary search algorithm. The choice of x makes sure that the array part that needs to be searched is halved each iteration. Read more about it here

This is a modification of a binary search.
Since the data is ordered, to determine if you can throw away the "upper" or "lower" half of a range of data, look at the middle element. When it is bigger than the number you're looking for, you can safely throw away the numbers past it (by reducing the range). When it is smaller than the number you are looking for, you can safely throw away the numbers before it (by reducing the range).
The key here is that if it is the number you are looking for, you need to crawl back towards the beginning of the range until you detect that the "next" number is not actually the "first" of that possibly repeated value.

Related

Efficient Data structure and Algorithm - Natural Sequence

1 - 1
2 - 2,3
3 - 4,5,6
4 - 7,8,9,10
Given any number from 4 to 6 ,I need the output as 3.
Given any number from 7 to 10 ,I need the output as 4.
I need the fastest solution for the above problem to solve an algorithm.
What I could think of is a brute force algorithm :
Given 7:
n-square + n = 7*2 = 14
1 + 1 = 2 < 14
4 + 2 = 6 < 14
9 + 3 = 12 < 14
16+ 4 = 20 >=14 --> So 4
Is there any better approach to arrive at the solution ? OR My approach to the algorithm itself is flawed ?
Brief explanation of the algo :
A,B,C
After every iteration every element becomes increased by one.
A,A,B,B,C,C
Given 3, C will be returned.
Given 4 or 5, A will be returned.
Given 6 or 7, B will be returned.
Given 8 or 9, C will be returned.
Given 10 or 11 or 12, A will be returned.
Given 13 or 14 or 15, B will be returned.
How the solution to the mathematical problem will help solve the algo :
Total number of elements = 3
Given number = 13 (Output to be B)
Divide and Ceiling = Ceil (13/3) = 5 [So 13 falls under when every element has become * 3] (From Mathematical problem : If given number is 5, 3 is to be used)
Starting index of when every element has become * 3 [IS_EQUAL_TO = ] 3 * 3(summation of previous iteration => 1 + 2) + 1 = 10
To Find the index = Ceil(13-10+1/3 (this 3,comes from the mathematical problem) ) = Ceil (4/3) = 2nd index = B
Given number of rows N, the size of the triangle is N(N+1)/2. You are essentially trying to find the least integer N such that N(N+1)/2 >= M, with M given. If you have a function to compute square roots, you can solve this equation in constant time.
N(N+1)/2 >= M, multiply both sides with 2,
N²+N >= 2M, complete the square, take the square root, blablabla
N >= sqrt(2M+1/4)-1/2
Therefore the answer is N = ceil(sqrt(2*M + .25) - .5)

Find last int (1-9) stored in long of bits (each int represented by 4 bits)

I am working on a Tic-Tac-Toe AI, and want to find the last move (Opponents last move before current turn) using a long provided by the Game Engine.
Each space is represented by a single digit integer 1-9 (which i will subtract 1 from to get moves 0-8, plus 9 for off-board moves which are stored in the long as 0xF).
0xE is used to represent NULL, but will be treated by my program the same as an off-board move.
Here is how the game state is encoded:
Used to encode game State, first 4 bits are first move, second 4 bits second move, (4 * 9 = 36 bits) bits 33-36 are the last Move. Each move is the coordinate singleton + 1, therefore the tictactoe board is recorded as...
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Normal equation for singleton is row*3+col, but you cannot record a state as 0, therefore game state moves are row*3+col + 1, note difference Coordinate singleton is 0..8, board game state position is 1..9;
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
The game state 0x159, X first move 9; O move 2 is 5;move 3 X is 1
X _ _
_ O _
_ _ 9
Sets off board set all 4 bits (aka 0xf).
e.g., 0x12f45, On X's second move (game move 3)
X picked a Coordinate outside the tictactoe range.
Duplicate guesses onto occupied square are just saved
e.g., 0x121 implies X has used position 1 on both his
first and second move
Null coordinate usually caused by exception is saved as 0xE
e.g., 0x1E3; implies on game move 2, O first move, O throw an exception
most likely causes index array out of bounds
As of now, here is how I am finding the last move using the engine's game state:
private int LastMoveFinder(final Board brd, int move)
{
char prevMove = Long.toHexString(brd.getGameState()).charAt(0);
if(prevMove == 'f' || prevMove == 'e')
return 9;
else
return Character.getNumericValue(prevMove) - 1;
}
But, I am sure there is a faster way (performance wise) to find the last move using some sort of bitshift method, as our AI's will be tested against each other for speed (nanoSec/move) and win-tie-loss ratio.
I have read up on bitshifting and searched all over stackoverflow for answers to questions like mine, but nothing I have tried to implement into my program has worked.
I am sure i'm missing something simple, but have not taken a course that covers bitshifting and masking yet, so I am at somewhat of a loss.
Thanks for your help.
You can get 4 bits of a int by AND'ing it with the bitmask 0xf shifted left by 4 * moveNumber bits. Then, shift the result right by 4 * moveNumber bits to get an int, and apply your move logic to that int. The modified method is:
/**
Assumes moveNumber is 0 indexed.
*/
private int LastMoveFinder(final Board brd, int moveNumber)
{
int moveMask = 0xf << (4 * moveNumber);
int prevMove = (brd.getGameState() & moveMask) >>> (4 * moveNumber);
if (prevMove == 0xf || prevMove == 0xe) {
return 9;
} else {
return prevMove - 1;
}
}

Partition 2d array in sub-arrays

I have to partition a 2d array (the size is given by the user) into sub-arrays given an input number by the user. The code i Wrote works well for most of the instances by there are some that I need some help with.
I do this by taking the square root of the input number. So for example:
If the user inserts [10, 10, 9] it means that this is a 10 * 10 array with 9 sub-arrays. Taking the square root of 9 works fine because it gives 3.
If the user inserts [8, 6, 6] it takes the square root of 6 and rounds it up for the longest side (which gives 3) and rounds it down for the shortest (which is 2). So 3 * 2 = 6. It also works fine.
Then there is a situation like 8. The square root of 8 gives 3 and 2. So the array is partitioned into 6 sub-arrays. Is there another way to find a better partitioning for numbers like 8, 14? Or is there a way to find the optimal distribution for such numbers (e.g. 2 * 4 = 8, 2 * 7 = 14)?
You can calculate them a bit different way:
int x = Math.round(Math.sqrt(n));
int y = Math.round(1. * n / x);
Thus you'll receive:
n = 8 => x = 3, y = 3
n = 14 => x = 4, y = 4
What you need to do is find the two nearest factors to the square root. Try this code:
long n = 14;
long y = 0;
long x = Math.round(Math.sqrt(n));
while(true){
if (n % x == 0) {
y = n/x;
break;
}
else {
x--;
}
}
You might also like to put in some error checking to cope with input errors. e.g. n<1.

Recursion with an Array

I'm having a bit of trouble understanding the concept of recursion. I understand that it is basically a method that calls itself and turns a big problem into a bunch of smaller parts to solve it. What I'm having difficulty with is using recursion with an array. Here is an example in my book:
//Precondition: x is an array of n integers
public int recur(int[] x, int n)
{
int t;
if(n == 1)
return x[0];
else
{
t = recur(x, n-1);
if(x[n-1] > t)
return x[n-1];
else
return t;
}
}
If anyone has the time, could you explain what this method does and how it works? Greatly appreciated!
This function returns the largest integer of an integer array.
Lets see how, Your function recur takes an integer array x and its length n.
If the length of array is 1 then the lone element x[0] is the largest one.
Else we get the largest element from the array starting with x[0] to x[n-2](that is array of length n - 1) and so on, when we get the largest element we keep on sending it as the return value till recursion finishes, finally returning the largest value.
This method finds the biggest number among the first n elements of an array.
It works by finding the biggest number among the first n-1 elements; then checking whether the nth element is bigger. The recursion comes in when it finds the biggest number in the first n-1 elements - it does that by calling itself with n-1 in place of n.
Of course, if n is 1, then there's nothing to check - we should just return the first element. This is the "base case" of the recursion.
Note that when I say the nth element, this is actually x[n-1], not x[n] because array indexes start from zero.
In recursion we have what is called base case which is a condition to make the recursion stop. In this situation the base case is if (n==1) where the first element of x[] is returned.
Let's go to the second part of the recursion. The function is calling itself but decrementing n, until it reachs to the base case. Once the base case is returned, the function will compare the first element, now t, with the next one x[n-1] (where n is equal to 2) and return the greater of both. When one value is returned the function goes to its previous call in the stack.
In other words, to analize recursion you should go through the function calls until the base case is reached and once there, start to go back leaded by the returns or the final execution of the function.
As stated in above answers this method will return largest among the first n values in the array, i would like to show this answer pictorially
Assume array with values
{5, 4, 2, 1, 8, 6, 4, 2, 12, 33}
-----------------------------------------------------------------------------------
caller | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1
===================================================================================
t | 12 | 8 | 8 | 8 | 8 | 5 | 5 | 5 | 5 | none
===================================================================================
return | 33 | 12 | 8 | 8 | 8 | 8 | 5 | 5 | 5 | 5
Here caller is the invoker of recursive method. and in the n = 10, 33 will be compared with 12 and 33 will be returned to invoker. Hence invoker will received largest value in the array.

check number present in a sequences

I am writing a program which I found on a coding competition website, I have sort of figured out how to solve the problem but, I am stuck on a math part of it, I am completely diluting the problem and showing what I need.
first I need to check if a number is part of a sequence, my sequence is 2*a+1 where a is the previous element in the sequence or 2^n-1 to get nth item in the sequence. so it is 1,3,7,15,31,63...
I don't really want to create the whole sequence and check if a number is present, but I am not sure what a quicker method to do this would be.
Second if I am given a number lets say 25, I want to figure out the next highest number in my sequence to this number. So for 25 it would be 31 and for 47 it would be 63, for 8 it would be 13.
How can i do these things without creating the whole sequence.
I have seen similar questions here with different sequences but I am still not sure how to solve this
Start by finding the explicit formula for any term in your sequence. I'm too lazy to write out a proof, so just add 1 to each term in your sequence:
1 + 1 = 2
3 + 1 = 4
7 + 1 = 8
15 + 1 = 16
31 + 1 = 32
63 + 1 = 64
...
You can clearly see that a_n = 2^n - 1.
To check if a particular number is in your sequence, assume that it is:
x = 2^n - 1
x + 1 = 2^n
From Wikipedia:
The binary representation of integers makes it possible to apply a
very fast test to determine whether a given positive integer x is a
power of two:
positive x is a power of two ⇔ (x & (x − 1)) equals to zero.
So to check, just do:
bool in_sequence(int n) {
return ((n + 1) & n) == 0;
}
As #Blender already pointed out your sequence is essentially 2^n - 1, you can use this trick if you use integer format to store it:
boolean inSequence(int value) {
for (int i = 0x7FFF; i != 0; i >>>= 1) {
if (value == i) {
return true;
}
}
return false;
}
Note that for every elements in your sequence, its binary representation will be lots of 0s and then lots of 1s.
For example, 7 in binary is 0000000000000000000000000000111 and 63 in binary is 0000000000000000000000000111111.
This solution starts from 01111111111111111111111111111111 and use an unsigned bitshift, then compare if it is equal to your value.
Nice and simple.
How to find the next higher number :
For example, we get 19 ( 10011 ) , should return 31 (11111)
int findNext(int n){
if(n == 0) return 1;
int ret = 2; // start from 10
while( (n>>1) > 0){ // end with 100000
ret<<1;
}
return ret-1;
}

Categories