Zeros are appending to my arrays in Java programm - java

If I print my array long pair[] , zeros are appending in empty slots. What is wrong? Any help is welcome.
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
diff = arr[j] - arr[i];
if (ndiff == diff) {
pair[x] = arr[i];
x++;
pair[x] = arr[j];
x++;
}
}
}
for(i=0;i!=pair.length;i++){
System.out.print(pair[i]+" ");
}

If you want to print only the values you put in pair, your for loop should run only up to the element with index x-1:
for(i=0; i < x; i++){
System.out.print(pair[i]+" ");
}

Related

why my selection sort program is not working?

public class first {
public static void main(String args[]){
int arr[]={5,4,1,3,2};
for(int i=0; i<arr.length-1;i++){
int smallest=arr[i];
for(int j=i+1; j<arr.length;j++){
if(smallest>arr[j]){
smallest=arr[j];
}
}
//swap
int temp=smallest;
smallest=arr[i];
arr[i]=temp;
}
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}
i have done this problem by getting the smallest in terms of index number
and the program works properly .
but when i am taking smallest in terms of number present at index number ,
this program did not work.your text
you need to save the smallest number index not the number it self so you know the positions to make the swap
for (int i = 0; i < arr.length - 1; i++) {
int indexOfSmallestNum = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[indexOfSmallestNum] > arr[j]) indexOfSmallestNum = j;
}
int temp = arr[i];
arr[i] = arr[indexOfSmallestNum];
arr[indexOfSmallestNum] = temp;
}

Insertion sort array in java

So I made this sortion sort method, all using for loops.
I revised it on white board over and over, it looks perfect, however, when I implemented it, it keeps giving me wrong sort, but if I reversed the if condition, it will give me the right answer but in reverse, this doesn't make sense!
public void insertionSort(){
for (int i = 1; i < items.length; i++){
for (int j = 0; j < i; j++){
if (items[i] < items[j]) {
int temp = items[i];
shift(items,j, i-1);
items[j] = temp;
}
}
}
}
private void shift(int[] array, int index1, int index2){
if (index1 == index2)
array[index1 + 1] = array[index1];
else{
for (int i = index2; i >= index1; i--)
array[i+1] = array[i];
}
}
Thanks for the input, I discovered the problem in my Array class, it simply doubles the array size, my mistake was that I used the array.length instead of count.
public void insertionSort(){
for (int i = 1; i < count; i++){
for (int j = 0; j < i; j++){
if (items[i] < items[j]) {
int temp = items[i];
shift(items,j, i-1);
items[j] = temp;
}
}
}
}

trouble implementing counting sort for strings

I get this error when I attempt to sort it based off the second char in the string but it runs
fine when I use the first char
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 8
at StringCountSort.countingSort(StringCountSort.java:27)
at StringCountSort.main(StringCountSort.java:38)
import java.util.Arrays;
public class StringCountSort{
public static void countingSort(String[] array, int size, int digit)
{
String[] result = new String[size+1];
int maximum = 122;
int[] count = new int[maximum + 1];
for (int i = 0; i < count.length; i++){
count[i] = 0;
}
for (int i = 0; i < size; i++){
count[array[i].charAt(digit)] +=1;
}
for (int i = 1; i < count.length; i++){
count[i] += count[i-1];
}
for (int i = size -1; i >= 0; i--){
result[count[array[i].charAt(digit)] - 1] = array[i];
count[array[i].charAt(0)]--;
}
for (int i = 0; i < size; i++) {
array[i] = result[i];
}
}
public static void main(String args[]){
String[] data = { "eg", "fa", "bz", "ch", "hv", "df", "ag" };
StringCountSort.countingSort(data, data.length, 1);
System.out.println("Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(data));
}
}
line 28 result[count[array[i].charAt(digit)] - 1] = array[i];
line 37 StringCountSort.countingSort(data, data.length, 1);
Change
count[array[i].charAt(0)]--;
to
count[array[i].charAt(digit)]--;
This should do the trick.
I also suggest the following improvements:
You don't need to pass the length of array as an argument.
You don't need to set every int of count to 0;
maximum should be Character.MAX_VALUE, to support every possible character.
The finished function could look like this:
public static void countingSort(String[] array, int digit) {
String[] result = new String[array.length];
int[] count = new int[Character.MAX_VALUE + 1];
for (int i = 0; i < array.length; i++){
count[array[i].charAt(digit)]++;
}
for (int i = 1; i < count.length; i++){
count[i] += count[i-1];
}
for (int i = array.length -1; i >= 0; i--){
result[count[array[i].charAt(digit)] - 1] = array[i];
count[array[i].charAt(digit)]--;
}
for (int i = 0; i < array.length; i++) {
array[i] = result[i];
}
}
Things that need to be changed are (if you want to sort the strings on the basis of first character)
count[array[i].charAt(digit)] +=1 to count[array[i].charAt(0)] +=1
result[count[array[i].charAt(digit)] - 1] = array[i]; count[array[i].charAt(0)]--; to result[--count[array[i].charAt(0)]] = array[i];
If you want to sort the string on the basis of the second character then simply change :
count[array[i].charAt(0)]--; to count[array[i].charAt(digit)]--;

what are passes using the method of sorting arrays using for loops

any one care to explain how passes work when sorting arrays as a grade 10 student
for (int i = 0; i < 9; i++) { // control passes
for (int j = i + 1; j < 10; j++) { // cntrol loops
if (arr[i] % 2 == 0) {
int temp = arr[i]; // what is this line used for
arr[i] = arr[j];
arr[j] = temp;
}
}
}

Maximum number from an array which is equal to product of two other numbers in that array

Recently, I took Linkedin placement test in which there was a question in which output for 4 test cases were wrong for me. I could not figure out what was my mistake becasue inputs/outputs were hidden.
Anyways here was the question:
Find the maximum element from an array where product of any other two elements would be equal to that number and return that number .If no, such element is there then return -1.
Here was my solution:
static int maxElement(int[] arr) {
Arrays.sort(arr);
int max = arr[arr.length-1];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
return -1;
}
I guess you need to find the possible maximum number in the array and the product of two elements in the array.
If I assume this, your code fails for this test case:
int[] arr = {2,4,5,3,7,6}; , where the answer should be 6
Check this below code it will work for above test-case.
Just add one more reverse for loop to check the possible value and product.
static int maxElement(int[] arr) {
Arrays.sort(arr);
for (int k = arr.length-1; k >= 0; k--) {
int max = arr[k];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
}
return -1;
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < a.length; i++) {
list.add(a[i]);
}
int maxSum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if ((a[i] * a[j]) > maxSum) {
if(list.contains(a[i] * a[j]))
maxSum = a[i] * a[j];
}
}
}
if (maxSum != 0)
return maxSum;
return -1;

Categories