DEV Community

Java Daily Coding Problem #004

Andrew (he/him) on May 10, 2019

Daily Coding Problem is a website which will send you a programming challenge to your inbox every day. I want to show beginners how to solve some o...
Collapse
 
lsurajpatill profile image
l-Suraj-Patil-l • Edited

I tried using minimum and maximum value to get the solution.
public static int minValue(int[] i)
{
int minValue=i[0];
for(int x : i)
if(x < minValue)
minValue = x;
return minValue;
}
public static int maxValue(int[] i)
{
int maxValue=i[0];
for(int x : i)
if(x > maxValue)
maxValue = x;
return maxValue;
}
public static int findSmallestMissingValue(int[] x)
{
int missingValue = 0;
for(int i = minValue(x); i <= maxValue(x); i ++)
{
boolean flag = false;
for(int j = 0; j < x.length ; j ++)
{
if(x[j]==i)
{
flag = true;
break;
}
}
if(!flag)
{
missingValue = i;
break;
}
}
return missingValue;
}

Collapse
 
awwsmm profile image
Andrew (he/him)

What are the time and space complexities of your solution?

Collapse
 
anthony_breeganzo profile image
Anthony Breeganzo

We can solve this problem in a simple way but time is O(N log N)
We just need to sort the array and start checking from 1 if there is any number missing.
public static int fmp(int[] a)
{
int n=a.length;
Arrays.sort(a);
int ans=1;
for(int i=0;i<n;i++)
{
if(a[i]==ans)
ans++;
}
return ans;
}