Im checkin a code in c++ and i'm trying to "translate" it to java. i wonder what this line does... (both are int arrays)
frequencies[values[i]]++;
and how can i translate it?
This is the code I extrated the line from
https://github.com/Tomaszal/HackerEarth/blob/master/Data%20Structures/Stacks/Fight%20for%20Laddus/main.c
I believe it gets the value from the i-th element in the values array, searches for it in the frequencies array and add 1+ to the index...I don't really get it
This was my attempt to the code above
int y=values[p];
frequencies[y]=frequencies[y]+1;
It gets the value from i-th element in the values array and passes this value as an indexer to frequencies array and increments the returned value by 1. A perfect translation would be
Increment the (i-th value)th value of frequencies by 1.
and it surely works the same in Java. You don't need to convert it to any other statement(s) in Java to work.
Related
I just found this code snippet and was wondering if there is any specific reason why the value is stored in a double1 array instead of just a "normal" double variable?
double[] potentialEnergy = new double[1];
if (potentialEnergy[0] != 0.0)
throw new RuntimeException();
Also, are there actually any cases where the initial value of a double is not 0.0 so you need to check that specifically?
For context: I recently started to read about graph visualisation and stumbled upon the sourcecode of an implementation for the kamada/kawai force directed algorithm. In there, starting at line 311, i found those 3 lines of code and didn't find any reason for it to be an array:
Is there any reason to create an array for only 1 element?
Yes: If you're passing it into a method that will provide a value by putting it in the array (rather than providing a value by returning it, perhaps because it returns some other value).
Generalizing that: If you're providing it to an API that expects an array, even if it's just a single-element array.
Also, are there actually any cases where the initial value of a double is not 0.0 so you need to check that specifically?
No, a freshly-allocated array contains elements with all bits off (which are zeros when it's a double array [or an array any other numeric type, including char], false for arrays of boolean, and null for arrays of a reference type).
I am learning about array as a data structure and I'm interested in how exactly in Java we obtain an element in an array by index.
What's happening under the hood when the following code is executed:
...
int i = array[2];
How JVM stores refferences to primitive types in array? How we obtain the element in O(1)? Does JVM calculates position of the element relative other element?
Arrays like int[], double[], etc. Is native arrays.
The Reference to native array is the reference to its first element.
Element with index "i" will be getted by dereferencing ("reference on the first element" + i * sizeOf("contained type")).
P.s. In the Java they are wrapped in other class for checking bounds, getting length and so on. In the low-level programming languages like C++ you can broke your programm, when u will try to change element by index grater than array length
I have been using the binarySearch method on an int[] array to find the offset of a specific int value but sometimes it works fine and other times it throws back a negative number.
In other questions it is suggested that I sort the array first, but I don't want to do this as the order they are in must be kept.
System.out.println("Index of last point: "+validFlag+" "+Arrays.binarySearch(validFlags,validFlag));
I find it odd that this works in some cases and not in others, in the case of the others I can assure you the int value is in the array!
Suggestions?
Here's some console output from the program:
Possible flags: 26317584
Current flag: 6
Index of last point: 6 -7
The main criteria of binary search is that your array has to be sorted. So if you want to use binarySearch to find an element then you must provide a sorted array. And if you don't want to sort the array then you can use linear search instead
using Arrays.sort() method you must really sort it first
if you want to just find the number you can use looping
for(int i=0;i<values.length;++i)
{
if(myNumber==values[i])
{
i=values.length;
foundValue=true;
}
}
I am trying to solve this problem using the least running time.
If we're given a 2D array, we need to return x if there exists a row where all values equal x, and there is a column where all values equal x.
For example, for the following 2d array,
0 3 1
2 3 1
1 1 1
we are supposed to return 1 since the last row and last column are all of same value 1. If no such number exists, we can return -1.
I know there are many ways to solve the problem and most of them are of O(n^2), I'm wondering if there is an efficient way(i.e O(n)) to find such value. (where n represents the number of cells in this array)
Ok, you clarified that you consider O(n) to be the number of values in the 2D array.
I'll outline the approach in pseudo-code, which you can translate to either Java or C++, whichever is your preference. Your question is tagged with both, and the pseudocode is trivial enough to be directly translatable into either C++ or Java. Or Perl. Or Python...
Part 1
Let's start with the first, easy step, how to check whether there's any row that contains the same value. The pseudocode for this is elementary:
start with the 0th row, n=0
check if matrix[n][0] through [n][m-1] (where m is the number of columns in the matrix) contain the same value. If so, you found it.
otherwise, increment n, to go to the next row, until you reach the bottom of the matrix.
You should be able to understand that. This is basic, elementary "Computer Science 101" stuff.
Part 2
Now, let's modify this pseudocode to simultaneously check the columns as well. Modify the above pseudocode as follows.
Create two one-dimensional vectors, call the first one top_values, or something, whose size is m, the number of columns in your matrix. Call the second one flags, it's a vector of boolean flags, also their size is m.
When you scan the 0th row, in the pseudocode given in the first part, copy the values from the 0th row, into top_values. That is, copy matrix[0][x] into top_values[x]. Also set flags[x] to true (you can initialize all flags to true even before you scan the 0th row, it doesn't matter).
When you scan each one of the remaining rows, using the pseudocode given in Part 1, compare matrix[y][x] (where y is the row# you're scanning) against top_values[x]. If they are not the same, set flags[x] to false.
At the end of the pseudocode from part 1, check your flags vector. If any value in it is still true, there's a column in your matrix whose values are the same.
Your homework assignment is to make a slight tweak to the above pseudocode to also tell you which value is the same, in some row or column.
Is is possible to get out of an ArrayList the first number of the first index?
Here's an Example:
In there are 5 items:
path = {0.5,5.0},{0.6,6.0},{0.7,7.0},{0.8,8.0},{0.9,9.0}
And I want to get the number 5.0 out of {0.5,5.0}...
I tried it with path.get(0) But it only gives me {0.5,5.0} back.
Is it possible to get 5.0 out of it without getting all the other numbers?
If your ArrayList contains arrays, this is the way to go
myList.get(0)[1] // You're getting the index 1 from the 1st array of your ArrayList
Otherwise, if it is containing other ArrayList's
myList.get(0).get(1) // Same logic as above applied to the Collection
If your curly braces in the ArrayList are actually brackets (they probably are), you can use:
myArray.get(0)[index] to get the index you want. In your example it is:
myArray.get(0)[1];
Note: if your ArrayList elements are also ArrayList then you need to use get(0).get(1) instead of get(0)[1].