0% found this document useful (0 votes)
3 views

Cat2.1

The document contains multiple Java programs that implement various algorithms and data structures. Key functionalities include checking for binary palindromes, multiplying integers using Booth's algorithm, finding the GCD using Euclid's algorithm, and implementing Karatsuba multiplication. Additional programs cover topics such as finding majority elements, leaders in arrays, maximum product subarrays, and rotating arrays using block swap techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Cat2.1

The document contains multiple Java programs that implement various algorithms and data structures. Key functionalities include checking for binary palindromes, multiplying integers using Booth's algorithm, finding the GCD using Euclid's algorithm, and implementing Karatsuba multiplication. Additional programs cover topics such as finding majority elements, leaders in arrays, maximum product subarrays, and rotating arrays using block swap techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Binary Palindrome:

package practice;

import java.util.*;

public class BinaryPalindrome {

public static boolean isPalindrome(int x) {

String binary=Integer.toBinaryString(x);

int left=0;

int right=binary.length()-1;

while (left < right) {

if (binary.charAt(left) != binary.charAt(right)) {

return false;

left++;

right--;

return true;

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a number to check if its binary representation is a palindrome:");

int number = scanner.nextInt();

if (isPalindrome(number)) {

System.out.println(number + " is a binary palindrome.");

} else {

System.out.println(number + " is not a binary palindrome.");

}
Booth Multiplier:

import java.util.Scanner;

public class Booth {

public int multiply(int n1, int n2) {

int m = n1;

int r = n2;

int A = n1;

int S = -n1;

int P = 0;

int count = Integer.SIZE;

while (count > 0) {

if ((r & 1) == 1) {

P += A;

S += m;

A <<= 1;

S <<= 1;

count--;

r >>= 1;

return P;

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

Booth b = new Booth();

System.out.println("Enter two integer numbers -");

int n1 = scan.nextInt();

int n2 = scan.nextInt();

int result = b.multiply(n1, n2);

System.out.println(result); } }
Euclid’s Algorithm:

import java.util.*;

public class Euclid {

public static int gcd(int a,int b) {

if(a==0) {

return b;

}else {

return gcd(b%a,a);

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

int a=sc.nextInt();

int b=sc.nextInt();

int result=gcd(a,b);

System.out.println("GCD of "+a+" and "+b+" is "+result);

}
Karatsuba:

import java.util.*;

public class Karatsuba {

public static long multiply(long x, long y) {

if(x<10 || y<10) {

return x*y;

}else {

int n= Math.max(Long.toString(x).length(), Long.toString(y).length());

int half = (n+1)/2;

long a=x / (long)Math.pow(10,half);

long b=x % (long)Math.pow(10,half);

long c=y / (long)Math.pow(10,half);

long d=y % (long)Math.pow(10,half);

long ac=multiply(a,c);

long bd=multiply(b,d);

long adbc=multiply(a+b,c+d)-ac-bd;

return (long)((ac*Math.pow(10,2*half))+((adbc)*Math.pow(10,half))+bd);

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);

long x=sc.nextLong();

long y=sc.nextLong();

long result=multiply(x,y);

System.out.println(result);

sc.close();

}
Longest sequence of 1 after flipping a bit:

import java.util.*;

public class Main {

public static int Ones(int n) {

String binary = Integer.toBinaryString(n);

int maxLength = 0;

int currentLength = 0;

int previousLength = 0;

for (char bit : binary.toCharArray()) {

if (bit == '1') {

currentLength++;

} else {

maxLength = Math.max(maxLength, currentLength + previousLength + 1);

previousLength = currentLength;

currentLength = 0;

maxLength = Math.max(maxLength, currentLength + previousLength + 1);

return maxLength;

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int result=Ones(n);

System.out.println(result);

}
Swap Nibbles:

import java.util.Scanner;

public class Swap_nibbles {

// Function to swap nibbles in an integer

public static int swapNibbles(int num) {

// Mask and shift to swap nibbles

return ((num & 0x0F) << 4) | ((num & 0xF0) >> 4);

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Input: integer from the user

System.out.println("Enter an integer (0-255):");

int num = sc.nextInt();

// Ensure the input is within the 8-bit range (0-255)

if (num < 0 || num > 255) {

System.out.println("Please enter a valid integer between 0 and 255.");

} else {

// Call the function to swap nibbles

int swappedNum = swapNibbles(num);

System.out.println("Original number in binary: " + String.format("%8s",


Integer.toBinaryString(num)).replace(' ', '0'));

System.out.println("After swapping nibbles in binary: " + String.format("%8s",


Integer.toBinaryString(swappedNum)).replace(' ', '0'));

System.out.println("After swapping nibbles, the number becomes: " + swappedNum);

sc.close();

}
Majority Element:

import java.util.Scanner;

public class Majority {

public static int findMajorityElement(int[] nums) {

int candidate = 0, count = 0;

for (int num : nums) {

if (count == 0) {

candidate = num;

count += (num == candidate) ? 1 : -1;

count = 0;

for (int num : nums) {

if (num == candidate) {

count++;

if(count>(nums.length/2)) {

return candidate;

else {

return -1;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

int[] nums = new int[n];

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


nums[i] = scanner.nextInt();

System.out.println(findMajorityElement(nums));

scanner.close();

Leaders of Array:

import java.util.*;

public class Leader {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int[] arr=new int[n];

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

arr[i]=sc.nextInt();

int leader = arr[n-1];

System.out.println(leader);

for(int i=n-2;i>0;i--) {

if(arr[i]>leader) {

leader=arr[i];

System.out.println(leader);

}
Maxequilibriumsum:

import java.util.*;

public class Maxsum {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc= new Scanner(System.in);

int n= sc.nextInt();

int[] arr=new int[n];

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

arr[i]=sc.nextInt();

int tsum=0;

int lsum=0;

int maxsum=Integer.MIN_VALUE;

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

tsum+=arr[i];

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

tsum-=arr[i];

if(lsum==tsum && lsum>maxsum) {

maxsum=lsum;

lsum+=arr[i];

System.out.println(maxsum);

}
Max Product Sub Array:

import java.util.*;

public class MaxproductSubArray {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int[] a=new int[n];

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

a[i]=sc.nextInt();

int max=a[0],m=a[0],min=a[0],temp=0;

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

if(a[i]>0) {

max=Math.max(a[i],max*a[i]);

min=Math.min(a[i],min*a[i]);

else if(a[i]==0) {

max=min=0;

}else {

temp=max;

max=Math.max(a[i],min*a[i]);

min=Math.min(a[i],temp*a[i]);

m=Math.max(m,max);

System.out.println(m);

}
Block swap:

import java.util.*;

public class Blockswap {

// Function to rotate the array using Block Swap algorithm

public static int[] rotate(int[] arr, int d, int n) {

d = d % n; // Handle case when d is greater than n

reverse(arr, 0, d - 1);

reverse(arr, d, n - 1);

return reverse(arr, 0, n - 1);

// Helper function to reverse a portion of the array

public static int[] reverse(int[] arr, int start, int end) {

while (start < end) {

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

start++;

end--;

return arr;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Input: actual size of the array

System.out.println("Enter the number of elements in the array:");

int n = sc.nextInt();

int[] arr = new int[n];

// Input: elements of the array

System.out.println("Enter the elements of the array:");


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

arr[i] = sc.nextInt();

// Input: number of positions to rotate

System.out.println("Enter the number of positions to rotate:");

int d = sc.nextInt();

// Rotate the array by d positions

rotate(arr, d, n);

// Output: rotated array

System.out.println("Rotated array:");

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

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

sc.close();

}
Hour glass:

import java.util.*;

public class hourglass {

public static int maxsum(int [][]arr,int r,int c) {

int maxi=0;

int sum=0;

if(r<3||c<3) {

System.out.println("Not possible");

for(int i=0;i<r-2;i++) {

for(int j=0;j<c-2;j++) {

maxi=(arr[i][j]+arr[i][j+1]+arr[i][j+2]+arr[i+1][j+1]+

arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2]);

maxi=Math.max(maxi, sum);

return maxi;

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);

int R=sc.nextInt();

int C=sc.nextInt();

int[][] arr=new int [R][C];

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

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

arr[i][j]=sc.nextInt();

}
int result=maxsum(arr,R,C);

System.out.println(result);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy