Skip to content

Instantly share code, notes, and snippets.

@GeorgePaiva
Created June 23, 2020 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgePaiva/dbfaf60aa811e4308c052976f1e5eef9 to your computer and use it in GitHub Desktop.
Save GeorgePaiva/dbfaf60aa811e4308c052976f1e5eef9 to your computer and use it in GitHub Desktop.
package desafiometa;
import java.util.*;
public class Questao01 {
public static void main(String[] args) {
Alvo();
}
private static void Alvo() {
ArrayList<Integer> listaTemp = new ArrayList<Integer>();
ArrayList<Integer> resultado = new ArrayList<Integer>();
System.out.print("Alvo: ");
var target = Integer.parseInt(new Scanner(System.in).nextLine());
ArrayList<Integer> nums = new ArrayList<Integer>();
System.out.println("Digite o tamanho do array: ");
var total = Integer.parseInt(new Scanner(System.in).nextLine());
System.out.println("Digite os números: ");
for (int i = 0; i < total; i++) {
var numero = Integer.parseInt(new Scanner(System.in).nextLine());
listaTemp.add(numero);
}
Collections.sort(listaTemp);
for (int i = 0; i < listaTemp.size(); i++) {
if (listaTemp.get(i) + listaTemp.get(i + 1) == target) {
resultado.add(i);
resultado.add(i + 1);
System.out.print(String.format("[%1$s,%2$s]", i, i + 1));
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment