Given three integers N, S, and K, the task is to create an array of N positive integers such that the bitwise OR of any two consecutive elements from the array is odd and there are exactly K subarrays with a sum equal to S where 1 ? K ? N / 2.
Examples:
Input: N = 4, K = 2, S = 6
Output: 6 7 6 7
Here, there are exactly 2 subarray {6} and {6}
whose sum is 6 and the bitwise OR of
any adjacent elements is odd.
Input: N = 8, K = 3, S = 12
Output: 12 13 12 13 12 13 13 13
Approach:
- Observe a pattern here {S, P, S, P, S, P, …, P, P, P, P}.
- Here P is an odd number > S and after every S there is an occurrence of P. It is known that the bitwise OR with an odd number is always an odd number, so it is confirmed that bitwise OR of each adjacent element is an odd number.
- Now, put exactly K number of S in the above pattern of the array.
- Except for S all the elements (which are P) are greater than S, so there can not be any subarray whose sum is exactly S other than those K subarrays.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printArr( int arr[], int n)
{
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
void findArray( int n, int k, int s)
{
int vis[n] = { 0 };
int cnt = 0;
int arr[n];
for ( int i = 0; i < n && cnt < k; i += 2) {
arr[i] = s;
vis[i] = 1;
cnt++;
}
int val = s;
if (s % 2 == 0)
val++;
else
val = val + 2;
for ( int i = 0; i < n; i++) {
if (vis[i] == 0) {
arr[i] = val;
}
}
printArr(arr, n);
}
int main()
{
int n = 8, k = 3, s = 12;
findArray(n, k, s);
return 0;
}
|
Java
class GFG
{
static void printArr( int arr[], int n)
{
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
static void findArray( int n, int k, int s)
{
int vis[] = new int [n] ;
int cnt = 0 ;
int arr[] = new int [n];
for ( int i = 0 ; i < n && cnt < k; i += 2 )
{
arr[i] = s;
vis[i] = 1 ;
cnt++;
}
int val = s;
if (s % 2 == 0 )
val++;
else
val = val + 2 ;
for ( int i = 0 ; i < n; i++)
{
if (vis[i] == 0 )
{
arr[i] = val;
}
}
printArr(arr, n);
}
public static void main (String[] args)
{
int n = 8 , k = 3 , s = 12 ;
findArray(n, k, s);
}
}
|
Python3
def printArr(arr, n) :
for i in range (n) :
print (arr[i], end = " " );
def findArray(n, k, s) :
vis = [ 0 ] * n;
cnt = 0 ;
arr = [ 0 ] * n;
i = 0 ;
while (i < n and cnt < k) :
arr[i] = s;
vis[i] = 1 ;
cnt + = 1 ;
i + = 2 ;
val = s;
if (s % 2 = = 0 ) :
val + = 1 ;
else :
val = val + 2 ;
for i in range (n) :
if (vis[i] = = 0 ) :
arr[i] = val;
printArr(arr, n);
if __name__ = = "__main__" :
n = 8 ; k = 3 ; s = 12 ;
findArray(n, k, s);
|
C#
using System;
class GFG
{
static void printArr( int []arr, int n)
{
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
static void findArray( int n, int k, int s)
{
int []vis = new int [n] ;
int cnt = 0;
int []arr = new int [n];
for ( int i = 0; i < n && cnt < k; i += 2)
{
arr[i] = s;
vis[i] = 1;
cnt++;
}
int val = s;
if (s % 2 == 0)
val++;
else
val = val + 2;
for ( int i = 0; i < n; i++)
{
if (vis[i] == 0)
{
arr[i] = val;
}
}
printArr(arr, n);
}
public static void Main()
{
int n = 8, k = 3, s = 12;
findArray(n, k, s);
}
}
|
Javascript
<script>
function printArr(arr , n) {
for (i = 0; i < n; i++)
document.write(arr[i] + " " );
}
function findArray(n , k , s) {
var vis = Array(n).fill(0);
var cnt = 0;
var arr = Array(n).fill(0);
for (i = 0; i < n && cnt < k; i += 2) {
arr[i] = s;
vis[i] = 1;
cnt++;
}
var val = s;
if (s % 2 == 0)
val++;
else
val = val + 2;
for (i = 0; i < n; i++) {
if (vis[i] == 0) {
arr[i] = val;
}
}
printArr(arr, n);
}
var n = 8, k = 3, s = 12;
findArray(n, k, s);
</script>
|
Output: 12 13 12 13 12 13 13 13
Time Complexity: O(N)
Auxiliary Space: O(N) it is using extra space for arrays