Category : vector

You are given a read only array of n integers from 1 to n. Each integer appears exactly once except A which appears twice and B which is missing. Return A and B. Here is the solution tried : vector<int> Solution::repeatedNumber(const vector<int> &A) { int n=A.size(); vector<int> res(2); unordered_map<int, int> m; for(int i=0;i<n;i++){ m[A[i]]++; if(m[A[i]]==2) ..

Read more

I am trying to do dfs traversal using recursive call, My Graph, and vector<int>visited are global variables. num_v variable corresponds to number of vertices,num_e corresponds to number of edges I tried calling two DFS calls in main, but my output matches to the correct DFS order for the first case. and it gives incorrect out ..

Read more

I would like to have a concept requiring as return type an arbitrary vector: template<typename T> concept HasVector = requires (T t) { { T.vec() } -> std::same_as<std::vector<int>>; //works { T.vec() } -> std::same_as<std::vector<foo>>; //want to put something arbitrary in here } Such that we would have something like the following: class A { std::vector<int> ..

Read more

The vector is std::vector<vector2> assualt_rifle = { {-35, 50},{5, 46},{-55, 42},{-42, 37},{0, 33},{16, 28},{29, 24},{38, 19},{42, 14},{42, 9},{38, 9},{30, 18},{17, 25},{0, 29},{-15, 32},{-27, 33},{-37, 32},{-43, 29},{-46, 24},{-45, 17},{-42, 8},{-35, 5},{-24, 14},{-11, 21},{12, 25},{36, 28},{49, 28},{49, 26},{38, 21}}; I need to get the delta between the current element of the vector and the previous one. After ..

Read more

Trying to solve ‘move zeros’ question. I have to shift all zeros of vector at last and then all other numbers need to be arranged in ascending order. class Solution { public: void moveZeroes(vector<int>& nums) { int position = nums.size()-1; for(int i=0;i<nums.size();i++){ if(nums[i]==0 && nums[position]!=0 && position>=i){ int temp=nums[i]; nums[i]=nums[position]; nums[position]=temp; position-=1; } for(int i=0;i<nums.size();i++){ ..

Read more

I have a struct called Person: Person { int id; int age; int salary; }; so I want to enter as many Person as I want with the standard input in the following form: [id, age, salary] [id, age, salary] … in one line. so I did write this code but it doesn’t give me ..

Read more