john madden john madden john madden
BAN USERI'm guessing they want this to be flexible to accept larger negative values (e.g. what happens when the input array contains -4?). So,
1) find the minimum of the input array, if not negative, use 0.
2) print line by line
public void main(int[] inputArray) {
int min = findMin(inputArray);
for(int i=0;i<inputArray.length;i++){
if(inputArray[i] > 0) {
for(int j=min;j<0;j++){
Console.Write(" ");
}
}
else{
for(int j=min;j<(min-inputArray[i]);j++){
Console.Write(" ");
}
}
for(int j=0;j<inputArray[i];j++){
Console.Write("*");
}
Console.WriteLine();
}
}
public int findMin(int[] array) {
int minInt = array[0];
for(int i=1;i<array.length;i++) {
if(array[i] < minInt)
{minInt=array[i];}
}
return minInt;
}
function findLeader(inputArr) {
var leaderArray = [];
for (var i = 0; i < inputArr.length; i++) {
if (isLeader(inputArr, i)) {
leaderArray.push(inputArr[i]);
}
}
return leaderArray;
}
function isLeader(inputArr, pos) {
for (var j = pos; j < inputArr.length; j++) {
if (inputArr[pos] < inputArr[j]) {
return false;
}
}
return true;
}
C# ( for MS :D )
public bool isPalindrome(inputStr){
if (inputStr.length == 0)
{
console.writeLine("invalid input");
return false;
}
for (int i = 0; i<=inputStr.length/2; i++){
if (inputStr[i] != inputStr[inputStr.length - 1 - i])
{return false;}
}
return true;
}
This solution keeps a map of the last occurrence of each letter and watches the subset's length from the position of the last duplicate (start of new subset).
runtime: O(n)
- john madden john madden john madden January 22, 2015in action here: https://ideone.com/9CB7he