Thursday, June 16, 2022
HomeWordPress DevelopmentDiscover the sum of diagonals passing by way of given coordinates for...

Discover the sum of diagonals passing by way of given coordinates for Q question


  

#embody <bits/stdc++.h>

utilizing namespace std;

const int n = 4;

const int m = 4;

  

int diagonal_sum(vector<vector<int> >& arr,

                 vector<int>& right_inclined_digsum,

                 vector<int>& left_inclined_digsum, int n,

                 int x, int y)

{

    

    int a = (n - x) + y - 1;

    int b = x + y;

    int sum = right_inclined_digsum[b]

              + left_inclined_digsum[a] - arr[x][y];

    return sum;

}

  

void precompute(int n, int m, vector<vector<int> > arr,

                vector<int>& right_inclined_digsum,

                vector<int>& left_inclined_digsum)

{

  

    

    for (int i = 0; i < n; i++) {

        for (int j = 0; j < m; j++) {

            right_inclined_digsum[i + j] += arr[i][j];

        }

    }

  

    

    for (int i = n - 1; i >= 0; i--) {

        for (int j = 0; j < m; j++) {

            left_inclined_digsum[n - 1 - i + j]

                += arr[i][j];

        }

  

        

    }

}

  

void clear up(vector<vector<int> >& arr, int Q,

           vector<pair<int, int> >& question)

{

    vector<int> right_inclined_digsum(n + m - 1, 0);

    vector<int> left_inclined_digsum(n + m - 1, 0);

  

    

    precompute(n, m, arr, right_inclined_digsum,

               left_inclined_digsum);

  

    

    int it = 0;

  

    whereas (Q--) {

        int x = question[it].first;

        int y = question[it].second;

        cout << diagonal_sum(arr, right_inclined_digsum,

                             left_inclined_digsum, n, x, y)

             << "n";

        it++;

    }

}

int predominant()

{

    vector<vector<int> > arr = { { 1, 2, 2, 1 },

                                 { 2, 4, 2, 4 },

                                 { 2, 2, 3, 1 },

                                 { 2, 4, 2, 4 } };

    int Q = 3;

  

    

    vector<pair<int, int> > question;

    question.push_back({ 0, 0 });

    question.push_back({ 3, 1 });

    question.push_back({ 3, 3 });

  

    

    clear up(arr, Q, question);

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments