Saikrishnan
BAN USEROutput for the below program: true
public class JumpGame {
public boolean jumpPossible(int index, int[] array, int lastIndex ){
if(lastIndex==0){
return true;
}
else if(index<array.length&&array[index]!=0){
return jumpPossible(array[index]+index,array,lastIndex-array[index]);
}
else
return false;
}
public static void main(String args[]){
int[] array={2,3,1,1,2,0,0};
int index=0;
JumpGame list1=new JumpGame();
System.out.println(list1.jumpPossible(index, array, array.length-1));
}
}
The output for the string "I love, driving 5 cars" is "I evol, gnivird 5 racs"
public class reverseWord {
public void revWord(String str){
int flag=0;
String word="";
String finalString="";
char[] charArray = str.toCharArray();
for(int i=0;i<charArray.length;i++){
if(charArray[i]!=' '&&charArray[i]!=','&&!Character.isDigit(charArray[i])){
flag=1;
}
else if(flag==1&&(charArray[i]==' '||charArray[i]==','||Character.isDigit(charArray[i])))
flag=2;
if(flag==1&&i==str.length()-1){
flag=2;
}
if(flag==1){
word=word+charArray[i];
}
else if(flag==2){
word= new StringBuffer(word).reverse().toString();
finalString=finalString+word;
flag=0;
word="";
}
if(flag==0){System.out.println("final else");
finalString=finalString+charArray[i];
}
}
System.out.println(finalString);
}
public static void main(String args[]){
reverseWord list1=new reverseWord();
list1.revWord("I love, driving 5 cars");
}
}
import java.util.*;
public class DuplicatesOfArray {
public static void distinctValue(int[] intArray) {
HashMap<Integer,Integer> hmap = new HashMap<Integer,Integer>();
ArrayList<Integer> list = new ArrayList<Integer>(intArray.length);
for(int i=0;i<intArray.length;i++){
hmap.put(intArray[i],0);
}
for(int i=0;i<intArray.length;i++){
if(hmap.get(intArray[i])==0){
hmap.put(intArray[i],1);
}
else if(hmap.get(intArray[i])==1){
hmap.put(intArray[i],2);
list.add(intArray[i]);
}
}
System.out.println(list);
}
public static void main(String args[]){
DuplicatesOfArray implementation=new DuplicatesOfArray();
int[] a={1,1,2,2,2,3,4,5,6,6,6,7,7};
implementation.distinctValue(a);
}
}
Here's my solution O(n) -
import java.util.ArrayList;
public class lowestDipsGraph {
public void getDips(){
int[] points={20,10,20,10,5,30,40,50,40,30,20,10,20,30,40,50,60,50,60,70,60};
boolean dip=false;
ArrayList<Integer> list = new ArrayList<Integer>(7);
for(int i=0;i<points.length;i++){
if(i==0){
if(points[i]<points[i+1]){
list.add(points[i]);
}
}
if(i==points.length-1){
if(points[i-1]>points[i]){
list.add(points[i]);
}
}
else{
if(points[i]>points[i+1]&&dip==false){
dip=true;
}
if(dip==true&&points[i+1]>points[i]){
list.add(points[i]);
dip=false;
}
}
}
System.out.println(list);
}
public static void main(String args[]){
lowestDipsGraph implementation=new lowestDipsGraph();
implementation.getDips();
}
}
I can see many of the solutions in Java are using String.indexOf() function which is O(n) complexity. My solution with a trade-of of O(n) space complexity does the operation in O(n) time. I use two HashSets , one with already traversed characters which aren't allowed to occur in the string and another with allowed set of characters. I move a character from the second to the first HashSet whenever I encounter an allowed character. I also use a temp character variable to detect the last character added to the first HashSet since this is the only character in the first HashSet which is allowed to present in the string. Please let me know if you find any problem with the solution.
- Saikrishnan February 06, 2017