Skip to content

Instantly share code, notes, and snippets.

@bijay-shrestha
Created May 17, 2021 15:15
Show Gist options
  • Save bijay-shrestha/a7fdfaf4df3df99dc3965a3f696906de to your computer and use it in GitHub Desktop.
Save bijay-shrestha/a7fdfaf4df3df99dc3965a3f696906de to your computer and use it in GitHub Desktop.
Question: Find the n-upcount foa a given array.
package com.hawa;
import lombok.extern.slf4j.Slf4j;
@Slf4j
/**
* Define the n-upcount of an array to be the number of times the partial sum goes from less
* than or equal to 'n' to greater than 'n' during the calculation of the sum of the elements of the array.
*/
public class NUpCountProblem {
public static void main(String[] args) {
int[] num = {2, 3, 1, -6, 8, -3, -1, 2};
int nUpCount = printNUpCount(num, 5);
log.info("The expected NUpCount is, {} ",nUpCount);
}
public static int printNUpCount(int[] a, int n){
int partialSum = 0;
int nUpCount = 0;
for(int i=0; i<a.length; i++){
partialSum = partialSum + a[i];
if(partialSum > n){
nUpCount = nUpCount + 1;
}
}
log.info("nUpCount :: {}", nUpCount);
return nUpCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment