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.
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:
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
.
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
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
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)
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;
}
};
largest_smallest = -1
for i in arr:
if i < x:
if i > largest_smallest:
largest_smallest = i
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.
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:
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
.
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
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
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)
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;
}
};
largest_smallest = -1
for i in arr:
if i < x:
if i > largest_smallest:
largest_smallest = i