Home > Backend Development > C++ > body text

After splitting the given array into two halves, after performing K circular shifts, find the sum of the array using bitwise OR

王林
Release: 2023-08-27 23:05:06
forward
1123 people have browsed it

After splitting the given array into two halves, after performing K circular shifts, find the sum of the array using bitwise OR

In C, splitting an array means dividing the array into multiple subarrays. The bitwise OR operator handles comparisons and calculations between two bitwise OR indices in C. In this article, we use k circular shifts, which means that the last index position will be moved to the zero index position, i.e., to the first array element according to k times.

Let us take an example to understand circular shift in array.

The given array is 1, 2, 3, 4, 5, 6, 7, with a length of 6.

Now we assign the value 3 to k, which means k circular shifts.

The operation steps of circular shift are as follows:

Step 1 − We move index[6] to index[1], and then index[5] saves the position of index[6]. The first circular shift becomes 7,1,2,3,4,5,6, so the circular shift will rotate.

Step 2 - Second circular shift - 6,7,1,2,3,4,5

The third step - The third circular shift-5,6,7,1,2,3,4 (final result)

grammar

vector l; vector_name(parameter 1, parameter 2)
Copy after login
  • Vector is used as a keyword and the data type is a data type mentioned by the user. Finally, vector_name represents the name of the user-assigned vector.

  • The first parameter specifies the size of the vector. The second parameter is the value used to initialize each element of the vector.

splitArray[i%2] = splitArray[i%2] | arr[(i+k)%arr.size()]
Copy after login
  • Split the array into two halves using the mod(%) operator. By using bitwise OR '|' we can get the value of each element in the two halves. value.

  • 'arr[(i k)%arr.size()]' Represents the shift index when performing a bitwise OR operation, which will give the two elements in the split array One, this depends on the correspondence of 'splitArray[i%2]'.

algorithm

  • We will use the header files 'iostream' and 'vector' to start the program.

  • We will define a function called 'Split_arr_sum_bitwise', which accepts 'arr' and 'k' as parameters. This function receives an array value and an updated array value after a circular shift.

  • We will initialize the vector variable named 'splitArray' inside the 'Split_arr_sum_bitwise' function. This function will store both halves of the array.

  • Next, use the bitwise OR operator '|' to store the value 0 in the variable sum, which will later be added to the array with a function named 'splitArray' .

  • Then we create the first for loop where we will iterate over the original array.

  • We will then create a second for loop that calculates the sum of the two halves using the bitwise operators ‘|’. This operator will split the array into two halves after K circular shifts.

  • Now begins the main function, here we initialize the array value into the 'array' variable and store the value '3' in the variable 'k ', this variable will define the number of rotation shifts for the given array.

  • Finally, in the print statement, we call the function named 'Split_arr_sum_bitwise' and pass the parameters 'K' and 'array' Pass it to get the final output.

The Chinese translation of

Example

is:

Example

In this program, we will use the bitwise OR operator to implement array sum, and then split the array into two halves after K circular shifts.

#include 
#include 
using namespace std;
int Split_arr_sum_bitwise(vector& arr, int k) {
   vector splitArray(2,0);
   int sum = 0;
   // Splitting the array into two halves after K-Circular Shift.
   for (int i = 0; i < arr.size(); i++) {
      splitArray[i % 2] = splitArray[i % 2] | arr[(i + k) % arr.size()];
   }
   // Sum of two halves using bitwise OR
   for (int i = 0; i < splitArray.size(); i++) {
      sum = sum | splitArray[i];
   }
   return sum;
}
int main() {
   vector array = {1, 2, 3, 4, 5, 6, 7};
   int k = 3;
   cout <<"The split sum of array halves using bitwise OR is "<
Copy after login

Output

The split sum of array halves using bitwise OR is 7
Copy after login

in conclusion

We explored the concept of K circular shifts, where an array is split in half, and also saw how the bitwise OR operation performs a sum to store the values ​​of the split array. The % modulo operator splits the array in half, showing the parity calculation at the array index position.

The above is the detailed content of After splitting the given array into two halves, after performing K circular shifts, find the sum of the array using bitwise OR. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!