Wednesday, July 3, 2024
HomeLanguagesJavascriptJavascript Program to Form coils in a matrix

Javascript Program to Form coils in a matrix

Given a positive integer n which represents the dimensions of a 4n x 4n matrix with values from 1 to n filled from left to right and top to bottom. Form two coils from the matrix and print the coils.

Examples:  

Input  : n = 1;
Output : Coil 1 : 10 6 2 3 4 8 12 16 
         Coil 2 : 7 11 15 14 13 9 5 1
Explanation : Matrix is 
1  2  3  4 
5  6  7  8 
9  10 11 12 
13 14 15 16

Input  : n = 2;
Output : Coil 1 : 36 28 20 21 22 30 38 46 54 
                  53 52 51 50 42 34 26 18 10 
                  2 3 4 5 6 7 8 16 24 32 40 
                  48 56 64 
        Coil 2 : 29 37 45 44 43 35 27 19 11 12 
                 13 14 15 23 31 39 47 55 63 62 
                 61 60 59 58 57 49 41 33 25 17
                 9 1 

The total elements in the matrix are 16n2. All elements are divided into two coils. Every coil has 8n2 elements. We make two arrays of this size. We first fill elements in coil1 by traversing them in the given order. Once we have filled elements in coil1, we can get elements of other coil2 using formula coil2[i] = 16*n*n + 1 -coil1[i]. 

Javascript




<script>
    // Javascript program to print 2 coils
    // of a 4n x 4n matrix.
     
    // Print coils in a matrix of
    // size 4n x 4n
    function printCoils(n)
    {
           
        // Number of elements in
        // each coil
        let m = 8 * n * n;
       
        // Let us fill elements in
        // coil 1.
        let coil1 = new Array(m);
        coil1.fill(0);
       
        // First element of coil1
        // 4*n*2*n + 2*n;
        coil1[0] = 8 * n * n + 2 * n;
        let curr = coil1[0];
       
        let nflg = 1, step = 2;
       
        // Fill remaining m-1 elements
        // in coil1[]
        let index = 1;
        while (index < m)
        {
               
            // Fill elements of current
            // step from down to up
            for (let i = 0; i < step; i++)
                {
                    // Next element from
                    // current element
                    curr = coil1[index++]
                    = (curr - 4 * n * nflg);
                       
                    if (index >= m)
                        break;
                }
                   
            if (index >= m)
                break;
           
            // Fill elements of current step
            // from up to down.
            for (let i = 0; i < step; i++)
            {
                curr = coil1[index++] = curr
                                     + nflg;
                if (index >= m)
                    break;
            }
               
            nflg = nflg * (-1);
            step += 2;
        }
       
        /* get coil2 from coil1 */
        let coil2 = new Array(m);
        coil2.fill(0);
           
        for (let i = 0; i < 8 * n * n; i++)
            coil2[i] = 16 * n * n + 1 - coil1[i];
       
        // Print both coils
        document.write("Coil 1 : ");
           
        for (let i = 0; i < 8 * n * n; i++)
            document.write(coil1[i] + " ");
           
        document.write("</br>" + "Coil 2 : ");
           
        for (let i = 0; i < 8 * n * n; i++)
            document.write(coil2[i] + " ");
    }
     
    let n = 1;
           
      printCoils(n);
     
</script>


Output:  

Coil 1 : 10 6 2 3 4 8 12 16 
Coil 2 : 7 11 15 14 13 9 5 1 

Time Complexity: O(n2), where n represents the given integer.
Auxiliary Space: O(n2), where n represents the given integer.

Please refer complete article on Form coils in a matrix for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments