HackerRank.com Challenge – Diagonal Difference

This HackerRank challenge took some thinking. I needed to be able to accept a variable size multidimensional array, add the diagonals, and then find the absolute value of their difference. I haven’t needed to work with multidimensional arrays much in the past, so it took some working through the algorithm to ensure I was incrementing my values in the correct spots.

These challenges are great mental floss for coding while I learn Java.

 

import java.io.*;
import java.util.*;
import java.lang.Math;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        
        //get the value that defines the size of the input matrix
        int n = in.nextInt();
        
        int[][] numberArray = new int[n][n];
        
        String input = in.nextLine();;
        
        int currentDiagnalPosition1 = 0, currentDiagnalPosition2 = n-1;
        int runningTotal1 = 0, runningTotal2 = 0;
        
        for(int j = 0; j < n; j++)
        {       
            input = in.nextLine();
            
            for(int k = 0; k < n; k++)
            {
                String[] data = input.trim().split("\\s+");
                
                numberArray[j][k] = Integer.parseInt(data[k]);
                
                if(currentDiagnalPosition1 == k)
                {
                    runningTotal1 = runningTotal1 + numberArray[j][k];
                }
                if(currentDiagnalPosition2 == k)
                {
                    runningTotal2 = runningTotal2 + numberArray[j][k];
                }                
                
            } 
            currentDiagnalPosition1 = currentDiagnalPosition1 + 1;
            currentDiagnalPosition2 = currentDiagnalPosition2 - 1;
        }
        
        System.out.println(Math.abs(runningTotal1-runningTotal2));
       
    }
}

HackerRank.com Challenge – A Very Big Sum

In this lesson on HackerRank, I was challenged to demonstrate my knowledge of the long data type by needing to add multiple large numbers that would have caused an integer to overflow.

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) {
        //Setup space on the heap to store our running total
        long total = 0;
        
        //Iterate over each element in array ar adding the value to the running total in total
        for(long number : ar)
        {
            total = total + number;
        }
        
        //return the sum contained in total
        return total;
    }

    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.com Challenge – Compare the Triplets

I recently completed the HackerRank.com Challenge “Compare the Triplets”. Below is the answer I submitted.

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 score_a = 0;
        int score_b = 0;
        
        //verify all numbers are in the appropriate range.
        if(a0 >= 1 && a0 <= 100 && 
           a1 >= 1 && a1 <= 100 && 
           a2 >= 1 && a2 <= 100 && 
           b0 >= 1 && b0 <= 100 && 
           b1 >= 1 && b1 <= 100 && 
           b2 >= 1 && b2 <= 100)
        {
            //System.out.println("All numbers in valid range");
            
            //tabulate score for a0/b0
            if(a0 > b0) 
            {
                score_a++;
            }
            else if(a0 < b0)
            {
                score_b++;
            }

            //tabulate score for a1/b1
            if(a1 > b1) 
            {
                score_a++;
            }
            else if(a1 < b1) 
            {
                score_b++;
            }              
            //tabulate score for a2/b2
            if(a2 > b2) 
            {
                score_a++;
            }
            else if(a2 < b2) 
            {
                score_b++;
            }
                    
            int[] number = {score_a, score_b};
            return number; 
        }
        else
        {
            System.out.println("The input needs to be between 1 and 100. Please correct the input and try again.");
            int[] number = {0, 0};
            return number; 
        }

    }

    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.com Challenge – Simple Array Sum

Here is my submission for HackerRank.com’s Simple Array Sum challenge. In it I used a foreach loop to add all of the values located in an array and return the result.

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 runningTotal = 0;
        
        for(int number : ar)
        {
            runningTotal = runningTotal + number;    
        }
        
        return runningTotal;
    }

    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);
    }
}

HackerRank.com 30 Days of Code – Day 1: Data Types

Recently I’ve been going through the HackerRank.com 30 Days of Code course to learn Java. Below is my code for their Day 1 lesson.

After reviewing the code I think I would prefer to add some data type checking to ensure that the data accepted from scan.nextLine() is actually the data type that I expect. It passed the unit tests provided by HackerRank but I doubt this would pass negative testing in a Test Driven Development organization.

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

public class Solution {
	
    public static void main(String[] args) {
        int i = 4;
        double d = 4.0;
        String s = "HackerRank ";
		
        Scanner scan = new Scanner(System.in);

        /* Declare second integer, double, and String variables. */
        int j;
        double e;
        String t;

        /* Read and save an integer, double, and String to your variables.*/
        j = Integer.parseInt(scan.nextLine());
        e = Double.parseDouble(scan.nextLine());
        t = scan.nextLine();

        /* Print the sum of both integer variables on a new line. */
        System.out.println(i + j);

        /* Print the sum of the double variables on a new line. */
        System.out.println(d + e);
        
        /* Concatenate and print the String variables on a new line; 
        	the 's' variable above should be printed first. */
        System.out.println(s + t);
        scan.close();
    }
}

Pages:12