Tomojit Banerjee (2011200001064)
ARRAY ASSIGNMENT
Q1:-
Code:
Tomojit Banerjee (2011200001064)
Output:
Written Code:
import java.util.*;
public class Main
private int a[];
private int arrsize,c=0;
private Scanner sc = new Scanner(System.in);
public void arraySizeInput()
System.out.println("Enter Array Size :");
arrsize = sc.nextInt();
public void arrayCreation()
a = new int[arrsize];
public void arrayValueInput()
{
Tomojit Banerjee (2011200001064)
System.out.println("Enter Int Value: ");
for(int i = 0; i<arrsize;i++)
a[i] = sc.nextInt();
public int arr2[];
public void countallzero()
for(int i = 0; i<arrsize;i++)
if(a[i]==0)
c = c+1;
arr2 =new int[c];
for(int i = 0; i<c;i++)
if(a[i]==0)
arr2[i]=a[i];
public void arrayValueDisplay()
System.out.println("The Array Values are :- ");
for(int i = 0; i<arrsize;i++)
if(a[i]!=0)
Tomojit Banerjee (2011200001064)
System.out.println(a[i]);
for(int i = 0; i<c;i++)
System.out.println(arr2[i]);
public static void main(String[] args)
Main obj=new Main();
obj.arraySizeInput();
obj.arrayCreation();
obj.arrayValueInput();
obj.countallzero();
obj.arrayValueDisplay();
Q2:-
Code:
Tomojit Banerjee (2011200001064)
Output:
Written Code:
import java.util.*;
class Main
{
Tomojit Banerjee (2011200001064)
private int a[];
private int arrsize;
private Scanner sc = new Scanner(System.in);
public void arraySizeInput()
System.out.println("Enter Array Size ");
arrsize = sc.nextInt();
public void arrayCreation()
a = new int[arrsize];
public void arrayValueInput()
System.out.println("Enter Int Value: ");
for(int i = 0; i<arrsize;i++)
a[i] = sc.nextInt();
public void arrayValueDisplay()
System.out.println("The array values are : ");
for(int i = 0; i<arrsize;i++)
System.out.println(a[i]);
int sum=a[0]+a[arrsize-1];
System.out.println("The sum of the largest and the smallest integer is: "+sum);
Tomojit Banerjee (2011200001064)
public static void main(String[] args)
Main obj=new Main();
obj.arraySizeInput();
obj.arrayCreation();
obj.arrayValueInput();
obj.arrayValueDisplay();
Q3:-
Code:
Output:
Tomojit Banerjee (2011200001064)
Written Code:
import java.util.*;
class Main
static int findLongest(int arr[],int n)
Arrays.sort(arr);
int ans = 0, c = 0;
ArrayList<Integer> v = new ArrayList<Integer>();
v.add(10);
for (int i = 1; i < n; i++)
if (arr[i] != arr[i - 1])
v.add(arr[i]);
for (int i = 0; i < v.size(); i++)
if (i > 0 &&v.get(i) == v.get(i - 1) + 1)
c++;
else
c = 1;
ans = Math.max(ans, c);
return ans+1;
Tomojit Banerjee (2011200001064)
public static void main(String[] args)
int arr[] = {49, 1, 3, 200, 2, 4, 70, 5};
int n = arr.length;
System.out.println("Length of the Longest contiguous subsequence is "+findLongest(arr, n));
Q4:-
Code:
Output:
Written Code:
Tomojit Banerjee (2011200001064)
class Main
static int No_of_triangles(int arr[], int n)
int c = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
for (int k = j + 1; k < n; k++)
if (arr[i] + arr[j] > arr[k]&& arr[i] + arr[k] > arr[j]&& arr[k] + arr[j] > arr[i])
c++;
return c;
public static void main(String[] args)
int arr[] = { 4, 6, 3, 7, 5, 9 };
int size = arr.length;
System.out.println( "Total number of triangles possible is "+No_of_triangles(arr, size));
Q5:-
Code:
Tomojit Banerjee (2011200001064)
Output:
Written Code:
import java.util.*;
public class Main
static int[] rearrange(int[] arr, int n)
int temp[] = new int[n];
int sn = 0, ln = n-1;
boolean flag = true;
for (int i=0; i < n; i++)
{
Tomojit Banerjee (2011200001064)
if (flag)
temp[i] = arr[ln--];
else
temp[i] = arr[sn++];
flag = !flag;
return temp;
public static void main(String[] args)
int nums[] = new int[]{10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int result[];
System.out.println("Original Array: ");
System.out.println(Arrays.toString(nums));
result = rearrange(nums,nums.length);
System.out.println("New Array: ");
System.out.println(Arrays.toString(result));
Q6:-
Code:
Tomojit Banerjee (2011200001064)
Output:
Written Code:
class Main
static int cola = 2, rowa = 3, colb = 3, rowb = 2;
static void Tensorproduct(int A[][], int B[][])
int[][] C= new int[rowa * rowb][cola * colb];
System.out.println("The product matix is :");
Tomojit Banerjee (2011200001064)
for (int i = 0; i < rowa; i++)
for (int k = 0; k < rowb; k++)
for (int j = 0; j < cola; j++)
for (int l = 0; l < colb; l++)
C[i + l + 1][j + k + 1] = A[i][j] * B[k][l];
System.out.print( C[i + l + 1][j + k + 1]+" ");
System.out.println();
public static void main (String[] args)
int A[][] = { { 1, 2 },{ 3, 4 },{ 8, 9 } };
int B[][] = { { 7, 5, 2 },{ 6, 8, 1 } };
Tensorproduct(A, B);