JetBrains Academy: Iterating over arrays - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: Iterating over arrays

Check if an array contains two numbers:

Write a program that reads an unsorted array of integers and two numbers n and m. The program must check if n and m occur next to each other in the array (in any order).

Input data format: The first line contains the size of an array. The second line contains elements of the array. The third line contains two integer numbers n and m. All numbers in the same line are separated by the space character.

Output data format: Only a single value: true or false.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean ifContains = false;
        int arraySize = scanner.nextInt();
        
        int[] array = new int[arraySize];
        for (int i = 0; i < arraySize; i++){
            array[i] = scanner.nextInt();
        }
        
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        for (int i = 1; i < arraySize; i++){
            if (array[i] == n && array[i-1] == m || array[i] == m && array[i-1] == n){
                ifContains = true;
                break;
            }
        }
        System.out.println(ifContains);
    }
}

Two numbers never occur to each other:

Write a program that reads an array of integers and two numbers n and m. The program must check that n and m never occur next to each other (in any order) in the array.

Input data format: The first line contains the size of an array. The second line contains elements of the array. The third line contains two integer numbers n and m. All numbers in the same line are separated by the space character.

Output data format: The result is a single value - true, if n and m never occur to each other, otherwise, false.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean neverOccur = true;
        int arraySize = scanner.nextInt();
        
        int[] array = new int[arraySize];
        for (int i = 0; i < arraySize; i++){
            array[i] = scanner.nextInt();
        }
        
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        for (int i = 1; i < arraySize; i++){
            if (array[i] == n && array[i-1] == m || array[i] == m && array[i-1] == n){
                neverOccur = false;
                break;
            }
        }
        System.out.println(neverOccur);
    }
}

Does an array contain N?:

Write a program that reads an array of integers and an integer number n. The program must check that array contains n.

Input data format: The first line contains the size of an array. The second line contains elements of the array separated by the space. The third line contains one integer number n.

Output data format: Only a single value: true or false.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int arrayLength = scanner.nextInt();
        
        int[] array = new int[arrayLength];
        for (int i = 0; i < arrayLength; i++){
            array[i] = scanner.nextInt();
        }

        int n = scanner.nextInt();
        
        boolean contains = false;
        for (int element : array){
            if (element == n){
                contains = true;
                break;
            }
        }
        System.out.println(contains);
    }
}

Count how many times a number occurs:

Write a program that read an array of ints and an integer number n.

The program must check how many times n occurs in the array.

Input data format: The first line contains the size of the input array. The second line contains elements of the array separated by spaces. The third line contains n.

Output data format: The result is only a single non-negative integer number.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int[] numbers = new int[size];
        for (int i = 0; i < size; i++){
            numbers[i] = scanner.nextInt();
        }
        int counter = 0;
        int num = scanner.nextInt();
        
        for(int n : numbers){
            if (n == num){
                counter++;
            }
        }
        System.out.println(counter);
    }
}

The sum of array elements:

Write a program that calculates the sum of the elements of an array of ints.

Input data format: The first line contains the size of an array. The second line contains elements of the array separated by spaces.

Output data format: The sum of the input array elements.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int length = scanner.nextInt();
        int sum = 0;
        for (int i = 0; i < length; i++){
            sum += scanner.nextInt();
        }
        System.out.println(sum);
    }
}

Sum array elements greater than a value:

Write a program that reads an array of ints and an integer number n. The program must sum all the array elements greater than n.

Input data format: The first line contains the size of an array. The second line contains elements of the array separated by spaces. The third line contains the number n.

Output data format: Only a single number representing the sum.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int length = scanner.nextInt();
        int[] array = new int[length];
        for (int i = 0; i < length; i++){
            array[i] = scanner.nextInt();
        }
        int n = scanner.nextInt();
        int sum = 0;
        
        for (int element : array){
            if (element > n){
                sum += element;
            }
        }

        System.out.println(sum);
    }
}

The minimum value of an array:

Write a program that reads an array of integers and finds the minimum value of the array.

Input data format: The first line contains the size of an array. The second line contains elements of the array separated by spaces.

Output data format: An integer number representing the minimum in the input array.

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int length = scanner.nextInt();
        int[] array = new int[length];
        for (int i = 0; i < length; i++){
            array[i] = scanner.nextInt();
        }
        Arrays.sort(array);
        int min = array[0];
        System.out.println(min);
    }
}

The index of the first max in an array:

Write a program that read an array of ints and finds the index of the first maximum in an array.

Input data format: The first line contains the number of elements in the array. The second line contains the array elements separated by spaces. An array always has at least one element.

Output data format: Only a single integer value - the index of the first maximum.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int length = scanner.nextInt();
        int[] array = new int[length];
        int maxIndex = 0;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < length; i++){
            array[i] = scanner.nextInt();
            if (array[i] > max){
                max = array[i];
                maxIndex = i;
            }
        }
        System.out.println(maxIndex);
    }
}

The maximum pairwise product:

Write a program that reads an array of ints and outputs the maximum pairwise product in the given array of non-negative numbers.

If the array has only one element then output the element.

Input data format: The first line of the input contains the number of elements in the array. The second line contains the elements of the array separated by spaces. The array always has at least one element.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int length = scanner.nextInt();
        int[] array = new int[length];
        int no1Max = 1;
        int no2Max = 1;
        for (int i = 0; i < length; i++){
            array[i]=scanner.nextInt();
            if(array[i] > no1Max){
                no1Max = array[i];
            } else if (array[i] > no2Max){
                no2Max = array[i];
            }
        }
        System.out.println(no1Max * no2Max);
    }
}

The longest ascending sequence:

Write a program that reads an array of ints and outputs the length of the longest sequence in ascending order. Elements of the sequence must go one after another.

Input data format: The first line contains the size of an array. The second line contains elements of the array separated by spaces.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int[] array = new int[size];
        for (int i = 0; i < array.length; i++){
            array[i] = scanner.nextInt();
        }

        int counter = 1;
        
        for (int i = 1; i < size; i++){
            int tempCounter = 1;
            while (i < size && array[i-1] < array[i]){
                tempCounter++;
                i++;
            }
            counter = tempCounter > counter ? tempCounter : counter;
        }
        System.out.println(counter);
    }
}

Alphabetical order:

Write a program that reads an array of strings and checks whether the array is in alphabetical order or not.

There are several rules to do it:

  1. You can compare chars with < to see if one comes before the other one (i.e. by comparing ASCII values).
  2. Shorter strings come before longer strings whenever the shorter string is a subset of the longer one. So, "a" comes before "abc".
  3. Finally, strings which are identical are in alphabetical order.

Input data format: The single input line contains strings separated by spaces.

Output data format: Only a single value - true or false.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] array = scanner.nextLine().split(" ");
        boolean ordered = true;

        for (int i = 1; i < array.length; i++){
            if (array[i-1].length() > array[i].length()){
                for (int j = 0; j < array[i].length(); j++) {
                    if (array[i - 1].charAt(j) >= array[i].charAt(j)) {
                        ordered = false;
                        break;
                    }
                }
            } else if (array[i-1].length() < array[i].length()){
                for (int j = 0; j < array[i-1].length(); j++) {
                    if (array[i - 1].charAt(j) > array[i].charAt(j)) {
                        ordered = false;
                        break;
                    }
                }
            } else {
                for (int j = 0; j < array[i].length(); j++) {
                    if (array[i - 1].charAt(j) > array[i].charAt(j)) {
                        ordered = false;
                        break;
                    }
                }
            }
        }
        System.out.println(ordered);
    }
}

Check if an array is sorted ascending:

Write a program that reads an array of int's and checks the array is sorted ascending (from smallest to largest number).

Input data format: The first line contains the size of an array. The second line contains elements of the array separated by spaces.

Output data format: Only a single value: true or false.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int length = scanner.nextInt();
        int[] array = new int[length];
        boolean ordered = true;

        for (int i = 0; i < length; i++) {
            array[i] = scanner.nextInt();
        }
        
        for(int i = 1; i < length; i++){
            if(array[i-1] > array[i]){
                ordered = false;
                break;
            }
        }
        System.out.println(ordered);
    }
}

Triples:

Write a program that reads an array of ints and outputs the number of "triples" in the array.

A "triple" is three consecutive ints in increasing order differing by 1 (i.e. 3,4,5 is a triple, but 5,4,3 and 2,4,6 are not).

Input data format: The first line contains the size of an array. The second line contains elements of the array separated by spaces.

Output data format: Only a single integer value - the number of "triples".

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int length = scanner.nextInt();
        int[] array = new int[length];
        for (int i = 0; i < length; i++){
            array[i] = scanner.nextInt();
        }

        int tripleCounter = 0;
        for (int i = 2; i < length; i++){
            if (array[i] - array[i-1] == 1 && array[i-1] - array[i-2] == 1){
                tripleCounter++;
            }
        }
        System.out.println(tripleCounter);
    }
}

Cyclically shifting elements:

Write a program that reads an array of ints named A and cyclically shift the elements of the array to the right: A[0] goes to the place of A[1], A[1] - to the place of A[2], ..., and the last element goes to the place of A[0]).

Input data format: The first line of the input contains the number of elements in the array. The second line – the elements of the array.

Output data format: The single line contains all shifted elements of the array. Elements must be separated by the space character.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int[] array = new int[size];
        for (int i = 0; i < size; i++){
            array[i] = scanner.nextInt();
        }

        int temp = array[size-1];
        for (int i = size-1; i >= 0; i--){
            if (i == 0){
                array[i] = temp;
            } else {
                array[i] = array[i-1];
            }
        }
        
        for (int i : array){
            System.out.print(i + " ");
        }
    }
}

Right rotation:

A right rotation is an operation which shifts each element of the array to the right.

For example, if a right rotation is 1 and array is {1,2,3,4,5}, the new array will be {5,1,2,3,4}.

Another example, if a right rotation is 2 and array {1,2,3,4,5}, the new array will be {4,5,1,2,3}, because {1,2,3,4,5} -> {5,1,2,3,4} -> {4,5,1,2,3}.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] data = scanner.nextLine().split(" ");
        int rotations = scanner.nextInt();
        int length = data.length;
        int[] numbers = new int[length];
        for (int i = 0; i < length; i++){
            numbers[i] = Integer.parseInt(data[i]);
        }

        int[] rotatedNumbers = new int[length];
        for (int i = 0; i < length; i++){
            rotatedNumbers[(i + rotations) % length] = numbers[i];
        }
        for (int elem : rotatedNumbers){
            System.out.print(elem + " ");
        }
    }
}

Individual taxes:

In some country, there are N companies, and the law of that country say that the taxes of each company are individual and appointed by a president. President wants to know which company pays the most taxes. But sadly, none of the president's proxies know math very well, so this work is transferred to you. Can you solve this problem?

The first line of the input contains N - the number of companies in this country. The second line contains yearly incomes of each company. All numbers are non-negative integers. The third line contains individual taxes for each company in percents of the company's income. All numbers are integers from 0 to 100 inclusive.

You should output № of the company that pays the most taxes. Keep in mind that the enumeration of the companies starts from number 1. If there are several companies with the same payments sizes, output the number of the company with the lowest number.

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int numberOfCompanies = scanner.nextInt();
        int[] incomes = new int[numberOfCompanies];
        double[] taxes = new double[numberOfCompanies];

        for(int i = 0; i < numberOfCompanies; i++){
            incomes[i] = scanner.nextInt();   
        }

        double maxTax = 0;
        int index = 1;
        for(int i = 0; i < numberOfCompanies; i++){
            taxes[i] = scanner.nextDouble() / 100 * incomes[i];
            if(maxTax < taxes[i]){
                maxTax = taxes[i];
                index = i + 1;
            } 
        }

        System.out.println(index);
    }
}