teji.catia
BAN USERThis problem can be resolved in either recursive or iterative way.
Recursive way is very inefficient as it may take O(n^2) or more and causes many method stacks being built upon.
Iterative solution is best with guaranteed O(n) running time. Its a below.
If someone wants to know about how formula is derived. He can mail me at teji.catia@gmail.com.
public class IterativeSeq {
public static void main(String[] args){
IterativeSeq t= new IterativeSeq();
System.out.println("output="+t.CountSeq("abcabcaabbcc"));
}
public static int CountSeq(String input){
int acount=0, bcount=0, ccount=0;
char[] chars = input.toCharArray();
for(int i=chars.length-1; i>=0;i--){
if(chars[i]=='c'){
ccount++;
}else if(chars[i]=='b'){
bcount=((int)Math.pow(2,ccount)-1)+(bcount*2);
}else if(chars[i]=='a'){
acount=bcount+(2*acount);
}
}
return acount;
}
}
- teji.catia March 03, 2017Its a problem of login and logout. Increment counter at any login and decrement counter at any logout.
Here is pseudo code:
Lets quantify login as -1 and logout as -2
Create a TreeMap as result<Integer,Integer>. Because it sorts entries with natural order or you can create comparator.
Iterate through log entries. and add it in result TreeMap as like <1.2,-1>,<4.5,-2>,<3.1,-1><6.7,-2>...etc.....But because its a Treemap the entries will be added in sorted order as <1.2,-1>,<3.1,-1>,<4.5,-2><6.7,-2>....etc
Iterate again this set. Increment counter at every -1 value and decrement at every -2 and add value back. So the list will become: <1.2,1>,<3.1,2>,<4.5,1><6.7,0>...etc.... which is what was desired.
This will only need one list to be created and hence save space too.
Here is what I think the Pseudo code can be:
Step 1: Count number of list and name it as as listCount
Step 2: Create an Array called arrayOfLists of HashMap<Integer,Integer> having length as listCount(each index corresponds to list index). the first Integer argument of HashMap tells the list number of overlapping list. The second Integer argument tells count about how many characters of both lists are overlapping.
Step 3: Create a Hashmap<Integer,list<Integer>> called clashMap: the first argument is number, the second is to store all overlapping lists that have this number.
Step 4: Iterate through all numbers of List one by one and put them into clashMap. If clashMap already had it, then check if present list number is already present in its list. If not add the list number in list. And also iterate this list and update arrayOfList:go to index of present list in arrayOfList and if it does not have the overlapping list, add it with count of overlapping characters as 1; otherwise increment that count.
when we have iterated through all the numbers we will have arrayOfLists telling us each list is overlapping with how many characters with which list. So we can then extract how many are overlapping with more then 3 elements.
The complexity will be O(n+k). where n is total number of elements in all list. And k is the number of Lists.
Just use an Array. Create an Array of size 24 with all values as 0. All coming orders that lie in the interval 00:00:00 to 00:59:59 should get added and stored at 0.......All coming orders that lie in the interval 01:00:00 to 01:59:59 should get added and stored at 1.....and so on
Just use an Array. Create an Array of size 24 with all values as 0. All coming orders that lie in the interval 00:00:00 to 00:59:59 should get added and stored at 0.......All coming orders that lie in the interval 01:00:00 to 01:59:59 should get added and stored at 1.....and so on
- teji.catia December 23, 2016public class IdentifyString {
//private static boolean[][] boolArray=new boolean[6][6];
private static int stringCount=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] inputArray = { {'B', 'B', 'A', 'B', 'B', 'N'},
{'B', 'B', 'M', 'B', 'B', 'O'},
{'B', 'B', 'A', 'B', 'B', 'Z'},
{'N', 'O', 'Z', 'A', 'M', 'A'},
{'B', 'B', 'B', 'B', 'B', 'M'},
{'B', 'B', 'B', 'B', 'B', 'A'} };
countOccurence(inputArray,"AMAZON",0,0);
System.out.println("count of string is "+stringCount);
}
public static void countOccurence(char[][] inputArray, String inputString,int startRow,int startCol){
String prevUsed;
int c=startCol;
boolean colsIteratedOnce=false;
boolean breakLoop=false;
//create array of characters AMAZON as amazonCharArray
char[] amazonCharArray=inputString.toCharArray();
//iterate matrix from left-right top-bottom.
outermost:for(int r=startRow;r<inputArray.length;r++){
if(colsIteratedOnce){
c=0;
}
colsIteratedOnce=true;
for(; c<inputArray[r].length; c++){
//if you find first char of amazonCharArray then
if(inputArray[r][c]==amazonCharArray[0]){
prevUsed="";
if(c+1<inputArray[r].length){
startRow=r;
startCol=c+1;
}else if(r+1<inputArray.length){
startRow=r+1;
startCol=0;
}else{
breakLoop=true;
}
//Iterate through amazonCharArray, look for next character of amazonCharArray at c+1,c-1,r+1,r-1 position.
//If found continue otherwise break
for(int i=1; i<amazonCharArray.length;i++){
if(r+1<inputArray.length && amazonCharArray[i]==inputArray[r+1][c] && prevUsed!=null && !"r-1".equals(prevUsed)){
if(i!=amazonCharArray.length-1){
r=r+1;
prevUsed="r+1";
continue;
}else{
//if all chars are found increment stringCount by 1.
stringCount=stringCount+1;
if(!breakLoop)
countOccurence(inputArray,inputString,startRow,startCol);
break outermost;
}
}
if(r-1>=0 && amazonCharArray[i]==inputArray[r-1][c] && prevUsed!=null && !"r+1".equals(prevUsed)){
if(i!=amazonCharArray.length-1){
r=r-1;
prevUsed="r-1";
continue;
}else{
//if all chars are found increment stringCount by 1.
stringCount=stringCount+1;
if(!breakLoop)
countOccurence(inputArray,inputString,startRow,startCol);
break outermost;
}
}
if(c+1<inputArray[r].length && amazonCharArray[i]==inputArray[r][c+1] && prevUsed!=null && !"c-1".equals(prevUsed)){
if(i!=amazonCharArray.length-1){
c=c+1;
prevUsed="c+1";
continue;
}else{
//if all chars are found increment stringCount by 1.
stringCount=stringCount+1;
if(!breakLoop)
countOccurence(inputArray,inputString,startRow,startCol);
break outermost;
}
}
if(c-1>=0 && amazonCharArray[i]==inputArray[r][c-1] && prevUsed!=null && !"c+1".equals(prevUsed)){
if(i!=amazonCharArray.length-1){
c=c-1;
prevUsed="c-1";
continue;
}else{
//if all chars are found increment stringCount by 1.
stringCount=stringCount+1;
if(!breakLoop)
countOccurence(inputArray,inputString,startRow,startCol);
break outermost;
}
}
r=startRow;
c=startCol;
break;
}
}
}
}
}
}
First of all pseudo code:
create array of characters AMAZON as amazonCharArray
iterate matrix from left-right top-bottom.
if you find first char of amazonCharArray then
Iterate through amazonCharArray, look for next character of amazonCharArray at c+1,c-1,r+1,r-1 position. If found continue oherwise break
if all chars are found increment stringCount by 1.
NOTE :
Here is the actual code:
public class IdentifyString {
//private static boolean[][] boolArray=new boolean[6][6];
private static int stringCount=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] inputArray = { {'B', 'B', 'A', 'B', 'B', 'N'},
{'B', 'B', 'M', 'B', 'B', 'O'},
{'B', 'B', 'A', 'B', 'B', 'Z'},
{'N', 'O', 'Z', 'A', 'M', 'A'},
{'B', 'B', 'B', 'B', 'B', 'M'},
{'B', 'B', 'B', 'B', 'B', 'A'} };
countOccurence(inputArray,"AMAZON",0,0);
System.out.println("count of string is "+stringCount);
}
public static void countOccurence(char[][] inputArray, String inputString,int startRow,int startCol){
String prevUsed;
int c=startCol;
boolean colsIteratedOnce=false;
boolean breakLoop=false;
//create array of characters AMAZON as amazonCharArray
char[] amazonCharArray=inputString.toCharArray();
//iterate matrix from left-right top-bottom.
outermost:for(int r=startRow;r<inputArray.length;r++){
if(colsIteratedOnce){
c=0;
}
colsIteratedOnce=true;
for(; c<inputArray[r].length; c++){
//if you find first char of amazonCharArray then
if(inputArray[r][c]==amazonCharArray[0]){
prevUsed="";
if(c+1<inputArray[r].length){
startRow=r;
startCol=c+1;
}else if(r+1<inputArray.length){
startRow=r+1;
startCol=0;
}else{
breakLoop=true;
}
//Iterate through amazonCharArray, look for next character of amazonCharArray at c+1,c-1,r+1,r-1 position.
//If found continue otherwise break
for(int i=1; i<amazonCharArray.length;i++){
if(r+1<inputArray.length && amazonCharArray[i]==inputArray[r+1][c] && prevUsed!=null && !"r-1".equals(prevUsed)){
if(i!=amazonCharArray.length-1){
r=r+1;
prevUsed="r+1";
continue;
}else{
//if all chars are found increment stringCount by 1.
stringCount=stringCount+1;
if(!breakLoop)
countOccurence(inputArray,inputString,startRow,startCol);
break outermost;
}
}
if(r-1>=0 && amazonCharArray[i]==inputArray[r-1][c] && prevUsed!=null && !"r+1".equals(prevUsed)){
if(i!=amazonCharArray.length-1){
r=r-1;
prevUsed="r-1";
continue;
}else{
//if all chars are found increment stringCount by 1.
stringCount=stringCount+1;
if(!breakLoop)
countOccurence(inputArray,inputString,startRow,startCol);
break outermost;
}
}
if(c+1<inputArray[r].length && amazonCharArray[i]==inputArray[r][c+1] && prevUsed!=null && !"c-1".equals(prevUsed)){
if(i!=amazonCharArray.length-1){
c=c+1;
prevUsed="c+1";
continue;
}else{
//if all chars are found increment stringCount by 1.
stringCount=stringCount+1;
if(!breakLoop)
countOccurence(inputArray,inputString,startRow,startCol);
break outermost;
}
}
if(c-1>=0 && amazonCharArray[i]==inputArray[r][c-1] && prevUsed!=null && !"c+1".equals(prevUsed)){
if(i!=amazonCharArray.length-1){
c=c-1;
prevUsed="c-1";
continue;
}else{
//if all chars are found increment stringCount by 1.
stringCount=stringCount+1;
if(!breakLoop)
countOccurence(inputArray,inputString,startRow,startCol);
break outermost;
}
}
r=startRow;
c=startCol;
break;
}
}
}
}
}
}
public class ReverseString {
public static void main(String[] args) {
String input = "aabdceaaabbbcd";
reverseWithUniqueCharacterSet(input);
}
public static void reverseWithUniqueCharacterSet(String input) {
LinkedHashSet<Character> output = new LinkedHashSet<Character>();
char[] inputCharArray=input.toCharArray();
for(int i=inputCharArray.length-1; i>=0;i--){
if(output.add(inputCharArray[i])){
System.out.print(inputCharArray[i]);
}
}
}
}
I beleieve this should be a dynamic obstacle resolving problem.
The player must start from top of Rectangle to bottom at shortest distance. The shortest distance will be a vertical line from top to bottom. But there are obstacles in shape of circles which may be intersecting. The player must avoid them.
I think the logic to solve should be as below:
a) The player start from top and tries to move bottom in straight vertical line.
b) If he encounters an obstacle the program will call method goLeftOrRight(). This method will determine how many intersecting circle are to right and how many are to left. The direction with least X-direction movement will be taken up.
NOTE: Above is a workable but still a bit naive implementaion. It may happen that suppose right direction has two obstacles and left have four. But right also have an intersecting obstacle just below the obstacle at its rightmost corner which further has 4 more intersecting obstacles on its right. In that case Right one will be more costly. Therefore the best solution will be we determine the total X-direction movement with all intersections horizontal or vertical and take that direction.
Reppamulapaya2, Area Sales Manager at Alcatel Lucent
Jorie , a Customer services manager with more than 6 years' experience working is responsible for managing the relationships between an ...
Repjesusitahyer, Data Engineer at ASAPInfosystemsPvtLtd
Hello Everyone, I am Jesusita and I am passionate about writing the stories about powerful mantra to get what you ...
Repsamuelcsmith700, Accountant at Absolute Softech Ltd
Je suis chef d'équipe de support Office dans une entreprise Action Auto. Je suis également rédacteur de blog. J ...
Repamandaben422, Graphics Programmer at Abs india pvt. ltd.
Hi, I am a webmaster from the USA. I think social networks have the power to connect two different people ...
Repverarfurman, Problem Setter at Cerberus Capital
I am Vera from Bullard USA, I am working as a Violin repairer in a Handyman company. My interest in ...
Reprajanadeep17, Android Engineer at 247quickbookshelp
I am a Special education teacher who adapts general education lessons and teaches various subjects to students with mild to ...
Replarrymmapp, Android test engineer at Software AG
Hello, I am name and I live in a city, USA. I am a professional's Podiatric doctor. I am ...
Repkellydbrown23, Jr. Software Engineer at Auto NInja
I live in College Park USA with my family, and my current job is clerk in Luria’s company. I ...
Repfarleym761, Accountant at Adobe
Enjoy meeting up with my friends and family, and I currently volunteer as a guest columnist for my local paper ...
Repkevinlmoses, Animator at Accenture
I am Experienced Building Manager who is an expert in public and industrial safety with a preventative mindset against fire ...
Repthonylermat, OOPS Experienced at 247quickbookshelp
I have been assigned based on the successful candidate's level of training and experience but will include types of ...
Repellahrivas6, Android test engineer at 247quickbookshelp
I am working as an internist and I work in medical offices, clinics, and hospitals. An internist may work independently ...
Repmonamore609, Android test engineer at Cisco Systems
Data entry clerks are responsible for inputting a high volume of data from multiple sources into a database, ensuring that ...
Repclairetsmith49, AT&T Customer service email at ADP
Hello, I am Claire and work as a Human resources and I am responsible for recruiting, screening, interviewing and how ...
Usually such a requirement is handled by Chain of Command Design Pattern.
- teji.catia March 09, 2017