-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
Arithmetic Number | ||
Given three integers 'A' denoting the first term of an arithmetic sequence , 'C' denoting the common difference of an arithmetic sequence and an integer 'B'. you need to tell whether 'B' exists in the arithmetic sequence or not. | ||
Example 1: | ||
Input: A = 1, B = 3, C = 2 | ||
Output: 1 | ||
Explaination: 3 is the second term of the | ||
sequence starting with 1 and having a common | ||
difference 2. | ||
Example 2: | ||
Input: A = 1, B = 2, C = 3 | ||
Output: 0 | ||
Explaination: 2 is not present in the sequence. | ||
Your Task: | ||
You do not need to read input or print anything. Your task is to complete the function inSequence() which takes A, B and C and returns 1 if B is present in the sequence. Otherwise, returns 0. | ||
Expected Time Complexity: O(1) | ||
Expected Auxiliary Space: O(1) | ||
*/ | ||
/* | ||
a=1 c=2 and b =5 | ||
AP sequence -> first term is 1 and common difference is 2 | ||
1+2 -> 3+2-> 5+2->7 | ||
5 is present | ||
time complexity is const | ||
an = a1 + (n-1)d (an -> nth term (which is b =5)) (a1 -> 1st term (which is a =1)) and d is c->2 | ||
b= a+(n-1)c | ||
n = (b-1)/c + 1 [n>=1] {n is number of terms} | ||
if c=0 and a==b then we dont have to go anywhere else(1st element required element) | ||
if c=0 and a!= b then we can't move so ans in 0 | ||
if c!=0 | ||
then (b-1)/c should be positive ( check b-a % c -> true then 1 should not return fractional value) | ||
if negative then n<1 which is not true then return 0 | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution | ||
{ | ||
public: | ||
int inSequence(int a, int b, int c) | ||
{ | ||
// code here | ||
if (c == 0) | ||
{ //diff is 0 | ||
if (a == b) | ||
{ //element is present there only | ||
return 1; | ||
} | ||
else | ||
{ //diff is 0 we can't move | ||
return 0; | ||
} | ||
} | ||
else | ||
{ | ||
if ((b - a) / c < 0) | ||
{ //if negative then return 0 as it should be positive | ||
return 0; | ||
} | ||
else | ||
{ | ||
if ((b - a) % c == 0) | ||
{ //not fractional | ||
return 1; | ||
} | ||
else | ||
{ //fractional | ||
return 0; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
#ifndef ONLINE_JUDGE | ||
freopen("input.txt", "r", stdin); | ||
freopen("output.txt", "w", stdout); | ||
#endif | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
int A, B, C; | ||
cin >> A >> B >> C; | ||
|
||
Solution ob; | ||
int ans = ob.inSequence(A, B, C); | ||
cout << ans << endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
Minimum Swaps to Sort | ||
Given an array of n distinct elements. Find the minimum number of swaps required to sort the array in strictly increasing order. | ||
Example 1: | ||
Input: | ||
nums = {2, 8, 5, 4} | ||
Output: | ||
1 | ||
Explaination: | ||
swap 8 with 4. | ||
Example 2: | ||
Input: | ||
nums = {10, 19, 6, 3, 5} | ||
Output: | ||
2 | ||
Explaination: | ||
swap 10 with 3 and swap 19 with 5. | ||
Your Task: | ||
You do not need to read input or print anything. Your task is to complete the function minSwaps() which takes the nums as input parameter and returns an integer denoting the minimum number of swaps required to sort the array. If the array is already sorted, return 0. | ||
Expected Time Complexity: O(nlogn) | ||
Expected Auxiliary Space: O(n) | ||
*/ | ||
|
||
/* | ||
1 5 4 3 2 | ||
1 2 3 4 5 | ||
1 is in its correct pos | ||
we have to swap 5 (min swap) -> greedy approach (swap with 2 is benifitial) | ||
1 2 4 3 5 | ||
we have to swap 4 (3 will be benefitial) | ||
1 2 3 4 5 | ||
approach | ||
pair vector -> number with index | ||
(1,0) (5,1) (4,2) (3,3) (2,4) | ||
we will convert 1 2 3 4 5 to original arr to get no of swaps | ||
sort vector (first parameter) | ||
(1,0) (2,4) (3,3) (4,2) (5,1) | ||
iterate this -> we are already getting their first indexes before sorting | ||
index is equal to second val of vector (check) -> if same then no change in pos | ||
now i++ | ||
1==4 ? no | ||
swap (with 4th index) | ||
count++ | ||
i-- | ||
check 5 is in pos or not ? 1==1 | ||
i++ | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution | ||
{ | ||
public: | ||
int minSwaps(vector<int> &v) | ||
{ | ||
// Code here | ||
int count = 0; | ||
int n = v.size(); | ||
vector<pair<int, int>> vect(n); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
vect[i] = {v[i], i}; //first element is element and second element is position (make pair) | ||
} | ||
sort(vect.begin(), vect.end()); //sorting according to first value | ||
for (int i = 0; i < n; i++) | ||
{ | ||
if (vect[i].second == i) | ||
{ //was in same position at first | ||
continue; | ||
} | ||
else | ||
{ | ||
count++; //increasing counter | ||
swap(vect[i], vect[vect[i].second]); //swapping with element which is at the v.second idx | ||
i--; //checking back if the swapped element is at correct pos or not | ||
} | ||
} | ||
return count; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
#ifndef ONLINE_JUDGE | ||
freopen("input.txt", "r", stdin); | ||
freopen("output.txt", "w", stdout); | ||
#endif | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
int n; | ||
cin >> n; | ||
vector<int> v(n); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> v[i]; | ||
} | ||
Solution ob; | ||
int ans = ob.minSwaps(v); | ||
cout << ans << endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
Sort by Set Bit Count | ||
Given an array of integers, sort the array (in descending order) according to count of set bits in binary representation of array elements. | ||
Note: For integers having same number of set bits in their binary representation, sort according to their position in the original array i.e., a stable sort. | ||
Example 1: | ||
Input: | ||
arr[] = {5, 2, 3, 9, 4, 6, 7, 15, 32}; | ||
Output: | ||
15 7 5 3 9 6 2 4 32 | ||
Explanation: | ||
The integers in their binary | ||
representation are: | ||
15 - 1111 | ||
7 - 0111 | ||
5 - 0101 | ||
3 - 0011 | ||
9 - 1001 | ||
6 - 0110 | ||
2 - 0010 | ||
4 - 0100 | ||
32 - 10000 | ||
hence the non-increasing sorted order is: | ||
{15}, {7}, {5, 3, 9, 6}, {2, 4, 32} | ||
Example 2: | ||
Input: | ||
arr[] = {1, 2, 3, 4, 5, 6}; | ||
Output: | ||
3 5 6 1 2 4 | ||
Explanation: | ||
3 - 0110 | ||
5 - 0101 | ||
6 - 0110 | ||
1 - 0001 | ||
2 - 0010 | ||
4 - 0100 | ||
hence the non-increasing sorted order is | ||
{3, 5, 6}, {1, 2, 4} | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution | ||
{ | ||
public: | ||
static bool comp(int a, int b) | ||
{ | ||
return __builtin_popcount(a) > __builtin_popcount(b); //> decending order sort (more setbit int will come first) | ||
//__builtin_popcount gives count of setbits | ||
} | ||
|
||
void sortBySetBitCount(int arr[], int n) | ||
{ | ||
//sort(arr, arr + n, comp);//can't handle this | ||
//gives original rep of array if 2 numbers have same set bit | ||
//then number which is first in original array will come first | ||
stable_sort(arr, arr + n, comp); | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
#ifndef ONLINE_JUDGE | ||
freopen("input.txt", "r", stdin); | ||
freopen("output.txt", "w", stdout); | ||
#endif | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
int n; | ||
cin >> n; | ||
int arr[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> arr[i]; | ||
} | ||
Solution obj; | ||
obj.sortBySetBitCount(arr, n); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cout << arr[i] << " "; | ||
} | ||
cout << endl; | ||
} | ||
|
||
return 0; | ||
} |