Java Solution - Limiting indexes


#1

public int numRange(ArrayList A, int B, int C) {

int totalCount = 0;
int totalSum = 0;
int size = A.size();
int maxIndex = size - 1;
for( int i = 0; i < size; i++ ){
    int currentIndex = size - i - 1;
    totalSum  += A.get(currentIndex );
    while( maxIndex > currentIndex && totalSum  > C ){
        totalSum -= A.get( maxIndex );
        maxIndex--;
    }
    if( totalSum < B ||  totalSum > C){
        continue;
    }
    totalCount += getTotalCount(currentIndex, maxIndex , totalSum,  A, B, C);
     
    
}
return totalCount;

}

public int getTotalCount(int currentIndex, int maxIndex, int totalSum, List list, int low, int high) {

int resultCount = 0;
while( maxIndex >= currentIndex ){
     
    if( totalSum >= low && totalSum <= high ){
        resultCount++;
        totalSum -= list.get(maxIndex);
        maxIndex--;

    }
    if( totalSum < low ||  totalSum > high ){
        break;
    }
}
return resultCount;

}