Pankaj Gadge
BAN USERint[] a = {0,1,2,6,3,4};
int key = 5;
for(int i=0; i <a.length; i++)
{
for(int j=i+1; j<a.length; j++)
{
if((i+j) == key)
System.out.println("("+i+","+j+")");
}
}
public static String findMax(String s1, String s2)
{
int flag = 0;
if(s1 != null && s2!= null)
{
for(int i=0; i<s1.length(); i++)
{
if(s1.charAt(i) > s2.charAt(i))
{
flag = 1;
return s1+s2;
}
else if(s1.charAt(i) < s2.charAt(i))
{
flag = 2;
return s2+s1;
}
}
}
Firstly, consider the scenarios:
1) When user types a particular word
if(word == appropriate vocabulary word)
no suggestion;
else
suggestions with the closet matching words from the given dictionary
Assumptions:
1) There a should be a dictionary of correct vocabulary words, a) you can create you own b) several API's can be found online.
2) The dictionary should be stored as a form of sorted hashmap with no duplicate values.
Proceesing
1) When user enters the word, compare that word with the value of dictionary sorted hashmap,
if match is found then proceed
else compare the word with the closest matching word in the dictionary, pull out the closest matching words for the user to display.
GO with Linked List.
Advantages: Inserting, Deleting simpler O(1)
Disadvantages: Searching a particular interval O(n)
class IntervalNode
{
int start;
int end;
IntervalNode next
}
That's what I said, right. n * n = n^2. Sorry, I cannot type a square ;). So, time complexity is O(n*n)
- Pankaj Gadge October 14, 2012So, here is the working version of the code that will print all sub arrays whose sum of the elements is more than the key.
Time complexity is O(n2) using recursion. Not sure, how to do with O(n)
import java.util.ArrayList;
public class SubArray {
static int key = 2;
static int count = 0;
public static void main(String args[])
{
int[] nums = {1,2,3};
printSubset(nums);
}
public static void printSubset(int[] nums)
{
for(int i = 0; i < nums.length; i++)
{
boolean[] ifPrint = new boolean[nums.length];
printSubset(nums, ifPrint, 0, i);
}
}
public static void printSubset(int[] nums, boolean[] ifPrint, int start, int remain)
{
int sum = 0;
if(remain == 0)
{
ArrayList<Integer> temp = new ArrayList<Integer>();
System.out.print("{");
for(int i=0; i < ifPrint.length; i++)
{
if(ifPrint[i])
{
temp.add(nums[i]);
System.out.print(nums[i]+",");
}
}
System.out.print("}\n");
for(int i=0; i < temp.size(); i++)
{
sum += temp.get(i);
}
if(sum > key)
System.out.println("My Array"+temp);
}
else
{
if(start+remain > nums.length)
;
else
{
for(int i = 0; i < nums.length; i++)
{
if(!ifPrint[i])
{
ifPrint[i] = true;
printSubset(nums, ifPrint, i+1, remain-1);
ifPrint[i] = false;
}
}
}
}
}
}
Yes,
- Pankaj Gadge November 03, 2012Binary tree can have duplicates where as Binary Search Tree (BST) cannot. However, Binary tree cannot have both child node values equal at the same level.
Inserting a element which is equal to root node value, it will put the value into left child first.
e.g.
5
/ \
3 7
/ /
3 7