0% found this document useful (0 votes)
3 views2 pages

Happy Number Calculation in Java

This Java program reads input from a file to process multiple test cases. For each test case, it calculates the minimum sum of a contiguous subarray of size at least half of the total elements and outputs this minimum along with the difference from the total sum. The program handles file reading and exceptions for missing files.

Uploaded by

Syed Saqhib
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Happy Number Calculation in Java

This Java program reads input from a file to process multiple test cases. For each test case, it calculates the minimum sum of a contiguous subarray of size at least half of the total elements and outputs this minimum along with the difference from the total sum. The program handles file reading and exceptions for missing files.

Uploaded by

Syed Saqhib
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

package firstprogram;

import [Link].*;
import [Link].*;

public class game {

public static void main(String[] args) {


try {

Scanner scanner = new Scanner(new


File("C:\\Users\\saqhi\\Downloads\\prob1_silver_dec24\\[Link]"));

int T = [Link]();

for (int t = 0; t < T; t++) {


int N = [Link]();
int[] A = new int[N];
for (int i = 0; i < N; i++) {
A[i] = [Link]();
}

long[] cum = new long[N + 1];


cum[0] = 0L;
for (int i = 0; i < N; i++) {
cum[i + 1] = cum[i] + A[i];
}

long minBessie = Long.MAX_VALUE;


int windowSize = N / 2 + 1;
for (int i = 0; i <= N - windowSize;
i++) {
long windowSum = cum[i +
windowSize] - cum[i];
if (windowSum < minBessie) {
minBessie = windowSum;
}
}

long total = cum[N];


[Link](minBessie + " " +
(total - minBessie));
}

[Link](); // Close the scanner to


free resources
} catch (FileNotFoundException e) {
[Link]("Error: Input file not
found!");
[Link]();
}
}
}

You might also like