addy
BAN USER
- 2of 2 votes
AnswersGiven a set of equalities and inequalities like A=B,B=C,F=J and A!=C, etc in two separate arrays (equalities[] and inequalities[]) and a method, separate that returns the two objects, e.g. separate(A=B) will return A and B, write an algorithm to find whether the entire set is consistent in constant time.
- addy in United States| Report Duplicate | Flag | PURGE
Yahoo SDE1 Algorithm
Here's one implementation:
public class TrimMultipleSpaces {
public String trim(String word) {
char[] p = word.toCharArray();
tr(p);
return new String(p);
}
private void tr(char[] word) {
boolean flag = false;
for (int i = 0; i < word.length; i++) {
if (word[i] == ' ' && flag) {
bringInFront(word, i);
} else if (word[i] == ' ' && !flag) {
bringInFront(word, i);
flag = true;
} else {
flag = false;
}
}
}
private void bringInFront(char[] word, int l) {
for (int i = l; i < word.length; i++) {
if (i + 1 < word.length - 1) {
word[i] = word[i + 1];
}
}
}
public static void main(String[] args) {
TrimMultipleSpaces obj = new TrimMultipleSpaces();
System.out.println(obj.trim(" Hello World "));
}
}
A recursive implementation in Java:
package ws.abhis.algos.ds;
public class ArrayManipulation1 {
public int countLastNumber(int[] arr) {
int sizeOfArray = arr.length;
int lastNumber = arr[sizeOfArray-1];
int[] newArr = {};
for (int i=0; i<arr.length-1; i++)
newArr[i] = arr[i];
int[] positions = searchForNumber(newArr, lastNumber);
return positions.length;
}
private int[] searchForNumber(int[] arr, int number) {
int low = 0;
int high = arr.length-1;
int mid = (high-low)/2;
int[] returnPositions = {};
if (arr[mid] == number)
returnPositions[0] = mid;
else {
//divide into left and right arrays
int[] leftArray = {};
for (int i=0; i<mid-1; i++)
leftArray[i] = arr[i];
int[] rightArray = {};
for (int i=mid+1, j=0; i<arr.length; i++, j++)
rightArray[j] = arr[i];
searchForNumber(leftArray, number);
searchForNumber(rightArray, number);
}
return returnPositions;
}
public static void main(String args[]) {
int[] defArr = {1,2,3,4,2,2,3,2};
ArrayManipulation1 obj = new ArrayManipulation1();
int cnt = obj.countLastNumber(defArr);
System.out.println(cnt);
}
}
RepSandraGabriel, Analyst at ABC TECH SUPPORT
We Data Entry Operators have a variety of duties, including processing documents, solving inconsistencies, securing information, updating databases and using ...
RepHad moderate success buying and selling weebles in Ohio. Had some great experience buying and selling wooden trains on Wall ...
RepOwenKary, Analyst at ASU
I am an experienced software engineer with a passion for java developing innovative programs that expedite the efficiency and effectiveness ...
RepRitterNicole, DIGITAL MARKETING at Arista Networks
I am Ritter, Experimental psychologist refers to work done who apply experimental methods to psychological study and the processes that ...
RepPennyStringer, Music director at Poore Simon's
I am a Music director and writer and live in Newark USA . I spend most of my time writing essays ...
RepEzraDavis, abc at A9
I am a hardworking and disciplined newly-certified architect with internship experience in designing commercial buildings and creating accurate 2D and ...
RepPurserCaudle, Network Engineer at AppPerfect
Hi I am Purser, a scientific writer who is a journalist who researches and reports on news and trends in ...
Here are a couple of ways I would do it:
and
- addy November 22, 2014