Check if Ap
bool checkIsAP(int arr[], int n)
// code here
sort(arr,arr+n);
int diff = arr[1]-arr[0];
for(int i=2;i<n;i++){
if(arr[i]-arr[i-1] != diff){
return false;
return true;
bool checkIsGP(double arr[], int n)
{
// Base Case
if (n == 1)
return true;
// Sort array
sort(arr, arr + n);
// After sorting, common ratio
// between consecutive elements
// must be same.
double r = arr[1] / arr[0];
// Traverse the given array and
// check if the common ratio
// between ith element and (i-1)th
// element is same or not
for (int i = 2; i < n; i++) {
if (arr[i] / arr[i - 1] != r)
return false;
}
return true;
}
bool checkIsHP(double arr[], int n)
{
// Base Case
if (n == 1) {
return true;
}
double rec[n];
// Find reciprocal of arr[]
for (int i = 0; i < n; i++) {
rec[i] = ((1 / arr[i]));
}
// After finding reciprocal, check if
// the reciprocal is in A. P.
// To check for A.P.
if (checkIsAP(rec, n))
return true;
else
return false;
}
#include <bits/stdc++.h>
using namespace std;
void func(int a, int b, pair<int,int>& ans){
ans.first = a+b;
[Link] = a-b;
int main()
pair<int,int>a;
func(2,3,a);
cout << a.first << " " << [Link];
OR
#include <iostream>
using namespace std;
pair<int,int> func(int a, int b){
pair<int,int>ans;
ans.first = a+b;
[Link] = a-b;
return ans;
int main()
pair<int,int>a = func(2,3);
cout << a.first << " " << [Link];
Print root nodes of a binary tree
void printLeafNodes(Node *root)
{
// if node is null, return
if (!root)
return;
// if node is leaf node, print its data
if (!root->left && !root->right)
{
cout << root->data << " ";
return;
}
// if left child exists, check for leaf
// recursively
if (root->left)
printLeafNodes(root->left);
// if right child exists, check for leaf
// recursively
if (root->right)
printLeafNodes(root->right);
}
Duplicates
vector<int> duplicates(long long arr[], int n) {
// code here
unordered_map<int,int>mp;
for(int i=0;i<n;i++){
mp[arr[i]]++;
vector<int>ans;
for(auto i : mp){
if([Link] > 1){
ans.push_back(i.first);
if([Link]()){
ans.push_back(-1);
sort([Link](),[Link]());
return ans;
Mul ply a and b without mul plica on
#include <bits/stdc++.h>
using namespace std;
int main()
int a=5,b=12;
int sign = 1;
if(a < 0 && b < 0){
sign = 1;
b = -b;
a = -a;
}else if(b < 0){
sign = -1;
b = -b;
}else if(a < 0){
sign = -1;
a = -a;
int ans = 0;
for(int i=1;i<=b;i++){
ans += a;
ans *= sign;
cout << ans << endl;
Overlap Of rectangles
struct Point {
int x, y;
};
// Returns true if two rectangles (l1, r1) and (l2, r2)
// overlap
bool doOverlap(Point l1, Point r1, Point l2, Point r2)
{
// if rectangle has area 0, no overlap
if (l1.x == r1.x || l1.y == r1.y || r2.x == l2.x || l2.y == r2.y)
return false;
// If one rectangle is on left side of other
if (l1.x > r2.x || l2.x > r1.x)
return false;
// If one rectangle is above other
if (r1.y > l2.y || r2.y > l1.y)
return false;
return true;
}
/* Driver program to test above function */
int main()
{
Point l1 = { 0, 10 }, r1 = { 10, 0 };
Point l2 = { 5, 5 }, r2 = { 15, 0 };
if (doOverlap(l1, r1, l2, r2))
printf("Rectangles Overlap");
else
printf("Rectangles Don't Overlap");
return 0;
}
Count New Date a er adding Days
#include<bits/stdc++.h>
using namespace std;
// Return if year is leap year or not.
bool isLeapYear(int y)
if (y%100 != 0 && y%4 == 0 || y %400 == 0)
return true;
return false;
// Func on to get the number of days in a specific month
int getDaysInMonth(int month, int year) {
if (month == 2) {
return isLeapYear(year) ? 29 : 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else {
return 31;
// Func on to add days to the given date
void addDays(int day, int month, int year, int numberOfDays) {
while (numberOfDays > 0) {
int daysInMonth = getDaysInMonth(month, year);
if (day + numberOfDays > daysInMonth) {
numberOfDays -= (daysInMonth - day + 1);
day = 1;
if (month == 12) {
month = 1;
year++;
} else {
month++;
} else {
day += numberOfDays;
numberOfDays = 0;
cout << "New Date: " << day << "/" << month << "/" << year << endl;
// Driven Program
int main()
int d = 14, m = 3, y = 2015;
int x = 366;
addDays(d, m, y, x);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int sumOfRange(int start, int end) {
int sum = 0;
for (int i = start; i <= end; i++) {
sum += i;
return sum;
void sumOfRange(int start, int end, int &sum) {
if(start > end){
return;
sum += start;
sumOfRange(start+1,end,sum);
int main() {
int start = -5, end = 10;
// cout << "Enter the star ng number: ";
// cin >> start;
// cout << "Enter the ending number: ";
// cin >> end;
int sum = 0;
sumOfRange(start,end,sum);
// int result;
// result = sumOfRange(start, end);
cout << "Sum of the numbers between " << start << " and " << end << " is " << sum << endl;
return 0;
#include <iostream>
using namespace std;
#include <iostream>
void resizeImage(int originalWidth, int originalHeight, int &newWidth, int &newHeight) {
double widthRa o = 50.0 / originalWidth;
double heightRa o = 50.0 / originalHeight;
double ra o = std::min(widthRa o, heightRa o);
newWidth = (originalWidth * ra o);
newHeight = (originalHeight * ra o);
int main() {
int originalWidth = 100; // Replace with the actual width of the image
int originalHeight = 80; // Replace with the actual height of the image
int newWidth, newHeight;
resizeImage(originalWidth, originalHeight, newWidth, newHeight);
std::cout << "New Width: " << newWidth << std::endl;
std::cout << "New Height: " << newHeight << std::endl;
return 0;