3

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 Answers5

2

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;
    }
JumperBot_
  • 551
  • 1
  • 6
  • 19
  • @JumperBot_ Code does not work the way you want it. It works the way you wrote it. There are no surprise here anywhere. – karatedog Jul 04 '22 at 09:44
2

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;
    }
RohithPg
  • 101
  • 1
  • 3
  • This is a good idea concerning it would theoretically take less milliseconds to process the data even though less notable for short Arrays like presented by the OP, deserves an upvote! – JumperBot_ Jul 02 '22 at 14:14
  • @JumperBot_ Thank you. Yes you are right, it wont make any difference if the code just needs to handle short arrays. This will be useful only for larger arrays. Also one more point to note is that , there are is no additional space being utilised as we are not copying the data to another list or map. Again, this also matters only when dealing with larger arrays and less jvm memory – RohithPg Jul 02 '22 at 18:46
1

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))
Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42
  • 1
    It is funny how people on Stackoverflow bashes OPs that ask for a code (clearly not showing any effort) while the subset of Pine people are much kinder and provide code without any scolding :-) – karatedog Oct 11 '22 at 09:54
-1

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

Soorya J
  • 145
  • 6
-1

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.

karatedog
  • 2,508
  • 19
  • 29