GFG recursion practice 01
package com.company;
public class CWR_Gfg_practice {
// recursion example
public static int test(int n){
if( n == 0){
return 0;
}
int a = 1 + test(n / 2);
System.out.println(a);
return a;
}
// Calculating the value of log n base 2.
public static int log2(int n) {
if (n == 0) {
return -1;
}
int count = 0;
while (n != 1) {
n = n >> 1;
count++;
}
return count;
}
//Given a number N, find the most significant set bit in the given number
public static int findMostSignificant(int n) {
int a = log2(n);
return (int) Math.pow(2, a);
}
//Given a number N, the task is to find the XOR of all numbers from 1 to N
public static int xorUptoN(int n){
int res = 0;
for(int i = 1; i<n+1; i++){
res = res ^ i;
}
return res;
}
//Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair of the element from the array.
public static int maximumAnd(int [] arr, int n){
int maxi = 0;
for(int i = 0; i<n; i++){
for(int j = i + 1; j<n; j++){
maxi = Math.max(arr[i] & arr[j], maxi);
}
}
return maxi;
}
public static void main(String[] args) {
// System.out.println(xorUptoN(6));
// int [] a ={4, 8, 16, 12};
// System.out.println(maximumAnd(a,4));
test(10);
System.out.println(test(10));
}
}
Comments
Post a Comment