Showing posts with label Language: Java 7. Show all posts
Showing posts with label Language: Java 7. Show all posts

Friday, July 28, 2017

Hackerrank: A Very Big Sum

Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005
Output
5000000015
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static long aVeryBigSum(int n, long[] ar) {
        // Complete this function
        long result = 0;
        
        for (int i = 0;i
        {
            result = result + ar[i];
        }
        
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        long[] ar = new long[n];
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextLong();
        }
        long result = aVeryBigSum(n, ar);
        System.out.println(result);
    }
}

Hackerrank: Compare the Triplets

Sample Input
5 6 7
3 6 10
Sample Output
1 1 
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static int[] solve(int a0, int a1, int a2, int b0, int b1, int b2){ // Complete this function int [] result = new int [2]; result [0] = 0; result [1] = 0; if (a0 >= 0 && a0 <= 100 && a1 >= 0 && a1 <= 100 && a2 >= 0 && a2 <= 100 && b0 >= 0 && b0 <= 100 && b1 >= 0 && b1 <= 100 && b2 >= 0 && b2 <= 100) { if (a0 > b0 ) result[0] = result[0]+1; if (a1 > b1 ) result[0] = result[0]+1; if (a2 > b2 ) result[0] = result[0]+1; if (a0 < b0 ) result[1] = result[1]+1; if (a1 < b1 ) result[1] = result[1]+1; if (a2 < b2 ) result[1] = result[1]+1; } return result; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int a0 = in.nextInt(); int a1 = in.nextInt(); int a2 = in.nextInt(); int b0 = in.nextInt(); int b1 = in.nextInt(); int b2 = in.nextInt(); int[] result = solve(a0, a1, a2, b0, b1, b2); for (int i = 0; i < result.length; i++) { System.out.print(result[i] + (i != result.length - 1 ? " " : "")); } System.out.println(""); } }

Hackerrank: Simple Array Sum

Problem:
Sample Input
6
1 2 3 4 10 11
Sample Output
31
Explanation
We print the sum of the array's elements, which is: .

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static int simpleArraySum(int n, int[] ar) {
        // Complete this function
        int result = 0;
        for(int i=0;i        {
            result = result + ar[i];
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] ar = new int[n];
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextInt();
        }
        int result = simpleArraySum(n, ar);
        System.out.println(result);
    }
}