0% found this document useful (0 votes)
45 views6 pages

QualComm 1

The document contains C++ code to calculate the integral image of a 2D matrix. It defines two functions - integralImage and integralImage_better. integralImage_better provides a more optimized solution by calculating column-wise sums on the fly instead of iterating through all elements for each sum. It takes in a 2D matrix, calculates the integral image and returns it. It is tested on sample input in main().

Uploaded by

mayank singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views6 pages

QualComm 1

The document contains C++ code to calculate the integral image of a 2D matrix. It defines two functions - integralImage and integralImage_better. integralImage_better provides a more optimized solution by calculating column-wise sums on the fly instead of iterating through all elements for each sum. It takes in a 2D matrix, calculates the integral image and returns it. It is tested on sample input in main().

Uploaded by

mayank singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <cmath>

#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

/* Round 4 */
// Integral image
// 0 1 1
// 1 1 1
// 1 2 1

// 0 1 2
// 1 3 5
// 2 6 9

vector<vector<int>> integralImage(vector<vector<int>> input) {

// Handle 0 size case


if (input.size() <= 0) {
vector < vector<int>> ans;
return ans;
}

// Get row and col


int row = input.size();
int col = input[0].size();

vector<vector<int>> ans(row, vector<int>(col));

// Traverse every element of matrix


for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {

// Traverse element till current index ans calculate sum


int sum = 0;
for (int ansi = 0; ansi <= i; ansi++) {
for (int ansj = 0; ansj <= j; ansj++) {
sum += input[ansi][ansj];
}
}

ans[i][j] = sum;
}
}

return ans;
}

vector<vector<int>> integralImage_better(vector<vector<int>> input) {


// Handle 0 size case
if (input.size() <= 0) {
vector < vector<int>> ans;
return ans;
}

// Get row and col


int row = input.size();
int col = input[0].size();

vector<vector<int>> ans(row, vector<int>(col));

// Traverse every element of matrix


for (int i = 0; i < row; i++) {
int curColSum = 0;
int sum = 0;
for (int j = 0; j < col; j++) {

// Store column sum and use it


int upperSum = 0;
if (i - 1 < 0)
upperSum = 0;
else
upperSum = ans[i-1][j];

curColSum += input[i][j];
sum = curColSum + upperSum;

/*
for (int ansi = 0; ansi <= i; ansi++) {
for (int ansj = 0; ansj <= j; ansj++) {
sum += input[ansi][ansj];
}
}
*/

ans[i][j] = sum;
}
}

return ans;
}

int main() {

// taking input from stdin


int row = 0;
int col = 0;
cin >> row;
cin >> col;

vector<vector<int>> input(row, vector<int>(col));


for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
int temp = 0;
cin >> temp;
input[i][j] = temp;
}
}

// get integral array


vector<vector<int>> ans;

//ans = integralImage(input);
ans = integralImage_better(input);

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


for (int j = 0; j < col; j++) {
cout << ans[i][j] << " ";
}
cout << endl;
}

return 0;
}

/* Round 1 question */
/
*************************************************************************
*****

Welcome to GDB Online.


GDB online is an online compiler and debugger tool for C, C++, Python,
Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly,
HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*************************************************************************
******/
#include <iostream>

using namespace std;

//Matrix mXn
// reshape(rXc) ->

// 3X3
// 1,2,3
// 4,5,6
// 1*9
vector<vector<int>> reshape(vector<vector<int>> inputMatrix, int row, int
col) {

//size check
int inputRow = inputMatrix.size();

if (inputRow != 0)
int inputCol = inputMatrix[0].size();

if (inputRow * inputCol != row * col)


return inputMatrix;

vector<vector<int>> ansMatrix;

// filling new matrix


int r = 0;
int c = 0;
for (int i = 0; i < inputRow; i++) {
for (int j = 0; j < inputCol; j++)
{
ansMatrix[r][c++] = inputMatrix[i][j];

if (c >= col) {
c = 0;
r++;
}
}
}
}

// Sorted arry

// 81, 4, 0, 9 36

// 0,4,9,36,81

// 0, 2, 6, 8, 10

// square each element


// combine version

// sorted version
// 0, 0,...

//Ans array
// -9, -2, 0, 3, 6, 0, 2, 6, 8, 10
//O(nlogn)

//i j
// -9, -2, 0, 3, 6,
vector<int> arrange(vector<int> in) {
// start from mod of min element
int minIndex = getMin(in);

// insert min to output array


vector<int> ans;
ans.push_back(in[minIndex]);

// Start from min val index and traverse i,j opposite side
int i = minIndex - 1;
int j = minIndex + 1;
while (i >= 0 && j <= in.size()) {
if (abs(in[i] < abs(in[j]))) {
ans.push_back(in[i] * in[i]);
i--;
}
else { //abs(in[i] > abs(in[j]))
ans.push_back(in[j] * in[j]);
j++;
}
}
}

int main() {

return 0;
}

/* Round 2 */
// rotate Matrix

template <typename T>


void swap(T& a, T& b) {
T temp = a;
a = b;
b = temp;
}

template <typename T>


void rotate(vector<vector<T>> in) {

// Transpose
for (int i = 0; i < r; i++) {
for (int j = 0; j < i; j++) {
swap(in[i][j], in[j][i]
}
}

//Rverse row wise


reverse();
}

// Copy constructor
class sample {
int a;

public:
sample(int a) {
this->a = a;
}

sample(sample& s) {
this->a = s.a;
}
}

/* Round 3 */
// compress and decompress problem
// Count no of zeros before a no. Make pair of <zeroCount, number>
// Return array

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy