Skip to main content

Command Palette

Search for a command to run...

Find the Prefix Common Array of Two Arrays

Published
2 min read
Find the Prefix Common Array of Two Arrays
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given two 0-indexed integer permutations A and B of length n.

A prefix common array of A and B is an array C such that C[i] is equal to the count of numbers that are present at or before the index i in both A and B.

Return the prefix common array of A and B.

A sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.

LeetCode Problem - 2657

class Solution {
    // Method to find the prefix common array between two arrays
    public int[] findThePrefixCommonArray(int[] A, int[] B) {
        // Initialize a result array to store the counts of common elements
        int[] result = new int[A.length];

        // Loop through elements of array A
        int i = 0;
        while (i < A.length) {
            // Initialize a counter to track the number of common elements
            int count = 0;

            // Nested loop to compare each element of A with elements of B
            for (int j = 0; j <= i; j++) {
                for (int k = 0; k <= i; k++) {
                    // If there is a match, increment the count and break out of the loop
                    if (A[j] == B[k]) {
                        count++;
                        break;
                    }
                }
            }

            // Store the count of common elements at the current index in the result array
            result[i] = count;
            // Move to the next index of the result array
            i++;
        }

        // Return the result array containing counts of common elements
        return result;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.