newspecies
BAN USERpublic class AllPossibleSteps {
public static void main(String[] args) {
int steps = 4;
printAllpossibleSteps("",steps);
}
public static void printAllpossibleSteps(String path, int steps) {
if(steps > 0) {
printAllpossibleSteps(path+" 1",steps-1);
printAllpossibleSteps(path+" 2",steps-2);
} else if(steps == 0) {
System.out.println(path);
}
}
}
public class PrintTriangle {
public static void main(String[] args) {
int max = 4;
printSequence(1,1,max);
}
private static void printSequence(int start, int endOffset, int maxRow) {
if(maxRow > 0) {
StringBuilder builder = new StringBuilder();
for(int i=start; i< start + endOffset; i++) {
builder.append(i + "*");
}
System.out.println(builder.toString().substring(0,builder.toString().length()-1));
printSequence(start + endOffset, endOffset + 1, maxRow - 1);
System.out.println(builder.toString().substring(0,builder.toString().length()-1));
}
}
}
What complexity were they expecting for this question?
Its in a way similar to this question. Here the sum is the input http://www.careercup.com/question?id=5727163577794560
Its not search time but lookup time is O(1)
- newspecies March 12, 2015Java code:
public class FindAllPairWithSum {
public static void main(String[] args) {
int[] numbers = {2,5,3,7,9,8};
final int sum = 11;
Map<Integer,Integer> numberMap = new HashMap<Integer,Integer>();
for(int i = 0; i < numbers.length; i++) {
numberMap.put(numbers[i], sum-numbers[i]);
}
for(int i = 0; i < numbers.length; i++) {
if(numberMap.containsKey(numberMap.get(numbers[i]))) {
System.out.println("("+numbers[i]+","+numberMap.get(numbers[i])+")");
numberMap.remove(numberMap.get(numbers[i]));
}
}
}
}
#include<stdio.h>
int findPath(int arr[][4],int posx, int posy, int maxx, int maxy)
{
int pathFound = 0;
printf("x:%d and y:%d\n",posx,posy);
if(posx < maxx && posy < maxy-1)
{
if(arr[posx][posy] == 1)
{
pathFound = findPath(arr,posx,posy+1,maxx,maxy);
}
if( pathFound == 0)
{
pathFound = findPath(arr,posx+1,posy,maxx,maxy);
}
}
else if(posy == maxx-1)
{
pathFound = 1;
}
return pathFound;
}
int main()
{
int arr[4][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,1,1,0}};
printf("%d",findPath(arr,0,0,4,4));
}
i think this works fine
- newspecies July 31, 2013
compexity of your solution is in between (L1*L2) to (L1*L2*L3).
- newspecies March 16, 2015L1 = arr1.length
L2 = arr2.length
L3 = arr3.length
solution exists where you can find common elements in (L1+L2+L3) iterration