Question

For Q1 input file, the first line gives n and m. The next n lines contain a[0],...,a[n−1] respecti...

For Q1 input file, the first line gives n and m. The next n lines contain a[0],...,a[n−1] respectively, one element per line.

input file

output will be   

please use python to do this question (java will be fine as well)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class BBB
{
  
  
   public static int findmin(int a[],int m)
   {
       int min=2147483647;
       int temp=0;
       int j=0;
      
       // finding the minimum of size of 2
       for(int i=0;i<=a.length-m-1;i++)
       {
           temp=0;
           for(j=i;j<i+m;j++)
               temp=temp+a[j];
          
          
           if(temp<min)
               min=temp;
          
          
       }
      
       return min;
      
      
      
      
   }
  
   public static void main(String args[]) throws FileNotFoundException {
       File file = new File("C:\\Users\\CHITTIMALLA\\Desktop\\input.txt");
      
       Scanner sc= new Scanner(file);
      
       int n=sc.nextInt();
      
       int m=sc.nextInt();
      
       int[] a= new int[n];
       // reading from file
       for(int i=0;i<n;i++)
           a[i]=sc.nextInt();
       // function call to findmin(a,m)
       System.out.println("The minimum sum of subarry of size "+m+ "is " +findmin(a,m));
      
      
      
      
   }

}

Sample Run:

Oproject-Eclipse File Edit Scurce Refactor Navigate Search Praject Run Window Help Access| | CorsalehjevaAnay1javamergelist j

Add a comment
Know the answer?
Add Answer to:
For Q1 input file, the first line gives n and m. The next n lines contain a[0],...,a[n−1] respecti...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT