给定三个整数N , S和K ,任务是创建一个N个正整数的数组,以使该数组中任何两个连续元素的按位或为奇数,并且正好有K个子数组的总和等于S ,其中1≤K ≤N / 2 。
例子:
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
方法:
- 在这里观察一个模式{S,P,S,P,S,P,…,P,P,P,P} 。
- 这里P是一个奇数> S,并且在每个S之后都会出现P。已知具有奇数的按位“或”始终是奇数,因此可以确认每个相邻元素的按位“或”是奇数。
- 现在,将精确的K个S放入数组的上述模式中。
- 除了S以外,所有元素(都是P)都大于S,因此除了那些K子数组之外,没有任何子数组的总和恰好是S。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
输出:
12 13 12 13 12 13 13 13
时间复杂度: O(N)