Yeah, swapping seems to be better, would keep the large digits in the lesser valuable positions.
I interpreted the rules this way, because it didn't seem right to lose a digit in the resulting number. That would be like assuming input set [1,2,0] as equivelent to [1,2].
vector<int> minValueHelper(vector<int> v){
set<int> s;
bool presentZero = false;
for(int i : v){
if(i==0)
presentZero = true;
s.insert(i);
}
if(presentZero){
// because 0 cannot be in front of any number
// minValue({1, 1, 7, 0}) should not return 017 but should return 107
vector<int> v(s.begin(),s.end());
// if only zero is given in vector return zero
// else swaping
if(v.size() > 1)
swap(v[0],v[1]);
return v;
}
return vector<int>(s.begin(),s.end());
}
int minValue(vector<int> v){
vector<int> minVector = minValueHelper(v);
string s = "";
for(int i : minVector)
s += to_string(i);
return stoi(s);
}
Web and Software Frontend Developer. Used to work in Full-stack JavaScript.Been at this for a few years now, and think I now understand my specific domain pretty well. Still a lot to learn though.
Location
Manchester, UK
Education
Web Systems Design BSc at Sheffield Hallam University, Sheffield, UK
this is solution in c . I have first sorted the array then solved it using simple logics . suggestions are welcome .
include
int sort(int A[] , int n );
int min(int A[] , int n);
int main()
{
int A[30];
int a , i , s;
printf("enter the number of elements you want to enter");
scanf("%d" ,&a);
Ruby:
Edit: Fixed the 0 edge case. Instead of adding 0 to the end of the number, put it in the 2. position.
Wouldn't rotating the array bring the biggest number at the front?
I think swapping the 1st and 2nd number would be ideal.
Also is having a leading zero really against the rules?
Yeah, swapping seems to be better, would keep the large digits in the lesser valuable positions.
I interpreted the rules this way, because it didn't seem right to lose a digit in the resulting number. That would be like assuming input set
[1,2,0]as equivelent to[1,2].Haskell. Also works for empty lists, returns 0.
this is why Haskell scares me :o
that looks like Overlord Wizardry compared
to
ClojureJavascript
function getMinimum(arr){
let sorted = arr.sort();
let distinct = '';
sorted.forEach(function(element){
if(distinct.indexOf(element) < 0){
distinct =
${distinct}${element};}
});
console.log(parseInt(distinct));
return parseInt(distinct);
}
Here is my C++ code
My try to solve this challenge :)
In my solution I don't include 0 in end result.
Java
Even works for [7,0,2], neat
this is solution in c . I have first sorted the array then solved it using simple logics . suggestions are welcome .
include
int sort(int A[] , int n );
int min(int A[] , int n);
int main()
{
int A[30];
int a , i , s;
printf("enter the number of elements you want to enter");
scanf("%d" ,&a);
}
int sort(int A[] , int n )
{
int i;
int temp , j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(A[j]<A[i])
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
}
int min(int A[] ,int n)
{
int s , j=0 , k , i , l=1;
}
JavaScript
Python