Most efficient way to find the smallest index where its value minus the value of a previous index is smaller than a given x?

user1760791

I have five long integers p, q, s, m and x. An array numbers[] is created by the following formula.

numbers[0] = s;
for(int i=1; i<numbers.Length;i++){
    numbers[i] = (p * numbers[i-1] + q) % m;
}

The first value of numbers (numbers[0]) is s.

What is the most efficient way to find index j where i < j and |numbers[j] - numbers[i]| <= x or |numbers[j] - numbers[i]| >= m-x.

For instance, in a case where p = 3, q= 7, s= 1, m= 29 en x= 1 the array will be:

numbers[0] = 1, numbers[1] = 10, numbers[2] = 8 and numbers[3] = 2.

In this case index j would be 3, because numbers[3] - numbers[0]<=x, because x is 1.

I thought about using something such as a variant of counting sort or radix sort but I can't get anything to work.

Gabriel Rainha

As i < j, then you need to grant that numbers has a length of at least 2.

You could do two nested loops, the outer one ranging from j = 1 to numbers.Length - 1 (granting the possible solution to be the smallest j) to i = 0 to i < j.

Then you compare both positions according your specs. If true, return j. If it finishes both loops, then there is no solution.

Edit: Code Sample

public int GetSmallestIndex(long[] numbers, long x, long m)
{
    if (numbers.Length >= 2)
    {
        for (int j = 1; j < numbers.Length; j++)
        {
            for (int i = 0; i < j; i++)
            {
                long diff = Math.Abs(numbers[j] - numbers[i]); 
                if (diff <= x || diff >= m - x)
                    return j;
            }
        }
    }

    return -1; //If no solution is found, return -1 as convention
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Most efficient way to find the smallest index where its value minus the value of a previous index is smaller than a given x?

From Dev

Most efficient way to insert an element into sorted array and find its index

From Dev

How do I find the largest value smaller than or equal to X and the smallest value greater than or equal to X?

From Dev

Pandas: Efficient way to get first row with element that is smaller than a given value

From Dev

Most efficient way to find the key of the smallest value in a dictionary from a subset of the dictionary

From Dev

What is the most efficient way to find the index of an element in a list, given only an element of a sublist (Python)

From Dev

Find array index if given value

From Dev

Find the closest index to given value

From Dev

Find column index where row value is greater than zero in R

From Dev

R delete rows in data frame where nrow of index is smaller than certain value

From Dev

find the index of the first value in x that is greater than 0.999

From Dev

Find steps to find given value of index

From Dev

Most efficient way to change a string field value to its substring

From Dev

Excel - find smallest difference between value and list, and associated index

From Dev

Return value larger than X with INDEX(MATCH)

From Dev

In a dataframe, find the index of the next smaller value for each element of a column

From Dev

How to find a value in a sorted C++ vector in the most efficient way?

From Dev

Efficient way to find index of interval

From Dev

Find index where elements change value numpy

From Dev

Find index of list entries where value is in between

From Dev

Maximal subset sum smaller than a given value

From Dev

TeeChart Get Series Y value or index by given X value

From Dev

TeeChart Get Series Y value or index by given X value

From Dev

return closest item to a given value in a list and its index

From Dev

most pythonic way to use a value from a list as an index to another list

From Dev

most pythonic way to use a value from a list as an index to another list

From Dev

How to find if nth value is smaller than the previous n-1 values

From Dev

In Perl, how can find the smallest value in a list and keeping the index corresponding to that value?

From Dev

Numpy: get index of smallest value based on conditions

Related Related

  1. 1

    Most efficient way to find the smallest index where its value minus the value of a previous index is smaller than a given x?

  2. 2

    Most efficient way to insert an element into sorted array and find its index

  3. 3

    How do I find the largest value smaller than or equal to X and the smallest value greater than or equal to X?

  4. 4

    Pandas: Efficient way to get first row with element that is smaller than a given value

  5. 5

    Most efficient way to find the key of the smallest value in a dictionary from a subset of the dictionary

  6. 6

    What is the most efficient way to find the index of an element in a list, given only an element of a sublist (Python)

  7. 7

    Find array index if given value

  8. 8

    Find the closest index to given value

  9. 9

    Find column index where row value is greater than zero in R

  10. 10

    R delete rows in data frame where nrow of index is smaller than certain value

  11. 11

    find the index of the first value in x that is greater than 0.999

  12. 12

    Find steps to find given value of index

  13. 13

    Most efficient way to change a string field value to its substring

  14. 14

    Excel - find smallest difference between value and list, and associated index

  15. 15

    Return value larger than X with INDEX(MATCH)

  16. 16

    In a dataframe, find the index of the next smaller value for each element of a column

  17. 17

    How to find a value in a sorted C++ vector in the most efficient way?

  18. 18

    Efficient way to find index of interval

  19. 19

    Find index where elements change value numpy

  20. 20

    Find index of list entries where value is in between

  21. 21

    Maximal subset sum smaller than a given value

  22. 22

    TeeChart Get Series Y value or index by given X value

  23. 23

    TeeChart Get Series Y value or index by given X value

  24. 24

    return closest item to a given value in a list and its index

  25. 25

    most pythonic way to use a value from a list as an index to another list

  26. 26

    most pythonic way to use a value from a list as an index to another list

  27. 27

    How to find if nth value is smaller than the previous n-1 values

  28. 28

    In Perl, how can find the smallest value in a list and keeping the index corresponding to that value?

  29. 29

    Numpy: get index of smallest value based on conditions

HotTag

Archive