Finding immediate smaller than X

Question:

So, we have been provided with a list and a value X. In the given list we have to find the immediate smaller value than X. Here is my code. Where am I doing it wrong? I am also attaching the provided test cases and the test case which I am not able to pass.

Sample input and output

I was able to pass the given sample test case but not the hidden one. Whenever I modify my code, it leaves one or another test case. I can see the solution but I want to correct my code.

And here is my code:

    z=min(arr)
    if (z<x):
        curr=arr[0]
        glob=z
        for i in range(0,n):
            curr=arr[i]
            if glob>curr:
                if curr<x:
                    glob=curr
                """elif glob >curr:
                    continue"""
            if(glob<curr):
                if curr<x:
                    glob=curr
            else:
                continue
        return glob  
    else:
        return -1

Test case I am not able to pass:

Hidden test case

Asked By: rsharma7

||

Answers:

You can try something like that:

arr = [4, 67, 13, 12, 15]
X = 16

smaller = -1

for i in arr:
    if 0 < X - i < X - smaller:
        smaller = i

print(smaller)  # or return if it's a function

Output:

15

And for arr = [1, 2, 3, 4, 5] and X = 1 it prints -1.

Answered By: Mushif Ali Nawaz

I have a more optimum solution, instead of using space for temporary element.Sort the array then , just traverse the array from right to left , check the first element smaller than x and just return it.

Even I am doing the same course from Geeksforgeeks as you are
Catchup

Answered By: Harsh Zota
  public static int immediateSmaller(int arr[], int n, int x){
    // Your code here
    int ele = -1;
    int diff = Integer.MAX_VALUE;

    for (int i = 0; i < n; i++)
        if (arr[i] < x && (x - arr[i]) < diff){
                ele = arr[i];
                diff = x - arr[i];
        }
        
    return ele;
}

java code

Answered By: user7585749

You can try this

def immediateSmaller(arr,n,x):

l = []
for no in arr:
    if no < x:
        l.append(no)
if l == []:
    return -1
return max(l)
Answered By: manjunath b v

Here what i have done is took a variable smaller and initialized it and iterated through the loop to check if there exists any elements in the array that is greater than smaller and also less than x and last returned it ,value of smaller is initialized to -1 as if no matching elements found we’ll return -1 to satisfy our condition.

int immediateSmaller(int arr[], int n, int x)
    {
        // your code here
        int smaller=-1;
        for(int i=0;i<n;i++)
        {
            if(arr[i]>smaller && arr[i]<x)
            smaller=arr[i];
            
        }return smaller;
    }
};
Answered By: 061_SWAPAN GHOSH
largest_smallest = -1
for i in arr: 
  if i < x: 
     if i > largest_smallest: 
        largest_smallest = i 
        
     
Answered By: dougofi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.