Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class OddPositionVsEvenPositionSum {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = 0;
- while (n < 2) { //gotta have at least one at odd and at even position
- n = Integer.parseInt(sc.nextLine());
- }
- int oddsSum = 0;
- int evensSum = 0;
- for (int i = 0; i < n; i++) {
- if (i%2 == 0) { //i=0 is the first and at an odd position
- oddsSum += Integer.parseInt(sc.nextLine());
- } else {
- evensSum += Integer.parseInt(sc.nextLine());
- }
- }
- if (Math.abs(evensSum - oddsSum) == 0) {
- System.out.println("Yes");
- System.out.println("Sum = " + evensSum);
- } else {
- System.out.println("No");
- System.out.println("Diff = " + Math.abs(evensSum - oddsSum));
- }
- }
- }
Add Comment
Please, Sign In to add comment