Question

In: Computer Science

FIRST You are given a binary string x and an array A[1 : n] of binary...

FIRST You are given a binary string x and an array A[1 : n] of binary strings. Assume that x and the elements of A have the same length. Let ⊕ denote the xor operator on binary strings. For example 1010 ⊕ 1101 = 0111, and 1110 ⊕ 1111 = 0001. Assume that xor’ing two strings takes O(1) time. Give a divide-and-conquer algorithm to check if there’s a subarray A[i : j] of A such that A[i] ⊕ · · · ⊕ A[j] = x. Your algorithm should return such a pair (i, j) if they exist; otherwise return (−1, −1). Your algorithm must be faster than O(n 2 ) time.

[We expect that your algorithm is written in pseudocode, together with an English description and a short, informal argument for its correctness and running time.]

Hint: If you are not familiar with the xor operator, try to think of it as the addition operator; the two operators have many similar properties. Still, the xor operator has this nice property: if you have x = a ⊕ b then x ⊕ a = b.

SECOND Implement your algorithm. A brute-force implementation will receive no credit. You must use either C, C++, Java, or Python 3, and there must be just a single file for the source code. In our pseudocode, array index starts from 1, but in most programming languages, array index starts from 0. For simplicity, in this implementation, we’ll use the latter convention, that is, your i and j must be within {0, . . . , n − 1}.

Solutions

Expert Solution

Algorithm for the above problem-

  1. Compute the prefix xor-sum array.
  2. Create a map mp in which we store the position of a prefixes and if any of the prefix is equal to X then print the range (1,i) as the answer.
  3. Traverse the prefix array if x^prefix[i] is present in the map then there exits a range with xor equal to X and print the range (mp[m^prefix[i]],i) as the answer.
  4. If we dont get any answer after traversing the prefix array then print (-1,-1).

The time complexity of this algorithm is O(nlogn).

C++ code for above problem-

Note- Considering binary strings as decimal number because it given that xor’ing two strings takes O(1) time.

#include<bits/stdc++.h>

using namespace std;

//prefix array and map
int prefix[1000];
map<int,int> mp;

int main() {

//input X and n->number of elements in array A
int x,n;
cin>>x>>n;

int i,j,k,l;

//input array element
for(i=1;i<=n;i++){
cin>>j;
//creating prefix array
prefix[i]=prefix[i-1]^j;
}

for(i=1;i<=n;i++){
//checking if the aubarray (0,i) has xor-sum X
if(prefix[i]==x){
cout<<"(0,"<<i-1<<")";
return 0;
}
//checking if the aubarray (mp[x^prefix[i]],i) has xor-sum X
if(mp[x^prefix[i]]!=0){
cout<<"("<<mp[x^prefix[i]]-1+1<<","<<i-1<<")";
return 0;
}
//mapping the prefix value and its position
mp[prefix[i]]=i;
}

//if the answer does not exits
cout<<"(-1,-1)";
return 0;
}
Code-

Input and Output-


If the answer helped then please upvote.And for any queries,please comment.


Related Solutions

Sort a string array by frequency given an array of string write a function that will...
Sort a string array by frequency given an array of string write a function that will return an array that is sorted by frequency example {"hello","hola","hello","hello","ciao","hola","hola","hola"} returned array should be {"hola","hello","ciao"}
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array...
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3: Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3:
Suppose you are given an integer c and an array, A, indexed from 1 to n,...
Suppose you are given an integer c and an array, A, indexed from 1 to n, of n integers in the range from 0 to 5n (possibly with duplicates). i.e. 0 <= A[i ] <= 5n " I = {1, .., n}. a.) Write an efficient algorithm that runs in O(n) time in a pseudo code for determining if there are two integers, A[i] and A[j], in A whose sum is c, i.e. c = A[i] + A[j], for 1...
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches: a. heapify A and then extract k elements one by one b. sort the array (e.g. using MergeSort or HeapSort) and then read the...
/** * This program will sort an n by n array by the first value in...
/** * This program will sort an n by n array by the first value in each row. * Selection sort algorithm is modified to do the sorting. * For example: * <p/> * If the original array is: * 1 2 3 4 5 * 3 4 5 1 2 * 5 2 3 4 1 * 2 3 1 4 5 * 4 2 3 1 5 * <p/> * The array after sorting is: * 1 2...
Algorithm1 prefixAverages1(X, n) Input array X of n integers Output array A of prefix averages of...
Algorithm1 prefixAverages1(X, n) Input array X of n integers Output array A of prefix averages of X A ← new array of n integers for i ← 0 to n − 1 do s ← X[0] for j ← 1 to i do s ← s + X[j] A[i] ← s / (i + 1) return A ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Algorithm2 prefixAverages2(X, n) Input array X of n integers Output array A of prefix averages of X A ← new array of...
You are given an array of n elements, and you notice that some of them are...
You are given an array of n elements, and you notice that some of them are duplicates, that is, they appear more than once in the array. Show how to remove all duplicates from the array in time O( n log2 n ).
Project: Given a string s and an integer array indices of the same length. The string...
Project: Given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the i th position moves to indices[i] in the shuffled string. Return the shuffled string. Example: Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. You need to do: Create a class called ShuffledStringApp. Keeping class StringOperation and IO in util package. In this project, you will...
Let T be a binary tree with n positions that is realized with an array representation...
Let T be a binary tree with n positions that is realized with an array representation A, and let f() be the level numbering function of the positions of T, as given in Section 8.3.2. Give pseudocode descriptions of each of the methods root, parent, left, right, isExternal, and isRoot.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT