JavaScript Program to Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts

We will write a JavaScript program to find the sum of an array by using bitwise OR after splitting the given array into two halves after K circular shifts. Our program will perform the task by taking an array and an integer K as inputs. First, we will split the array into two halves after performing K circular shifts. Then, we will perform bitwise OR on both the halves to get a new array. Finally, we will find the sum of the new array obtained from the bitwise OR operation.

Approach

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

  • Split the shifted array into two halves.

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

  • Sum up all the elements obtained from step 3 to get the result.

  • Return the result as the sum of the array after performing the bitwise OR operation and splitting the array into two halves.

Understanding Circular Shifts

A circular shift moves array elements to the right by K positions, where elements that go beyond the array length wrap around to the beginning. For example, shifting [1,2,3,4,5] by 2 positions gives [4,5,1,2,3].

Example

Here is an example of how you can find the sum of an array using Bitwise OR after splitting the array into two halves after K circular shifts:

function splitArrayAndGetSum(array, k) {
    // Perform K circular shifts and split into two halves
    let firstHalf = [];
    let secondHalf = [];
    let halfLength = Math.ceil(array.length / 2);
    
    // Split the circularly shifted array into two halves
    for (let i = 0; i < array.length; i++) {
        let shiftedIndex = (i + k) % array.length;
        if (i < halfLength) {
            firstHalf.push(array[shiftedIndex]);
        } else {
            secondHalf.push(array[shiftedIndex]);
        }
    }
    
    console.log("First half:", firstHalf);
    console.log("Second half:", secondHalf);
    
    // Perform bitwise OR between corresponding elements
    let result = [];
    let maxLength = Math.max(firstHalf.length, secondHalf.length);
    
    for (let i = 0; i < maxLength; i++) {
        let val1 = firstHalf[i] || 0;
        let val2 = secondHalf[i] || 0;
        result.push(val1 | val2);
    }
    
    console.log("After bitwise OR:", result);
    
    // Calculate sum of the result array
    let sum = result.reduce((acc, val) => acc + val, 0);
    return sum;
}

let array = [1, 2, 3, 4, 5, 6, 7];
let k = 3;
console.log("Original array:", array);
console.log("K shifts:", k);
console.log("Final sum:", splitArrayAndGetSum(array, k));
Original array: [ 1, 2, 3, 4, 5, 6, 7 ]
K shifts: 3
First half: [ 4, 5, 6, 7 ]
Second half: [ 1, 2, 3 ]
After bitwise OR: [ 5, 7, 7 ]
Final sum: 19

Step-by-Step Process

Let's trace through the example with array [1,2,3,4,5,6,7] and K=3:

function demonstrateProcess() {
    let array = [1, 2, 3, 4, 5, 6, 7];
    let k = 3;
    
    console.log("Step 1: Original array -", array);
    
    // Show circular shift effect
    let shifted = [];
    for (let i = 0; i < array.length; i++) {
        shifted[i] = array[(i + k) % array.length];
    }
    console.log("Step 2: After", k, "circular shifts -", shifted);
    
    // Split into halves
    let halfLength = Math.ceil(array.length / 2);
    let firstHalf = shifted.slice(0, halfLength);
    let secondHalf = shifted.slice(halfLength);
    
    console.log("Step 3: First half -", firstHalf);
    console.log("Step 3: Second half -", secondHalf);
    
    // Bitwise OR operation
    console.log("Step 4: Bitwise OR results:");
    for (let i = 0; i < Math.max(firstHalf.length, secondHalf.length); i++) {
        let val1 = firstHalf[i] || 0;
        let val2 = secondHalf[i] || 0;
        console.log(`${val1} | ${val2} = ${val1 | val2}`);
    }
}

demonstrateProcess();
Step 1: Original array - [ 1, 2, 3, 4, 5, 6, 7 ]
Step 2: After 3 circular shifts - [ 4, 5, 6, 7, 1, 2, 3 ]
Step 3: First half - [ 4, 5, 6, 7 ]
Step 3: Second half - [ 1, 2, 3 ]
Step 4: Bitwise OR results:
4 | 1 = 5
5 | 2 = 7
6 | 3 = 7
7 | 0 = 7

Explanation

  • The function takes an array and an integer k as inputs.

  • We first perform K circular shifts by accessing elements at position (i + k) % array.length.

  • The shifted array is split into two halves using Math.ceil(array.length / 2) to handle odd-length arrays.

  • Bitwise OR operation is performed between corresponding elements of both halves using the | operator.

  • The final sum is calculated by adding all elements from the OR operation result.

  • The function returns the computed sum as the final result.

Conclusion

This program effectively combines array manipulation, circular shifts, and bitwise operations to solve a complex computational problem. The key insight is understanding how circular shifts work and properly handling the bitwise OR between split array halves.

Updated on: 2026-03-15T23:19:01+05:30

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements