GeeksForGeeks - POTD | GFG POTD Answer
Canal cerrado
1 218
Suscriptores
Sin datos24 horas
-97 días
-5730 días
Archivo de publicaciones
Hey, I am inviting you to sign up for Worry-free Shopping! Avail up to Rs.1 Lakh Instant Credit today with Flipkart Pay Later. It is super simple, Just Shop Now and Pay Next Month or in Easy EMIs. There is more.
Sign Up and Get a Gift Card worth Rs.100.
Why Wait? Click Here
https://dl.flipkart.com/s/sUW3LruuuN
class Solution{
public:
//Function to merge the arrays.
void merge(long long arr1[], long long arr2[], int n, int m)
{
int end=n-1; //take end as first array last element
int start=0; //start as a first element
while(end>=0&&startarr2[start]) //swap to store the all smallest number in the first array
swap(arr1[end--],arr2[start++]);
else
break;
}
sort(arr1,arr1+n); //sort the all stored small and the large numbers in the both arrays
sort(arr2,arr2+m);
}
};
class Solution
{
public:
//Function to sort an array using a quick sort algorithm.
void quickSort(int arr[], int low, int high)
{
if(low < high)
{
// It gives the correct position for the partition of the array because
//The element at this position has smaller elements before it and bigger elements after it
int partition_point = partition(arr, low, high);
// repeating the same process to sort the smaller elements before partition_point
quickSort(arr, low, partition_point - 1);
// repeating the same process to sort the bigger elements after partition_point
quickSort(arr, partition_point + 1, high);
}
}
public:
int partition (int arr[], int low, int high)
{
//Taking the last element as a pivot
int pivot = arr[high];
int j = low - 1;
// in this loop smaller elements are placed at starting
for(int i = low;i <= high-1; i++)
{
if(arr[i] < pivot)
{
j++; // increase the j value when an element is less than the pivot then
swap(arr[i],arr[j]); // swap this element to the jth position
}
}
swap(arr[j + 1],arr[high]); // placing the pivot after all the elements smaller than it
return j+1; // return the current position of the pivot
}
};
class Solution {
public:
int stockBuyAndSell(int n, vector &prices) {
// code here
int ans=0; //set ans variable to store the ans
for(int i=0;i
class Solution{
public:
int countSubArrayProductLessThanK(const vector& a, int n, long long k) {
int count = 0;
long long int product = 1;
int j = 0;
for(int i = 0; i < n; i++)
{
product *= a[i];
while(product >= k && j <= i)
{
product /= a[j];
j++;
}
count += (i-j+1);
}
return count;
}
};
class Solution{
public:
int maxIndexDiff(int arr[], int n) {
int i=0;
int j=n-1;
int diff = 0;
while(i
class Solution{
public:
int setSetBit(int x, int y, int l, int r){
int temp = 0;
for(int pos = l ; pos <= r ; pos++)
{
if(1 & ( y >> (pos - 1)))
{
temp = temp | (1 << ( pos - 1));
}
}
x |= temp;
return x;
}
};
class Solution {
public:
int setBits(int N) {
int cnt = 0;
while(N){
cnt += (N & 1);
N >>= 1;
}
return cnt;
}
};
class Solution{
public:
int isDivisible(string s){
int c=0;
int x=1;
for(int i=s.size()-1;i>=0;i--)
{
if(s[i]=='1') c+=x;
x=(x==1)?2:1;
}
return (c%3==0)?1:0;
}
};
class Solution{
public:
int nextHappy(int N){
for (int i = N + 1; i < 1000000; i++) {
int count = 0;
int temp = i;
int sum = 0;
while (temp) {
int r = temp % 10;
sum += r * r;
temp /= 10;
if (sum == 1 && temp == 0) {
return i;
}
if (sum != 0 && temp == 0) {
temp = sum;
sum = 0;
}
if (count > 50) {
break;
}
count++;
}
}
}
};
