Banana-Leopard/TASTY huggingface dataset:
// C++ program to rotate a matrix
// by 90 degrees
#include <bits/stdc++.h>
using namespace std;
#define N 4
 
// An Inplace function to
// rotate a N x N matrix
// by 90 degrees in
// anti-clockwise direction
void rotateMatrix(int mat[][N])
{ // REVERSE every row
    for (int i = 0; i < N; i++)
        reverse(mat[i], mat[i] + N);
 
    // Performing Transpose
    for (int i = 0; i < N; i++) {
        for (int j = i; j < N; j++)
            swap(mat[i][j], mat[j][i]);
    }
}
 
// Function to print the matrix
void displayMatrix(int mat[N][N])
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}
 
/* Driver code */
int main()
{
    int mat[N][N] = { { 1, 2, 3, 4 },
                      { 5, 6, 7, 8 },
                      { 9, 10, 11, 12 },
                      { 13, 14, 15, 16 } };
 
    // Function call
    rotateMatrix(mat);
 
    // Print rotated matrix
    displayMatrix(mat);
 
    return 0;
}

Time complexity: quadratic
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
 
 //Function to rotate matrix anticlockwise by 90 degrees.
void rotateby90(vector<vector<int> >& matrix)
{
      int n=matrix.size();
    int mid;
     if(n%2==0)
         mid=n/2-1;
     else
         mid=n/2;
     for(int i=0,j=n-1;i<=mid;i++,j--)
     {
         for(int k=0;k<j-i;k++)
         {
             swap(matrix[i][j-k],matrix[j][i+k]);     //ru  ld
             swap(matrix[i+k][i],matrix[j][i+k]);     //lu ld
             swap(matrix[i][j-k],matrix[j-k][j]);     //ru rd
          }
     }     
}
 
void printMatrix(vector<vector<int>>& arr)
{
 
  int n=arr.size();
  for(int i=0;i<n;i++)
  {
      for(int j=0;j<n;j++)
    {
        cout<<arr[i][j]<<" ";
    }
    cout<<endl;
  }
}
int main() {
    
    vector<vector<int>> arr = { { 1, 2, 3, 4 },
                      { 5, 6, 7, 8 },
                      { 9, 10, 11, 12 },
                      { 13, 14, 15, 16 } };
    rotateby90(arr);
    printMatrix(arr);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to rotate a matrix by 180 degrees
#include <bits/stdc++.h>
#define N 3
using namespace std;
 
// Function to Rotate the matrix by 180 degree
void rotateMatrix(int mat[][N])
{
    // Simply print from last cell to first cell.
    for (int i = N - 1; i >= 0; i--) {
        for (int j = N - 1; j >= 0; j--)
            printf("%d ", mat[i][j]);
 
        printf("\n");
    }
}
 
// Driven code
int main()
{
    int mat[N][N] = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    };
 
    rotateMatrix(mat);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program for left rotation of matrix by 180
#include <bits/stdc++.h>
using namespace std;
 
#define R 4
#define C 4
 
// Function to rotate the matrix by 180 degree
void reverseColumns(int arr[R][C])
{
    for (int i = 0; i < C; i++)
        for (int j = 0, k = C - 1; j < k; j++, k--)
            swap(arr[j][i], arr[k][i]);
}
 
// Function for transpose of matrix
void transpose(int arr[R][C])
{
    for (int i = 0; i < R; i++)
        for (int j = i; j < C; j++)
            swap(arr[i][j], arr[j][i]);
}
 
// Function for display the matrix
void printMatrix(int arr[R][C])
{
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++)
            cout << arr[i][j] << " ";
        cout << '\n';
    }
}
 
// Function to anticlockwise rotate matrix
// by 180 degree
void rotate180(int arr[R][C])
{
    transpose(arr);
    reverseColumns(arr);
    transpose(arr);
    reverseColumns(arr);
}
 
// Driven code
int main()
{
    int arr[R][C] = { { 1, 2, 3, 4 },
                      { 5, 6, 7, 8 },
                      { 9, 10, 11, 12 },
                      { 13, 14, 15, 16 } };
    rotate180(arr);
    printMatrix(arr);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
 
/**
 * Reverse Row at specified index in the matrix
 * @param data matrix
 * @param index row index
 */
void reverseRow(vector<vector<int>>& data,
                int index)
{
    int cols = data[index].size();
    for(int i = 0; i < cols / 2; i++)
    {
        int temp = data[index][i];
        data[index][i] = data[index][cols - i - 1];
        data[index][cols - i - 1] = temp;
    }
}
 
/**
 * Print Matrix data
 * @param data matrix
 */
void printMatrix(vector<vector<int>>& data)
{
    for(int i = 0; i < data.size(); i++)
    {
        for(int j = 0; j < data[i].size(); j++)
        {
            cout << data[i][j] << " ";
        }
        cout << endl;
    }
}
 
/**
 * Rotate Matrix by 180 degrees
 * @param data matrix
 */
void rotateMatrix180(vector<vector<int>>& data)
{
    int rows = data.size();
    int cols = data[0].size();
 
    if (rows % 2 != 0)
    {
         
        // If N is odd reverse the middle
        // row in the matrix
        reverseRow(data, data.size() / 2);
    }
     
    // Swap the value of matrix [i][j] with
    // [rows - i - 1][cols - j - 1] for half    
    // the rows size.
    for(int i = 0; i <= (rows/2) - 1; i++)
    {
        for(int j = 0; j < cols; j++)
        {
            int temp = data[i][j];
            data[i][j] = data[rows - i - 1][cols - j - 1];
            data[rows - i - 1][cols - j - 1] = temp;
        }
    }
}
 
// Driver code   
int main()
{
    vector<vector<int>> data{ { 1, 2, 3, 4, 5 },
                              { 6, 7, 8, 9, 10 },
                              { 11, 12, 13, 14, 15 },
                              { 16, 17, 18, 19, 20 },
                              { 21, 22, 23, 24, 25 } };
 
    // Rotate Matrix
    rotateMatrix180(data);
 
    // Print Matrix
    printMatrix(data);
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07

Time complexity: quadratic
Space complexity: constant
// C++ program to check if all rows of a matrix
// are rotations of each other
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
 
// Returns true if all rows of mat[0..n-1][0..n-1]
// are rotations of each other.
bool isPermutedMatrix( int mat[MAX][MAX], int n)
{
    // Creating a string that contains elements of first
    // row.
    string str_cat = "";
    for (int i = 0 ; i < n ; i++)
        str_cat = str_cat + "-" + to_string(mat[0][i]);
 
    // Concatenating the string with itself so that
    // substring search operations can be performed on
    // this
    str_cat = str_cat + str_cat;
 
    // Start traversing remaining rows
    for (int i=1; i<n; i++)
    {
        // Store the matrix into vector in the form
        // of strings
        string curr_str = "";
        for (int j = 0 ; j < n ; j++)
            curr_str = curr_str + "-" + to_string(mat[i][j]);
 
        // Check if the current string is present in
        // the concatenated string or not
        if (str_cat.find(curr_str) == string::npos)
            return false;
    }
 
    return true;
}
 
// Drivers code
int main()
{
    int n = 4 ;
    int mat[MAX][MAX] = {{1, 2, 3, 4},
        {4, 1, 2, 3},
        {3, 4, 1, 2},
        {2, 3, 4, 1}
    };
    isPermutedMatrix(mat, n)? cout << "Yes" :
                              cout << "No";
    return 0;
}

Time complexity: cubic
Space complexity: linear
// C++ implementation to sort the given matrix
#include <bits/stdc++.h>
using namespace std;
 
#define SIZE 10
 
// function to sort the given matrix
void sortMat(int mat[SIZE][SIZE], int n)
{
    // temporary matrix of size n^2
    int temp[n * n];
    int k = 0;
 
    // copy the elements of matrix one by one
    // into temp[]
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            temp[k++] = mat[i][j];
 
    // sort temp[]
    sort(temp, temp + k);
     
    // copy the elements of temp[] one by one
    // in mat[][]
    k = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            mat[i][j] = temp[k++];
}
 
// function to print the given matrix
void printMat(int mat[SIZE][SIZE], int n)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << mat[i][j] << " ";
        cout << endl;
    }
}
 
// Driver program to test above
int main()
{
    int mat[SIZE][SIZE] = { { 5, 4, 7 },
                            { 1, 3, 8 },
                            { 2, 9, 6 } };
    int n = 3;
 
    cout << "Original Matrix:\n";
    printMat(mat, n);
 
    sortMat(mat, n);
 
    cout << "\nMatrix After Sorting:\n";
    printMat(mat, n);
 
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program to find median of a matrix
// sorted row wise
#include<bits/stdc++.h>
using namespace std;
 
const int MAX = 100;
 
// function to find median in the matrix
int binaryMedian(int m[][MAX], int r ,int c)
{
    int min = INT_MAX, max = INT_MIN;
    for (int i=0; i<r; i++)
    {
        // Finding the minimum element
        if (m[i][0] < min)
            min = m[i][0];
 
        // Finding the maximum element
        if (m[i][c-1] > max)
            max = m[i][c-1];
    }
 
    int desired = (r * c + 1) / 2;
    while (min < max)
    {
        int mid = min + (max - min) / 2;
        int place = 0;
 
        // Find count of elements smaller than or equal to mid
        for (int i = 0; i < r; ++i)
            place += upper_bound(m[i], m[i]+c, mid) - m[i];
        if (place < desired)
            min = mid + 1;
        else
            max = mid;
    }
    return min;
}
 
// driver program to check above functions
int main()
{
    int r = 3, c = 3;
    int m[][MAX]= { {1,3,5}, {2,6,9}, {3,6,9} };
    cout << "Median is " << binaryMedian(m, r, c) << endl;
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to print Lower
// triangular and Upper triangular
// matrix of an array
#include<iostream>
 
using namespace std;
 
// Function to form
// lower triangular matrix
void lower(int matrix[3][3], int row, int col)
{
    int i, j;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (i < j)
            {
                cout << "0" << " ";
            }
            else
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Function to form upper triangular matrix
void upper(int matrix[3][3], int row, int col)
{
    int i, j;
     
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (i > j)
            {
                cout << "0" << " ";
            }
            else
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    int matrix[3][3] = {{1, 2, 3},
                        {4, 5, 6},
                        {7, 8, 9}};
    int row = 3, col = 3;
     
    cout << "Lower triangular matrix: \n";
    lower(matrix, row, col);
     
    cout << "Upper triangular matrix: \n";
    upper(matrix, row, col);
         
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
vector<int> spiralOrder(vector<vector<int> >& matrix)
{
    int m = matrix.size(), n = matrix[0].size();
    vector<int> ans;
 
    if (m == 0)
        return ans;
 
    vector<vector<bool> > seen(m, vector<bool>(n, false));
    int dr[] = { 0, 1, 0, -1 };
    int dc[] = { 1, 0, -1, 0 };
 
    int x = 0, y = 0, di = 0;
 
    // Iterate from 0 to m * n - 1
    for (int i = 0; i < m * n; i++) {
        ans.push_back(matrix[x][y]);
        // on normal geeksforgeeks ui page it is showing
        // 'ans.push_back(matrix[x])' which gets copied as
        // this only and gives error on compilation,
        seen[x][y] = true;
        int newX = x + dr[di];
        int newY = y + dc[di];
 
        if (0 <= newX && newX < m && 0 <= newY && newY < n
            && !seen[newX][newY]) {
            x = newX;
            y = newY;
        }
        else {
            di = (di + 1) % 4;
            x += dr[di];
            y += dc[di];
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    vector<vector<int> > a{ { 1, 2, 3, 4 },
                            { 5, 6, 7, 8 },
                            { 9, 10, 11, 12 },
                            { 13, 14, 15, 16 } };
 
    // Function call
    for (int x : spiralOrder(a)) {
        cout << x << " ";
    }
    return 0;
}
 
// This code is contributed by Yashvendra Singh

Time complexity: linear
Space complexity: linear
// C++ Program to print a matrix spirally
 
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
 
void spiralPrint(int m, int n, int a[R][C])
{
    int i, k = 0, l = 0;
 
    /* k - starting row index
        m - ending row index
        l - starting column index
        n - ending column index
        i - iterator
    */
 
    while (k < m && l < n) {
        /* Print the first row from
               the remaining rows */
        for (i = l; i < n; ++i) {
            cout << a[k][i] << " ";
        }
        k++;
 
        /* Print the last column
         from the remaining columns */
        for (i = k; i < m; ++i) {
            cout << a[i][n - 1] << " ";
        }
        n--;
 
        /* Print the last row from
                the remaining rows */
        if (k < m) {
            for (i = n - 1; i >= l; --i) {
                cout << a[m - 1][i] << " ";
            }
            m--;
        }
 
        /* Print the first column from
                   the remaining columns */
        if (l < n) {
            for (i = m - 1; i >= k; --i) {
                cout << a[i][l] << " ";
            }
            l++;
        }
    }
}
 
/* Driver Code */
int main()
{
    int a[R][C] = { { 1, 2, 3, 4 },
                    { 5, 6, 7, 8 },
                    { 9, 10, 11, 12 },
                    { 13, 14, 15, 16 } };
 
    // Function Call
    spiralPrint(R, C, a);
    return 0;
}
 
// This is code is contributed by rathbhupendra

Time complexity: quadratic
Space complexity: constant
// C++. program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
#define R 4
#define C 4
 
// Function for printing matrix in spiral
// form i, j: Start index of matrix, row
// and column respectively m, n: End index
// of matrix row and column respectively
void print(int arr[R][C], int i, int j, int m, int n)
{
    // If i or j lies outside the matrix
    if (i >= m or j >= n)
        return;
 
    // Print First Row
    for (int p = j; p < n; p++)
        cout << arr[i][p] << " ";
 
    // Print Last Column
    for (int p = i + 1; p < m; p++)
        cout << arr[p][n - 1] << " ";
 
    // Print Last Row, if Last and
    // First Row are not same
    if ((m - 1) != i)
        for (int p = n - 2; p >= j; p--)
            cout << arr[m - 1][p] << " ";
 
    // Print First Column,  if Last and
    // First Column are not same
    if ((n - 1) != j)
        for (int p = m - 2; p > i; p--)
            cout << arr[p][j] << " ";
 
    print(arr, i + 1, j + 1, m - 1, n - 1);
}
 
// Driver Code
int main()
{
    int a[R][C] = { { 1, 2, 3, 4 },
                    { 5, 6, 7, 8 },
                    { 9, 10, 11, 12 },
                    { 13, 14, 15, 16 } };
 
    // Function Call
    print(a, 0, 0, R, C);
    return 0;
}
// This Code is contributed by Ankur Goel

Time complexity: quadratic
Space complexity: constant
// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
 
bool isInBounds(int i, int j)
{
    if (i < 0 || i >= R || j < 0 || j >= C)
        return false;
    return true;
}
 
// check if the position is blocked
bool isBlocked(int matrix[R][C], int i, int j)
{
    if (!isInBounds(i, j))
        return true;
    if (matrix[i][j] == -1)
        return true;
    return false;
}
 
// DFS code to traverse spirally
void spirallyDFSTravserse(int matrix[R][C], int i, int j,
                          int dir, vector<int>& res)
{
    if (isBlocked(matrix, i, j))
        return;
    bool allBlocked = true;
    for (int k = -1; k <= 1; k += 2) {
        allBlocked = allBlocked
                     && isBlocked(matrix, k + i, j)
                     && isBlocked(matrix, i, j + k);
    }
    res.push_back(matrix[i][j]);
    matrix[i][j] = -1;
    if (allBlocked) {
        return;
    }
 
    // dir: 0 - right, 1 - down, 2 - left, 3 - up
    int nxt_i = i;
    int nxt_j = j;
    int nxt_dir = dir;
    if (dir == 0) {
        if (!isBlocked(matrix, i, j + 1)) {
            nxt_j++;
        }
        else {
            nxt_dir = 1;
            nxt_i++;
        }
    }
    else if (dir == 1) {
        if (!isBlocked(matrix, i + 1, j)) {
            nxt_i++;
        }
        else {
            nxt_dir = 2;
            nxt_j--;
        }
    }
    else if (dir == 2) {
        if (!isBlocked(matrix, i, j - 1)) {
            nxt_j--;
        }
        else {
            nxt_dir = 3;
            nxt_i--;
        }
    }
    else if (dir == 3) {
        if (!isBlocked(matrix, i - 1, j)) {
            nxt_i--;
        }
        else {
            nxt_dir = 0;
            nxt_j++;
        }
    }
    spirallyDFSTravserse(matrix, nxt_i, nxt_j, nxt_dir,
                         res);
}
 
// To traverse spirally
vector<int> spirallyTraverse(int matrix[R][C])
{
    vector<int> res;
    spirallyDFSTravserse(matrix, 0, 0, 0, res);
    return res;
}
 
// Driver Code
int main()
{
    int a[R][C] = { { 1, 2, 3, 4 },
                    { 5, 6, 7, 8 },
                    { 9, 10, 11, 12 },
                    { 13, 14, 15, 16 } };
 
    // Function Call
    vector<int> res = spirallyTraverse(a);
    int size = res.size();
    for (int i = 0; i < size; ++i)
        cout << res[i] << " ";
    cout << endl;
    return 0;
} // code contributed by Ephi F

Time complexity: quadratic
Space complexity: constant
// C++ program to find unique element in matrix
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
 
// function that calculate unique element
int unique(int mat[R][C], int n, int m)
{
    // declare map for hashing
    unordered_map<int, int> mp;
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            // increase freq of mat[i][j] in map
            mp[mat[i][j]]++;
 
    int flag = false;
    // print unique element
    for (auto p : mp) {
        if (p.second == 1) {
            cout << p.first << " ";
            flag = 1;
        }
    }
 
    if (!flag) {
        cout << "No unique element in the matrix";
    }
}
 
// Driver program
int main()
{
    int mat[R][C] = { { 1, 2, 3, 20 },
                      { 5, 6, 20, 25 },
                      { 1, 3, 5, 6 },
                      { 6, 7, 8, 15 } };
 
    // function that calculate unique element
    unique(mat, R, C);
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// CPP Program to swap diagonal of a matrix
#include <bits/stdc++.h>
using namespace std;
 
// size of square matrix
#define N 3
 
// Function to swap diagonal of matrix
void swapDiagonal(int matrix[][N]) {
  for (int i = 0; i < N; i++)
    swap(matrix[i][i], matrix[i][N - i - 1]);
}
 
// Driver Code
int main() {
  int matrix[N][N] = {{0, 1, 2},
                      {3, 4, 5},
                      {6, 7, 8}};
 
  swapDiagonal(matrix);
 
  // Displaying modified matrix
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      cout << matrix[i][j] << " ";
    cout << endl;
  }
 
  return 0;
}

Time complexity: quadratic
Space complexity: constant
// CPP program for finding max path in matrix
#include <bits/stdc++.h>
#define N 4
#define M 6
using namespace std;
 
// To calculate max path in matrix
int findMaxPath(int mat[][M])
{
 
    for (int i = 1; i < N; i++) {
        for (int j = 0; j < M; j++) {
 
            // When all paths are possible
            if (j > 0 && j < M - 1)
                mat[i][j] += max(mat[i - 1][j],
                             max(mat[i - 1][j - 1],
                             mat[i - 1][j + 1]));
 
            // When diagonal right is not possible
            else if (j > 0)
                mat[i][j] += max(mat[i - 1][j],
                            mat[i - 1][j - 1]);
 
            // When diagonal left is not possible
            else if (j < M - 1)
                mat[i][j] += max(mat[i - 1][j],
                            mat[i - 1][j + 1]);
 
            // Store max path sum
        }
    }
    int res = 0;
    for (int j = 0; j < M; j++)
        res = max(mat[N-1][j], res);
    return res;
}
 
// Driver program to check findMaxPath
int main()
{
     
    int mat1[N][M] = { { 10, 10, 2, 0, 20, 4 },
                    { 1, 0, 0, 30, 2, 5 },
                    { 0, 10, 4, 0, 2, 0 },
                    { 1, 0, 2, 20, 0, 4 } };
             
    cout << findMaxPath(mat1) << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to find number of subarrays
// having product exactly equal to k.
#include <bits/stdc++.h>
using namespace std;
 
// Function to find number of subarrays
// having product equal to 1.
int countOne(int arr[], int n){
    int i = 0;
     
    // To store number of ones in
    // current segment of all 1s.
    int len = 0;
     
    // To store number of subarrays
    // having product equal to 1.
    int ans = 0;
     
    while(i < n){
         
        // If current element is 1, then
        // find length of segment of 1s
        // starting from current element.
        if(arr[i] == 1){
            len = 0;
            while(i < n && arr[i] == 1){
                i++;
                len++;
            }
             
            // add number of possible
            // subarrays of 1 to result.
            ans += (len*(len+1)) / 2;
        }
        i++;
    }
     
    return ans;
}
 
/// Function to find number of subarrays having
/// product exactly equal to k.
int findSubarrayCount(int arr[], int n, int k)
{
    int start = 0, endval = 0, p = 1,
        countOnes = 0, res = 0;
 
    while (endval < n)
    {
        p *= (arr[endval]);
 
        // If product is greater than k then we need to decrease
        // it. This could be done by shifting starting point of
        // sliding window one place to right at a time and update
        // product accordingly.
        if(p > k)
        {
            while(start <= endval && p > k)
            {
                p /= arr[start];
                start++;
            }
        }
         
         
        if(p == k)
        {
            // Count number of succeeding ones.
            countOnes = 0;
            while(endval + 1 < n && arr[endval + 1] == 1)
            {
                countOnes++;
                endval++;
            }
 
            // Update result by adding both new subarray
            // and effect of succeeding ones.
            res += (countOnes + 1);
 
            // Update sliding window and result according
            // to change in sliding window. Here preceding
            // 1s have same effect on subarray as succeeding
            // 1s, so simply add.
            while(start <= endval && arr[start] == 1 && k!=1)
            {
                res += (countOnes + 1);
                start++;
            }
 
            // Move start to correct position to find new
            // subarray and update product accordingly.
            p /= arr[start];
            start++;
        }
 
        endval++;
    }
    return res;
}
 
// Driver code
int main()
{
    int arr1[] = { 2, 1, 1, 1, 3, 1, 1, 4};
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    int k = 1;
     
    if(k != 1)
        cout << findSubarrayCount(arr1, n1, k) << "\n";
    else
        cout << countOne(arr1, n1) << "\n";
     
    int arr2[] = { 2, 1, 1, 1, 4, 5};
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    k = 4;
     
    if(k != 1)
        cout << findSubarrayCount(arr2, n2, k) << "\n";
    else
        cout << countOne(arr2, n2) << "\n";
    return 0;
}

Time complexity: linear
Space complexity: constant