Notes
![]() ![]() Notes - notes.io |
2. Add one to each digit of a number
3.Fibonacci sequence
4.Leaders in an array
5.Use Minimum Square Tiles
6.Find Missing Element
7.Left rotate an array
8.Reverse an array in groups
9.Verify Prime Number
10.Buy maximum toys possible
11.Distribute in Circle
12.Reduce the array to zero
13.Make a group for competition
14.GCD of two numbers
15.Average people per city
16.Cut the chocolate smartly for maximum pi...
17.Reach a single digit
18.Fill the gaps
19.Multiple Sum
20.How many duplicates in array
21.Find the special index of array
22.Count the total reward points
23.Make Array Positive
24.Grab the recursive gifts
25.Count the square numbers
26.Pascal's Triangle
27.Find the winner in elections
28.Scoring based on difficulty level
29.Array Sum and Pair Product Puzzle
30.Turning point in stock prices
31.Compare two arrays
32.Continuous Subtraction Game
33.Action Game
34.Same Digits
35.Largest number possible with digits
36.Make pairs in the party
37.Form a new number
38.Fibonacci sequence using recursion
39.Factorial using recursion
40.Check the numbers for power of 2
41.Find the rightmost set bit of a number
42.Count the set bits of a number
43.Rotate a 2-D array by 90 degrees
44.Collect coins in snake game
45.Escape The Bombs
46.Bit Difference
47.Toggle the number except kth bit
48.Counter Diagonal Traversal
49.Check if strings are rotations or not
50.Compress a string
51.Expand a string
52.Vowel Substrings
53.Keyword Count
54.Reverse the words of a string
55.Reverse a linked list
56.Find the middle node of the list
57.Find the merge point of two lists
58.Rotate a linked List
59.Check List for Palindrome
60.Add Two Numbers Represented by Lists
61.Find the loop in Linked list
62.Move the Smallest and largest to head an...
63.Swap Two Nodes of Doubly Linked List
64.Rearrange the Even-Odd Nodes of Doubly L...
65.Remove Duplicates from a sorted doubly l...
66.Data Collection By Telecom Company
67.Code Compiler Output
68.Intelligent Code Editor
69.Evaluate the postfix expression using St...
70.Count the number of nodes in a circular ...
71.Given list is circular or not
72.Matrix Multiplication
73.String Odd Pairs
74.Cover N units distance by jumping
75.Palindrome Sentence
76.Same Bit Positions
77.Saving the Earth with Binary Fever
78.Prank With Wallpaper Image
79.Compatibility Test
80.Toggle bits in the given range
81.Decode Enemy Message
82.Same Species OR Not
83.Swap all odd and even bits
1. Reverse a 4 digit number
import java.util.Scanner;
// Other imports go here
// Do NOT change the class name
class Main{
public static void main(String[] args)
{
// Write your code here
int reminder , reverse=0;
Scanner s=new Scanner(System.in);
int n=s.nextInt();
while(n!=0){
reminder=n%10;
reverse=reverse*10+reminder;
n=n/10;
}
System.out.println(reverse);
}}
2.Add one to each digit of a number
import java.util.Scanner;
// Other imports go here
// Do NOT change the class name
class Main{
public static void main(String[] args)
{
Scanner scan =new Scanner (System.in);
int t,n;
int r,s,c;
r=c=s=0;
t=scan.nextInt();
for(int j=1;j<=t;j++)
{
n=scan.nextInt();
while(n!=0)
{
r=(n%10)+1;
s=s+(int)(r*Math.pow(10,c));
n=n/10;
c++;
}
System.out.println(s);
c=s=r=0;
}
}
}
3.Fibonacci sequence
static void fib_sequence(int n) {
// Write your code here
long ans=0;
long a=0;
long b=1;
if(n==1){
System.out.println(0);
}
if(n>=2){
System.out.println(a);
System.out.println(b);
}
while((n-2)>0){
ans=a+b;
a=b;
b=ans;
n=n-1;
System.out.println(b);
}
}
4.Leaders in an array
class Result{
// Return the array that contains leaders
static int[] findTheLeaders(int a[], int n){
int i,j;
int c=0;
int arr[]=new int[n];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
break;
}
if(j==n)
arr[c]=a[i];
c++;
}
return arr;
}
}
5.Use Minimum Square Tiles
class Result {
static long gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
} static long minTiles(int m, int n) {
long gcd=gcd(m,n);
return ((m/gcd)*(n/gcd));
} }
6.Find Missing Element
class Result
{
static int getMissingElement(int[] A, int[] B)
{
int m,n,s1,s2;
s1=s2=0;
int i,j;
m=A.length;
n=B.length;
for(i=0;i<m;i++)
{
s1=s1+A[i];
}
for(j=0;j<n;j++)
{
s2=s2+B[j];
}
return (s1-s2);
}
}
7.Left rotate an array
class Result {
static void reverse(int arr[], int s , int e)
{
while(s < e)
{
int temp = arr[s];
arr[s] = arr[e];
arr[e] = temp;
s++;
e--;
}
}
static void rotateArray(int[] arr, int R) {
// Write your code here
int k = R%arr.length;
reverse(arr , 0 , arr.length-1);
reverse(arr , 0 , arr.length-k-1);
reverse(arr , arr.length-k,arr.length-1);
}
}
8.Reverse an array in groups
class Result {
static void reverse(int a[], int start, int end){
int i=start;
int j=end;
while(i<j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;
}
}
static int[] reverseSubarray(int a[], int n, int k) {
// Write your code here
if(k==1){
return a;
}
for(int i=0; i<n; i+=k){
int l=i;
int r=Math.min(i+k-1,n-1);
reverse(a,l,r);
}
return(a);
}
}
9.Verify Prime Number
static boolean verifyPrime(int n) {
// Write your code here
Boolean bool = true;
if(n <= 1)return false;
for(int i = 2; (i*i)<=n; i++)
{
if(n % i == 0)
{
bool = false;
break;
}
}
return bool;
}
10.Buy maximum toys possible
import java.util.*;
class Result {
static int maxToys(int cost[], int n, int X) {
int count = 0;
Arrays.sort(cost);
int i = 0;
while(i < n && cost[i] <= X)
{
count++;
X -= cost[i];
i++;
}
return count;
}
}
11.Distribute in Circle
class Result {
static long distribute(long n, long l, long p) {
// Complete the function
if(n<=l-p+1) return n+p-1;
n = n-(l-p+1);
return (n%l == 0) ? l : n%l;
}
}
12.Reduce the array to zero
class Result {
static int min(ArrayList<Integer> arr)
{
int min = Integer.MAX_VALUE;
for(int i = 0; i<arr.size(); i++)
{
if(arr.get(i) < min && arr.get(i) != 0)
{
min = arr.get(i);
}
}
return min;
}
static ArrayList<Integer> reduceArray(ArrayList<Integer> arr) {
// Write your code here
ArrayList<Integer> ans = new ArrayList<Integer>();
int count = -1;
while(count != 0){
count = 0;
int min = min(arr);
for(int i = 0; i<arr.size(); i++)
{
if(arr.get(i) == 0){
continue;
}
else{
int val = arr.get(i) - min;
arr.set(i , val);
count++;
}
}
if(count != 0) ans.add(count);
}
return ans;
}
}
13.Make a group for competition
class Result {
static long totalWays(int rollNo[], int N) {
// Write your code here
long answer =pow(2,N) -1;
return answer;
}
static long pow(int m , int n){
long result=1;
for(int i=0; i<n;i++){
result *=2;
}
return result;
}
}
14.GCD of two numbers
class Result{
static long GCD(long a, long b){
// Complete the function
while(a!=b){
if(a==0)
return b;
else if(b==0)
return a;
if(a<b)
b=b%a;
else
a=a%b;
}
return a;
}
}
15.Average people per city
static int findAverage(int n, int m, int[][] arr) {
// Write your code here
long sum = 0;
for(int i = 0; i<m; i++)
{
int a = arr[i][0];
int b = arr[i][1];
int k = arr[i][2];
sum += ((b - a + 1) * k);
}
return (int)(sum/n);
}
16.Cut the chocolate smartly for maximum pieces
class Result {
static long maxPieces(long k) {
// Write your code here
return (long )(k*k/4);
}
}
17.Reach a single digit
// Return the total number of steps to reach a single digit
static int totalSteps(long n) {
// Write your code here
int i=0;
while(n>=10)
{
n=SumOfDigit(n);
i++;
}
return i;
}
static long SumOfDigit(long n){
long sum=0;
while(n>0){
long digit=n%10;
sum+=digit;
n=n/10;
}
return sum;
}
18.Fill the gaps
class Result {
static int fillTheGaps(int[] arr) {
// Write your code here
int count=0;
Arrays.sort(arr);
for(int i=1; i<arr.length; i++){
if(arr[i]-arr[i-1]-1>0)
{
count+=arr[i]-arr[i-1]-1;
}
}
return count;
}
}
19.Multiple Sum
class Result {
static long multipleSum(long x) {
// Write your code here
long sum=0;
long a=(x/3);
long b=(x/5);
long c=(x/15);
sum=(3*((a*(a+1))/2))+(5*((b*(b+1))/2))-(15*((c*(c+1))/2));
return sum;
}
}
20.How many duplicates in array
class Result {
public static int howManyDuplicates(int n, int arr[]) {
int count=0;
boolean flag=false;
Arrays.sort(arr);
int element=arr[0];
for(int i=1;i<arr.length;i++){
if(arr[i]==element && flag==false){
flag=true;
count=count+1;
}
else if(arr[i]>element) {
flag=false;
element=arr[i];
}
}
return count;
}
}
21.Find the special index of array
class Result {
static int specialIndex(int arr[], int n) {
int suml=0;
int sumr=0;
for(int i=1;i<n;i++){
sumr=sumr+arr[i];
}
if(sumr==0){
return 0;
}
for(int i=1;i<n;i++){
suml=suml+arr[i-1];
sumr=sumr-arr[i];
if(suml==sumr){
return i;
}
}
return -1;
}
}
22.Count the total reward points
int countPoints(int n) {
int sum=0;
while(n>0){
int rem=n%10;
n=n/10;
if(rem==8){
sum=sum+2;
}
else if(rem==0||rem==4||rem==6||rem==9){
sum=sum+1;
}
}
return sum;
}
23.Make Array Positive
class Result {
static int minOperations(int arr[], int n) {
// Write your code here
int count=0;
for(int i=0; i<n; i++){
if(arr[i]<0 && count%2==0 || arr[i]>0 && count%2!=0){
count++;
}
}
return count;
}
}
24.Grab the recursive gifts
class Result {
public static int enjoyOffer(int x, int y, int z) {
// Write your code here
x=x/y;
int k=x;
if(z>x){
return x;
}else{
while(k>=z){
k=k-z;
x++;
k++;
}
}
return x;
}
}
25.Count the square numbers
static List<Integer> countSquares(int n, int[][] arr) {
// Write your code here
List <Integer> li=new ArrayList<>();
for(int i=0; i<n; i++){
int x=arr[i][0];
int y=arr[i][1];
int sqrtx=(int)Math.ceil(Math.sqrt(x));
int sqrty=(int)Math.floor(Math.sqrt(y));
li.add(sqrty-sqrtx+1);
}
return li;
}
26.Pascal's Triangle
void pascalTriangle(int k) {
// Complete this function
for(int i=0; i<=k; i++)
{
int a=1;
for(int j=1; j<=i; j++){
cout<<a;
a=(a*(i-j))/j;
if(i!=j)
cout<<"*";
}
cout<<endl;
}
}
27.Find the winner in elections
class Result {
static int findWinner(int[] arr, int n) {
// Write Your Code here
int count=1;
int maxCount=0;
for(int i=0; i<n; i++){
if(arr[maxCount]==arr[i]){
count ++;
}else{
count--;
}
if(count==0){
maxCount=i;
count=1;
}
}
int k=0;
for(int i=0; i<n; i++){
if(arr[maxCount]==arr[i]){
k++;
}
}if(k<=n/2){
return -1;
}
return arr[maxCount];
}
}
28.Scoring based on difficulty level
class Result {
static boolean checkScoring(int arr[], int n) {
// Write your code here
boolean flag=false;
int sum=0;
for(int i=0; i<n; i++ ){
if(arr[i]>=sum){
sum=sum+arr[i];
flag=true;
}else{
flag=false;
return flag;
}
}
return flag;
}
}
29.Array Sum and Pair Product Puzzle
class Result {
static boolean pairExists(int arr[], int n) {
// Write your code here
boolean flag=false;
Arrays.sort(arr);
int sum=0;
if(n>1)
{
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
int sum2=sum*2;
int max=arr[n-1]*arr[n-2];
int product=arr[0]*arr[1];
if(max>sum2 || product>sum2){
flag=true;
}
}
return flag;
}
}
30.Turning point in stock prices
class Result {
static int turningPoint(int arr[], int n) {
// Write your code here
if(arr[0]>arr[1]){
for(int i=0; i<n-1; i++){
if(arr[i]<arr[i+1]){
return i+1;
}
}
}
if(arr[0]<arr[1]){
for(int i=0; i<n-1; i++){
if(arr[i]>arr[i+1]){
return i+1;
}
}
}
return -1;
}
}
31.Compare two arrays
long compareSecondArray(vector<int> A, vector<int> B) {
int x=B.size();
int y=A.size();
sort(A.begin(),A.end());
sort(B.begin(),B.end());
int step=0;
long sum=0;
for(int i=0;i<x;i++){
for(int j=step;j<y;j++){
if(B[i]<A[j]){
break;
}
else{
step++;
}
}
sum=sum+step;
}
return sum;
}
32.Continuous Subtraction Game
long subtractionGame(vector<long> arr) {
// Write your code here
int x=arr.size();
while(x>0){
for(int i=0; i<x-1; i++){
arr[i]=arr[i]-arr[i+1];
}
x--;
}
return arr[0];
}
33.Action Game
import java.util.*;
class Result {
static long maxPoints(int arr[], int n, int k) {
// Write your code here
long sum=0;
long res=0;
for(int i=0; i<n; i++){
if(arr[i]<=k){
sum=sum+(k-arr[i]);
}
if(arr[i]>k || i==n-1){
res=Math.max(sum,res);
sum=0;
}
}
return res;
}
}
34.Same Digits
string nextGreator(string num) {
// Write your code here
int n=num.length();
if(n==1){
return to_string(-1);
}
int i=0;
for(i=n-1;i>0;i--){
if(num[i]>num[i-1]){
break;
}
}
if(i!=0){
for(int j=n-1;j>=i;j--){
if(num[i-1]<num[j]){
swap(num[i-1],num[j]);
break;
}
}
sort(num.begin()+i,num.end());
}
else{
return to_string(-1);
}
return num;
}
35.Largest number possible with digits
int formLargestNumber(int s, int d) {
// Write your code here
int x=0;
if(s==0){
if(d==1){
return 0;
}
else{
return -1;
}
}
if(s>9*d){
return -1;
}
while(d>=0){
if(s>=9){
x=x+(9*pow(10,d-1));
s=s-9;
d--;
}
else{
x=x+(s*pow(10,d-1));
s=s-s;
d--;
}}
return x;
}
36.Make pairs in the party
class Result {
static int minResult(int[] boys, int[] girls) {
// Write your code here
Arrays.sort(girls);
Arrays.sort(boys);
int sum=0;
int len=boys.length;
for(int i=0; i<boys.length; i++ ){
sum+=(boys[i]*girls[len-i-1]);
}
return sum;
}
}
37.Form a new number
int evenDigits(int n,int i=0,int even=0)
{
if(n==0){
return even;
}
if((n%10)%2==0){
even+=(n%10)*pow(10,i);
i++;
}
return evenDigits(n/10,i,even);
}
38.Fibonacci sequence using recursion
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int fib(int x){
if(x==1 || x==0)
return x;
return (fib(x-2)+fib(x-1));
}
int main()
{
int n;
cin>>n;
for(int i=n-1;i>=0;i--)
cout<<fib(i)<<endl;
return 0;
}
39.Factorial using recursion
int factorial(int n) {
// Write your code here
if(n<=1)
return 1;
return n*factorial(n-1);
}
40.Check the numbers for power of 2
public static List<Integer> findPowers(List<Integer> nums) {
// Write your code here
ArrayList<Integer> arr=new ArrayList<>();
for(int i=0;i<nums.size();i++){
int count=0;
int number=nums.get(i);
while(number>0){
if((number & 1) == 1){
count++;
}
number=number>>1;
}
if(count == 1){
arr.add(1);
}
else{
arr.add(0);
}
}
return arr;
}
}
41.Find the rightmost set bit of a number
int rightMostSetBit(int num){
// Write your code here
if(num==0){
return -1;
}
int count=0;
for(int i=0; i<32; i++){
int a=num&1;
if(a==1){
return count;
}else{
num=num>>1;
count++;
}
}
return -1;
}
42.Count the set bits of a number
int countBits(int num)
{
int count =0;
while(num>0){
int a=(num&1);
if(a==1){
count++;
}num=num>>1;
}
return count;
}
43.Rotate a 2-D array by 90 degrees
import java.util.Scanner;
// Other imports go here, Do NOT change the class name
class Main
{
static void display(int[][] arr,int n)
{
for(int i=0;i<n;i++)
{
for(int j=n-1;j>=0;j--)
{
System.out.print(arr[j][i]);
if(j!=0)
{
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String[] args)
{
// Write your code here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int k=0;k<t;k++)
{
int n = sc.nextInt();
int[][] arr = new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j] = sc.nextInt();
}
}
display(arr,n);
System.out.println();
}
}
}
44.Collect coins in snake game
class Result
{
// Print the number of coins collected from each cell, in the order of snake's movement.
static void collectCoins(int board[][], int m, int n) {
// Write your code here
int i,k,l;
k=0;
l=0;
while(k<n && l<m){
if(k<n){
for(i=l;i<m;i++){//m=rows
System.out.print(board[i][k]+" ");
}
k++;
}
if(l<m){
for(i=k;i<n;i++){//n=cols
System.out.print(board[m-1][i]+" ");
}
m--;
}
if(k<n){
for(i=m-1;i>=l;i--){
System.out.print(board[i][n-1]+" ");
}
n--;
}
if(l<m){
for(i=n-1;i>=k;i--){
System.out.print(board[l][i]+" ");
}
l++;
}
}
}
}
45.Escape The Bombs
import java.util.*;
class Result {
static int minScore(int mat[][], int m, int n) {
// Write your code here
HashMap<Integer,Integer> map1=new HashMap<>();
HashMap<Integer,Integer> map2=new HashMap<>();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(mat[i][j] == 0){
map1.put(i,1);
map2.put(j,1);
}
}
}
int count=0;
for(int i=0;i<m;i++){
if(map1.get(i) == null){
for(int j=0;j<n;j++){
if(map2.get(j) == null){
count+=mat[i][j];
}
}
}
}
return count;
}
}
46.Bit Difference
class Result
{
static int bitsDifferent(int num1, int num2){
int xor=num1^num2;
int count=0;
while(xor != 0){
if((xor & 1)==1){
count++;
}
xor=xor>>>1;
}
return count;
}
}
47.Toggle the number except kth bit
class Result{
static int toggleExceptKthBit(int n,int k){
// Write your code here
int num=1;
num=num<<k;
if((n & num) == 0){
n= ~(n | num);
return n;
}
else{
return (~n | num);
}
}
}
48.Counter Diagonal Traversal
class Result {
static void counterDiagonal(int mat[][], int n) {
// Write your code here
int martixFirstInd=0;
int matrixSecondInd=0;
int indForSecondTraverse=0;
int traverseLen=0;
while(traverseLen<(n+n-1)){
if(traverseLen<n){
int k=martixFirstInd;
int l=matrixSecondInd;
int firstTravInd=0;
while(firstTravInd<=matrixSecondInd){
System.out.print(mat[k][l]+" ");
k++;
l--;
firstTravInd++;
}
if(matrixSecondInd == n-1){
matrixSecondInd--;
martixFirstInd++;
indForSecondTraverse=matrixSecondInd;
}
matrixSecondInd++;
}
else{
int k=martixFirstInd;
int l=matrixSecondInd;
int secondTravInd=0;
while(secondTravInd<=indForSecondTraverse){
System.out.print(mat[k][l]+" ");
k++;
l--;
secondTravInd++;
}
martixFirstInd++;
indForSecondTraverse--;
}
traverseLen++;
}
}
}
49.Check if strings are rotations or not
class Result {
// return 1 for YES and 0 for NO.
static int isRotation(String str1, String str2) {
// Write your code here
if(str1.length() != str2.length()){
return 0;
}
else{
String s=str1+str1;
int index = s.indexOf(str2);
if(index == -1){
return 0;
}
return 1;
}
}
}
50.Compress a string
class Result {
static String compressString(String str) {
// Write your code here
String s1="";
for(int i=0;i<str.length();i++){
int count=1;
char c=str.charAt(i);
while(i+1<str.length() && (str.charAt(i)==str.charAt(i+1))){
count++;
i++;
}
if(count == 1){
s1=s1+c;
}
else{
s1=s1+c+count;
}
}
return s1;
}
}
51.Expand a string
class Result {
// Return the final string after expansion
static String expandString(String str) {
// Write your code here
String s="";
char c=str.charAt(0);
for(int i=0;i<str.length();i++){
if((str.charAt(i)>='a' && str.charAt(i)<='z') || (str.charAt(i)>='A' && str.charAt(i)<='Z')){
s+=str.charAt(i);
}
else{
int count=0;
while(i<str.length() && str.charAt(i)>='0' && str.charAt(i)<='9'){
count=(count*10)+Character.getNumericValue(str.charAt(i));
i++;
}
for(int j=0;j<count-1;j++){
s+=c;
}
i--;
}
if(i<str.length()){
c=str.charAt(i);
}
}
return s;
}
}
52.Vowel Substrings
class Result {
static int countVowelSubstr(String str) {
// Write your code here
int count=0;
String s="AEIOUaeiou";
int len=str.length();
for(int i=0;i<len;i++){
if(s.indexOf(str.charAt(i)) != -1){
count= (count+(len-i))%10007;
}
}
return count%10007;
}
}
53.Keyword Count
class Result {
static int keywordCount(String str, String word) {
// Write your code here
HashMap<String,Integer> map=new HashMap<>();
String s="";
for(int i=0;i<str.length();i++){
if(str.charAt(i)==' '){
if(map.get(s) == null){
map.put(s,1);
}
else{
map.put(s,map.get(s)+1);
}
while(i+1<str.length() && str.charAt(i+1)==' '){
i++;
}
s="";
}
else{
s+=str.charAt(i);
}
}
if(s!=""){
if(map.get(s)==null){
map.put(s,1);
}
else{
map.put(s,map.get(s)+1);
}
}
if(map.get(word)==null){
return 0;
}
else{
return map.get(word);
}
}
}
54.Reverse the words of a string
void revWordsString(string str) {
// Write your code here
stack<char>st;
for(int i=0;i<str.length();i++){
if(str[i]!= ' ')
st.push(str[i]);
else{
while(st.empty()==false){
cout<<st.top();
st.pop();
}
cout<<" ";
}
}
while(st.empty()==false){
cout<<st.top();
st.pop();
}
}
55. Reverse a LL:- JAVA
class Result {
// Return the new head of reversed list
static Node reverseList(Node head) {
// Write your code here
Node curr = head;
Node prev = null;
while(curr!=null)
{
Node nextNode = curr.next;
curr.next = prev;
prev = curr;
curr = nextNode;
}
head = prev;
return head;
}
}
----------------------------------------------------------------
56.Find the middle node of the list:- JAVA
static Node findMiddle(Node head) {
// Write your code here
Node fast = head;
Node slow = head;
while(fast!=null && fast.next!=null)
{
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
-------------------------------------------------------------------
57.Find merge point of two list:- JAVA
class Result {
static Node findMergePoint(Node head1, Node head2){
if(head1==null || head2==null)return null;
int size1=0;
Node curr=head1;
while(curr!=null){
curr=curr.next;
size1++;
}
int size2=0;
curr=head2;
while(curr!=null){
curr=curr.next;
size2++;
}
if(size1>size2){
int delta=size1-size2;
while(delta-->0){
head1=head1.next;
}
}
else{
int delta=size2-size1;
while(delta-->0){
head2=head2.next;
}
}
while(head1!=null && head2!=null && head1!=head2){
head1=head1.next;
head2=head2.next;
}
return head1;
}
}
--------------------------------------------------------------------
58.Rotate a linked List:-
static Node rotateList(Node head, int k) {
// Write your code here
if(head==null || k==0)
{
return head;
}
Node curr = head;
Node prev = head;
for(int i=0;i<k;i++)
{
if(curr.next==null)
{
return head;
}
prev = curr;
curr = curr.next;
}
if(prev==null || curr==null)
{
return head;
}
prev.next = null;
Node res = curr;
while(curr.next!=null)
{
curr = curr.next;
}
curr.next = head;
head = res;
return head;
}
---------------------------------------------------------------
59. Check List for Palindrome:-class Result {
static Node rev(Node hed)
{
Node pre = null;
Node forward = null;
Node cr = hed;
while(cr != null)
{
forward = cr.next;
cr.next = pre;
pre = cr;
cr = forward;
}
return pre;
}
static int checkPalindrome(Node head) {
// Write your code here
Node cur = head;
Node dummy = new Node(-1);
Node tmp = dummy;
while(cur != null)
{
tmp.next = new Node(cur.data);
tmp = tmp.next;
cur = cur.next;
}
cur = head;
Node cur1 = rev(cur);
Node prev = dummy.next;
while(cur1 != null)
{
if(cur1.data != prev.data)
{
return 0;
}
cur1 = cur1.next;
prev = prev.next;
}
return 1;
}
}
------------------------------------------------------------------
60. Add Two Numbers Represented by
class Result {
static Node addListNumbers(Node head1, Node head2) {
// Write your code here
Node ans = new Node();
Node temp = ans;
int carry =0;
while(head1 != null || head2 != null || carry != 0){
int sum = 0;
if(head1 != null){
sum += head1.data;
head1=head1.next;
}
if(head2 != null){
sum += head2.data;
head2 = head2.next;
}
sum += carry;
carry = sum/10;
Node curr = new Node(sum%10);
temp.next = curr;
temp = temp.next;
}
return ans.next;
}
}
-----------------------------------------------------
61. Find the loop in Linked list:-
class Result {
static int loopInList(Node head) {
// Write your code here
Node slow=head;
Node fast=head;
while(fast!=null && fast.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast)
break;
}
if(fast==null || fast.next==null)
return 0;
slow=head;
while(slow!=fast){
slow=slow.next;
fast=fast.next;
}
int count=1;
while(slow.next!=fast){
count++;
slow=slow.next;
}
return count;
}
}
-------------------------------------------------------------
62.Move the Smallest and largest to head and tail of list
static Node shiftSmallLarge(Node head) {
// Write your code here
if(head == null || head.next == null)
{
return head;
}
Node curr = head;
int min = head.data;
int max = head.data;
while(curr!=null)
{
if(curr.data<min)
{
min = curr.data;
}
if(curr.data>max)
{
max = curr.data;
}
curr = curr.next;
}
Node res = new Node(-1);
Node temp = res;
temp.next = new Node(min);
temp = temp.next;
curr = head;
while(curr!=null)
{
if(curr.data!=min && curr.data!=max)
{
temp.next = new Node(curr.data);
temp = temp.next;
}
curr = curr.next;
}
temp.next = new Node(max);
return res.next;
}
63.Swap Two Nodes of Doubly Linked List:- Java
Node swapNodes(Node head, int x, int y) {
// Write your code here
Node temp1 = null, temp2 = null, curr = head;
while(curr != null){
if(curr.data == x)
temp1 = curr;
if(curr.data == y)
temp2 = curr;
curr = curr.next;
}
if(temp1 != null && temp2 != null){
int temp = temp1.data;
temp1.data = temp2.data;
temp2.data = temp;
}
return head;
}
64.Rearrange the Even-Odd Nodes of Doubly Linked List
static Node rearrangeList(Node head) {
// Write your code here
Node res = new Node(-1);
Node curr = head;
Node curr1 = res;
int i = 0;
while(curr!=null)
{
if(i%2!=0)
{
curr1.next = new Node(curr.data);
curr1 =curr1.next;
}
i++;
curr = curr.next;
}
i = 0;
curr = head;
while(curr!=null)
{
if(i%2==0)
{
curr1.next = new Node(curr.data);
curr1 =curr1.next;
}
i++;
curr = curr.next;
}
return res.next;
}
=============================================
65. Data Collection By Telecom Company:-
class Result {
static int collectData(int arr[], int n) {
// Write your code here
int res[] = new int[n];
for(int i=0;i<n;i++)
{
res[i]=-1;
}
Stack<Integer> stack = new Stack<>();
for(int i=0;i<n;i++)
{
int curr = arr[i];
if(stack.isEmpty())
{
stack.push(i);
}
else
{
if(curr <= arr[stack.peek()])
{
stack.push(i);
}
else
{
while(!stack.isEmpty() && curr>arr[stack.peek()])
{
int index = stack.pop();
res[index] = curr;
}
stack.push(i);
}
}
}
int sum = 0;
for(int i=0;i<n;i++)
{
sum+=res[i];
}
return sum;
}
}
----------------------------------------------------------------
66. Code Compiler Output :-
class Result {
// Return true if the compiler output will be success, else return false
static boolean check(char brak,char[] arr,int top)
{
if(brak==')' && arr[top]=='(')
{
return true;
}
else if(brak==']' && arr[top]=='[')
{
return true;
}
else if(brak=='}' && arr[top]=='{')
{
return true;
}
else
{
return false;
}
}
static boolean compilerOutput(String code) {
// Write your code here
char[] arr = new char[code.length()];
int top = -1;
for(int i=0;i<code.length();i++)
{
if(code.charAt(i)=='(' || code.charAt(i)=='[' || code.charAt(i)=='{')
{
top++;
arr[top]=code.charAt(i);
}
else if(code.charAt(i)==')' || code.charAt(i)==']' || code.charAt(i)=='}')
{
if(top==-1)
{
return false;
}
else if(check(code.charAt(i),arr,top)==true)
{
top--;
}
else
{
return false;
}
}
}
if(top==-1)
{
return true;
}
else
{
return false;
}
}
}
----------------------------------------------------------------------
67.Intelligent Code Editor:-
class Result {
static int highlightedBrackets(String expr) {
// Write your code here
int open = 0;
int close = 0;
for(int i=0;i<expr.length();i++)
{
if(expr.charAt(i)=='{')
{
open++;
}
else if(expr.charAt(i)=='}')
{
if(open==0)
{
close++;
}
else
{
open--;
}
}
}
if((open+close)%2!=0)
{
return -1;
}
return (int) Math.ceil(open/2.0) + (int) Math.ceil(close/2.0);
}
}
-------------------------------------------------------------------
68.Evaluate the postfix expression using Stack:-
static int evalPostfix(CQStack s, String exp)
{
for(int i=0;i<exp.length();i++){
char ch = exp.charAt(i);
if(Character.isDigit(ch)){
s.push(ch-'0');
}
else{
int val1 = s.pop();
int val2 = s.pop();
switch(ch){
case '+' :
s.push(val2+val1);
break;
case '-' :
s.push(val2-val1);
break;
case '*':
s.push(val2*val1);
break;
case '/':
s.push(val2/val1);
break;
case '^':
s.push((int)Math.pow(val2,val1));
break;
}
}
}
return s.pop();
}
====================================
69.Count the number of nodes in a circular linked list :- C++
int countNodes(struct Node* head) {
Node* curr = head;
int count = 0;
while(curr!=NULL)
{
count++;
if(curr->next==head)
{
break;
}
curr = curr->next;
}
return count;
}
------------------------------------------------------------------
70. given list is circular or not:- C++
static int isCircular(Node head) {
// Write your code here
if(head==null)
{
return 1;
}
if(head.next==head)
{
return 1;
}
if(head.next==null)
{
return 0;
}
Node fast = head;
Node slow = head;
while(fast!=null && fast.next!=null)
{
slow = slow.next;
fast = fast.next.next;
if(slow==fast)
{
if(slow==head)
{
return 1;
}
else
{
return 0;
}
}
}
return 0;
}
================================= ST-2===========================================
71.Matrix Multiplication:-
class Result {
static void multiplyMatrix(int A[][],int B[][], int R1, int C1, int R2, int C2) {
// Write your code here
int sum = 0;
for(int i=0;i<R1;i++)
{
for(int j=0;j<C2;j++)
{
sum = 0;
for(int k=0;k<C1;k++)
{
sum += A[i][k]*B[k][j];
}
System.out.print(sum+" ");
}
System.out.println();
}
}
}
---------------------------------------------------------------
72.String Odd Pairs:-
class Result {
static boolean oddPairExists(String str) {
// Write your code here
int n;
int l = str.length();
int count = 0;
for(int i=0;i<l;i++)
{
if(str.charAt(i)>='0' && str.charAt(i)<='9')
{
while((i<l) && str.charAt(i)>='0' && str.charAt(i)<='9')
{
n = str.charAt(i)-'0';
if(n%2!=0)
{
count++;
}
i++;
}
}
if(count>=2)
{
return true;
}
count=0;
}
return false;
}
}
---------------------------------------------------------------------- 73.Cover N units distance by jumping:-
class Result {
static int minDistance(int N) {
// Write your code here
if(N==0)
{
return 0;
}
int count = 1;
while(N>0)
{
if((N&1)==1)
{
return count;
}
count *= 2;
N = N>>1;
}
return count;
}
}
-----------------------------------------------------------
74.Palindrome Sentence:
class Result {
// Return true if the given sentence is a palindrome, else return false
static boolean isPalindrome(String sentence) {
// Write your code here
String str1 = "";
char ch;
for(int i=0;i<sentence.length();i++)
{
ch = sentence.charAt(i);
if( (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
{
str1 = str1 + ch;
}
}
String str2 = "";
for(int i=sentence.length()-1; i>=0 ;i--)
{
ch = sentence.charAt(i);
if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
{
str2 += ch;
}
}
if(str1.equalsIgnoreCase(str2))
{
return true;
}
else
{
return false;
}
}
}
--------------------------------------------------------------------
75.Same Bit Positions :-
class Result {
static int sameBits(int n, int arr[]) {
// Write your code here
int bit1 = arr[0];
int bit0 = arr[0];
for (int i = 1; i < n; i++) {
bit1 = bit1 & arr[i];
bit0 = bit0 | arr[i];
}
int XOR = bit1 ^ bit0;
int count = 0;
while (XOR != 0) {
XOR = XOR & (XOR-1);
count++;
}
int result = 32-count;
return result;
}
}
--------------------------------------------------------------
76.Saving the Earth with Binary Fever:-
class Result {
// Return the minimum number of jumps
static int getMinJumps(long n) {
// Write your code here
int count = 0;
while(n>0)
{
if((n&1)==1)
{
count++;
}
n = n>>1;
}
return count;
}
}
------------------------------------------------------------------
77. Prank With Wallpaper Image:-
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 image[][], int N) {
// Write your code here
for(int i=0;i<N;i++)
{
for(int j=i;j<N;j++)
{
int temp = image[i][j];
image[i][j] = image[j][i];
image[j][i] = temp;
}
}
for(int i=0;i<N/2;i++)
{
for(int j=0;j<N;j++)
{
int temp = image[i][j];
image[i][j] = image[N-1-i][j];
image[N-1-i][j] = temp;
}
}
}
}
-------------------------------------------------------
![]() |
Notes is a web-based application for online 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 14 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