This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool canMakeArithmeticProgression(vector<int>& arr) { | |
int n=arr.size(); | |
sort(arr.begin(),arr.end()); | |
int d=arr[1]-arr[0]; | |
for(int i=2;i<n;i++) | |
{ | |
if(arr[i]-arr[i-1]!=d) | |
return false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int minCostToMoveChips(vector<int>& position) { | |
int n=position.size(); | |
int odd=0,even=0; | |
for(int i=0;i<n;i++) | |
{ | |
if(position[i]&1) | |
odd++; | |
else |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | |
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | |
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | |
* }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool uniqueOccurrences(vector<int>& arr) { | |
map<int,int> freq,freq_freq; | |
for(int i=0;i<arr.size();i++) | |
freq[arr[i]]++; | |
for(pair<int,int> p:freq) | |
{ | |
freq_freq[p.second]++; | |
if(freq_freq[p.second] > 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> sortedSquares(vector<int>& nums) { | |
int n=nums.size(); | |
int st=-1,min=INT_MAX; | |
for(int i=0;i<n;i++) | |
{ | |
if(abs(nums[i]) < min) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
static bool cmp(pair<int,int> a,pair<int,int> b) | |
{ | |
if(a.first==b.first) | |
return a.second<b.second; | |
return a.first<b.first; | |
} | |
vector<int> kWeakestRows(vector<vector<int>>& mat, int k) { | |
vector<pair<int,int>> arr; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool canBeEqual(vector<int>& target, vector<int>& arr) { | |
map<int,int> store; | |
for(int i=0;i<target.size();i++) | |
{ | |
store[target[i]]++; | |
store[arr[i]]--; | |
} | |
for(pair<int,int> p:store) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
string reverseWords(string s) { | |
int n=s.size(); | |
int l=0; | |
for(int i=0;i<n;i++) | |
{ | |
if(s[i]==' ' or i==n-1) | |
{ | |
int r=i-1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int hammingDistance(int x, int y) { | |
int z= (x ^ y); | |
int ans=0; | |
for(int i=31;i>=0;i--) | |
{ | |
if(z & (1<<i)) | |
ans++; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int arrayPairSum(vector<int>& nums) { | |
int n=nums.size(); | |
sort(nums.begin(),nums.end()); | |
int sum=0; | |
for(int i=0;i<n;i+=2) | |
sum+=nums[i]; | |
return sum; | |
} |
NewerOlder