Find an array of size N that satisfies the given conditions

Last Updated : 28 Sep, 2022

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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;

// Utility function to print the
// contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}

// Function to generate and print
// the required array
void findArray(int n, int k, int s)
{

    // Initially all the positions are empty
    int vis[n] = { 0 };

    // To store the count of positions
    // i such that arr[i] = s
    int cnt = 0;

    // To store the final array elements
    int arr[n];

    for (int i = 0; i < n && cnt < k; i += 2) {

        // Set arr[i] = s and the gap between
        // them is exactly 2 so in for loop
        // we use i += 2
        arr[i] = s;

        // Mark the i'th position as visited
        // as we put arr[i] = s
        vis[i] = 1;

        // Increment the count
        cnt++;
    }
    int val = s;

    // Finding the next odd number after s
    if (s % 2 == 0)
        val++;
    else
        val = val + 2;

    for (int i = 0; i < n; i++) {
        if (vis[i] == 0) {

            // If the i'th position is not visited
            // it means we did not put any value
            // at position i so we put 1 now
            arr[i] = val;
        }
    }

    // Print the final array
    printArr(arr, n);
}

// Driver code
int main()
{
    int n = 8, k = 3, s = 12;

    findArray(n, k, s);

    return 0;
}
Java
// Java implementation of the approach 
class GFG
{
    
    // Utility function to print the 
    // contents of an array 
    static void printArr(int arr[], int n) 
    { 
        for (int i = 0; i < n; i++) 
            System.out.print(arr[i] + " "); 
    } 
    
    // Function to generate and print 
    // the required array 
    static void findArray(int n, int k, int s) 
    { 
    
        // Initially all the positions are empty 
        int vis[] = new int[n] ; 
    
        // To store the count of positions 
        // i such that arr[i] = s 
        int cnt = 0; 
    
        // To store the final array elements 
        int arr[] = new int[n]; 
    
        for (int i = 0; i < n && cnt < k; i += 2) 
        { 
    
            // Set arr[i] = s and the gap between 
            // them is exactly 2 so in for loop 
            // we use i += 2 
            arr[i] = s; 
    
            // Mark the i'th position as visited 
            // as we put arr[i] = s 
            vis[i] = 1; 
    
            // Increment the count 
            cnt++; 
        } 
        int val = s; 
    
        // Finding the next odd number after s 
        if (s % 2 == 0) 
            val++; 
        else
            val = val + 2; 
    
        for (int i = 0; i < n; i++) 
        { 
            if (vis[i] == 0)
            { 
    
                // If the i'th position is not visited 
                // it means we did not put any value 
                // at position i so we put 1 now 
                arr[i] = val; 
            } 
        } 
    
        // Print the final array 
        printArr(arr, n); 
    } 
    
    // Driver code 
    public static void main (String[] args)
    { 
        int n = 8, k = 3, s = 12; 
    
        findArray(n, k, s); 
    } 
}

// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach 

# Utility function to print the 
# contents of an array 
def printArr(arr, n) : 

    for i in range(n) : 
        print(arr[i], end= " "); 

# Function to generate and print 
# the required array 
def findArray(n, k, s) :

    # Initially all the positions are empty 
    vis = [0] * n; 

    # To store the count of positions 
    # i such that arr[i] = s 
    cnt = 0; 

    # To store the final array elements 
    arr = [0] * n;
    i = 0;
    
    while (i < n and cnt < k) :
        
        # Set arr[i] = s and the gap between 
        # them is exactly 2 so in for loop 
        # we use i += 2 
        arr[i] = s; 

        # Mark the i'th position as visited 
        # as we put arr[i] = s 
        vis[i] = 1; 

        # Increment the count 
        cnt += 1;
        i += 2;
    val = s; 
    
    # Finding the next odd number after s 
    if (s % 2 == 0) :
        val += 1; 
    else :
        val = val + 2; 

    for i in range(n) :
        if (vis[i] == 0) :

            # If the i'th position is not visited 
            # it means we did not put any value 
            # at position i so we put 1 now 
            arr[i] = val; 

    # Print the final array 
    printArr(arr, n); 

# Driver code 
if __name__ == "__main__" :

    n = 8; k = 3; s = 12; 

    findArray(n, k, s); 

# This code is contributed by AnkitRai01
C#
// C# implementation of the approach 
using System;

class GFG 
{ 
    
    // Utility function to print the 
    // contents of an array 
    static void printArr(int []arr, int n) 
    { 
        for (int i = 0; i < n; i++) 
            Console.Write(arr[i] + " "); 
    } 
    
    // Function to generate and print 
    // the required array 
    static void findArray(int n, int k, int s) 
    { 
    
        // Initially all the positions are empty 
        int []vis = new int[n] ; 
    
        // To store the count of positions 
        // i such that arr[i] = s 
        int cnt = 0; 
    
        // To store the final array elements 
        int []arr = new int[n]; 
    
        for (int i = 0; i < n && cnt < k; i += 2) 
        { 
    
            // Set arr[i] = s and the gap between 
            // them is exactly 2 so in for loop 
            // we use i += 2 
            arr[i] = s; 
    
            // Mark the i'th position as visited 
            // as we put arr[i] = s 
            vis[i] = 1; 
    
            // Increment the count 
            cnt++; 
        } 
        int val = s; 
    
        // Finding the next odd number after s 
        if (s % 2 == 0) 
            val++; 
        else
            val = val + 2; 
    
        for (int i = 0; i < n; i++) 
        { 
            if (vis[i] == 0) 
            { 
    
                // If the i'th position is not visited 
                // it means we did not put any value 
                // at position i so we put 1 now 
                arr[i] = val; 
            } 
        } 
    
        // Print the final array 
        printArr(arr, n); 
    } 
    
    // Driver code 
    public static void Main() 
    { 
        int n = 8, k = 3, s = 12; 
    
        findArray(n, k, s); 
    } 
} 

// This code is contributed by AnkitRai01 
JavaScript
<script>
// javascript implementation of the approach     
// Utility function to print the
    // contents of an array
    function printArr(arr , n) {
        for (i = 0; i < n; i++)
            document.write(arr[i] + " ");
    }

    // Function to generate and print
    // the required array
    function findArray(n , k , s) {

        // Initially all the positions are empty
        var vis = Array(n).fill(0);

        // To store the count of positions
        // i such that arr[i] = s
        var cnt = 0;

        // To store the final array elements
        var arr = Array(n).fill(0);

        for (i = 0; i < n && cnt < k; i += 2) {

            // Set arr[i] = s and the gap between
            // them is exactly 2 so in for loop
            // we use i += 2
            arr[i] = s;

            // Mark the i'th position as visited
            // as we put arr[i] = s
            vis[i] = 1;

            // Increment the count
            cnt++;
        }
        var val = s;

        // Finding the next odd number after s
        if (s % 2 == 0)
            val++;
        else
            val = val + 2;

        for (i = 0; i < n; i++) {
            if (vis[i] == 0) {

                // If the i'th position is not visited
                // it means we did not put any value
                // at position i so we put 1 now
                arr[i] = val;
            }
        }

        // Print the final array
        printArr(arr, n);
    }

    // Driver code
    
        var n = 8, k = 3, s = 12;

        findArray(n, k, s);

// This code contributed by umadevi9616 
</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
 

Comment