Home  >  Article  >  Web Front-end  >  JavaScript program to find array sum using bitwise OR after splitting given array into two halves after K circular shifts

JavaScript program to find array sum using bitwise OR after splitting given array into two halves after K circular shifts

WBOY
WBOYforward
2023-09-21 10:37:021038browse

JavaScript 程序在 K 循环移位后将给定数组分成两半后使用按位或查找数组总和

We will write a JavaScript program that splits a given array into two halves after K circular shifts and then finds the sum of the array using bitwise OR. Our program will perform the task by taking as input an array and an integer K. First, after performing K circular shifts, we split the array in half. We will then perform a bitwise OR on the two halves to get a new array. Finally, find the sum of the new arrays obtained by bitwise OR operation.

method

  • First, perform K circular shifts on the given array.

  • Split the shifted array into two halves.

  • Performs a bitwise OR operation on each element of both halves of the array.

  • Add all the elements obtained in step 3 to get the result.

  • After performing a bitwise OR operation and splitting the array in half, return the result as the sum of the arrays.

Example

Here is an example of how to sum an array using bitwise OR after splitting the array in half after K circular shifts -

function splitArrayAndGetSum(array, k) {
   let splitArray = [];
   let sum = 0;
   
   // Split the array into two halves after K circular shifts
   for (let i = 0; i < array.length; i++) {
      splitArray[i % 2] = splitArray[i % 2] | array[(i + k) % array.length];
   }
   
   // Get the sum of the two halves using Bitwise OR
   for (let i = 0; i < splitArray.length; i++) {
      sum = sum | splitArray[i];
   }
   return sum;
}
let array = [1, 2, 3, 4, 5, 6, 7];
let k = 3;
console.log(splitArrayAndGetSum(array, k));

illustrate

  • This function takes an array and an integer k as input.

  • VariablesplitArray is used to store the two halves of the split array.

  • Variablesum is used to store the sum of the two halves of the array.

  • In the first for loop, after k circular shifts, the array is divided into two halves. Use the modulo operator % to split the array in half. The value of each element in the two halves is obtained by using the bitwise OR operator | and the value of the corresponding element in the original array after k circular shift.

  • In the second for loop, use the bitwise OR operator | to get the sum of the two halves. The final result is stored in the variable sum.

  • Finally, the splitArrayAndGetSum function returns the value of sum.

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

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete