How can I merge two arrays in ascending order?
I have the code below and I need to merge this array in ascending order, and it should be in a separate method. So in this case the method should return {1, 4, 8, 9, 11, 12, 13}
public class MergingArrays {
public static void main(String[] args) {
int [] array1 = {4, 8, 12, 13};
int [] array2 = {1, 9, 11};
merge(array1, array2);
}
public static int[] merge(int array1[], int array2[]) {
}
}
Assuming that you are looking to merge two sorted arrays, you could do it like this.
public static int[] merge(int array1[], int array2[]) {
int i = 0, j = 0, k = 0;
int size1 = array1.length;
int size2 = array2.length;
int[] result = new int[size1 + size2];
while (i < size1 && j < size2) {
if (array1[i] < array2[j]) {
result[k++] = array1[i++];
} else {
result[k++] = array2[j++];
}
}
// Store remaining elements
while (i < size1) {
result[k++] = array1[i++];
}
while (j < size2) {
result[k++] = array2[j++];
}
return result;
}
public static int[] merge(int[] array1, int[] array2) {
int totalElements = array1.length + array2.length, array2Index = 0;
int[] toReturn = new int[totalElements];
for (int i = 0; i < array1.length; i++) {
toReturn[i] = array1[i];
}
for (int i = array1.length; i < totalElements; i++) {
toReturn[i] = array2[array2Index++];
}
//Manual Ascending Array Sorting
for (int i = 0; i < totalElements; i++) {
for (int j = 0; j < totalElements; j++) {
if(toReturn[i] < toReturn[j]) {
int temp = toReturn[i];
toReturn[i] = toReturn[j];
toReturn[j] = temp;
}
}
}
return toReturn;
}
You can use apache commons class and the sort function of Collections class
import org.apache.commons.lang.ArrayUtils;
import java.util.Collections;
public class MergingArrays {
public static void main(String[] args) {
int [] array1 = {4, 8, 12, 13};
int [] array2 = {1, 9, 11};
merge(array1, array2);
}
public static int[] merge(int array1[], int array2[]) {
return Collections.sort(ArrayUtils.addAll(array1, array2));
}
}
Related
By using following I am able to find most frequently occurring integer in an array. But following code it will not work for few scenarios. How can I fix the code inside for loop? I want to enhance this approach only.
class FindingMostFrequencyOccur {
public static void main(String args[]) {
int A[] = { 1, 2, 3, 3, 1, 3, 1};
int M = 3; // Maximum Number in Array
int result = findFrequency(M, A);
System.out.println("Result "+result);
}
static int findFrequency(int M, int[] A) {
int N = A.length;
int[] count = new int[M + 1];
for (int i = 0; i <= M; i++)
count[i] = 0;
int maxOccurence = 1;
int index = -1;
for (int i = 0; i < N; i++) {
if (count[A[i]] > 0) {
int tmp = count[A[i]];
if (tmp > maxOccurence) {
maxOccurence = tmp;
index = i;
}
count[A[i]] = tmp + 1;
} else {
count[A[i]] = 1;
}
}
return A[index];
}
}
Can you we improve in this line
if (count[A[i]] > 0) {
int tmp = count[A[i]];
if (tmp > maxOccurence) {
maxOccurence = tmp;
index = i;
}
count[A[i]] = tmp + 1;
} else {
count[A[i]] = 1;
}
To get the frequency of m in a use:
static int findFrequency(int m, int[] a) {
return (int)IntStream.of(a).filter(i-> i==m).count();
}
To get a map of all frequencies in a use:
static Map<Integer, Integer> findFrequency(int[] a) {
Map<Integer, Integer> m = new HashMap<>();
IntStream.of(a).distinct().forEach(i->{
m.put(i, findFrequency(i,a));
});
return m;
}
In this logic, you take each element of the array, and find the number of times it is repeated to the right of it, in the array. This is given by local_frequency. If this happens to be more than max_frequency, then this number at arr[i] is stored in number, and then max_frequency stores local_frequency
public static void main(String[] args)
{
int[] arr = {1, 2, 3, 4, 3, 2, 1, 5, 5, 5, 4, 4, 3, 4};
int result = findMostFrequent(arr);
System.out.println(result + " is the most frequent number");
}
public static int findMostFrequent(int[] arr)
{
int number = arr[0];
int maxFrequency = 0;
for(int i =0 ; i < arr.length; i++)
{
int local_frequency = 0;
for(int j = i; j < arr.length; j++)
{
if(arr[i] == arr[j])
local_frequency++;
}
if(local_frequency > maxFrequency)
{
number = arr[i];
maxFrequency = local_frequency;
}
}
return number;
}
public static void main(String[] args) {
int[][] arr = {{2, 3, 4}, {3, 4, 5, 2}};
System.out.println(line(arr));
}
public static int[] line(int[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
size++;
}
}
int[] array = new int[size];
int place = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
array[place] = arr[i][j];
place++;
}
}
return array;
}
The eror I'm getting is
----jGRASP exec: java Problem
[I#15db9742
----jGRASP: operation complete.
Every object has a toString() method, and the default method is to display the object's class name representation, then "#" followed by its hashcode. So what you're seeing is the default toString() representation of an int array. To print the data in the array, you can use
System.out.println(java.util.Arrays.toString(line(arr)));
Or you can loop through the array with a for loop like this
int [] res = line(arr);
for(int i=0;i<res.length;i++){
System.out.println(res[i]);
}
import java.util.Arrays;
public class TEst {
public static void main(String[] args) {
int[][] arr = { { 2, 3, 4 }, { 3, 4, 5, 2 } };
System.out.println(Arrays.toString(line(arr)));
}
public static int[] line(int[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
size++;
}
}
int[] array = new int[size];
int place = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
array[place] = arr[i][j];
place++;
}
}
return array;`enter code here`
}
}
As far as i can see it is working:
public static void main(String[] args) {
int[][] arr = {{2, 3, 4}, {3, 4, 5, 2}};
int[] i = line(arr);
Arrays.stream(i).forEach(x -> System.out.println(x + ""));
}
public static int[] line(int[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
size++;
}
}
int[] array = new int[size];
int place = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
array[place] = arr[i][j];
place++;
}
}
return array;
}
the only thing is when you are writing out your result you get the object reference not the array in plain text.
2
3
4
3
4
5
2
Process finished with exit code 0
I am trying to merge to arrays of int type with the same size. Here's my code
public class classA
{
static int[] mergeArray(int[] arr1, int arr2[])
{
int arr3[] = new int[arr1.length+ arr2.length];
int count = 0;
for(int i = 0; i < arr1.length; i++){
arr3[count] = arr1[i];
count++;
arr3[count] = arr2[i];
}
for(int i = 0; i < arr3.length; i++){
System.out.print(arr3[i]);
}
return arr3;
}
public static void main(String[] args) throws IOException
{
int arr1[] = {1,2,3};
int arr2[] = {4,5,6};
int arr3[] = mergeArray(arr1,arr2);
}
}
When I try printing the numbers in first for loop, it gives me 1,4,2,5,3,6 which the correct output. But, When I try printing it outside the first for loop it gives me output 1,2,3,6,0,0. Can someone help me?
TIA
In your for loop, when you copy from arr2 you need to increment your count.
arr3[count] = arr2[i];
count++;
You could also simplify your code a bit like,
static int[] mergeArray(int[] arr1, int[] arr2) {
int[] arr3 = new int[arr1.length * 2];
int count = 0;
for (int i = 0; i < arr1.length; i++) {
arr3[count++] = arr1[i];
arr3[count++] = arr2[i];
}
return arr3;
}
And print in main like
int arr3[] = mergeArray(arr1, arr2);
System.out.println(Arrays.toString(arr3));
or in Java 8+, use a flatMap and IntStream like
static int[] mergeArray(int[] arr1, int[] arr2) {
return IntStream.range(0, arr1.length)
.flatMap(i -> IntStream.of(arr1[i], arr2[i])).toArray();
}
for the same result.
Following your code, you forget to upgrade the count after appending the second value:
public class classA
{
static int[] mergeArray(int[] arr1, int arr2[])
{
int arr3[] = new int[arr1.length+ arr2.length];
int count = 0;
for(int i = 0; i < arr1.length; i++){
arr3[count] = arr1[i];
count++;
arr3[count] = arr2[i];
count++; //////////////// here!
}
for(int i = 0; i < arr3.length; i++){
System.out.print(arr3[i]);
}
return arr3;
}
public static void main(String[] args) throws IOException
{
int arr1[] = {1,2,3};
int arr2[] = {4,5,6};
int arr3[] = mergeArray(arr1,arr2);
}
}
explain:
in you code you do count++; just once in the foor loop, but, the foor loop goes from 0 to arr1.length, the is equal to arr3.length/2, it means you forget the half of the values, that's why the 0's appear here, because when you start a new array the defaul value in int[] are 0.
Use arr3[count+1] = arr2[i] and count += 2 instead.
It could be easily understood than using count++ twice.
static int[] mergeArray(int[] arr1, int arr2[])
{
int arr3[] = new int[arr1.length+ arr2.length];
int count = 0;
for(int i = 0; i < arr1.length; i++){
arr3[count] = arr1[i];
arr3[count+1] = arr2[i]; ///////here
count+=2; ///////and here
}
for(int i = 0; i < arr3.length; i++){
System.out.print(arr3[i]);
}
return arr3;
}
You simply need to add two values and then increase the kth value.
static int[] array1 = { 1, 2, 3, 4,9 };
static int[] array2 = { 5, 6, 7, 8 };
private static void mergeArrays(int[] arr1, int[] arr2) {
int[] arr3 = new int[arr1.length + arr2.length];
int k = 0;
for (int i = 0,j=0; i < arr1.length && j < arr2.length; i++,j++) {
arr3[k] = arr1[i];
arr3[++k] = arr2[j];
k++;
}
for (int i = 0; i < arr3.length; i++) {
System.out.println("arr3: " + arr3[i]);
}
}
I have an array where a = [2,3,4,5,6].
I want output as multiplication of all array elements as
[2*3,2*4,2*5,2*6, 3*4,3*5,3*6, and so on till 5*6] and output will be in the following format
a = [6,8,10,12,12,15,18.....30]
I have following program - what modification do I need to do
import java.io.*;
import java.util.*;
import java.lang.Math;
class Multipy
{
static void modify(int arr[], int n)
{
int prev = arr[0];
for (int i=0; i<n-1; i++)
{
arr[i] = prev * arr[i+1];
}
}
public static void main(String[] args)
{
int arr[] = {2,3,4,5,6};
int n = arr.length;
modify(arr, n);
for (int i=0; i<n-1; i++)
System.out.print(arr[i]+" ");
}
}
You should return int array as result.
static int[] modify(int[] array) {
int length = array.length;
int[] result = new int[length * (length - 1) / 2];
int k = 0;
for (int i = 0; i < length; ++i)
for (int j = i + 1; j < length; ++j)
result[k++] = array[i] * array[j];
return result;
}
And
int[] array = {2, 3, 4, 5, 6};
System.out.println(Arrays.toString(modify(array)));
result
[6, 8, 10, 12, 12, 15, 18, 20, 24, 30]
Here's the problem: Write a method called swapPairs that accepts an array of integers and swaps the elements at adjacent indexes. That is, elements 0 and 1 are swapped, elements 2 and 3 are swapped, and so on. If the array has an odd length, the final element should be left unmodified. For example, the list {10,20,30,40,50} should become {20,10,40,30,50} after a call to your method.
Write method printArray that is passed an array and will print out each element.
Use this method to print the array modified by swapPairs.
This is my code:
public static void swapPairs(int[] a){
int len=a.length;
if(len%2 ==0){
for(int i=0; i<len; i=i+2){
a[i]=a[i+1];
a[i+1]=a[i];
int[] b={a[i]+a[i+1]};
}
}
if(len%2 !=0){
for(int j=0; j<len; j=j+2){
a[j]=a[j+1];
a[j+1]=a[j];
a[len-1]=a[len-1];
int[] b={a[j]+a[j+1]+a[len-1]};
}
}
}
public static void printArray(int[] a){
System.out.println(a);
}
However, what it returns is [I#2a139a55
What you need to print is Arrays.toString(a)
Now, you are just printing the Hashcode of your Array object
First, your swap method could be simplified. Add both numbers together, and then subtract each from the sum (to get the other number). Something like,
public static void swapPairs(int[] a) {
for (int i = 0; i < a.length - 1; i += 2) {
int c = a[i] + a[i + 1];
a[i] = c - a[i];
a[i + 1] = c - a[i + 1];
}
}
Then you could use Arrays.toString(int[]) to get a String. Like,
public static void printArray(int[] a) {
System.out.println(Arrays.toString(a));
}
I tested the above like
public static void main(String[] args) {
int[] t = { 1, 2, 3, 4 };
printArray(t);
swapPairs(t);
printArray(t);
}
And I got
[1, 2, 3, 4]
[2, 1, 4, 3]
After almost breaking my computer several times, here's the actual working code:
public static void swapPairs(int[] a){
int len=a.length;
if(len%2 ==0){
for(int i=0; i<len; i=i+2){
int c=a[i]+a[i+1];
a[i]=c-a[i];
a[i+1]=c-a[i+1];
}
}
if(len%2 !=0){
for(int j=0; j<len-1; j=j+2){
int c=a[j]+a[j+1];
a[j]=c-a[j];
a[j+1]=c-a[j+1];
}
a[len-1]=a[len-1];
}
}
public static void printArray(int[] a){
int len=a.length;
for(int i=0;i<len;i++)
System.out.print(a[i]+" ");
}
public static void swapPairs(int[] arr){
int length = arr.length%2 == 0? arr.length : arr.length-1;
for(int i=0; i<length; i=i+2) {
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
// print the array
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
public static void swapPairs(int[] a){
int len = a.length - a.length % 2; // if len is odd, remove 1
for(int i = 0; i < len; i += 2) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
public static void printArray(int[] a){
System.out.println(Arrays.toString(a));
}
public static void main(String[] args) {
int[] iArr1 = {10, 20, 30, 40, 50};
int[] iArr2 = {10, 20, 30, 40, 50, 60};
swapPairs(iArr1);
swapPairs(iArr2);
printArray(iArr1); // [20, 10, 40, 30, 50]
printArray(iArr2); // [20, 10, 40, 30, 60, 50]
}
final int[] number = new int[] { 1, 2, 3, 4, 5, 6, 7 };
int temp;
for (int i = 0; i < number.length; i = i + 2) {
if (i > number.length - 2) {
break;
}
temp = number[i];
number[i] = number[i + 1];
number[i + 1] = temp;
}
for (int j = 0; j < number.length; j++) {
System.out.print(number[j]);
}
import java.util.Scanner;
public class SwapEveryPair {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = scn.nextInt();
}
for (int i = 0; i < (arr.length - 1); i += 2) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
for(int i=0; i< arr.length; i++){
System.out.println(" " + arr[i]);
}
}
}``
I think this should work,
public static void swapAlternate(int[] input){
for(int i=0;i<input.length - 1; i+=2){
int temp = input[i];
input[i] = input[i+1];
input[i+1] = temp;
}
}