Project 2
1. public class sub array sum
{
void sub array (int arr[],int n,int sum)
{
for(int i=0;i<n;i++)
{
int current sum=arr[i];
if(current sum=sum)
{
system.out.printIn("sum found at indexe " +i);
return;
}
else
{
for(int j=i+1;j<n;j++)
{
Current sum +=arr[j];
if(current sum===sum)
{
system.out.printIn(" sum found between indexes " + i + " and" +j);
return;
}
}
}
}
system.out.printIn("no sub array found");
int arr[]={12375,12};
{12345678910,15,0};
int n=arr.length;
int sum=23;
array sum.sub array sum (arr,n,sum);
}
}
OUTPUT Sum found at indexes 13 4.
2. public class permutaTION
{
public static void main(string[] args)
{
string str="ABC";
int n=str.length();
permutation permutation =new permutation();
permutation.permute(str,0,n-1);
}
private void permute (string str,int 1,int r)
{
if(l ==r)
system.out.printIn(str);
else
{
for(int i=1;i<=r;i++);
permute(str,l+1,r);
str=swap(str,l,i);
}
}
}
public string swap(string a,int i,int j)
{
char temp;
char[] char array=a.to char array();
temp=char array[i];
char array[i]=char array[j];
char array[j]=temp;
return string .value of(char array);
}
}
OUTPUT ABC ACB BAC CAB CBA.
3. using namespace std;
class node
{
public :
int data;
node* left;
node* right;
};
node*new node(int data)
{
node* node=new node();
node-> data=data;
node->left =null;
node->right =null;
return (node);
}
int identical trees(node * a,node * b)
{
if(a==null && b==null)
return 1;
if(a!=null && b !=null)
{
return (a->data ==b->data && identical trees(a->left,b->right ) && identical trees(a->right,b->right));
}
return 0;
}
int main()
{
node*root=new node(1);
node*root=new node(1);
root1->left=new node(2);
root1->right=new node(3);
root1->left->left=new node(4);
root1->left->right=new node(5);
if(identical trees(root 1,root2))
cout << "both trees are identical ".;
else
cout<<"trees are not identical.";
return 0;
}
OUTPUT BOTH TREES ARE IDENTICAL.
4. Class node
{
int data;
node left,right;
public node(int item)
{
data=item;
left=right=null;
}
}
public class binary tree
{
node root;
int get leaf count()
{
return get leaf count(root);
}
int get leaf count(root);
}
int get leaf count(node mode)
{
if(node==null)
return 0;
if(node.left==null && node.right==null)
return 1;
else
return get leaf count(node.left) +get leaf count(node.right);
}
public static void main(string [] args)
{
binary tree tree =new binary tree();
tree.root=new node(1);
tree.root.left=new node(2);
tree.root.right=new node(3);
tree.root.left.left=new node(5);
system.out.printIn(" the leaf count of binary tree is :" +tree.get leaf count());
}
}
OUTPUT THE LEAF COUNT OF A BINARY TREE IS 2 AND 3.
5. import java.lang.math;
class GFG
{
STATIC boolean is power of two(int n)
{
if(n==0)
return false;
return int(math.ceil(math.log(n).math.log(2))==(int)(math.floor((math.log(n)/math.log(2)));
}
public static void main(string[] args)
{
if(is power of two (1)
system.out.printIn("yes");
else
system.out.printIn("no");
if(is power of two(98))
system.out.printIn("yes");
else
system.out.printIn("no");
}
}
7.import java.io.*;
import java.util.*;
class GFG
{
Public static boolean are distinct (string str,int i,int j)
{
boolean[] visited =new boolean[26];
for(int k=i ;k<=j;k++)
{
if(visited(str.char at(k)-a)== true)
return false;
return true;
}
}
}