java algorithm for search with different increment number

Multi tool use


java algorithm for search with different increment number
I have next data:
need create algorithm on java whet will return result dependent on input value
for example:
......
I do not know the data in advance, I know only algorithm in witch they populating
I have started with next code:
public int getAmountOfUnits(int duration ) {
if (duration >= 1 && duration <= 7) return 0;
if (duration >= 8 && duration <= 82) {}
return 1;
}
Can anyone help me?
'40' should return '3', right? By the way, if there's no mathematical correlation in that sequence, there's no real "algorithm" that you can use. You are forced to maintain a structure with those mappings, and return the correct value by checking that.
– Andrea ジーティーオー
1 hour ago
yes, but need help to translate it in java
– Orion
1 hour ago
"'40' should return '3', right?" yes, mistake, I will fix
– Orion
1 hour ago
Sorry to say this, but I speak in the name of the site: these requests are not intended for SO. You should have some code to share at least, in order that the community could address the issue with more precision. The community won't write code from scratch.
– Andrea ジーティーオー
1 hour ago
2 Answers
2
Here's a solution using Arrays.binarySearch
,
Arrays.binarySearch
static int indexInRanges(int rangeEndPoints, int value) {
int bsPos = Arrays.binarySearch(rangeEndPoints, value);
if (bsPos < 0)
return ~bsPos;
else
return bsPos;
}
// test code
public static void main (String args) throws java.lang.Exception
{
int rangeEndPoints = { 7, 22, 37, 52, 67, 82, 90, 114, 138, 162, 186, 210, 234, 258, 282, 306, 330, 354 };
System.out.println("1 -> " + indexInRanges(rangeEndPoints, 1));
System.out.println("40 -> " + indexInRanges(rangeEndPoints, 40));
System.out.println("350 -> " + indexInRanges(rangeEndPoints, 350));
}
This code does not attempt to handle more general cases, such as:
Alternatively you can write that if < 0
etc thing as return bsPos ^ (bsPos >> 31)
at the cost of making it less obvious what it does.
if < 0
return bsPos ^ (bsPos >> 31)
As a special case, since most of your ranges are the same size, you could hack something together with only a few if/else
cases and some divisions, but that hard-codes the list of ranges in a particularly hard-to-change way.
if/else
thx for help, but I cant create int rangeEndPoints ={} I dont know I do not know the data in advance
– Orion
52 mins ago
I know only algorithm in wich they populating
– Orion
51 mins ago
@Orion you can make that array when you get the data then
– harold
45 mins ago
There are several ways for doing that task.This is the one of simple algorithm.
import java.util.Scanner;
class search{
public static void main(String a){
Scanner sc=new Scanner(System.in);
System.out.print("Enter Value :");
int value=sc.nextInt();
System.out.print("Enter increment value :");
int increment=sc.nextInt();
int i;
int counter=0;int c=0;
int result={1,8,23,38,53,68,83,91,115,139,163,187,211,235,259,283,307,331};
for(i=0;i<result.length;i++){
counter++;
if(value<=(result[i]+increment)){
System.out.print("result is :" + (counter-1));
counter=0;
c++;
break;
}
}
if(c==0){
System.out.print("Not found!");
}
}
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It is not a question about an issue, but an assignment you ask. Anyway, you write the logic in english, just translate it in java and you have your answer
– wargre
1 hour ago