简体   繁体   中英

How to check each variable in the array, if the next element in the array is increasing?

I'm supposed to 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 (ie 3,4,5 is a triple, but 5,4,3 and 2,4,6 are not).

How do I check for the "triples"?

Current Code:

import java.util.Scanner;

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

The following code loops through the entire array of integers. Inside of the loop it is checked if the third integer exists inside of the array ( (i + 2) < array.Length ) and the other 2 conditions are all about whether value1 is the same as the value2 decreased by 1 ( array[i] == array[i + 1] - 1 and array[i + 1] == array[i + 2] - 1 ):

for (int i = 0; i < array.Length; i++)
{
    if((i + 2) < array.Length && array[i] == array[i + 1] - 1 && array[i + 1] == array[i + 2] - 1)
        System.out.println("Three values at indexes" + i + " " + (i + 1) + " and " + (i + 2) + " are a triple");
}

The code below is C# and sadly not compatible to Java that easily, I'll just leave that here for anyone who wants to know how its handled in C# (the vt variable is a so called ValueTriple ):

(int, int, int) vt;
for (var i = 0; i < array.Length; i++)
{
    if (i + 2 >= array.Length) continue;
    vt = (array[i], array[i + 1], array[i + 2]);
    if (vt.Item1 == vt.Item2 - 1 && vt.Item2 == vt.Item3 - 1)
        Console.WriteLine($"Three values at indexes {i}, {i + 1} and {i + 2} (Values: {array[i]}, {array[i + 1]}, {array[i + 2]}) are a triple");
}

You may try following code

import java.util.Scanner;

public class Triplet {

    public static void main(String[] args) {
        // put your code here
        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();
        }
        Integer counter = 0;
        for(int i = 0; i < size-2; i++) {
            if(array[i] == array[i+1] - 1 && array[i] == array[i+2] - 2) { //checking if three consecutive ints in increasing order differing by 1 
                counter++;
            }
        }
        System.out.println(counter);
    }

}

Hope this will help.

A method to find out the number of triplets could look like this. You then just have to call the method depending how your input is obtained and you wish to present the result.

public static int getNumberOfTriplets(int[] toBeChecked) {
    int numberOfTriplets = 0;
    int nextIndex = 0;
    while (nextIndex < toBeChecked.length - 2) {
        int first = toBeChecked[nextIndex];
        int second = toBeChecked[nextIndex + 1];
        int third = toBeChecked[nextIndex + 2];
        if ((first + 1 == second) && (second + 1 == third)) {
            numberOfTriplets++;
        }
        nextIndex++;
    }
    return numberOfTriplets;
}

Regardless of allowing the numbers to be in more than one triplet, the answer is fairly similar in how I would personally approach it:

//determines if the input sequence is consecutive
public boolean isConsecutive(int... values) {
    return IntStream.range(1, values.length)
            .allMatch(i -> values[i] == values[i - 1] + 1);
}

public int countTriples(int[] input, boolean uniques) {
    if (input.length < 3) {
        return 0;
    }
    int back = 0;
    for(int i = 2; i < input.length; i++) {
        if (isConsecutive(input[i - 2], input[i - 1], input [i]) {
            back++;
            if (uniques) { //whether to disallow overlapping numbers
                i += 2; //triple found, ignore the used numbers if needed
            }
        }
    }
    return back;
}

Then in calling it:

Int[] input = new int[] {1, 2, 3, 5, 6, 7, 8};
countTriples(input, true); //3
countTriples(input, false); //2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM