Adding an element before particular value in an array - java

I need to add an element before every occurrence of a particular element. I can do this using for loop and calculate the index and then add the element. Is there any efficient method without using for loop or using streams.
e.g: int[] a = {1,2,4,5,7,9,2,5}
Add element {3} before every occurrence of 2 which result in {1,3,2,4,5,7,9,3,2,5}
Attempt:
int[] a = {1, 2, 4, 5, 7, 9, 2, 5};
int[] new1 = null;
int[] indexToAdd = IntStream.range(0, a.length)
.filter(i -> a[i] == 2)
.map(i -> i)
.toArray();
for(int j = 0; j<indexToAdd.length; j++){
final Integer innerj = new Integer(j);
new1 = IntStream.range(0,a.length)
.map(i -> {
if (i < indexToAdd[innerj]) {
return a[i];
} else if (i == indexToAdd[innerj]) {
return 3 ;
} else {
return a[i - 1];
}
}).toArray();
}

The most efficient way is to create another array on the fly so that we always add elements at the end which is O(1) as compared to adding elements somewhere in the middle of the array which is O(n):
int[] arr1 = {1,2,4,5,7,9,2,5};
List<Integer> list2 = new ArrayList<>();
for(int i=0; i < arr1.length; i++)
{
int elem = arr1[i];
if(elem == 2)
{
list2.add(3);
}
list2.add(elem);
}
Integer[] arr2 = list2.toArray(new Integer[0]);
arr1 = Arrays.stream(integers).mapToInt(i->i).toArray();
Another 1 liner using streams:
int[] arr1 = {1, 2, 4, 5, 7, 9, 2, 5};
arr1 = Arrays.stream(arr1)
.flatMap(x -> x == 2 ? Stream.of(3, x).mapToInt(i -> i) : Stream.of(x).mapToInt(i -> i))
.toArray();

Related

all number have been rebeated less than N times

i'm tring to return an array that have all number have been rebeated less than N times.
expected output : 1,4
this is my cod :
-- Main --
int[] data = {1, 2, 2, 3, 3, 3, 4, 5, 5};
int n = 1;
Solution.solution(data, n);
-- class Solution --
public static int[] solution(int[] data, int n) {
int l = data.length, count = 0;
int[] Narr = new int[l];
for(int i =0 ; i < l; i++){
count = 0;
for(int j=1 ; j < l ; j++){
if(data[i] == data[j]){
count++;
}
if(j == l-1){
if(count < n){
Narr[i] = data[i];
System.out.println(Narr[i]);
}
}
}
}
return Narr;
}
I would break this down to multiple steps.
First lets create a countMap that holds track of all the occurrences:
int[] data = {1, 2, 2, 3, 3, 3, 4, 5, 5};
int n = 1;
Map<Integer, Long> countMap = Arrays.stream(data).boxed()
.collect(Collectors.groupingBy(s -> s, Collectors.counting()));
System.out.println(countMap); // {1=1, 2=2, 3=3, 4=1, 5=2}
Now all we need to do is to check for the condition count <= n:
List<Integer> resultList = new ArrayList<>();
countMap.forEach((integer, count) -> {
if (count <= n) resultList.add(integer);
});
System.out.println(resultList); // [1, 4]
And last but not least converting the list back to array:
// {1, 4}
int[] resultArray = resultList.stream()
.mapToInt(Integer::intValue)
.toArray();

How to sum the elements of two 2d arrays?

I'm trying to add the elements of two two-dimensional arrays with each other, by using the java stream API.
I managed the problem with a one-dimensional array, but I don't know how to proceed further with a two-dimensional array.
Here is the code to transform:
public static int[][] add(final int[][] m1, final int[][] m2) {
int[][] e = new int[m2.length][m2[0].length];
for (int i = 0; i < m1.length; i++) {
for (int j = 0; j < m1[i].length; j++) {
e[i][j] = m1[i][j] + m2[i][j];
}
}
return e;
}
And this is the code which I wrote for the same purpose but only with a one-dimensional array:
public static int[] addOneDimension(final int[] a, final int b[]) {
int[] c = IntStream.range(0, a.length)
.map(i -> a[i] + b[i])
.toArray();
return c;
}
In particular, I don't know how to use the map() method on two-dimensional arrays.
This can be implemented using IntStream and its methods mapToObj to handle rows and map to handle elements in each row:
static int[][] add(int[][] a, int [][] b) {
return IntStream.range(0, a.length)
.mapToObj(i -> add(a[i], b[i])) // int[] is object
.toArray(int[][]::new); // create new 2D array
}
static int[] add(int[] a, int[] b) {
return IntStream.range(0, a.length)
.map(i -> a[i] + b[i]) // processing int operands
.toArray(); // IntStream.toArray() returns int[]
}
Test
int[][] a = {
{1, 2},
{3, 4}
};
int[][] b = {
{10, 20},
{30, 40}
};
System.out.println("Sum of a + b = ");
Arrays.stream(add(a, b))
.map(Arrays::toString)
.forEach(System.out::println);
Output
Sum of a + b =
[11, 22]
[33, 44]
Single-method implementation may be as follows:
static int[][] add2D(int[][] a, int [][] b) {
return IntStream
.range(0, a.length)
.mapToObj(i -> IntStream
.range(0, a[i].length)
.map(j -> a[i][j] + b[i][j])
.toArray()
)
.toArray(int[][]::new);
}
You can use Stream#reduce method to sum the elements of two or more 2d arrays:
public static int[][] sumArrays(int[][]... arrays) {
// reduce the stream of arrays into a single
// array by sequentially summing array pairs
return Arrays.stream(arrays).reduce((a1, a2) -> IntStream
// iterate over the indices of
// the rows of the largest array
.range(0, Math.max(a1.length, a2.length))
.mapToObj(i -> IntStream
// iterate over the indices of
// the cells of the largest row
.range(0, Math.max(
i < a1.length ? a1[i].length : 0,
i < a2.length ? a2[i].length : 0))
// sum the elements of two rows if exist, or 0 otherwise
.map(j -> (i < a1.length && j < a1[i].length ? a1[i][j] : 0)
+ (i < a2.length && j < a2[i].length ? a2[i][j] : 0))
.toArray())
.toArray(int[][]::new))
.orElse(null);
}
// test
public static void main(String[] args) {
int[][] arr1 = {
{1},
{1, 1}};
int[][] arr2 = {
{2},
{2, 2},
{2, 2, 2}};
int[][] arr3 = {
{3, 3, 3, 3},
{3, 3, 3, 3},
{3, 3, 3, 3},
{3, 3, 3, 3}};
int[][] sum = sumArrays(arr1, arr2, arr3);
// output
Arrays.stream(sum).map(Arrays::toString).forEach(System.out::println);
//[6, 3, 3, 3]
//[6, 6, 3, 3]
//[5, 5, 5, 3]
//[3, 3, 3, 3]
}
See also: Sum of 2 different 2d arrays

Combing two arrays in ascending order with unknown values inside the array

I have managed to create both arrays, however I can't figure out how to combine the two arrays. Every tutorial I see merges them as such:
int[] arr1 = {3, 3, 5, 6, 8, 9};
int[] arr2 = {3, 4, 5, 6};
// Output: 3, 4, 5, 6, 8, 9
What I need is something that would output: 3, 3, 3, 4, 5, 5, 6, 6, 8, 9
Here is the code I have written so far:
import java.util.Scanner;
public class Merger {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] arr1 = new int[10000];
int[] arr2 = new int[10000];
int[] merged = new int[20000];
int a1 = 0;
int a2 = 0;
int size = -1;
int size2 = -1;
int sizecheck = 0;
int size2check = 0;
System.out
.println("Enter the values for the first array, up to 10,000 values, enter a negative number to quit");
for (int i = 0; i < arr1.length; i++) {
arr1[i] = scan.nextInt();
merged[i] = arr1[i];
if (arr1[i] <= 0) {
break;
}
if (size <= arr1[i]) {
size = arr1[i];
sizecheck++;
}
a1++;
}
System.out
.println("Enter the values for the second array, up to 10,000 values, enter a negative number to quit");
for (int i = 0; i < arr2.length; i++) {
arr2[i] = scan.nextInt();
merged[i + a1] = arr2[i];
if (arr2[i] <= 0) {
break;
}
if (size2 <= arr2[i]) {
size2 = arr2[i];
size2check++;
}
a2++;
}
System.out.println("First Array: ");
for (int i = 0; i < a1; i++) {
System.out.print(" " + arr1[i]);
}
System.out.println("\nSecond Array: ");
for (int i = 0; i < a2; i++) {
System.out.print(" " + arr2[i]);
}
}
}
This prints both arrays out, however does not combine and sort the two.
Here's the code ! There may be a faster/easier way to do it but this one works as long as the 2 arrays are sorted
public static void main(String[] args) {
int[] a1 = {1, 2, 3, 5};
int[] a2 = {1, 3, 4, 4, 4, 5};
int[] a3 = merge(a1, a2);
for (int i : a3) {
System.out.print(i);
}
}
public static int[] merge(int[] a1, int[] a2) {
int[] a3 = new int[a1.length + a2.length];
int indexA1 = 0;
int indexA2 = 0;
for (int i = 0; i < a3.length; i++) {
int n;
if (indexA1 == a1.length && indexA2 < a2.length) {
n = a2[indexA2];
indexA2++;
} else if (indexA1 < a1.length && indexA2 == a2.length) {
n = a1[indexA1];
indexA1++;
} else {
if (a1[indexA1] < a2[indexA2]) {
n = a1[indexA1];
indexA1++;
} else {
n = a2[indexA2];
indexA2++;
}
}
a3[i] = n;
}
return a3;
}
I assume that you are not yet familiar with Streams, but I would like to give you an example of what you can do with them.
Add import
import java.util.stream.IntStream;
Add this at the end of your main method
System.out.println("");
IntStream arr1Stream = IntStream.of(arr1).limit(a1); //creates an InStream with the first a1 values of arr1
IntStream arr2Stream = IntStream.of(arr2).limit(a2);
int[] both = IntStream.concat(arr1Stream, arr2Stream).sorted().toArray(); //combines the two streams, sorts them an converts them to an Array
System.out.println(Arrays.toString(both)); //easy way to print an array
The easiest way is to use a Stream.
int[] arr1 = {3, 3, 5, 6, 8, 9};
int[] arr2 = {3, 4, 5, 6};
Stream both arrays.
flatMap them to a single IntStream
sort them
convert to an array
int [] combined = Stream.of(arr1,arr2)
.flatMapToInt(Arrays::stream)
.sorted()
.toArray();
System.out.println(Arrays.toString(combined));
Prints
[3, 3, 3, 4, 5, 5, 6, 6, 8, 9]
A non-stream approach can be done as follows:
// increase arr1 to make room for arr2
int oldLen = arr1.length;
arr1 = Arrays.copyOf(arr1, arr1.length+arr2.length);
// copy arr2 starting at 0, to arr1 starting at the old length
// positon of arr1 for a length of arr2
System.arraycopy(arr2, 0, arr1, oldLen, arr2.length);
// sort and print
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
Prints
[3, 3, 3, 4, 5, 5, 6, 6, 8, 9]
Although your question, as asked, said nothing about merging sorted arrays, here is how you would do it.
The algorithm is simple. Just iterate thru each array and compare current values.
if arr1[i] <= arr2[k], copy arr1[i] to result, advance i by 1
else copy arr2[k] to result, advance k by 1.
in all cases the index to result, r, is advanced by 1
public int[] merge(int[] arr1, int[] arr2) {
// result array
int[] result = new int[arr1.length + arr2.length];
int r = 0;
int k = 0;
int i = 0;
// Iterate thru the arrays, copying the lowest or equal value
// to the target array. This process will cease when one of the arrays
// has been fully processed.
for (; i < arr1.length && k < arr2.length; ) {
for (; k < arr2.length && i < arr1.length;) {
if (arr1[i] <= arr2[k]) {
result[r++] = arr1[i++];
}else {
result[r++] = arr2[k++];
}
}
}
Having reached this far in the algorithm, one of the arrays must have been completely processed. So try and copy both. For the empty array, the while loop basically acts like an if statement.
while (i < arr1.length) {
result[r++] = arr1[i++];
}
while (k < arr2.length) {
result[r++] = arr2[k++];
}
// return the result
return result;
}
public class Merger {
public static void main(String[] args) {
int[] arr1 = { 3, 3, 5, 6, 8, 9 };
int[] arr2 = { 3, 4, 5, 6 };
int[] res = merge(arr1, arr2);
System.out.println(Arrays.toString(res));
}
public static int[] merge(int[] arr1, int[] arr2) {
int[] res = new int[arr1.length + arr2.length];
for (int i = 0, a1 = 0, a2 = 0; i < res.length; i++) {
if (a1 == arr1.length)
res[i] = arr2[a2++];
else if (a2 == arr2.length)
res[i] = arr1[a1++];
else
res[i] = arr1[a1] <= arr2[a2] ? arr1[a1++] : arr2[a2++];
}
return res;
}
}
A simple solution can be written using the classes, Arrays and System.
Steps:
Copy elements of arr1[] into a new array (say, output[]) whose size is the sum of the sizes of the given arrays.
Copy the elements of arr2[], after the element of arr1[], into output[]
Sort output[]
Demo:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr1 = { 3, 3, 5, 6, 8, 9 };
int[] arr2 = { 3, 4, 5, 6 };
// Copy elements of arr1[] into a new array whose size is the sum of the sizes
// of the given arrays
int[] output = Arrays.copyOf(arr1, arr1.length + arr2.length);
// Copy the elements of arr2[], after the element of arr1[], into output[]
System.arraycopy(arr2.clone(), 0, output, arr1.length, arr2.length);
// Sort output[]
Arrays.sort(output);
// Display output[]
System.out.println(Arrays.toString(output));
}
}
Output:
[3, 3, 3, 4, 5, 5, 6, 6, 8, 9]
You can use System.arraycopy method for this purpose:
int[] arr1 = {3, 3, 5, 6, 8, 9};
int[] arr2 = {3, 4, 5, 6};
// create a new array of total length
int[] arr3 = new int[arr1.length + arr2.length];
// copy first array to the beginning of the total array
System.arraycopy(arr1, 0, arr3, 0, arr1.length);
// copy second array to the end of the total array
System.arraycopy(arr2, 0, arr3, arr1.length, arr2.length);
// sort the total array
Arrays.sort(arr3);
System.out.println(Arrays.toString(arr3));
// [3, 3, 3, 4, 5, 5, 6, 6, 8, 9]

Print Array Combination

I have the following input
int combinationCount = 3;
int arr[] = {1, 1, 2, 2, 3};
combinationCount is {1,2,3}, combinationCount defines the number of sequence of number. For Eg: combinationCount = 3 means {1,2,3} and combinationCount = 2 means {1,2}
Array is always sorted, I want to print the output as the number of combinations as follow
[1,2,3], [1,2,3], [1,2,3], [1,2,3] //I have to iterate the whole array as it is just logic for a problem
Output Explanation (I want to print values, not index):
This is just an explanation of output which shows the index position of the value printed.
Index position of each value
[0, 2, 4], [0, 3, 4], [1, 2, 4], [1, 3, 4]
Example 2
int combinationCount = 2; // means combination is {1,2}
int arr[] = {1, 2, 2};
Print: [1,2], [1,2]
Example 3
int combinationCount = 3; // means combination is {1,2,3}
int arr[] = {1, 1, 3};
Print nothing
The program which I written is as follow:
int combinationCount = 3;
int arr[] = {1, 1, 2, 2, 3};
List<Integer> list = new ArrayList<>();
int prev = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 1) {
prev = 1;
list = new ArrayList<>();
list.add(1);
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] == prev + 1) {
prev = arr[j];
list.add(arr[j]);
} else if (arr[j] > (prev + 1)) {
break;
}
}
if (list.size() == combinationCount) {
System.out.print(list + ",");
}
} else {
break;
}
}
Output coming as
[1,2,3],[1,2,3]
which is not correct
Somewhere I am missing loop and how optimized code we can write? Any suggestions pls. Kindly let me know for any concern.
You can use Cartesian Product. I have used this answer as reference.
public class Test {
public static List<List<Integer>> product(List<List<Integer>> lists) {
List<List<Integer>> result = new ArrayList<>();
int solutions = lists.stream().mapToInt(List::size).reduce(1, (a, b) -> a * b);
for (int i = 0; i < solutions; i++) {
int j = 1;
List<Integer> tempList = new ArrayList<>();
for (List list : lists) {
tempList.add((Integer) list.get((i / j) % list.size()));
j *= list.size();
}
result.add(tempList);
}
return result;
}
public static void main(String[] args) {
int combinationCount = 2, count = 0;
int arr[] = {1, 1, 3};
Map<Integer, List<Integer>> map = new HashMap<>();
List<List<Integer>> combinations = new ArrayList<>();
for (Integer idx = 0; idx < arr.length; idx++) {
map.computeIfAbsent(arr[idx], k -> new ArrayList<>()).add(idx);
}
for (int i = 1; i <= combinationCount; i++) {
if (map.getOrDefault(i, null) != null)
count += 1;
}
if (count == combinationCount) {
List result = product(new ArrayList(map.values()));
System.out.println(result);
} else {
System.out.println("No combination found");
}
}
}
Output:
No combination found

Rank values of an Array

I have this input of array: int[] input = {1,3,2,2,33,1}; I need to make score for it like this output: {1,3,2,2,4,1} so the smallest number gets 1 and if there is smaller it gets 2 an so on.
another example: for input {1,10,3,44,5,2,5} -outputs-> {1,5,3,6,4,2,4}
This is my try but it does not work as expected.
public static int[] getRanksArray(int[] array) {
int[] result = new int[array.length];
for (int i = 0; i < array.length; i++) {
int count = 0;
for (int j = 0; j < array.length; j++) {
if (array[j] != array[i]) {
count++;
}
}
result[i] = count + 1;
}
return result;
}
Edit: Updated to handle double rather than int input array.
First sort an array representing indices of the input array. Then walk through this array incrementing a rank counter whenever you encounter successive elements that are not equal (ideone)
public static int[] rank(double[] nums)
{
Integer[] idx = new Integer[nums.length];
for(int i=0; i<idx.length; i++) idx[i] = i;
Arrays.sort(idx, (a, b) -> (Double.compare(nums[a], nums[b])));
// Or use this for descending rank
// Arrays.sort(idx, (a, b) -> (Double.compare(nums[b], nums[a])));
int[] rank = new int[nums.length];
for(int i=0, j=1; i<idx.length; i++)
{
rank[idx[i]] = j;
if(i < idx.length - 1 && nums[idx[i]] != nums[idx[i+1]]) j++;
}
return rank;
}
Test:
System.out.println(Arrays.toString(rank(new double[] {1,3,2,2,33,1})));
System.out.println(Arrays.toString(rank(new double[] {1,10,3,44,5,2,5})));
Output:
[1, 3, 2, 2, 4, 1]
[1, 5, 3, 6, 4, 2, 4]
This can be solved using a sorted map storing lists/sets of indexes mapped by the values of the input array.
Then you can iterate over this map and fill the rank array with incrementing indexes.
Implementation:
public static int[] rank(int[] arr) {
TreeMap<Integer, List<Integer>> map = new TreeMap<>();
for (int i = 0; i < arr.length; i++) {
List<Integer> indexes = map.compute(arr[i], (k, v) -> v == null ? new ArrayList<>() : v);
indexes.add(i);
map.putIfAbsent(arr[i], indexes);
}
int[] rank = new int[arr.length];
int id = 1;
for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
for(Integer i : entry.getValue()) {
rank[i] = id;
}
id++;
}
return rank;
}
Test:
int[][] d = {
{1,3,2,2,33,1},
{1,10,3,44,5,2,5}
};
for (int[] input : d) {
System.out.println(Arrays.toString(rank(input)));
}
output:
[1, 3, 2, 2, 4, 1]
[1, 5, 3, 6, 4, 2, 4]

Categories

Resources