NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Remove Duplicates from a sorted doubly linked list Java



class Result {

static Node removeDupsDLL(Node head) {

// Write your code here

if(head==null || head.next==null){

return head;

}

Node i=head;

Node j=head.next;

while(j!=null){

if(i.data==j.data){

j=j.next;

}

else{

i.next=j;

j.prev=i;

i=i.next;

j=j.next;

}

}

i.next=null;

return head;

}

}

------------------------------------------------------------------------------

Matrix Multiplication Java



class Result {

// Print the resultant matrix after (A * B)

static void multiplyMatrix(int A[][],int B[][], int R1, int C1, int R2, int C2) {

// Write your code here

int res[][]=new int[R1][C2];

for(int i=0; i<R1;i++){

for(int j=0; j<C2;j++){

for(int k=0; k<C1;k++){

res[i][j]+=A[i][k]*B[k][j];

}

}

}

for(int i=0; i<R1;i++){

for(int j=0; j<C2;j++){

System.out.print(res[i][j]+" ");

}

System.out.println();

}

}

}

------------------------------------------------------------------------------

String Odd Pairs Java



import static java.lang.Character.isDigit;

class Result {

// Return true if the string contains a pair of adjacent odd numbers in it

static boolean check(ArrayList<Integer> arr){

if(arr.size()<2){

return false;

}

for(int i=0; i<arr.size(); i++){

for(int j=i+1; j<arr.size();j++){

if(arr.get(i)%2!=0 && arr.get(j)%2!=0)

return true;

}

}

return false;

}

static boolean oddPairExists(String str) {

// Write your code here

str+=" ";

ArrayList<Integer> arr= new ArrayList<>();

for(int i=0; i<str.length(); i++){

char c=str.charAt(i);

if(isDigit(c)){

arr.add(Character.getNumericValue(c));

}

else{

if(check(arr)){

return true;

}

arr.clear();

}

}

return false;

}

}

------------------------------------------------------------------------------

Cover N units distance by jumping Java



class Result {

static int minDistance(int num) {

// Write your code here

int count =0;

int rsb= num & -num;

while(rsb!=0){

rsb=rsb>>1;

count++;

}

return (int)Math.pow(2,count-1);

}

}

------------------------------------------------------------------------------

Palindrome Sentence Java



class Result {

// Return true if the given sentence is a palindrome, else return false

static boolean check(char c){

if(c>=65 && c<=90 || c>=97 && c<=122 || c>=48 && c<=57){

return true;

}

return false;

}

static boolean isPalindrome(String sentence) {

// Write your code here

int l=0;

int r=sentence.length()-1;

while(l<=r){

char lc=sentence.charAt(l);

char rc=sentence.charAt(r);

if(check(lc) && check(rc)){

if(Character.toLowerCase(lc) == Character.toLowerCase(rc)){

l++;

r--;

}

else{

return false;

}

}

else if(!check(lc)){

l++;

}

else if(!check(rc)){

r--;

}

}

return true;

}

}

------------------------------------------------------------------------------

Same Bit Positions Java



class Result {

static int sameBits(int n, int arr[]) {

// Write your code here

int one=arr[0];

int zero=arr[0];

int x=arr.length;

for(int i=0; i<x; i++){

one=one & arr[i];

zero=zero | arr[i];

}

int y= one ^ zero;

int count=0;

while(y!=0){

y= y& y-1;

count++;

}

return 32-count;

}

}

------------------------------------------------------------------------------

Saving the Earth with Binary Fever Java



class Result {

// Return the minimum number of jumps

static int getMinJumps(long n) {

// Write your code here

int count =0;

while(n!=0){

n = n & n-1;

count++;

}

return count;

}

}

------------------------------------------------------------------------------

Prank With Wallpaper Image Java



class Result {

// Do not print anything, the output will be printed automatically in the backend.

// You just need to modify the given matrix by rotating it.

static void rotateImage(int arr[][], int N) {

// Write your code here

// taking transpose of a matrix

for(int i=0; i<N; i++){

for(int j=i; j<N; j++){

int temp=arr[i][j];

arr[i][j]=arr[j][i];

arr[j][i]=temp;

}

}

// revrsing columns

for(int i=0; i<N; i++){

int li=0;

int ri=N-1;

while(li<=ri){

int temp=arr[li][i];

arr[li][i]=arr[ri][i];

arr[ri][i]=temp;

li++;

ri--;

}

}

}

}

------------------------------------------------------------------------------

Compatibility Test Java



class Result {

static int areCompatible(String aditi_str, String rohan_str) {

// Write your code here

int a=aditi_str.length();

int r=rohan_str.length();

if(r>a)

return 0;

return rohan_str.equals(aditi_str.substring(a-r,a))?1:0;

}

}

------------------------------------------------------------------------------

Toggle bits in the given range Java



class Result {

static int toggleRangeBits(int num, int l, int r)

{

int x=(int)Math.pow(2,r-l+1)-1;

return ~(~num^x<<l);

}

}

------------------------------------------------------------------------------

Decode Enemy Message Java



class Result {

static String reverse(String s){

String nstr="";

for (int i=0; i<s.length(); i++)

{

Character ch= s.charAt(i);

nstr= ch+nstr;

}

return nstr;

}

// Return the decoded message string

static String decodeMessage(String str) {

// Write your code here

str=str+" ";

String res=" ";

String wrd="";

for(int i=0; i<str.length(); i++){

if(str.charAt(i)!=' ')

wrd=wrd+str.charAt(i);

if(str.charAt(i)==' '){

res=res+" "+reverse(wrd);

wrd="";

}

}

return res.trim();

}

}

------------------------------------------------------------------------------

Find the sum of row majors Java



class Result {

/*

* Complete the 'findTheSum' function below.

* @params

* mat -> matrix, on which operations needs to be performed

* n -> number of rows in matrix

* m -> number of cols in matrix

*

* @return

* An integer, denoting the sum after performing all the operations

*/

public static int findTheSum(int mat[][], int n, int m) {

// Write your code here

for(int i=0; i<n; i++){

Arrays.sort(mat[i]);

}

int res=0;

for(int j=m-1; j>=0; j--){

int max=Integer.MIN_VALUE;

for(int i=0; i<n; i++){

max=Math.max(max, mat[i][j]);

}

res+=max;

}

return res;

}

}

------------------------------------------------------------------------------

Same Species OR Not Java



class Result

{

// Return true if the creatures belong to the same species, else return false

static boolean belongToSameSpecies(String DNA_str1, String DNA_str2)

{

int k = 0, count = 0;

char ch1 = 0, ch2 = 0;

DNA_str2 = DNA_str2 + " ";

for(int i=0; i<DNA_str1.length(); i++)

{

ch1 = DNA_str1.charAt(i);

ch2 = DNA_str2.charAt(k);

if (ch1 == ch2)

{

k++;

count++;

}

}

if (count == DNA_str2.length()-1)

return true;

else

return false;

}

}

------------------------------------------------------------------------------

Swap all odd and even bits Java



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&&currentRow<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&&currentCol<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++;

}

}

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&&currentRow<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&&currentCol<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++;

}

}



     
 
what is notes.io
 

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

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.