# hackearth-exercise-very-difficult-to-AND Operator

• ### Question

• AND Operation

You are given an array A of length N and Q tasks.

Each task is of the following type:

L R V: Apply the bitwise-and operator with V for all A, where L <= i <= R.

You need to print the array after perfoming all the task.

Input format:

-          First line: Two integers, N length of array, and Q number of tasks.

-          Second line: N space.separated elemets of the array.

-          Each of the following Q lines contains a task in the described format.

Output format:

-          Print the final arrays after performing all the Q tasks.

Constraints:

1 <= N <= 10 (potency 5)

1 <= Q <= 2*10 (potency 5)

1 <= Ai, V <= 10 (potency 9)

1 <= L <= R <= N

Sample input:

5 2

7 7 7 7 7

1 3 4

1 5 6

Sample Output:

4 4 4 6 6

Explanation:

After first task, the array will be 4,4,4,7,7

After second  task, the array will be 4,4,4,6,6

the problem as such, this exercise is supposed to happen to javascript or c #. and I do not know what to put in the code since I do not understand the problem

Thanks

Hugo Gil

Wednesday, April 24, 2019 4:53 PM

• AND Operation

You are given an array A of length N and Q tasks.

Each task is of the following type:

L R V: Apply the bitwise-and operator with V for all A, where L <= i <= R.

You need to print the array after perfoming all the task.

Input format:

-          First line: Two integers, N length of array, and Q number of tasks.

-          Second line: N space.separated elemets of the array.

-          Each of the following Q lines contains a task in the described format.

Output format:

-          Print the final arrays after performing all the Q tasks.

Constraints:

1 <= N <= 10 (potency 5)

1 <= Q <= 2*10 (potency 5)

1 <= Ai, V <= 10 (potency 9)

1 <= L <= R <= N

Sample input:

5 2

7 7 7 7 7

1 3 4

1 5 6

Sample Output:

4 4 4 6 6

Explanation:

After first task, the array will be 4,4,4,7,7

After second  task, the array will be 4,4,4,6,6

I do not know what to put in the code since I do not understand the problem

When asking for help on a project such as this you *must* be specific about what
part(s) of the project you can't handle. You can't expect us to go into deep
detail of every aspect of the assignment, when you may already understand
much of it. Don't waste our time offering advice that you don't need. Be very

Now as to the project itself, let's look at component parts that you should be
able to program yourself. What follows is my interpretation of the specs.

(1) Get line 1, with or without prompting for the expected data.
(2) Convert the substrings to integers,
(3) Enforce the constraints for N and Q.
(4  Allocate the array.
(5) Get line 2.
(6) Convert the values to integers and ensure that they are valid, and of the
right number of values for the array size.
(7) Populate the array with the values.
(8) Get the number of tasks specified by Q one line at a time.
(9) Convert the substrings from each line to integers.
(10) Enforce the constraints for L, R and V.
(11) Do a bitwise AND using V to the elements of array A, from L to R.
e.g. - 1 3 4 means do a bitwise AND of 4 and each element from 1 to 3
(one-relative numbering - convert to zero-relative for indexing) of
array A.
(12) When all Q tasks have been done, display the values in array A.

Explanation of examples:

Array A is allocated with a size of 5.

Array A is populated with 5 values, each of which is 7 in the example.

The first task 1 3 4 is applied:
Enforce the constraints for L, R and V.
A bitwise AND (&=) is done on the first three elements of array A using
value V (4).
Starting values in array: 7 7 7 7 7
When you do 7 & 4 you get 4.
As only the first three elements of A are altered in this task,
the result is 4 4 4 7 7

The second task 1 5 6 is applied:
Enforce the constraints for L, R and V.
A bitwise AND (&=) is done on the first five elements of array A using
value V (6).
Starting values in array: 4 4 4 7 7
When you do 4 & 6 you get 4.
When you do 7 & 6 you get 6.
As all five elements of A are altered in this task,
the result is 4 4 4 6 6

Check/enforce all of the described constraints to prevent erroneous data.

There is an unfortunate history in these forums of some responders being
unable to curtail their enthusiasm and therefore they often post complete
code solutions to problems. Some even do this when the OP has clearly stated
that it is an assignment. Hopefully they will show some restraint and allow
you to try and work out the coding yourself.

I could give you a complete program, but that would be unethical and do you
more harm than good as far as learning goes. When I get a minute I may post
an executable of an implementation of what I have described above. This will
allow you to download it if you wish and see how it processes different input
values.

- Wayne

• Marked as answer by Thursday, April 25, 2019 2:30 PM
Thursday, April 25, 2019 5:24 AM

### All replies

• Hello,

Who gave you this? Is this a work or homework assignment?

Either way you need to make an effort to write code to some degree then ask for help. In regards to not understanding the problem, talk to the person who gave it to you.

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

Wednesday, April 24, 2019 5:07 PM
• The fact is that it is a holiday task, I have no way of asking, that is why I asked for help in the forum

Hugo Gil

Wednesday, April 24, 2019 5:14 PM
• AND Operation

You are given an array A of length N and Q tasks.

Each task is of the following type:

L R V: Apply the bitwise-and operator with V for all A, where L <= i <= R.

You need to print the array after perfoming all the task.

Input format:

-          First line: Two integers, N length of array, and Q number of tasks.

-          Second line: N space.separated elemets of the array.

-          Each of the following Q lines contains a task in the described format.

Output format:

-          Print the final arrays after performing all the Q tasks.

Constraints:

1 <= N <= 10 (potency 5)

1 <= Q <= 2*10 (potency 5)

1 <= Ai, V <= 10 (potency 9)

1 <= L <= R <= N

Sample input:

5 2

7 7 7 7 7

1 3 4

1 5 6

Sample Output:

4 4 4 6 6

Explanation:

After first task, the array will be 4,4,4,7,7

After second  task, the array will be 4,4,4,6,6

I do not know what to put in the code since I do not understand the problem

When asking for help on a project such as this you *must* be specific about what
part(s) of the project you can't handle. You can't expect us to go into deep
detail of every aspect of the assignment, when you may already understand
much of it. Don't waste our time offering advice that you don't need. Be very

Now as to the project itself, let's look at component parts that you should be
able to program yourself. What follows is my interpretation of the specs.

(1) Get line 1, with or without prompting for the expected data.
(2) Convert the substrings to integers,
(3) Enforce the constraints for N and Q.
(4  Allocate the array.
(5) Get line 2.
(6) Convert the values to integers and ensure that they are valid, and of the
right number of values for the array size.
(7) Populate the array with the values.
(8) Get the number of tasks specified by Q one line at a time.
(9) Convert the substrings from each line to integers.
(10) Enforce the constraints for L, R and V.
(11) Do a bitwise AND using V to the elements of array A, from L to R.
e.g. - 1 3 4 means do a bitwise AND of 4 and each element from 1 to 3
(one-relative numbering - convert to zero-relative for indexing) of
array A.
(12) When all Q tasks have been done, display the values in array A.

Explanation of examples:

Array A is allocated with a size of 5.

Array A is populated with 5 values, each of which is 7 in the example.

The first task 1 3 4 is applied:
Enforce the constraints for L, R and V.
A bitwise AND (&=) is done on the first three elements of array A using
value V (4).
Starting values in array: 7 7 7 7 7
When you do 7 & 4 you get 4.
As only the first three elements of A are altered in this task,
the result is 4 4 4 7 7

The second task 1 5 6 is applied:
Enforce the constraints for L, R and V.
A bitwise AND (&=) is done on the first five elements of array A using
value V (6).
Starting values in array: 4 4 4 7 7
When you do 4 & 6 you get 4.
When you do 7 & 6 you get 6.
As all five elements of A are altered in this task,
the result is 4 4 4 6 6

Check/enforce all of the described constraints to prevent erroneous data.

There is an unfortunate history in these forums of some responders being
unable to curtail their enthusiasm and therefore they often post complete
code solutions to problems. Some even do this when the OP has clearly stated
that it is an assignment. Hopefully they will show some restraint and allow
you to try and work out the coding yourself.

I could give you a complete program, but that would be unethical and do you
more harm than good as far as learning goes. When I get a minute I may post
an executable of an implementation of what I have described above. This will
allow you to download it if you wish and see how it processes different input
values.

- Wayne

• Marked as answer by Thursday, April 25, 2019 2:30 PM
Thursday, April 25, 2019 5:24 AM
• Thank you very much, this is really what we are looking for, that they will explain the problem to me.