InterviewSolution
Saved Bookmarks
| 1. |
For example, findTwoSum({ 3, 1, 5, 7, 5, 9 }, 10) should return a std::pair<int, int> containing any of the following pairs of indices: |
|
Answer» #include <iostream> // M x N matrix #DEFINE M 4 #define N 5 // DYNAMICALLY Allocate Memory for 2D ARRAY in C++ int main() { // dynamically allocate memory of size M*N int* A = new int[M * N]; // assign VALUES to allocated memory for (int i = 0; i < M; i++) for (int j = 0; j < N; j++) *(A + i*N + j) = rand() % 100; // print the 2D array for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) std::cout << *(A + i*N + j) << " "; // or (A + i*N)[j]) std::cout << std::endl; } // deallocate memory delete[] A; RETURN 0; } |
|