📌  相关文章
📜  最小子数组,除以数组大小,其乘积剩下余数K

📅  最后修改于: 2021-05-06 09:08:27             🧑  作者: Mango

给定一个由N个整数组成的数组arr []和一个整数K ,任务是找到最小子数组的长度,该子数组的乘积除以N得出余数K。如果不存在这样的子数组,则打印“ -1”
例子:

方法:
想法是生成给定数组的所有可能的子数组,并打印最小子数组的长度,最小子数组的所有元素乘积除以N会得到余数K。
下面是上述方法的实现:

C++
// C++ Program to implement 
// the above approach 
#include  
using namespace std; 
  
// Function to find the subarray of 
// minimum length 
int findsubArray(int arr[], int N, int K) 
{ 
  
    // Initialise the minimum 
    // subarray size to N + 1 
    int res = N + 1; 
  
    // Generate all possible subarray 
    for (int i = 0; i < N; i++) { 
  
        // Intialise the product 
        int curr_prod = 1; 
  
        for (int j = i; j < N; j++) { 
  
            // Find the product 
            curr_prod = curr_prod * arr[j]; 
  
            if (curr_prod % N == K 
                && res > (j - i + 1)) { 
  
                res = min(res, j - i + 1); 
                break; 
            } 
        } 
    } 
  
    // Return the minimum size of subarray 
    return (res == N + 1) ? 0 : res; 
} 
  
// Driver Code 
int main() 
{ 
    // Given array 
    int arr[] = { 2, 2, 3 }; 
  
    int N = sizeof(arr) 
            / sizeof(arr[0]); 
  
    int K = 1; 
  
    int answer = findsubArray(arr, N, K); 
  
    if (answer != 0) 
        cout << answer; 
    else
        cout << "-1"; 
  
    return 0; 
}


Java
// Java program to implement the
// above approach
import java.util.*;
  
class GFG{
  
// Function to find the subarray of
// minimum length
static int findsubArray(int arr[], 
                        int N, int K)
{
      
    // Initialise the minimum
    // subarray size to N + 1
    int res = N + 1;
  
    // Generate all possible subarray
    for(int i = 0; i < N; i++)
    {
          
        // Initialise the product
        int curr_prod = 1;
  
        for(int j = i; j < N; j++) 
        {
              
            // Find the product 
            curr_prod = curr_prod * arr[j]; 
    
            if (curr_prod % N == K &&
                 res > (j - i + 1))
            { 
                res = Math.min(res, j - i + 1);
                break;
            }
        }
    }
      
    // Return the minimum size of subarray
    return (res == N + 1) ? 0 : res;
}
  
// Driver code
public static void main(String[] args)
{
      
    // Given array
    int arr[] = { 2, 2, 3 };
      
    int N = arr.length;
    int K = 1;
      
    int answer = findsubArray(arr, N, K);
      
    if (answer != 0)
        System.out.println(answer);
    else
        System.out.println("-1");
}
}
  
// This code is contributed by offbeat


Python3
# Python3 program to implement 
# the above approach 
  
# Function to find the subarray of 
# minimum length 
def findsubArray(arr, N, K): 
      
    # Initialise the minimum 
    # subarray size to N + 1 
    res = N + 1
      
    # Generate all possible subarray 
    for i in range(0, N): 
          
        # Intialise the product 
        curr_prad = 1
          
        for j in range(i, N): 
              
            # Find the product 
            curr_prad = curr_prad * arr[j] 
  
            if (curr_prad % N == K and
                res > (j - i + 1)): 
                res = min(res, j - i + 1) 
                break
                  
    # Return the minimum size of subarray 
    if res == N + 1: 
        return 0
    else: 
        return res 
      
# Driver code 
if __name__ == '__main__': 
      
    # Given array 
    arr = [ 2, 2, 3 ] 
    N = len(arr) 
    K = 1
      
    answer = findsubArray(arr, N, K) 
      
    if (answer != 0): 
        print(answer) 
    else: 
        print(-1) 
          
# This code is contributed by virusbuddah_


C#
// C# program to implement the
// above approach
using System;
  
class GFG{
  
// Function to find the subarray of
// minimum length
static int findsubArray(int []arr, 
                        int N, int K)
{
      
    // Initialise the minimum
    // subarray size to N + 1
    int res = N + 1;
  
    // Generate all possible subarray
    for(int i = 0; i < N; i++)
    {
          
        // Initialise the product
        int curr_prod = 1;
  
        for(int j = i; j < N; j++) 
        {
              
            // Find the product 
            curr_prod = curr_prod * arr[j]; 
  
            if (curr_prod % N == K &&
                res > (j - i + 1))
            { 
                res = Math.Min(res, j - i + 1);
                break;
            }
        }
    }
      
    // Return the minimum size of subarray
    return (res == N + 1) ? 0 : res;
}
  
// Driver code
public static void Main(String[] args)
{
      
    // Given array
    int []arr = { 2, 2, 3 };
      
    int N = arr.Length;
    int K = 1;
      
    int answer = findsubArray(arr, N, K);
      
    if (answer != 0)
        Console.WriteLine(answer);
    else
        Console.WriteLine("-1");
}
}
  
// This code is contributed by amal kumar choubey


输出:

2

时间复杂度: O(N 2 )
辅助空间: O(1)