I am preparing for a coding interview and I have the following questions:
Find the biggest positive number sequence in the Integer Array.
If Input is
[1,2,5,6,-7,5,7,8,5,6,7,-6,7,0]
, then output should be[5,7,8,5,6,7]
, which is the longest positive digit sequence.
Below is my code for the above program:
public class Test1 {
public static void main(String args[]){
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int numberArray[] = {-2, -3, -5, -6, -7, -7, -8, 9, 0, -6, 3, -1, -2, 0, -9, -12};
/* TEST DATA
* int numberArray[] = {-2, -3, -5, -6, -7}; //DATA WITH ALL NEGATIVE NUMBERS. EXPECTED OUTPUT is SIZE : 0
* int numberArray[] = {}; //DATA WITH ALL NO VALUES. EXPECTED OUTPUT is SIZE : 0
* int numberArray[] = {-2, -3, 7, -6, -7}; //DATA WITH ONE POSITIVE DATA. EXPECTED OUTPUT is SIZE : 1
* int numberArray[] = {-2, 3, 5, 6, -7, 23, 4, 4, 1}; //DATA WITH VALID VALUES. EXPECTED OUTPUT is SIZE : 4
*/
//Initializing all values. At Start, Start And End Index Points to First Location i.e., Index=0;
int startIndex=0;
int endIndex=0;
// Stores the Length of Sequence and Its Start Index
int validSequenceSize=0;
int validSequenceStartIndex=0;
for(int index=0; index<numberArray.length; index++){
// If series started with Positive Number and StartIndex is '0', Move EndIndex to Next Location.
// This make sure the StartIndex and End Index are always different.
if(numberArray[index]>0 && startIndex==0){
endIndex=endIndex+1;
}
//Setting the Value of End Index. if value is Negative or reached the end of Series, Set the end Index
if(numberArray[index]<0 || numberArray.length-1==index ){
if(startIndex!=endIndex){
endIndex=index;
int difference=endIndex-startIndex;
// Check the difference between Start and End Index of Valid Positive Number Sequence
if(difference>=validSequenceSize && difference!=0){
validSequenceSize=difference;
// if the Last Digit is Positive Number, Increase the valid Sequence size by 1
if(numberArray[numberArray.length-1]>0 && numberArray.length-1==index){
validSequenceSize=validSequenceSize+1;
}
validSequenceStartIndex=startIndex;
}
}
//Setting value of Start Index after One Set of Calculation is Completed.
if(endIndex!=numberArray.length-1 && numberArray[index]<0 ){
startIndex=endIndex+1;
}
}
}
//RESULT
System.out.println("Size of Sequence : "+validSequenceSize);
if(validSequenceSize>0){
System.out.println("Valid Sequence Starts at Index[" +validSequenceStartIndex +"]");
for (int index=validSequenceStartIndex; index<validSequenceStartIndex+validSequenceSize;index++){
System.out.println("Values of Biggest Positive Sequence in Index["+index+"] -- "+numberArray[index]);
}
}
}
}
Is this the correct approach? I am able to find the sequence, but I am not sure if this is the most efficient. The interview mainly focus on
- optimizing the code;
- reducing the variable usage;
- looping.
Also, would sorting the integer array before proceeding the operation result in better performance?
Please let me know your inputs/thoughts and guide me to optimize the code.