#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>&
nums) {
int N = nums.size();
vector<int> result;
vector<int> a(101);
vector<int> b(101);
for (int i = 0; i < N; i++) {
a[nums[i]]++; // what does this mean?
}
for (int i = 1; i < 101; i++) {
b[i] = a[i - 1] + b[i - 1];
}
for (int i = 0; i < N; i++) {
result.push_back(b[nums[i]]);
}
for (int i = 0; i < N; i++)
{
cout << result[i] << " ";
}
return result;
}
};
int main()
{
Solution solution;
vector<int> vect = { 8,1,2,2,3 };
solution.smallerNumbersThanCurrent(vect);
return 0;
}
what does this line mean? a[nums[i]]++;
is there any example or other way to write that?
Answer:
we can write a[num[i]]++ as
a[num[i]] = a[num[i]] + 1;
num[i] return the value from vector num at ith position then this value is taken as the index value for the vector a and at this index, lets say we get value of num[i] = k, then value at a[k] will be incremented by one.
Note: Let me know if you have any doubt.
#include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { int...
Analyze the following code. #include <iostream> using namespace std; class B { public: B() { }; int k; }; int main() { B b; cout << b.k << endl; return 0; } The program has a compile error because b.k cannot be accessed. The program has a runtime error because b.k does not have a value. The program displays unpredictable number. The program displays 1.
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
#include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS = 8; vector <double> inverse(NUM_ITEMS); int j; double temp; for (int i = 0; i < NUM_ITEMS; i++) { inverse.at(i) = 1 / (i + 1.0); } cout << fixed << setprecision(2); cout << "Original vector..." << endl; for (int i = 0; i < NUM_ITEMS; i++) { cout << inverse.at(i) << " "; } cout << endl; cout << "Reversed vector..." << endl; for...
What is the output of this program? #include <iostream> using namespace std; class rect { int x, y; public: void val (int, int); int area () { return (x * y); } }; void rect::val (int a, int b) { x = a; y = b; } int main () { rect rect; rect.val (3, 4); cout << "rect area: " << rect.area(); return 0; } a) rect area:7 b) rect area: 12 c) rect area:24 d) none of the...
Add comments on this Fibonacci C++ program in detail. #include <iostream> using namespace std; int fib(int n){ int a = 0,b = 1,c; if(n == 0 || n == 1){ return n; } while(n>2){ c = b + a; a = b; b = c; n--; } return c; } int main(){ int n; cout<<"Enter n: "; cin>>n; int result = fib(n); cout<<"Fib("<<n<<") = "<<result<<endl; return 0; }
What does this print to the screen? #include <iostream> using namespace std; int main () { int *a; a = new int[2]; for(int i = 0; i < 2; i++) *(a + i) = i; for(int i = 0; i < 2; i++) cout << a[i] << " "; delete [] a; return 0; } I'm getting 0 1 Please step me through the process.
#include <iostream>
using namespace std;
int * newZeroArray(int size) {
//your code here
}
int main() {
int size = 10;
int * A = newZeroArray(size);
for(int i = 0; i < size; i++)
cout << A[i] << " ";
cout << endl;
}Write a function that takes a size, creates a new array of that size with all zeros, and returns the array. If the size is not a valid size for an array, do not return a valid...
What is printed by the following program? (read the code carefully) #include <iostream> using namespace std; class A { public: int x; }; int main() { A *a = new A; A *b = new A; a->x=1; b->x=1; if (a == b) { cout << "black"; } else { cout << "white"; } delete a; delete b; return 0; }
Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n =0; int i = 0; cout << "Please enter a strictly positive number:"; cin >> n if (n <= 0) exit(EXIT_FAILURE) while (n > 1) n-n/2; i << endl; cout"Output:" return 0; Answer the following questions: What is the output of the program for each of the following values of n: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9? What does...
#include <iostream> using namespace std; int main() { int i,n; int counter=0; cin >> n; for (i = 1; i <= n; i++) { if (n % i == 0 ) { counter++; } /*else { counter++; i++; }*/ } if (counter == 2) cout << n <<...