Find subArray in array whose sum matches required sum in java


In this core java programming tutorial we will write a program to Find subArray in array whose sum matches required sum in java.



Find subArray in array whose sum matches required sum
Example- in inputString(123451) following subString [123, 51] have sum=6



Full Program/SourceCode/ Example to Find subArray in array whose sum matches required sum in java >
 
import java.util.HashSet;
import java.util.Set;

/** Copyright (c), AnkitMittal  JavaMadeSoEasy.com */
public class SubStringWithRequiredSum {
   static Set<String> s=new HashSet<String>();
   public static void main(String[] args) {
          String inputString="123451";
          int requiredSum=6;
         
          findPairs(inputString.toCharArray(),requiredSum);
          System.out.println("in inputString("+inputString+") following subString "+s+" have sum="+requiredSum);
         
   }
  
   /**
   * find substring whose sum matches required sum.
   */
   static void findPairs(char ar[], int requiredSum){
         
          for(int i=0;i<ar.length-1;i++){
                 int sum=0;
                 for(int j=i;j<ar.length;j++){
                       sum=sum+Integer.parseInt(String.valueOf(ar[j]));
                      
                       if(sum>requiredSum){
                              break;
                       }
                       else if (sum==requiredSum){
                              int iRef=i;
                              char subArray[]=new char[j-i+1];
                              for(int k=0;k<j-i+1;k++){
                                     subArray[k]=ar[iRef++];
                              }
                              s.add(new String(subArray));
                              break;
                       }
                      
                 }
          }
   }
}
/*OUTPUT
in inputString(123451) following subString [123, 51] have sum=6
*/




So in this core java programming tutorial we wrote a program how to Find subArray in array whose sum matches required sum in java.

Previous program                                                                  Next program




RELATED LINKS>



>Pattern/Pyramid generating programs in java








No comments:

Post a Comment