Store repeating sequence of integer in another array [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have two arrays a[] and b[].
int a[]={3,1,1,1,7,4,6,6,3,1};
int b[]=new int[a.length];
Array length in actual problem can change.
The values in the array must be less then value of array length as seen.
The output must be:
b = 3 1 0 0 7 4 6 0 3 1
So basically if there is a sequence of same value in a[] then only 1st of its value must be placed at same index in b[] rest should be zero till the sequence exists.
Answer in java syntax will be helpful.
Thank you in advance

int a[]={3,1,1,1,7,4,6,6,3,1};
int b[]=new int[a.length];
int temp = a[0];
b[0] = temp;
for(int i = 1; i < a.length; i++) {
if(a[i] == temp)
b[i] = 0;
else
b[i] = a[i];
temp = a[i];
}

hint:
1) create hashmap to store processed values
2) iterate over the first array: if current value is stored in map, then fill 0, otherwise store this value in HashMap and copy the value to new array

Related

Bitwise operations in Java give different value with similar code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Short question. I am quite new to doing bit/bytewise operations in Java, but I noticed something strange.
The code below gives as output:
2
0
code:
String[] nodes = new String[3];
boolean in;
int index = 0;
int hash = "hi".hashCode();
in = (nodes[(index = nodes.length - 1) & hash]) != null;
System.out.println(index);
index = (nodes.length - 1) & hash;
System.out.println(index);
Why is it, that index has a different value even though the operation to assign a value to index is identical in the 5th and 7th line of code?
Like I said, I'm new to bitwise/bytewise operations so I'm probably missing some background information.
(index = nodes.length - 1) & hash
assigns nodes.length - 1 to index, then bitwise-ands it with hash.
index = (nodes.length - 1) & hash
bitwise-ands nodes.length - 1 with hash, then assigns the result to index.
Not the same thing.
Your first assigment to index happens in the parenthesis here:
in = (nodes[(index = nodes.length - 1) & hash]) != null;
Which is
index = nodes.length - 1
and has a value of "two". Your second assignment (index = (nodes.length - 1) & hash;) differs; you added a & hash which presumably has the two bit as 0 thus when you perform the bitwise & it becomes 0.

Is this a correct BubbleSort Algorithm? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I wrote the following code to sort the elements in the array values using a BubbleSort. Is this correct or is there anything missing? My test cases are good, but maybe it's the test cases that are also missing something.
public void sort(ValuePair[] values) {
ValuePair value = null;
for (int i = 0; i < values.length; i++) {
for (int j = 1 + i; j < values.length; j++) {
if (values[i].getValue() > values[j].getValue()) {
value = values[j];
values[j] = values[i];
values[i] = value;
}
}
}
}
Your code is correct in that it will sort the array. However it will always require N*(N-1) passes over the array. This is not
the typical algorithm used to implement
a bubble sort. The typical algorithm uses repeat loop with a test for sorted. This is somewhat more efficient because it
terminates as soon as the array is sorted (consider the case where you start with a sorted array).
Read the Wikepedia article on bubble sort it demonstrates this very well.
A somewhat improved version pseudocode version of Bubble Sort goes something like this:
procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
if A[i-1] > A[i] then
swap(A[i-1], A[i])
swapped = true
end if
end for
n = n - 1
until not swapped
end procedure
The lesson here is that while your algorithm and the Wikepedia algorithm both have the same big O characteristics, a small change
in the way they have been implemented can make a significant difference in their actual performance characteristics.

Finding the largest number in an array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to figure how to find the largest number in an array of random numbers.
So far I cant manage to get it right.
Its an array[50] and the random, numbers are between 0-100.
Thanks!
Loop through the array and keep track of the largest number found already in an int variable.
public int findMax(int[] numbers)
{
int max = 0;
for (int i = 0; i < numbers.length; ++i)
if (numbers[i] > max) max = numbers[i];
return max;
}
(You can also initialize max to int.MIN_VALUE or something if it helps behave more suitably in the case where an empty array is passed in.)
Use:
java.utils.Arrays.sort(yours_array);
int largest = yours_array[yours_array.length - 1] ;
Collections.max and you can use it as follows with a raw array:
List<Integer> triedArray = new ArrayList<Integer>();
ArrayUtils.addAll(intArray);
This does add a dependency on commons-lang, though.
Assuming the int[] is named array
Try to use a loop like the following:
int biggest = array[0]
for(int a = 1; a < array.length; a++){
if(array[a] > biggest){
biggest = array[a]
}
}
At the end, your variable biggest will hold the largest value in the array.

Error with index of an array in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Why it doesn't work?
P.S. I am beginner in Java.
int userInfo[];
userInfo = new int[2];
userInfo[0] = 11;
userInfo[1] = 20;
userInfo["result"] = userInfo[0] + userInfo[1];
System.out.println(userInfo["result"]);
Only an int can be an index into an array. A String won't work. If you need 3 slots, declare your array to be length 3 and then you can use userInfo[2].
The JLS, Section 10.4 makes it pretty clear:
Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (§5.6.1) and become int values.
int userInfo[];
userInfo = new int[2];
userInfo[0] = 11;
userInfo[1] = 20;
int result = userInfo[0] + userInfo[1];
System.out.println(result);
The string can not be the index in array.
Your array has 2 slots, and you used them to store the numbers. To get the sum, do this:
int sum = userInfo[0] + userInfo[1];
Also, even if your array had a third slot, you can only access individual elements by their numerical index (0, 1, or 2 in this case). Not by a String like result.
In java, arrays only have zero and positive integer indexes. This means that an array can only be accessed with 0 to size of the array minus 1.
If you want to do something like:
userInfo["result"] = userInfo[0] + userInfo[1];
You can try the following:
int result = userInfo[0] + userInfo[1];
System.out.println(result);
or:
Map<String,Intgeer> example = new HashMap<String,Intgeer>();
example.put("result", new Integer(userInfo[0] + userInfo[1]));
System.out.println(example.get("result"));
There are several problems here as I mentioned in comments. You declare an array of int here:
int userInfo[];
Then try to pass a string into it here (that won't work):
userInfo["result"]; // This is bad news
Your cleaned-up code should look like this:
int userInfo[];
userInfo = new int[2];
userInfo[0] = 11;
userInfo[1] = 20;
int sumArrayValues = userInfo[0] + userInfo[1];
System.out.println(sumArrayValues);
Happy coding!

Java: binary series representation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am doing some experiments on my own about quantization processes etc.
I try to implement a binarization process which makes a "binary string" which will get processed by xor afterwards and some other stuff.
Anyhow the binarization is the following, where d and u are some numbers that will get compared:
String b = "";
for (int i = 0; i < u.length; u++) {
if(d[i] < u[i]) {
b[i] += '0';
} else {
b[i] += '1';
}
}
Currently like described I have a string where each character is 0 or 1.
Using a BigInteger gives me an Object where I can XOR two values against each other:
BigInteger bi = new BigInteger(b, 2);
(...)
BigInteger result = bi.xor(other_bi);
Is there another way to achieve what I want to do? I didn't find anything but maybe there is one I have not found?
The BitSet class is more appropriate for representing a sequence of bits. To set a bit you would use the BitSet.set method.
Taking your example as a BitSet.
BitSet bs = new BitSet(u.length);
for (int i = 0; i < u.length; u++)
if(d[i] >= u[i])
bs.set(i);
BitSet bs2 = ...
bs2.xor(bs); // changes and reuses bs2.
int bitsSet = bs2.cardinality();
int firstSetBit = bs2.nextSetBit(0);

Categories