ParthaKuchana.com   Stocks & Technology
Simple Array Sum Solution in Java | HackerRank Problem Explained | Partha Kuchana
Simple Array Sum Solution in Java | HackerRank Problem Explained | Partha Kuchana


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class CalculationResult { // Renamed Result to CalculationResult
public static int simpleArraySum(List ar) {
// Write your code here
// Initialize a variable to store the sum
int sum=0;

for (int num : ar){
sum += num;
}
for (int i=0;i // sum += ar.get(i);
// }
return sum;
}
}

public class Solution {

public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int arrayCount = Integer.parseInt(reader.readLine().trim());

List numbers = Stream.of(reader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());

int sum = CalculationResult.simpleArraySum(numbers);

writer.write(String.valueOf(sum));
writer.newLine();

reader.close();
writer.close();
}
}
© 2024 parthakuchana.com