Notes
Notes - notes.io |
class Result
{
static int swapEvenOddBits(int num)
{
int EvenBits= num & 0xAAAAAAAA;
int OddBits=num & 0x55555555;
EvenBits>>=1;
OddBits<<=1;
return EvenBits | OddBits;
}
}
------------------------------------------------------------------------------
Data Collection By Telecom Company Java
class Result {
static int collectData(int height[], int n) {
// Write your code here
int res[]=new int[n];
int k=0;
Stack<Integer> s= new Stack<>();
for(int i=n-1; i>=0; i--){
while(s.size()>0 && s.peek()<=height[i]){
s.pop();
}
if(s.size()==0){
res[k++]=-1;
}
else{
res[k++]=s.peek();
}
s.push(height[i]);
}
int sum=0;
for(int i=0; i<res.length; i++){
sum+=res[i];
}
return sum;
}
}
------------------------------------------------------------------------------
Code Compiler Output C++
// Return true if the compiler output will be success, else return false
bool isMatch(char ch1, char ch2){
if(ch1 =='('&& ch2 ==')') return true;
if(ch1 =='['&& ch2 ==']') return true;
if(ch1 =='{'&& ch2 =='}') return true;
return false;
}
bool compilerOutput(string x) {
string s="";
for(int i=0; i<x.length(); i++){
if(x[i]=='('|| x[i]==')' || x[i]=='['|| x[i]==']' || x[i]=='{' || x[i]=='}')
s=s+x[i];
}
int top = -1;
for(int i=0;i<s.length();i++){
if(top<0 || !isMatch(s[top], s[i])){
++top;
s[top] = s[i];
}else{
--top;
}
}
return top ==-1;
}
------------------------------------------------------------------------------
Intelligent Code Editor Java
class Result {
static int highlightedBrackets(String expr) {
// Write your code here
Stack<Character> s= new Stack<>();
for(char c: expr.toCharArray()){
if(c=='{'){
s.push(c);
}
else if(c=='}'){
if(s.isEmpty() || s.peek()!='{'){
s.push(c);
}
else{
s.pop();
}
}
}
int m=0;
int n=0;
while(!s.isEmpty()){
if(s.peek()=='{'){
m++;
}
else if(s.peek()=='}'){
n++;
}
s.pop();
}
// if there sum is odd return -1;
if((m+n)%2!=0){
return -1;
}
// if both odd
if(m%2!=0 && n%2!=0){
return ((m+n)/2)+1;
}
// if both even
if(m%2==0 && n%2==0){
return ((m+n)/2);
}
return 0;
}
}
------------------------------------------------------------------------------
Evaluate the postfix expression using Stack Java
/* isEmpty(), isFull(), push(int) and int pop() functions available on Stack. */
static int evalPostfix(CQStack s, String exp) {
// Write your code here
char [] arr=exp.toCharArray();
for(int i=0; i<exp.length(); i++)
{
if(arr[i]>='0' && arr[i]<='9'){
s.push(Integer.parseInt(Character.toString(arr[i])));
}
else{
int b=s.pop();
int a=s.pop();
if(arr[i]=='+'){
s.push(a+b);
}
else if(arr[i]=='-'){
s.push(a-b);
}
else if(arr[i]=='*'){
s.push(a*b);
}
else if(arr[i]=='/'){
s.push(a/b);
}
else if(arr[i]=='^'){
s.push((int)Math.pow(a,b));
}
}
}
return s.pop();
}
------------------------------------------------------------------------------
Count the number of nodes in a circular linked list C++
int countNodes(struct Node* head) {
int count = 0;
Node* temp = head;
if(head==NULL) return count;
do{
count++;
temp = temp->next;
}while(temp!=head);
return count;
}
------------------------------------------------------------------------------
Given list is circular or not Java
static int isCircular(Node head) {
// Write your code here
if(head == null)
return 1;
Set<Node> st = new HashSet<>();
Node cur = head;
while(head != null){
if(st.contains(head)){
if(head == cur)
return 1;
return 0;
}
st.add(head);
head = head.next;
}
return 0;
}
------------------------------------------------------------------------------
Add one to each digit of a 4 digit C++
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int n,i=1;
cin>>n;
int temp = n;
int ans=0;
while(temp != 0)
{
int r = temp%10;
if(r == 9)
{
r = 0;
}
else{
r += 1;
}
ans += r*i;
i = i*10;
temp = temp/10;
}
cout<<ans<<endl;
return 0;
}
------------------------------------------------------------------------------
Sum of the first and the last digit of a 4 digit number C++
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int n;
cin>>n;
int sum = (n%10)+(n/1000);
cout<<sum<<endl;
return 0;
}
------------------------------------------------------------------------------
Calculate Discount based on type of customer C++
#include<iostream>
#include<cstdio>
#include<cmath>
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << fixed << setprecision(2);
float n;
cin>>n;
if(n>30000)
{
float dis = (17*n)/100;
n -= dis;
}
else if(n>10000)
{
float dis = (15*n)/100;
n -= dis;
}
else if(n>=5001 && n<10000)
{
float dis = (10*n)/100;
n -= dis;
}
else if(n>=1001 && n<5000)
{
float dis = (5*n)/100;
n -= dis;
}
else if(n<1000)
{
float dis = (0*n)/100;
n -= dis;
}
cout<<n<<endl;
return 0;
}
------------------------------------------------------------------------------
Pyramid - 6 C++
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
void print(int n)
{
for(int i=n ; i>0 ; i--)
{
cout<<i;
}
for(int j=2 ; j<=n ; j++)
{
cout<<j;
}
cout<<endl;
}
int main()
{
int n;
cin>>n;
for(int i=1 ; i<=n ; i++)
{
print(i);
}
return 0;
}
------------------------------------------------------------------------------
Generate first n Prime numbers C++
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
void Prime(int n, int m)
{
int count = 0;
for (int i = 2; i <= n; i++)
{
int t = 0;
if(count == m)
{
break;
}
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{
t = 1;
}
}
if (t == 0)
{
cout << i << endl;
count++;
}
}
}
int main()
{
int n, m;
cin >> n >> m;
Prime(n, m);
return 0;
}
------------------------------------------------------------------------------
Partition a Array C++
void partitionArray(int arr[], int n, int x)
{
int i, j, temp;
i = 0;
j = n-1;
while (i < j)
{
while (arr[i] <=x && i<j)
i++;
while (arr[j] > x && i<j)
j--;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
------------------------------------------------------------------------------
Count words C++
int countWords(string str) {
// Write your code here
int n = str.length();
int k=0,c=0;
for(int i=0;i<n;i++){
if(str[i]==' ' && str[i+1]!=' ')
{
c++;
}
}
if(n==c){
return 0;
}
else
return c+1;
}
------------------------------------------------------------------------------
Keyword Count C++
int keywordCount(string str, string word) {
// Write your code here
int n1 = str.length();
int n2 = word.length();
string temp="";
int c=0;
for(int i=0;i<n1;i++){
if(str[i]==' '){
continue;
}
else{
temp = temp + str[i];
if(str[i+1]==' ' || str[i+1]==' '){
if(temp==word)
c++;
temp="";
}
}
}
return c;
}
------------------------------------------------------------------------------
Reduce the given sequence C++
string reduceSequence(string str, char ch) {
// Write your code here
int n = str.length();
string str1="";
for(int i=0;i<n;i++){
if(str[i]==ch){
if(str[i]!=str[i+1]){
str1 = str1 + str[i];
}
}
else{
str1 = str1 + str[i];
}
}
return str1;
}
------------------------------------------------------------------------------
Implement strtok function C++
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
string strtok_code(string str1, char delim){
// Complete the function
}
int main()
{
int t,i,j=0;
string str;
char ch;
cin>>t;
while(t--){
cin>>str>>ch;
// Write your code here...
int n = str.length();
for(int i=0;i<n;i++){
if(str[i]==ch){
str[i]='n';
}
}
for(int i=0;i<n;i++){
cout<<str[i];
}
cout<<endl;
}
}
------------------------------------------------------------------------------
Remove duplicates from sorted string C++
string removeDuplicates(string str) {
// Write your code here
int n = str.length();
string temp="";
for(int i=0;i<n;i++){
if(str[i]!=str[i+1]){
temp = temp + str[i];
}
}
return temp;
}
------------------------------------------------------------------------------
Add two matrices C++
#include<iostream>
#include<cstdio>
#include<cmath>
// Include headers as needed
using namespace std;
int main()
{
// Write your code here
// Return 0 to indicate normal termination
int r , c;
cin>>r>>c;
int a[r][c],b[r][c],C[r][c];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin>>a[i][j];
}
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin>>b[i][j];
}
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
C[i][j]=a[i][j]+b[i][j];
}
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cout<<C[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
------------------------------------------------------------------------------
Binary equivalent using recursion C++
int decimalToBinary(int n)
{
if(n < 1)
{
return 0;
}
return (n%2)+10*decimalToBinary(n/2);
}
------------------------------------------------------------------------------
Recursively remove all adjacent duplicates C++
bool freq(string s)
{
for(int i=0 ; i<s.length() ; i++)
{
if(s[i-1] == s[i])
{
return false;
}
}
return true;
}
string processString(string a)
{
// Complete the function
int n = a.length();
string s;
for(int i=0 ; i<a.length() ; i++)
{
s += a[i];
}
if(freq(s))
{
return s;
}
else{
s = "";
}
for(int i=1 ; i<a.length() ; i++)
{
if(a[i-1] != a[i])
{
if(i-1 != 0)
{
if(a[i-2] != a[i-1])
{
s += a[i-1];
}
}
else{
s += a[i-1];
}
}
else{
i += 1;
}
}
if(a[n-1] != a[n-2])
{
s += a[n-1];
}
if(freq(s))
{
return s;
}
else{
return processString(s);
}
}
------------------------------------------------------------------------------
Operator Overloading in Matrix class C++
class Matrix
{
int m,n,a[20][20];
public:
Matrix(int x,int y)
{
m=x;
n=y;
}
void readmat()
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
}
Matrix operator +(Matrix a2)
{
Matrix ans(m,n);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
ans.a[i][j] = a[i][j] + a2.a[i][j];
return ans;
}
Matrix operator -(Matrix a2)
{
Matrix ans(m,n);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
ans.a[i][j] = a[i][j] - a2.a[i][j];
return ans;
}
void display()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++){
if(j==n-1 && i==m-1){
cout<<a[i][j]<<endl;
}
else{
cout<<a[i][j]<<" ";
}
}
}
}
};
------------------------------------------------------------------------------
C++: Polymorphism - 1 C++
int main()
{
// Write your code here
string name;
int age,sal;
getline(cin,name);
cin>>age>>sal;
int choice;
cin>>choice;
if(choice==1){
cout<<"Name: "<<name<<"nAge: "<<age<<"nSalary: "<<sal<<"nDept: CSE"<<endl;
}else if(choice==2){
cout<<"Name: "<<name<<"nAge: "<<age<<"nSalary: "<<sal<<"nDept: ECE"<<endl;
}else if(choice==3){
cout<<"Name: "<<name<<"nAge: "<<age<<"nSalary: "<<sal<<"nDept: ME"<<endl;
}
return 0;
}
------------------------------------------------------------------------------
C++: Polymorphism - 2
class rectangle:public figure{
int a,b;
public:
virtual void area(){
cin>>a>>b;
cout<<"area of rectangle is "<<a*b<<endl;
}
};
class triangle:public figure{
int a,b;
public:
virtual void area(){
cin>>a>>b;
cout<<"area of triangle is "<<(a*b)/2<<endl;
}
};
------------------------------------------------------------------------------
Catch all exceptions C++
#include <iostream>
using namespace std;
int main() {
string name;
int marks1, marks2, marks3;
while (true) {
cin >> name;
cin >> marks1;
if (marks1 > 100 || marks1 < 0) {
cout << "Invalid Marks" << endl;
continue;
}
cin >> marks2;
if (marks2 > 100 || marks2 < 0) {
cout << "Invalid Marks" << endl;
continue;
}
cin >> marks3;
if (marks3 > 100 || marks3 < 0) {
cout << "Invalid Marks" << endl;
continue;
}
break;
}
cout << "Done" << endl;
return 0;
}
------------------------------------------------------------------------------
Merge Student Rows C++
#include <vector>
using namespace std;
vector<int> mergeStudents(vector<int> marks1, vector<int> marks2) {
int n = marks1.size();
int m = marks2.size();
// Initialize the merged array with size (n + m)
vector<int> merged(n + m);
// Initialize pointers to the highest marks in both arrays
int i = 0, j = 0;
// Merge the arrays in descending order
for (int k = 0; k < n + m; k++) {
if (i >= n) {
merged[k] = marks2[j];
j++;
} else if (j >= m) {
merged[k] = marks1[i];
i++;
} else if (marks1[i] > marks2[j]) {
merged[k] = marks1[i];
i++;
} else {
merged[k] = marks2[j];
j++;
}
}
return merged;
}
------------------------------------------------------------------------------
Students standing in a line C++
int studentShifted(int arr[], int N) {
// Write your code here
int l = 0, r = N-1;
while(l<=r){
int mid = l+(r-l)/2;
int prev = (mid-1+N)%N;
int next = (mid+1)%N;
if(arr[prev] >= arr[mid] && arr[next] >= arr[mid])
return N-mid-1;
else if(arr[mid] > arr[N-1])
r = mid-1;
else
l = mid+1;
}
}
------------------------------------------------------------------------------
Reverse and Original number are equal or not?
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int n;
cin>>n;
int rev=0;
int temp=n;
while(n>0){
int r=n%10;
rev=rev*10+r;
n=n/10;
}
if(temp==rev){
cout<<"Equal";
}
else{
cout<<"Not Equal";
}
return 0;
}
Given character is vowel, consonants
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
char ch;
cin>>ch;
if(ch=='a' || ch=='A'){
cout<<"vowel";
}
else if(ch=='e' || ch=='I'){
cout<<"vowel";
}
else if(ch=='i' || ch=='E' ){
cout<<"vowel";
}
else if(ch=='o' || ch=='O'){
cout<<"vowel";
}
else if(ch=='u' || ch=='U'){
cout<<"vowel";
}
else{
cout<<"consonant";
}
return 0;
}
Create Simple calculator of four operations using switch case
#include<iostream>
#include<cstdio>
#include<cmath>
#include <iomanip>
using namespace std;
int main()
{
float a;
cin>>a;
float b;
char op;
cin>>op;
cin>>b;
switch(op){
case '+':
cout << fixed << setprecision(2)<<a+b;
break;
case '-':
cout << fixed << setprecision(2)<<a-b;
break;
case '*':
cout << fixed << setprecision(2)<<a*b;
break;
case '/':
cout << fixed << setprecision(2)<<a/b;
break;
default:
break;
}
return 0;
}
Sum of first n terms (using for loop)
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int sum=0;
int n;
cin>>n;
while(n>0){
sum=sum+n;
n--;
}
cout<<sum;
return 0;
}
Multiplication table ( using for loop)
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int n;
int m;
cin>>n>>m;
for(int i=1;i<=m;i++){
cout<<n*i<<endl;
}
return 0;
}
Factorial of a number
long fact(int n) {
// Write your code here
if(n==0){
return 1;
}
return n*fact(n-1);
}
Sum of a set of numbers
#include<iostream>
#include<cstdio>
#include<cmath>
// Include headers as needed
using namespace std;
int main()
{
int sum=0;
int n;
cin>>n;
for(int i=1;i<=n;i++){
int m;
cin>>m;
sum=sum+m;
}
cout<<sum<<endl;
// Write your code here
// Return 0 to indicate normal termination
return 0;
}
Rotate a list
#include<iostream>
#include<cstdio>
#include<cmath>
// Include headers as needed
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int j;
cin>>j;
for(int i=0;i<j;i++){
int temp=a[0];
for(int k=0;k<n;k++){
a[k]=a[k+1];
}
a[n-1]=temp;
}
for(int i=0;i<n;i++){
if(i==n-1){
cout<<a[i];
}
else{
cout<<a[i]<<" ";
}
}
cout<<endl;
}
// Write your code here
// Return 0 to indicate normal termination
return 0;
}
Maximum element in an Array
int maxElement(int arr[], int N) {
// Write Your Code here
int max=arr[0];
for(int i=0;i<N;i++){
if(arr[i]>max){
max=arr[i];
}
}
return max;
}
Kth largest number
int kthLargest(int arr[], int size, int k) {
// Write your code here
int l=0;
for(int i=0;i<k+1;i++){
l=i;
for(int j=i+1;j<size;j++){
if(arr[l]<arr[j]){
l=j;
}
}
int temp=arr[l];
arr[l]=arr[i];
arr[i]=temp;
}
return arr[k];
}
Find occurrences of palindrome words in a string
int countPalindrome(string str)
{
int count=0;
string s;
for(int i=0;i<str.length();i++){
if(str[i]==' ' || i==str.length()-1){
int flag=1;
if(i==str.length()-1){
s+=str[i];
}
for(int j=0;j<s.length()/2;j++){
if(s[j]!=s[s.length()-j-1]){
flag=0;
break;
}
}
if(flag){
count++;
}
s.clear();
}
else{
s+=str[i];
}
}
return count;
}
Capitalize first letter of each word in a string
// Alter the string according to specifications above
void capitalizeFirstChar(string &str)
{
if(str[0]>='a'&&str[0]<='z'){
str[0]=str[0]-32;
}
for(int i=0;i<str.length();i++){
if(str[i-1]==' '&&str[i]>='a'&&str[i]<='z'){
str[i]=str[i]-32;
}
}
}
Reverse a string
void reverseString (string &str) {
int len = str.length();
for(int i=0; i<len/2 ; i++){
int temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}
}
String is pangram or not
bool isPangram(string str) {
// Write your code here
int a[127]={0};
for(int i=0; i<str.length(); i++){
a[str[i]]++;
}
for(int i=97; i<=122; i++){
if(a[i]==0){
return false;
}
}
return true;
}
Minimum indexed character in strings
char minIndexChar(string str1, string str2){
for(int i = 0; i<str1.length();i++){
for(int j = 0; j<str2.length();j++){
if(str1[i]==str2[j]){
return str2[j];
}
}
}
return 0;
}
Form a palindrome string
int minInsertions(string str1, string str)
{
int i=0;
int j=str1.length()-1;
int count=0;
while(i<j){
if(str1[i]!=str1[j]){
i++;
count++;
}
else{
i++;
j--;
}
}
return count;
}
Matrix Multiplication
// Print the resultant matrix after (A * B)
void multiplyMatrix(int A[SIZE][SIZE],int B[SIZE][SIZE],int R1,int C1,int R2,int C2)
{
// Write your code here
int ans[R1][C2];
for(int i=0; i<R1; i++){
for(int j=0; j<C2; j++){
ans[i][j] = 0;
for(int k=0; k<C1; k++){
ans[i][j] += A[i][k]*B[k][j];
}
}
}
for(int i=0; i<R1; i++){
for(int j=0; j<C2; j++){
printf("%d ", ans[i][j]);
}
printf("n");
}
}
Spirally traversing a matrix
void printSpiral(int a[ROWS][COLS], int r, int c) {
// Write your code here
int top=0, down=r-1, left= 0, right=c-1;
int dir=0;
while(top<=down && left<=right){
if(dir==0){
for(int i=left; i<=right; i++){
cout<<a[top][i]<<endl;
}
top++;
}
else if(dir==1){
for(int i=top; i<=down; i++){
cout<<a[i][right]<<endl;
}
right--;
}
else if(dir==2){
for(int i=right; i>=left; i--){
cout<<a[down][i]<<endl;
}
down--;
}
for(int i=down; i>=top; i--){
cout<<a[i][left]<<endl;
}
left++;
}
dir=(dir+1)%4;
}
}
Row or Column sum
long solveQuery(int N, int W, int i, char ch) {
// Write your code here
int sum=0,R=0;
if(N%W==0){
R=N/W;
}
else{
R=N/W+1;
}
if(ch=='C'&& i>W){
return 0;
}
else if(ch=='C'){
long count=0;
int ele=i;
int currentRow=0;
while(ele<=N&¤tRow<R){
count+=ele;
ele+=W;
currentRow++;
}
return count;
}
else if(ch=='R'&&i>R){
return 0;
}
else if(ch=='R'){
long count=0;
int ele=(W*(i-1))+1;
int currentCol=0;
while(ele<=N&¤tCol<W){
count+=ele;
ele++;
currentCol++;
}
return count;
}
}
Sum of Range
int sumOfRange(int min, int max){
// Write your code here
int sum=0;
for(int i=min ; i<=max ; i++)
{
sum += i;
}
return sum;
}
Verify Prime Number
bool verifyPrime(int n) {
// Write your code here
if(n<=1){
return false;
}
int t=0;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0){
t=1;
break;
}
}
if(t==0){
return true;
}
else{
return false;
}
}
Prime factors of a number
void primeFactors(int n){
// Write your code here
int i=2;
while(n > 1)
{
while(n%i == 0)
{
cout<<i<<endl;
n = n/i;
}
i++;
}
}
Greatest Common divisor of two integers
int gcd(int i, int j){
// Write your code here
int min;
if(i<j){
min=i;
}
else{
min=j;
}
int ans=1;
for(int k=2;k<=min;k++){
if(i%k==0 && j%k==0){
ans=k;
}}
return ans;
}
Binary To Decimal Conversion
int binaryToDecimal(string binary) {
// Write your code here
int n = binary.length();
int dec;
int k=0;
for(int i=n-1 ; i>=0 ; i--)
{
dec += (binary[i]-'0')*pow(2,k);
k++;
}
return dec;
}
Functions : Addition of numbers
// write the overloaded sum() functions for 2, 3 and 4 arguments
int sum(int a,int b){
return a+b;
}
int sum(int a,int b,int c){
return a+b+c;
}
int sum(int a,int b,int c,int d){
return a+b+c+d;
}
Functions : Display of Strings
void display(string a)
{
cout<<a<<endl;
}
void display(string a , string b)
{
cout<<a<<'-'<<b<<endl;
}
Cut the sticks
#include <limits.h>
int* cutSticks(int lengths_size, int *lengths, int *result_size) {
int* result = (int*) malloc(lengths_size * sizeof(int));
int smallest;
int i, j;
* result_size = 0;
while (1) {
smallest = INT_MAX;
for (i = 0; i < lengths_size; i++) {
if (lengths[i] < smallest && lengths[i] > 0) {
smallest = lengths[i];
}
}
if (smallest == INT_MAX) {
break;
}
result[*result_size] = 0;
for (i = 0; i < lengths_size; i++) {
if (lengths[i] >= smallest) {
lengths[i] -= smallest;
result[*result_size]++;
}
}
(*result_size)++;
}
return result;
}
Merge two Arrays
int* mergeArrays(int a[], int b[], int asize, int bsize) {
int* result = new int[asize + bsize];
int i = 0, j = 0, k = 0;
while (i < asize && j < bsize) {
if (a[i] < b[j]) {
result[k++] = a[i++];
} else {
result[k++] = b[j++];
}
}
while (i < asize) {
result[k++] = a[i++];
}
while (j < bsize) {
result[k++] = b[j++];
}
return result;
}
Fibonacci sequence using recursion
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int fib(int n)
{
if(n==1)
{
return 0;
}
if(n==2)
{
return 1;
}
return fib(n-1)+fib(n-2);
}
int main()
{
int n;
cin>>n;
for(int i=n ; i>0 ; i--)
{
cout<<fib(i)<<endl;
}
}
Prime factors using recursion
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
void P_fact(int n , int i)
{
if(n<=1)
{
return;
}
else if(n%i == 0)
{
cout<<i<<endl;
P_fact(n/i,i);
}
else{
P_fact(n,i+1);
}
}
int main()
{
int n;
cin>>n;
P_fact(n,2);
return 0;
}
Find all permutations of a string
void helper(string str, vector<string> &permutations , int ind)
{
if(ind == str.length())
{
permutations.push_back(str);
return;
}
for(int j=ind ; j<str.length() ; j++)
{
swap(str[j],str[ind]);
helper(str,permutations,ind+1);
swap(str[j],str[ind]);
}
}
void allPermutations(string str, vector<string> &permutations){
helper(str,permutations,0);
}
Calculate amount using compound interest
#include<iostream>
#include<cstdio>
#include<math.h>
#include<iomanip>
#include<bits/stdc++.h>
using namespace std;
class calculate{
float a,b,c,d,e;
public:
void cal(){
cin>>a>>b>>c;
d=a*(pow((1+b/100),c));
}
void print(){
cout<<fixed<<setprecision(1)<<d<<"n";
}
};
int main(){
calculate c;
c.cal();
c.print();
return 0;
}
Class - Rectangle
class Rectangle
{
// Write your code here
public:
int x;
int y;
int width;
int height;
Rectangle(int a, int b, int c, int d){
x=a;
y=b;
width=c;
height=d;
}
int getHeight(){
return height;
}
int getWidth(){
return width;
}
int getX(){
return x;
}
int getY(){
return y;
}
string toString(){
char s[100];
sprintf(s, "Rectangle[x=%d,y=%d,width=%d,height=%d]", x,y,width,height);
return s;
}
};
Class - TimeSpan
class TimeSpan {
private:
int _hours;
int _minutes;
public:
TimeSpan(int hours, int minutes) {
_hours = hours;
_minutes = minutes;
}
int getHours() {
return _hours;
}
int getMinutes() {
return _minutes;
}
void add(int hours, int minutes) {
_hours += hours;
_minutes += minutes;
if (_minutes >= 60) {
_hours += 1;
_minutes -= 60;
}
}
void add(TimeSpan tp) {
add(tp.getHours(), tp.getMinutes());
}
double getTotalHours() {
return _hours + _minutes / 60.0;
}
string toString() {
return to_string(_hours) + " Hours, " + to_string(_minutes) + " Minutes";
}
};
Class - Date
class Date
{
int month,day;
public:
Date(int a,int b){
month=a;
day=b;
}
int daysInMonth(){
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
return 31;
}
else if(month==4 || month==6 || month==9){
return 30;
}
else{
return 28;
}
}
int getDay(){
return day;
}
int getMonth(){
if(month>12){
month=1;
}
return month;
}
void nextDay(){
if((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && (day==31)){
month++;
day=1;
}
else if((month==4 || month==6 || month==9) && day==30){
day=1;
month++;
}
else if(month==2 && day==28){
day=1;
month++;
}
else if(month==12 && day==31){
day=1;
month=1;
}
else{
day++;
}
}
string toString(){
return to_string(month)+"/"+to_string(day);
}
int absoluteDay(){
int c=0;
for(int i=1;i<month;i++){
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
c=c+31;
}
else if(i==4||i==6||i==9||i==11){
c=c+30;
}
else{
c=c+28;
}
}
c=c+day;
return c;
}
};
Class - Circle
class Circle
{
float radius;
public:
Circle(float r){
radius=r;
}
float area(){
return 3.14159*radius*radius;
}
float circumference(){
return 2*3.14159*radius;
}
float getRadius(){
return radius;
}
string toString(){
int point=radius*10;
int x=radius;
return "Circle{radius="+to_string(x)+"."+to_string(point%10)+"}";
}
};
Class - SafeArray
class SafeArray{
//Complete the class definition
int size,index,value;
int a[20];
public:
SafeArray(int n){
size=n;
}
void putElement(int index,int value){
if(index>0 && index
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team