import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class OpgaverFormiddag {

    public static void main(String[] args) {
        Random random = new Random();
        int[] opgave1 = {47,89,85,28,95,47,98,89,38,38,48,28,49,5900,2,47,47,47,89,89,89,89,20,1,4,55,66,3,2,123,4214,12312,512,12,41,24,124,12,123,12,31,54,534,534,34,65,765,7,5,47,1202,4,5868,123123,41,43};
        int indexValue = 47;
        System.out.println("\nOPGAVE 1: \n"+Opgave1.lastIndexOf(opgave1,indexValue)+" " + opgave1[Opgave1.lastIndexOf(opgave1,indexValue)]);
        System.out.println("\nOPGAVE 2: \n" + Opgave2.countInRange(opgave1,500,2000));
        int opgave3ArrayLength = 500;
        int[] opgave3Array = new int[opgave3ArrayLength];
        for (int i = 0; i < opgave3ArrayLength; i++) {
            opgave3Array[i] = random.nextInt(101);
        }
        System.out.println("\nOPGAVE 3A: \n" + Opgave3.mode(opgave3Array));
        int opgave3BArrayLength = random.nextInt(1000);
        int[] opgave3BArray = new int[opgave3BArrayLength];
        for (int i = 0; i < opgave3BArrayLength; i++) {
            opgave3BArray[i] = random.nextInt(opgave3BArrayLength);
        }
        System.out.println("\nOPGAVE 3B: \n" + Opgave3.modeHighValue(opgave3BArray));

        System.out.println("\nOPGAVE 4: \n");
        Opgave4.addLargeInteger();
    }
}

class Opgave1 {
    public static int lastIndexOf(int[] array, int value) {
        int index = -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == value) {
                index = i;
            }
        }
        return index;
    }
}

class Opgave2 {
    public static int countInRange(int[] array, int low, int high) {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] >= low && array[i] <= high) {
                count++;
            }
        }
        return count;
    }
}

class Opgave3 {
    public static int mode(int[] array) {
        int[] counts = new int[101];
        for (int i = 0; i < array.length; i++) {
            counts[array[i]]++;
        }
        int highestCount = 0;
        for (int i = 0; i < counts.length; i++) {
            if (counts[i] > highestCount) {
                highestCount = counts[i];
            }
        }
        return highestCount;
    }

    public static int modeHighValue(int[] array2) {
        if  (array2.length == 0) {
            return 0;
        }
        Arrays.sort(array2);
        int[] counts2 = new int[array2[array2.length-1]+1];
        for (int i = 0; i < array2.length; i++) {
            counts2[array2[i]]++;
        }
        int highestCount = 0;
        for (int i = 0; i < counts2.length; i++) {
            if (counts2[i] > highestCount) {
                highestCount = counts2[i];
            }
        }
        return highestCount;
    }
}

class Opgave4 {
    public static void addLargeInteger() {
        final int DIGITS = 50;
        int[] sum = new int[DIGITS];
        Scanner input = new Scanner(System.in);
        System.out.print("First non-negative integer: ");
        String int1 = input.nextLine();
        System.out.print("Second non-negative integer: ");
        String int2 = input.nextLine();
        boolean skipZero = false;
        String sumString = "";
        for (int i = 0; i < DIGITS; i++) {
            int int1Value = (i < 50-int1.length() ? 0 : int1.charAt(int1.length()+i-50) - '0');
            int int2Value = (i < 50-int2.length() ? 0 : int2.charAt(int2.length()+i-50) - '0');
            int tempSum =  int1Value + int2Value;
            if (tempSum >= 10) {
                if (i == 0) {
                    throw new RuntimeException("Sum bliver over 50 cifre");
                }
                sum[i-1]++;
                tempSum -= 10;
            }
            sum[i] = tempSum;
        }
        for (int i = 0; i < sum.length; i++) {
            if (skipZero || sum[i] != 0) {
                sumString += sum[i];
                skipZero = true;
            }
        }
        System.out.println("Sum: " + sumString);
    }
}