Hello I am a Java noob and I have trouble understanding this code.
Here is the code:
//print distinct triples (i, j, k) such that a[i] + a[j] + a[k] = 0
public static void printAll(int[] a) {
int N = a.length;
Arrays.sort(a);
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
int k = Arrays.binarySearch(a, -(a[i] + a[j]));
if (k > j) StdOut.println(a[i] + " " + a[j] + " " + a[k]);
}
}
}
I don't quiet understand this line
int k = Arrays.binarySearch(a, -(a[i] + a[j]));
Could anyone help me with this?
Thank you very much.
[...] what makes me confused is that why use -(a[i]+a[j]) at here.
They look for this value, since that's the only value that can make i + j + k = 0:
Given
a[i] + a[j] + a[k] = 0
and then solving for a[k] gives:
a[k] = -(a[i] + a[j])
The binarySearch method searches for the position of the second argument (in this case, -(a[i] + a[j])) in the array provided in the first argument. The array must be sorted. for example:
int[] ia = [ 2, 4, 6, 8, 10 ];
int i = Arrays.binarySearch(ia, 6);
// i == 2, since ia[2] = 6
Related
Currently working on problems from codility for practice, and for some reason I'm unable to get more than 83% correctness overall, originally I solved it with 100% correctness but with N^2 time complexity (it needs to be N or lower)
I've adjusted my code to be able to solve in O(N) but now my correctness has dropped to 77%, I'm currently trying to solve for cases of 2 elements
ie) [1000,-1000] should return 2000, but I return a 0;
Link to Question on Codility:https://app.codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/
The question:
A non-empty array A consisting of N integers is given. Array A represents numbers on a tape.
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|
In other words, it is the absolute difference between the sum of the first part and the sum of the second part.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [2..100,000];
each element of array A is an integer within the range [−1,000..1,000]
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int pval = Integer.MAX_VALUE;
int sum = 0;
int pone = 0;
int ptwo = 0;
int currdiff = 0;
for(int i = 0; i<A.length; i++ ){
sum += A[i];
}
ptwo = sum;
for(int j = 0; j< A.length; j++){
pone += A[j];
ptwo -= A[j];
currdiff = Math.abs(ptwo - pone);
if(currdiff < pval)
pval = currdiff;
}
return pval;
}
}
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts
The "non-empty" is crucial here. If you would try printing both parts in the second loop you would see that in the last iteration the second part is empty.
All you need to do is skip the last iteration in you loop:
public int solution(int[] A) {
int pval = Integer.MAX_VALUE;
int sum = 0;
int pone = 0;
int ptwo = 0;
int currdiff = 0;
for(int i = 0; i<A.length; i++ ){
sum += A[i];
}
ptwo = sum;
for(int j = 0; j< A.length-1; j++){ //<- notice -1 here
pone += A[j];
ptwo -= A[j];
currdiff = Math.abs(ptwo - pone);
if(currdiff < pval)
pval = currdiff;
}
return pval;
}
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 (i.e. 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
In this case, I want to add two numbers in this array in to obtain a specific sum when added, let’s say, 4. I also want to output what indices are being added in order to obtain that specific sum, just to see the inner workings of my code. What am I doing wrong?
public static int addingNumbers(int[] a) {
int i1 = 0, i2 = 0;
for(int i = 0, j = i + 1; i < a.length && j < a.length; i++, j++) {
if(a[i] + a[j] == 4) { // index 0 and index 2 when added gives you a sum 4
i1 = i;
i2 = j;
}
}
System.out.println("The indices are " + i1 + " and " + i2);
return i1;
}
public static void main(String args[]) {
int[] a = {1, 2, 3, 4, 5, 6};
System.out.println(addingNumbers(a));
}
The error you are making is using only one loop that iterates over the array once:
for(int i = 0, j = i + 1; i < a.length && j < a.length; i++, j++) {
In your loop you are setting i to 0 and j to 1, then you increment them with every step. So you are only comparing adjacent places in your array:
iteration: a[0] + a[1]
iteration: a[1] + a[2]
iteration: a[2] + a[3]
etc. pp
Since your array doesn't have two adjacent elements that sum up to 4 your if(a[i] + a[j] == 4) will never be entered and i1, i2 will still be 0 when the loop is finished.
To compare every array element with each other you should use 2 nested loops:
public static int addingNumbers(int[] a) {
int i1 = -1, i2 = -1;
for(int i = 0; i < a.length ; i++) {
for(int j = i+1; j < a.length ; j++) {
if(a[i] + a[j] == 4) { // index 0 and index 2 when added gives you a sum 4
i1 = i;
i2 = j;
}
}
}
if(i1>=0 && i2 >=0) {
System.out.println("The indices are " + i1 + " and " + i2);
}
return i1;
}
Note that this will only print out the last detected 2 indices that add up to 4. If you want to be able to detect multiple possible solutions and print them out could for example move the System.out.println into the if block.
It can never be == 4 because 1+2=3 then 2+3=5. So it does nothing.
There is a logic error in your code. The sum you are checking in your code is never for.
I added some debug output for easy checking:
public static int addingNumbers(int[] a) {
int i1 = 0, i2 = 0;
for(int i = 0, j = i + 1; i < a.length && j < a.length; i++, j++) {
int sum = a[i] + a[j];
System.out.println(sum);
if(sum == 4) { // index 0 and index 2 when added gives you a sum 4
i1 = i;
i2 = j;
}
}
System.out.println("The indices are " + i1 + " and " + i2);
return i1;
}
Output is: 3
5
7
9
11
The indices are 0 and 0
0
this algorithm will never be able to add a[0] to a[2], be cause when you put j=i+1 it will always be 0+1 then 1+2 ... The sum of tow adjacent numbers is never pair.
An other matter is the condition to stop your loop must be j < a.length-1
try to explain more of what you want from your algorithm.
Are you over complicating this on purpose?
Trying to figure out your intention for this task.
Why don't you just do (this is pseudo):
for length of i {
if (a[i] + a[i+1] == 4) {
System.out.println("The indices are " + a[i] + " and " + a[i+1]);
}
}
I'm creating a program that can sort objects (vector) with the Bubble sort method. I found a code on the internet which helped me alot to create it (Bubble sort in Arrays):
http://www.programmingsimplified.com/java/source-code/java-program-to-bubble-sort
When i compile the program i don't get any syntax error, but the results are not correct. I think i made an error in the IF-Statement, but i'm not sure if that's the only error. Here is the result i get when i run it:
Input number of integers to sort
5
Enter 5 integers
2
0
1
6
4
Sorted list of numbers
1
2
3
3
3
And here's my code:
import java.util.Scanner;
import java.util.*;
import java.io.*;
class BubbleSortVector {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
Vector v ;
v = new Vector();
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
//v.addElement(c);
v.insertElementAt(in.nextInt(),c);
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if ((Integer)v.elementAt(d) > (Integer)v.elementAt(d+1)) /* For descending order use < */
{
swap = (Integer)v.elementAt(d);
v.insertElementAt(d+1,d);
v.insertElementAt(swap,d+1);
}
}
}
System.out.println("Sorted list of numbers");
for (c = 0; c < n; c++)
System.out.println(v.elementAt(c));
}
}
// ...
Vector<Integer> v = new Vector<>();
// ...
for (c = 0; c < (n - 1); c++) {
for (d = 0; d < n - c - 1; d++) {
if (v.get(d) > v.get(d + 1)) {
swap = v.get(d);
v.set(d, v.get(d + 1));
v.set(d + 1, swap);
}
}
}
// ...
Your following code is wrong, inserting d+1 at index d means you're using the value of the loop index/counter into the vector, not the actual value that is at d+1
swap = (Integer)v.elementAt(d);
v.insertElementAt(d+1,d); // this is incorrect, d is the index/loop counter
v.insertElementAt(swap,d+1);
Chage that middle line to:
v.insertElement((Integer)v.elementAt(d+1), d);
you can solve it by simple swapping method. after loop you print the array.
int[] Array = new int[5]{2 , 0 , 6 , 1 , 4};
int temp = 0;
for (int i = 0; i < Array.Length; i++)
{
for (int j = 0; j < Array.Length - 1; j++)
{
if (Array[j] > Array[j + 1])
{
temp = Array[j + 1];
Array[j + 1] = Array[j];
Array[j] = temp;
}
}
}
I am trying to input values from a 1d array into a 2d array in java.
This is what I have so far:
int[] input2 = {
0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0
};
int[][] arr = new arr[3][4];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.println("index" + ((i * arr.length) + j));
arr[i][j] = input2[(i * arr.length) + j];
//System.out.print(" " + arr[i][j]);
}
//System.out.println();
}
But what it outputs is:
index0
index1
index2
index3
index3
index4
index5
index6
index6
index7
index8
index9
which means that I am getting the indexes wrong from 1d array.
Where did I go wrong ?
Your mistake is that in each step you multiply by the number of rows rather than the number of columns.
If you want to get to the first element of the second row, you have to skip all the elements of the first row first. That would be 1 * arr[0].length. So your method may work in an X by X array, but not in an X by Y array.
It could well be filling in 2 dimensions, but ((i*arr.length) + j) is probably just adding the values. I would add '+ ", "' in the middle of it to see what is actually getting output.
Try this in your loop -
((i*arr[0].length) + j)
You should change your index formula from :
(i*arr.length) + j
to
(i*arr[i].length) + j
you can try this:
int count = 0;
for(int i=0; i< arr.length; i++)
{
for(int j=0; j<arr[i].length; j++)
{
//System.out.println("index" + ((i*arr.length) + j) );
arr[i][j] = input2[count++];
System.out.print(" " + arr[i][j]);
}
System.out.println();
}
Try
int[] input2 = {0,1,0,0,0,1,1,0,1,0,1,0};
int[][] arr = new arr[3][4];
for(int i=0; i< arr.length; i++)
{
for(int j=0; j<arr[i].length; j++)
{
System.out.println("index" + ((i*arr[i].length) + j) );
arr[i][j] = input2[(i*(arr[i].length)) + j];
//System.out.print(" " + arr[i][j]);
}
//System.out.println();
}
(Replaced ((i*arr.length) + j) with ((i*arr[i].length) + j)