Answers for "Find the smallest length of a contiguous subarray of which the sum is greater than or equal to specified value in java"

0

Find the smallest length of a contiguous subarray of which the sum is greater than or equal to specified value in java

public static int minSubArrayLengthBruteForce(int[] nums, int k){
 
      //Declare a variable and assign integer max value
      int minLength = Integer.MAX_VALUE;
      int sum = 0;
 
      //outer loop starts from 0th index
      for(int i = 0; i < nums.length; i++) {
          sum = 0;
          //Inner starts from the value of i
          for(int j = i; j < nums.length; j++) {
              //Add the value in sum variable
              sum = sum + nums[j];
 
               /*
                 If the value of sum is greater than or equal to
                 the value of k.
                */
               if(sum >= k ) {
                 //Update the value of minlength, if it's applicable
                  minLength = Math.min(minLength, (j-i)+1);
                  break;
               }
            }
       }
 
        //Return minlength
        return (minLength == Integer.MAX_VALUE) ? 0 : minLength;
   }
Posted by: Guest on June-29-2021

Code answers related to "Find the smallest length of a contiguous subarray of which the sum is greater than or equal to specified value in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language