给定三个整数N 、 S和K ,任务是创建一个由N 个正整数组成的数组,使得数组中任意两个连续元素的按位 OR 为奇数,并且恰好有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
Javascript
输出:
12 13 12 13 12 13 13 13
时间复杂度: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。