📌  相关文章
📜  所有元素都是完美正方形的最大子数组

📅  最后修改于: 2021-05-06 07:29:12             🧑  作者: Mango

给定一个数组arr整数元素,任务是找到最大子数组的长度arr这样子数组的所有元素都是完美的正方形。

例子:

方法:

  • 从左到右遍历数组。用0初始化max_lengthcurrent_length变量。
  • 取一个整数和一个float变量,对于数组的每个元素,将其平方根存储在这两个变量中。
  • 如果两个变量相等,即当前元素是一个完美的平方,则增加current_length变量并继续。否则,将current_length设置为0
  • 在每个步骤中,将max_length分配为max_length = max(current_length,max_length)
  • 最后输出max_length的值。

下面是上述方法的实现:

C++
// C++ program to find the length of the
// largest sub-array of an array every
// element of whose is a perfect square
#include 
using namespace std;
  
// function to return the length of the
// largest sub-array of an array every
// element of whose is a perfect square
int contiguousPerfectSquare(int arr[], int n)
{
    int a;
    float b;
  
    int current_length = 0;
    int max_length = 0;
  
    for (int i = 0; i < n; i++) {
        b = sqrt(arr[i]);
        a = b;
  
        // if both a and b are equal then
        // arr[i] is a perfect square
        if (a == b)
            current_length++;
        else
            current_length = 0;
  
        max_length = max(max_length, current_length);
    }
    return max_length;
}
  
// Driver code
int main()
{
    int arr[] = { 9, 75, 4, 64, 121, 25 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << contiguousPerfectSquare(arr, n);
  
    return 0;
}


Java
// Java program to find the length of the
// largest sub-array of an array every
// element of whose is a perfect square
import java.io.*;
  
class GFG {
      
  
  
// function to return the length of the
// largest sub-array of an array every
// element of whose is a perfect square
 static int contiguousPerfectSquare(int []arr, int n)
{
    int a;
    float b;
  
    int current_length = 0;
    int max_length = 0;
  
    for (int i = 0; i < n; i++) {
        b = (float)Math.sqrt(arr[i]);
        a = (int)b;
  
        // if both a and b are equal then
        // arr[i] is a perfect square
        if (a == b)
            current_length++;
        else
            current_length = 0;
  
        max_length = Math.max(max_length, current_length);
    }
    return max_length;
}
  
// Driver code
  
    public static void main (String[] args) {
            int arr[] = { 9, 75, 4, 64, 121, 25 };
    int n = arr.length;
  
    System.out.print(contiguousPerfectSquare(arr, n));
    }
}
// This code is contributed by inder_verma..


Python3
# Python 3 program to find the length of 
# the largest sub-array of an array every
# element of whose is a perfect square
from math import sqrt
  
# function to return the length of the
# largest sub-array of an array every
# element of whose is a perfect square
def contiguousPerfectSquare(arr, n):
    current_length = 0
    max_length = 0
  
    for i in range(0, n, 1):
        b = sqrt(arr[i])
        a = int(b)
  
        # if both a and b are equal then
        # arr[i] is a perfect square
        if (a == b):
            current_length += 1
        else:
            current_length = 0
  
        max_length = max(max_length, 
                         current_length)
      
    return max_length
  
# Driver code
if __name__ == '__main__':
    arr = [9, 75, 4, 64, 121, 25]
    n = len(arr)
  
    print(contiguousPerfectSquare(arr, n))
  
# This code is contributed by 
# Surendra_Gangwar


C#
// C# program to find the length of the
// largest sub-array of an array every
// element of whose is a perfect square
using System;
  
class GFG {
      
  
  
// function to return the length of the
// largest sub-array of an array every
// element of whose is a perfect square
static int contiguousPerfectSquare(int []arr, int n)
{
    int a;
    float b;
  
    int current_length = 0;
    int max_length = 0;
  
    for (int i = 0; i < n; i++) {
        b = (float)Math.Sqrt(arr[i]);
        a = (int)b;
  
        // if both a and b are equal then
        // arr[i] is a perfect square
        if (a == b)
            current_length++;
        else
            current_length = 0;
  
        max_length = Math.Max(max_length, current_length);
    }
    return max_length;
}
  
// Driver code
  
    public static void Main () {
            int []arr = { 9, 75, 4, 64, 121, 25 };
    int n = arr.Length;
  
  Console.WriteLine(contiguousPerfectSquare(arr, n));
    }
}
// This code is contributed by inder_verma..


PHP


输出:
4