Problem Statement
Given two arrays A[] and B[] of N and M integers respectively. The task is to count the number of unordered pairs formed by choosing an element from array A[] and other from array B[] in such a way that their sum is an even number.
Note that an element will only be a part of a single pair.
Input: A[] = {9, 14, 6, 2, 11}, B[] = {8, 4, 7, 20}
Output: 4
{9, 7}, {14, 8}, {6, 4} and {2, 20} are the valid pairs.
Source
https://www.geeksforgeeks.org/count-pairs-from-two-arrays-with-even-sum/
My Problem
I am wondering how output doesn't have so many other pairs whose sum will be even i.e. {11,7}, {2,3} etc. and many others.
As you mentioned in the question, Element will only be a part of single pair
{11,7} and {2,3} are already considered in {9, 7} and {2, 20}.
It's given in the problem statement,
Note that an element will only be a part of a single pair.
That means 7 has already been paired in {9, 7} so you can't pair {11, 7}
Related
Let say I have Array(string, doesn't matter) of size n.
For example int array: int[] MyArr = {1, 2, 3, 4, 5};
Array length is 5, I want to know number of subsets with length of 3 elements from this array without rearranging elements of this array, it is easy for small arrays to calculate it but I would like to know general formula for array of n elements and subset of m consecutive elements.
For example above it is
{1, 2, 3}
{2, 3, 4}
{3, 4, 5}
not {1, 2, 4} or {2, 5, 1}, only consecutive elements
Note: array shouldn't be sorted
In C, is there a way to print an entire array. For example print (in full) each row from a 2D array, NOT iterating through its contents and printing them independently? Below is an example of how I imagined the code would be:
int main() {
int Numbers[4][4] = {{5, 1, 1, 6},
{2, 2},
{3, 3, 3},
{4, 4, 4}};
for (int i=0; i<=3; i++){
printf("%i \n", Numbers[i]);
}
return 0;
}
I expect the output to be along the lines of:
{5, 1, 1, 6}
{2, 2}
{3, 3, 3}
{4, 4, 4}
However, when run the code I get the following output
-414054224
-414054208
-414054192
-414054176
No, there is no way to do what you are asking. You cannot do operations on whole arrays in C; you must iterate through and print each element separately. Array expressions in C lose their "array-ness" under most circumstances.
How do I use a for loop to add array 1 to array 2 and become the expected array like the example below?
What I expect is adding the first element in array 1 to array 2's second 1234 and continue adding up:
Array 1 = [4, 8]
Array 2 = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Expected Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
I am thinking of something like this, but just feel very strange.
N=4
for (int i=N; i<array.size; i++)
{
array2[i] = array1[...];
}
I guess your question is when
int array1[] = {4, 8};
int array2[] = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};
int arrayAnswer[] = {array2[0],array2[1],array2[2],array2[3],
array2[0]+array1[1],array2[1]+array1[1],array2[2]+array1[1],array2[3]+array1[1],
array2[0]+array1[2],array2[1]+array1[2],array2[2]+array1[2],array2[3]+array1[2]}
is it right? if so, please leave the comment and I will edit more
First of all, the question you made is not a good question. Please try to give more detailed information about your problem next time.
Answer for your question is, skip fist four member of array and add array1[1] on from array2[4] to array [7], and add array1[2] on from array2[8] to array [11]. You are going to need two loop. such as
for (i = 0; i < 2; i++){
for (j = 0; j < 4; j++){
//your function
}
}
I am not going to give you direct answer for the problem so that you can design your own. However, if your first array is {0, 4, 8}, your coding is going to be much easier. ty
As I know to get Stream from 2 dimensional array but I want to know how I can get Stream from below 3 dimensional array?
int[][][] data = {
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
},
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
};
If you can do it with a two-dimensional array then doing it for N dimensional array is not that difficult.
The solution can be done as follows:
IntStream result = Arrays.stream(data)
.flatMap(Arrays::stream)
.flatMapToInt(Arrays::stream);
To better help understand what is going on above, you can split the method invocations as follows:
// the call to Arrays.stream yields a Stream<int[][]>
Stream<int[][]> result1 = Arrays.stream(data);
// the call to flatMap yields a Stream<int[]>
Stream<int[]> result2 = result1.flatMap(Arrays::stream);
// the call to flatMapToInt yields a IntStream
IntStream intStream = result2.flatMapToInt(Arrays::stream);
You just need to call flatMap another time to change the stream from int[][] to stream of int[].
IntStream stream = Arrays.stream(data)
.flatMap(twoDArray -> Arrays.stream(twoDArray))
.flatMapToInt(oneDArray -> Arrays.stream(oneDArray));
How to initialize three dimensional char array without pointers in c and access it?
I tried the following:
char card[1][3][15]={
{"iron","man"},
{"contagious","heide"},
{"string","middle"}
};
but I am getting
**Error:too many initializers**
**Warning: Array is only partially initialized**
Lets take a simple example...You can use your own values instead of these integers:
declaration:
int arr[2][3][4] = { { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} },
{ {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} } };
I hope, it is clear to you.
Considering your example itself:
I think it should be
char card[1][3][15]={ {"iron","man", "contagious"}};
What this means is that you can effectively create 3 char arrays each of length 15. Your first dimension of 1 doesn't have much effect.
So, you can make it like
char card[2][3][15]={ {"iron","man", "contagious"},
{"iron","man", "contagious"}};
So, for your simple understand, the number of rows indicate the first dimension, the number of columns in each row indicates the second dimension and the number of elements(in this case chars) in each column indicates the 3rd dimension.
So, now you can see that for the data in your question, you should declare the array as char char[3][2][15]
char card[1][3][15]={ { {"iron","man"},{"contagious","heide"},{"string","middle"}}
};
You should put another braces brackets inside. I think it will be helpful to you.