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


Array is a set of a single non primitive similar data types (values or variables) that stores the elements in a memory with a fixed number of values. After creating an array with certain elements, the length of this data set became fixed. Here non primitive means, these data types can be used to call a method to perform a particular operation which can be null in character. Here the bitwise sum means, sum of certain numbers with an exactly 2 bits set. Bitwise OR denotes that each integer is present in a subarray. It is an adjacent non-void element sequence present in an array. In the sorting operation for an array, there is a concept of halves. In this method the whole concept returns a true value if it is the possible situation to make a division for an array.

Here in this article we will learn how to find sum of an array using bitwise OR operation after splitting that particular array in two halves after performing K circular shifts methodology.

The binary representation of an integer by performing K circular shift

What is a circular shift in data structure?

In data structure the bitwise rotation of the elements known as the circular shift, which does not save the sign bit of a particular number. For the circular shift there are two types of shifting method present

  • Regular left shift − It is a multiplication process done by 2 (mod 2^N). Here, N is the particular bits of an integer type

  • Circular left shift − It is a multiplication method for the number of bits. Where the multiplication can be done by the method 2(mod (2^N - 1)).

Now, let’s assume there are two positive integers present in a list. By performing the circular method we have to swap the positions of those two elements by binary representation.

  • The left circular shift

    • Final bit will move to the first position.

    • Shift all the other bits to the next position instead.

  • The right circular shift:

    • Move the first bit to the last.

    • Shift the remaining bit to the previous bit.

What is K circular shift in data structure?

Let’s assume an array A[] with a length of N. Here N is an even number. If Q is a query here and K is the representation of a positive number. If we apply the circular shift on this array, the sum of those particular elements will perform by Bitwise OR on the halves array.

Input: A[] = {12, 23, 4, 21, 22, 76}, Q = 1, K = 2 
Output: 117

Algorithm for a circular shift:-

  • Step 1 − Build a segment tree for that array.

  • Step 2 − Assign an input variable by, i= K%(N/2).

  • Step 3 − Then, use that build tree to get a value of Bitwise OR.

  • Step 4 − The first element will be (N/2)-i-1 elements.

  • Step 5 − Calculate the Bitwise OR range by using [(N/2)-i and N-i-1].

  • Step 6 − Add two results to get an answer of i th number query.

Syntax to perform a circular shift with circular linked list:-

struct Node {
    int data;
    struct Node * next;
};

/* Node Initialization */
struct node *last;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
struct node *four = NULL;
struct node *five = NULL;
struct node *six = NULL;

/* Allocate the memory using malloc function */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
four = malloc(sizeof(struct node));
five = malloc(sizeof(struct node));
six = malloc(sizeof(struct node));

/* Assign some data values in the list */
one->data = 1;
two->data = 2;
three->data = 3;
four->data = 4;
five->data = 5;
six->data = 6;

/* Connect the nodes with pointers */
one->next = two;
two->next = three;
three->next = four;
four->next = five;
five->next = six;
six->next = one;

/* Save address of sixth node in last position */
last = six;

Here in this syntax, we have six nodes with data items 1,2,3,4,5,6 respectively. In this, first node stores the address of two, two stores the address of three and so on. For the last it will be a NULL which points the node one. Here the time complexity is O(N + Q*log(N)) and auxiliary space is O(4*MAX).

Approach

  • Approach 1 − Find a Bitwise OR of two equal halves of an array by using Java.

  • Approach 2 − An efficient Solution to sum of Bitwise OR of all pairs in a given array using Java

Find a Bitwise OR of two equal halves of an array by using Java

Here in this Java build code we will calculate the sum of Bitwise OR pairs. Here I is the OR bitwise operator. And time complexity is O(n).

Example 1

import java.util.*;

public class KCTPRDD{
   static int MAX = 102001;
   static int []seg = new int[4 * MAX];
   static void build(int node, int l,int r, int a[]){
      if (l == r)
         seg[node] = a[l];

      else{
         int mid = (l + r) / 2;

         build(2 * node, l, mid, a);
         build(2 * node + 1, mid + 1, r, a);

         seg[node] = (seg[2 * node] |
            seg[2 * node + 1]);
      }
   }

   static int query(int node, int l, int r, int start, int end, int a[]) {
      
      if (l > end || r < start)
         return 0;

      if (start <= l && r <= end)
         return seg[node];

      int mid = (l + r) / 2;
      return ((query(2 * node, l, mid, start, end, a)) | (query(2 * node + 1, mid + 1, r, start, end, a)));
   }

   static void orsum(int a[], int n, int q, int k[]){
      
      build(1, 0, n - 1, a);

      for(int j = 0; j < q; j++) {
      int i = k[j] % (n / 2);
         int sec = query(1, 0, n - 1,  n / 2 - i, n - i - 1, a);
         int first = (query(1, 0, n - 1, 0,  n / 2 - 1 - i, a) | query(1, 0, n - 1,  n - i, n - 1, a));
         int temp = sec + first;
         System.out.print(temp + " ");
      }
   }
   public static void main(String[] args) {
      int a[] = { 7, 16, 10, 2001, 1997, 2022, 24, 11 };
      int n = a.length;
      int q = 2;
      int k[] = { 4, 2 };
      orsum(a, n, q, k);
   }
}

Output

4062 2078

An efficient Solution to sum of Bitwise OR of all pairs in a given array:

Here in this Java code we will shift the each element to that particular array by using the method of K%(N/2). Then traverse it to calculate the OR of the two halves for each query.

Example 2

import java.io.*;

public class KCIRLLTP {
	static int pairORSum(int arr[], int n){
		int ans = 0; 
		for (int i = 0; i < n; i++)
			for (int j = i + 1; j < n; j++)
				ans += arr[i] | arr[j];

		return ans;
	}
	public static void main(String args[]) {
		int arr[] = { 16, 07, 10, 2001, 1997 };
		int n = arr.length;
		System.out.println(pairORSum(arr, n));
	}
}

Output

14107

Conclusion

In this article today, we have learned how to find out the sum of an array using Bitwise OR after splitting the given array in two halves after a K circular shifts.

Updated on: 06-Apr-2023

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements