How to find next adjacent bigger number from given list of numbers for an input number

Viewed 126

I am trying to find next adjacent bigger number for an input number from given list of numbers.

Please help me with Java code or algorithm to implement below.

Given list of numbers = [50, 20, 40, 30, 10]

Input number = 15

Output = 20 (next adjacent bigger number of 15)

5 Answers

You could iterate through a sorted array and find out which is adjacent and bigger.

    //Add the imports BEFORE the "class{"
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    private static int check_bigger_adjacent(final int input_number){
      final Integer[] array_of_numbers={50, 20, 40, 30, 10};
      //Sort the array.
      final List<Integer> sorted_list=Arrays.asList(array_of_numbers);
      Collections.sort(sorted_list);
      final int[] new_sorted_array_of_numbers=new int[sorted_list.size()];
      for(int i=0;i<sorted_list.size();i++){
        new_sorted_array_of_numbers[i]=Integer.parseInt(sorted_list.get(i).toString());
      }
      for(int i:new_sorted_array_of_numbers){
        if(i>input_number)
          return i;
      }
      // If the number is way too big for your included array.
      return -1;
    }

I think the fastest approach will be a solution of Time Complexity of O(n) by iterating through the array just one time like below. All other approaches where you will be using some form of sorting would atleast increase the time complexity by a minimum of O(nlogn). You may need to add a boolean check in the below solution if you suspect that Integer.MAX_VALUE also can be part of the array.

   public static void main(String args[]) {
      int[] arrNums= {50,20,40,30,10};
      
     System.out.println(findNextBigNum(arrNums,15));
    }

    private static int findNextBigNum(int[] arrNums, int num) {
        // TODO Auto-generated method stub
        
        int nextBiggestNum=Integer.MAX_VALUE;
        for(int i=0;i<arrNums.length;i++)
        {
            if(arrNums[i]>num && arrNums[i] <nextBiggestNum)
                nextBiggestNum=arrNums[i];
        }
        
        return nextBiggestNum==Integer.MAX_VALUE?-1:nextBiggestNum;
    }

This Pine code will do what you're looking for

//@version=5
indicator("test")

var int     num = input.int(15, "Number")

var         a = array.from(50, 20, 40, 30, 10)
var label   lbl = label.new(na, na, "")
var int     adjacent = na

if barstate.isfirst
    array.sort(a)
    idx = array.binary_search_rightmost(a, num)
    adjacent := array.get(a, idx)

if barstate.islast
    label.set_xy(lbl, bar_index, high)
    label.set_text(lbl, str.format("adjacent = {0}",adjacent))

The simplest approach is like filtering all the greatest numbers and then finding the smallest among them.

import java.util.*;

public class Main{
public static void main(String[] args) {
    int[] numbers = {50, 20, 40, 30, 10};
    int target = 9;
    
    System.out.println("Adjacent Greatest number of " + target + " is " + findAdjacentGreatest(numbers,target));
}

// METHOD TO FIND THE ADJACENT GREATEST NUMBER
public static int findAdjacentGreatest(int[] numbers, int target) {
   // USED ARRAY LIST BECAUSE WE DONT KNOW THE CORRECT SIZE OF THE ARRAY TO INITIALISE THE ARRAY
    ArrayList<Integer> greaterNumbers = new ArrayList<Integer>();
    
    for (int i=0; i<numbers.length;i++) {
        if(numbers[i] > target) {
            greaterNumbers.add(numbers[i]);
        }
    }
    
    // RETURN THE MINIMUM OF THE GREATEST NUMBERS
    return Collections.min(greaterNumbers);
    }    
}

live code here

Algorithm, if List is small and you don't care about time:

  • sort the list in increasing order, so smallest number first
  • scan through the list starting from the beginning
  • compare input with current value of list. If value is greater than input, you found the first "adjacent" number

Algorithm, if List can be huge and you care about time:

  • create a variable (VAR) with a value of nil (or anything that cannot be read from the list, 0, 9999, "HOGEFUGE", whatever)
  • scan through the list starting from the beginning
  • compare input with value. If value is greater than input then:
    • check if VAR has been set
      • if not, set it to value
      • if set, check if value is less than VAR's value
        • if less, put value into VAR
        • if greater then leave VAR as is
    • iterate through the list, get next value

At the end, you will get a number in VAR, that is greater than your input and has the smallest difference between input and value for all values in your list.

Related