Finding the Largest increase in an array filled with random numbers

user1010101

For instance you are given an array [3,5,8,10,6,12,4]. You are to find the largest possible increase between two pairs of element i and j where j > i.

In the above case the answer would return 9 -> 12 - 3 = 9.

So i though of the obvious solution which is O(N^2). Here is my code

public class Max {

    public static void main(String[] args) {

        int[] array = {3,5,8,10,6,12,4};

        System.out.println(getMax(array));


    }


    public static int getMax(int [] arr)
    {
        int maxVal = 0;

        for(int i = arr.length-1; i>0; i--)
        {
            for(int j = 0; j<i; j++)
            {
                if(arr[i]-arr[j] > maxVal)
                {

                    maxVal = arr[i] - arr[j];
                }
            }

        }

        return maxVal;
    }

}

However i was wondering if it is possible to improve the solution to O(NlogN) because what if we use divide and conquer approach? Can someone guide me ?

UPDATE I simply can't find the max and min because the index of j has to be greater than index i. If i simply just look for the max and min value then i might get a case where index i is greater than index j and that is not allowed.

IVlad

Keep the minimum so far and update accordingly:

min = array[0]
max_diff = 0
for each element e, starting from the second:
  if e - min > max_diff:
    max_diff = e - min
  if e < min:
    min = e

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

Finding the 3 largest numbers in an array/list of n numbers without the sorting

From Dev

Efficient way of finding the 5 largest numbers in an array at any given interval

From Dev

Finding the largest time in an array

From Dev

Returning array of largest numbers

From Dev

Finding the nth largest in an int array

From Dev

Finding Largest Values in Array of Arrays

From Dev

Finding the largest number in a column in array

From Dev

Finding the largest group of consecutive numbers within a partition

From Dev

Having trouble finding the largest numbers in a subarray

From Dev

Numpy array filled with random numbers so that you only change the value by one along the x/y axis

From Dev

Creating a new column filled with random numbers

From Dev

Finding specific numbers in an array?

From Dev

finding the sum of numbers in an array

From Dev

Extract largest numbers from an array

From Dev

Find largest and smallest numbers in an array

From Dev

Finding Largest Element in an Array using JavaScript

From Dev

Java, Finding Kth largest value from the array

From Dev

C finding second largest number in an array

From Dev

Finding largest element in a two dimensional array

From Dev

Finding the third largest even fraction in a given array

From Dev

(Java ) Finding the largest number in an array and it's location

From Dev

C++ finding the (largest) index of the largest element in an array

From Dev

Difference between these 2 ways of finding Random Numbers?

From Java

Create an Array with random numbers

From Dev

Fill an array with random numbers

From Dev

Generate random numbers with Array

From Dev

Finding the primary numbers in an array of pointer

Related Related

  1. 1

    Java - Finding Largest and Smallest Numbers using an Array

  2. 2

    Java - Finding Largest and Smallest Numbers using an Array

  3. 3

    Finding the 3 largest numbers in an array/list of n numbers without the sorting

  4. 4

    Efficient way of finding the 5 largest numbers in an array at any given interval

  5. 5

    Finding the largest time in an array

  6. 6

    Returning array of largest numbers

  7. 7

    Finding the nth largest in an int array

  8. 8

    Finding Largest Values in Array of Arrays

  9. 9

    Finding the largest number in a column in array

  10. 10

    Finding the largest group of consecutive numbers within a partition

  11. 11

    Having trouble finding the largest numbers in a subarray

  12. 12

    Numpy array filled with random numbers so that you only change the value by one along the x/y axis

  13. 13

    Creating a new column filled with random numbers

  14. 14

    Finding specific numbers in an array?

  15. 15

    finding the sum of numbers in an array

  16. 16

    Extract largest numbers from an array

  17. 17

    Find largest and smallest numbers in an array

  18. 18

    Finding Largest Element in an Array using JavaScript

  19. 19

    Java, Finding Kth largest value from the array

  20. 20

    C finding second largest number in an array

  21. 21

    Finding largest element in a two dimensional array

  22. 22

    Finding the third largest even fraction in a given array

  23. 23

    (Java ) Finding the largest number in an array and it's location

  24. 24

    C++ finding the (largest) index of the largest element in an array

  25. 25

    Difference between these 2 ways of finding Random Numbers?

  26. 26

    Create an Array with random numbers

  27. 27

    Fill an array with random numbers

  28. 28

    Generate random numbers with Array

  29. 29

    Finding the primary numbers in an array of pointer

HotTag

Archive