#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...
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...
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...
Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height; int weight; public: void setHeight(int h){ height = h; } int getHeight(){ return height; } void setWeight(int w){ weight = w; } int getWeight(){ return weight; } virtual void makeSound() = 0; virtual void eat(); }; class Dog: public Animal{ public: void makeSound(){ cout << "Bow Bow\n"; } void eat (){ cout <<"The dog is eating\n"; } void Catch(){ cout << "The dog is...
What does this print to the screen? #include <iostream> using namespace std; int main() { int c = 5; int *p = &c; *p *= c; cout << *(++p); return 0; } I get 0 in Xcode, but it looks to me like it should be giving me the memory location. Please step me through the process.
Help 4. (1596) #include <iostream> using namespace std; int main int i, j; int sum; sum 20; for (i 1; i 2; i++)t for (j = 1;js-2; j++) { sum = sum + i +j; cout <c'when i and j are:" i ce<jsum issum cendl. return 0; Show the output values of j and sum: sum-sum +i+j 5. (15%) Write a program A. to define two classes I. class Shape comprising the following members: class Shape Shape(string colorVar):/lconstructor void setColor(string...