给定一个由N 个正整数组成的数组arr[] ,任务是找到一个长度为K的子数组,使得子数组的每个元素的串联都可以被X整除。如果不存在这样的子数组,则打印“-1” 。如果存在多个这样的子数组,则打印其中任何一个。
例子:
Input: arr[] = {1, 2, 4, 5, 9, 6, 4, 3, 7, 8}, K = 4, X = 4
Output: 4 5 9 6
Explanation:
The elements of the subarray {4, 5, 9, 6} concatenates to form 4596, which is divisible by 4.
Input: arr[] = {2, 3, 5, 1, 3}, K = 3, X = 6
Output: -1
朴素方法:解决问题的最简单方法是生成所有可能的长度为K 的子数组,并打印其元素串联可被X整除的子数组。如果不存在这样的子数组,则打印“-1” 。否则,打印任何这些子数组。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:可以使用滑动窗口技术优化上述方法。请按照以下步骤解决问题:
- 通过连接前K 个数组元素生成一个数字。将它存储在一个变量中,比如num 。
- 检查生成的数字是否可以被X整除。如果发现为真,则打印当前子数组。
- 否则,在[K, N]范围内遍历数组,并对每个元素执行以下步骤:
- 将元素arr[i]的数字添加到变量num 。
- 从num的前面删除元素arr[i – K]的数字。
- 现在检查形成的当前数字是否可以被X整除。如果发现为真,则打印[i – K, i]范围内的当前子数组。
- 否则,检查下一个子数组。
- 如果不存在这样的子数组,则打印“-1” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the starting
// index of the subarray whose
// concatenation is divisible by X
int findSubarray(vector arr,
int K, int X)
{
int i, num = 0;
// Generate the concatenation
// of first K length subarray
for (i = 0; i < K; i++) {
num = num * 10 + arr[i];
}
// If num is divisible by X
if (num % X == 0) {
return 0;
}
// Traverse the remaining array
for (int j = i; j < arr.size(); j++) {
// Append the digits of arr[i]
num = (num % (int)pow(10, j - 1))
* 10
+ arr[j];
// If num is divisible by X
if (num % X == 0) {
return j - i + 1;
}
}
// No subarray exists
return -1;
}
// Function to print the subarray in
// the range [answer, answer + K]
void print(vector arr, int answer,
int K)
{
// No such subarray exists
if (answer == -1) {
cout << answer;
}
// Otherwise
else {
// Print the subarray in the
// range [answer, answer + K]
for (int i = answer;
i < answer + K; i++) {
cout << arr[i] << " ";
}
}
}
// Driver Code
int main()
{
// Given array arr[]
vector arr = { 1, 2, 4, 5, 9,
6, 4, 3, 7, 8 };
int K = 4, X = 4;
// Function Call
int answer = findSubarray(arr, K, X);
// Function Call to print subarray
print(arr, answer, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to return the starting
// index of the subarray whose
// concatenation is divisible by X
static int findSubarray(ArrayList arr, int K,
int X)
{
int i, num = 0;
// Generate the concatenation
// of first K length subarray
for(i = 0; i < K; i++)
{
num = num * 10 + arr.get(i);
}
// If num is divisible by X
if (num % X == 0)
{
return 0;
}
// Traverse the remaining array
for(int j = i; j < arr.size(); j++)
{
// Append the digits of arr[i]
num = (num % (int)Math.pow(10, j - 1)) *
10 + arr.get(j);
// If num is divisible by X
if (num % X == 0)
{
return j - i + 1;
}
}
// No subarray exists
return -1;
}
// Function to print the subarray in
// the range [answer, answer + K]
static void print(ArrayList arr, int answer,
int K)
{
// No such subarray exists
if (answer == -1)
{
System.out.println(answer);
}
// Otherwise
else
{
// Print the subarray in the
// range [answer, answer + K]
for(int i = answer; i < answer + K; i++)
{
System.out.print(arr.get(i) + " ");
}
}
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
ArrayList arr = new ArrayList(
Arrays.asList(1, 2, 4, 5, 9, 6, 4, 3, 7, 8));
int K = 4, X = 4;
// Function call
int answer = findSubarray(arr, K, X);
// Function call to print subarray
print(arr, answer, K);
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program for the above approach
# Function to return the starting
# index of the subarray whose
# concatenation is divisible by X
def findSubarray(arr, K, X):
num = 0
# Generate the concatenation
# of first K length subarray
for i in range(0, K):
num = num * 10 + arr[i]
# If num is divisible by X
if num % X == 0:
return 0
i = K
# Traverse the remaining array
for j in range(i, len(arr)):
# Append the digits of arr[i]
num = ((num % int(pow(10, j - 1))) *
10 + arr[j])
# If num is divisible by X
if num % X == 0:
return j - i + 1
# No subarray exists
return -1
# Function to print the subarray in
# the range [answer, answer + K]
def print_subarray(arr, answer, K):
# No such subarray exists
if answer == -1:
print(answer)
# Otherwise
else:
# Print the subarray in the
# range [answer, answer + K]
for i in range(answer, answer + K):
print(arr[i], end = " ")
# Driver Code
if __name__ == "__main__":
# Given array arr[]
arr = [ 1, 2, 4, 5, 9,
6, 4, 3, 7, 8 ]
K = 4
X = 4
# Function call
answer = findSubarray(arr, K, X)
# Function call to print subarray
print_subarray(arr, answer, K)
# This code is contributed by akhilsaini
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return the starting
// index of the subarray whose
// concatenation is divisible by X
static int findSubarray(List arr, int K,
int X)
{
int i, num = 0;
// Generate the concatenation
// of first K length subarray
for(i = 0; i < K; i++)
{
num = num * 10 + arr[i];
}
// If num is divisible by X
if (num % X == 0)
{
return 0;
}
// Traverse the remaining array
for(int j = i; j < arr.Count; j++)
{
// Append the digits of arr[i]
num = (num % (int)Math.Pow(10, j - 1)) *
10 + arr[j];
// If num is divisible by X
if (num % X == 0)
{
return j - i + 1;
}
}
// No subarray exists
return -1;
}
// Function to print the subarray in
// the range [answer, answer + K]
static void print(List arr, int answer,
int K)
{
// No such subarray exists
if (answer == -1)
{
Console.WriteLine(answer);
}
// Otherwise
else
{
// Print the subarray in the
// range [answer, answer + K]
for(int i = answer; i < answer + K; i++)
{
Console.Write(arr[i] + " ");
}
}
}
// Driver Code
static public void Main()
{
// Given array arr[]
List arr = new List(){ 1, 2, 4, 5, 9,
6, 4, 3, 7, 8 };
int K = 4, X = 4;
// Function call
int answer = findSubarray(arr, K, X);
// Function call to print subarray
print(arr, answer, K);
}
}
// This code is contributed by akhilsaini
Javascript
输出:
4 5 9 6
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live