Skip to content

Commit d5197fc

Browse files
authored
Merge branch 'SarthakKeshari:master' into master
2 parents 15eae36 + 339399c commit d5197fc

21 files changed

Lines changed: 1277 additions & 0 deletions
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Given two arrays arr1 and arr2. We need to find the intersection of both the arrays and return the result as a list.
3+
*/
4+
5+
import java.util.*;
6+
7+
public class intersectionOfTwoArrays{
8+
9+
public static void main(String[] args){
10+
Scanner sc = new Scanner(System.in);
11+
int n1 = sc.nextInt();
12+
int[] arr1 = new int[n1];
13+
int n2 = sc.nextInt();
14+
int[] arr2 = new int[n2];
15+
for(int i = 0; i < n1; i++){
16+
arr1[i] = sc.nextInt();
17+
}
18+
for(int i = 0; i < n1; i++){
19+
arr2[i] = sc.nextInt();
20+
}
21+
intersection(arr1, arr2, n1, n2);
22+
sc.close();
23+
}
24+
25+
public static void intersection(int[] a,int[] b, int n, int m){
26+
HashMap<Integer,Integer> map = new HashMap<>(); // created a hashmap which will store distinct elements with their frequencies
27+
for(int i = 0; i < n; i++){
28+
map.put(a[i],map.getOrDefault(a[i],0)+1); // updating frequencies of each element
29+
}
30+
int[] arr = new int[100001]; // created an array which will store the elements which are present in both the arrays
31+
Arrays.fill(arr,0);
32+
for(int i = 0; i < m; i++){
33+
if(map.containsKey(b[i])){ // updating array which contains elements that are present in both the arrays
34+
arr[b[i]] = 1;
35+
}
36+
}
37+
ArrayList<Integer> al = new ArrayList<>(); // creating a list
38+
for(int i = 0; i < m; i++){
39+
if(arr[b[i]]==1){ // checking if the element is present in both the arrays or not
40+
al.add(b[i]); // adding the elements to the list
41+
}
42+
}
43+
System.out.println(al);
44+
}
45+
46+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Write a program that reads an array of ints and outputs the length of the longest sequence in strictly ascending order.
5+
* Print the elements of longest ascending sequence in an array
6+
* Elements of the sequence must go one after another. A single number is assumed to be an ordered sequence with the length = 1.
7+
*/
8+
9+
public class longestAscendingSequence {
10+
11+
public static void main(String[] args) {
12+
13+
//Read elements and size
14+
Scanner scanner = new Scanner(System.in);
15+
int tam = scanner.nextInt();
16+
int[] numbers = new int[tam];
17+
18+
for (int i = 0; i < numbers.length; i++) {
19+
numbers[i] = scanner.nextInt();
20+
}
21+
22+
//Defining auxiliary variables
23+
24+
int maxTmp = 0;
25+
int count = 1;
26+
int pos = -1;
27+
28+
//Traverse the array and increment the amount by 1 the longest amount in the array, in addition to obtaining the position
29+
for (int i = 0; i < numbers.length - 1; i++) {
30+
if (numbers[i] < numbers[i + 1]) {
31+
++count;
32+
} else {
33+
if (count > maxTmp) {
34+
maxTmp = count;
35+
pos = i;
36+
}
37+
count = 1;
38+
}
39+
}
40+
41+
//Check if count or maxTemp is the highest to assign the position from which the numbers will start printing
42+
if (maxTmp > count) {
43+
pos = (pos - maxTmp);
44+
}
45+
46+
//Get max value
47+
int max = Math.max(count, maxTmp);
48+
System.out.println("The max value is : " + max);
49+
50+
//Print elements
51+
for (int i = pos + 1; i <= pos + max; i++) {
52+
System.out.print(numbers[i] + " ");
53+
}
54+
55+
scanner.close();
56+
57+
58+
/*
59+
Value test
60+
10
61+
1 2 4 1 2 3 5 7 4 3
62+
63+
12
64+
1 2 4 1 2 3 5 7 8 9 10 11
65+
66+
20
67+
1 2 4 1 2 3 5 7 4 3 5 8 9 10 12 15 16 2 5 6
68+
69+
15
70+
1 0 2 4 6 3 8 7 0 1 5 8 2 1 3
71+
72+
3
73+
1 2 4
74+
75+
4
76+
1 2 0 0
77+
78+
8
79+
1 2 3 4 5 6 0 3*/
80+
81+
}
82+
83+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//Addition of 2 n*n matrices
2+
//this program works for m*n matrices also!
3+
import java.util.Scanner;
4+
public class matrixAddition {
5+
6+
public static void main(String[] args) {
7+
Scanner reader=new Scanner(System.in);
8+
9+
int matrix_1[][];
10+
int matrix_2[][];
11+
int add_matrix[][];
12+
13+
//taking input of number of rows and columns from user
14+
System.out.println("enter row size of both matrices to be added :");
15+
int row = reader.nextInt();
16+
System.out.println("enter column size of both matrices to be added :");
17+
int col = reader.nextInt();
18+
19+
// declaring matrices of user defined rows and columns
20+
matrix_1 = new int[row][col];
21+
matrix_2 = new int[row][col];
22+
add_matrix = new int[row][col];
23+
24+
//entering elements in matrix 1
25+
System.out.println("Enter elemtents in Matrix 1\n");
26+
for(int i=0; i<row; i++){
27+
for(int j=0; j<col; j++){
28+
matrix_1[i][j] = reader.nextInt();
29+
}
30+
}
31+
32+
//entering elements in matrix 2
33+
System.out.println("Enter elemtents in Matrix 2\n");
34+
for(int i=0; i<row; i++){
35+
for(int j=0; j<col; j++){
36+
matrix_2[i][j] = reader.nextInt();
37+
}
38+
}
39+
40+
// adding matrix 1 and 2
41+
for( int i=0; i<row; i++){
42+
for(int j=0; j<col; j++){
43+
add_matrix[i][j] = matrix_1[i][j] + matrix_2[i][j];
44+
}
45+
}
46+
47+
// Printing output matrix
48+
System.out.println("Resultant Matrix: \n");
49+
for(int i=0; i<row; i++){
50+
for(int j=0; j<col; j++){
51+
System.out.print(add_matrix[i][j]+" ");
52+
53+
} System.out.println();
54+
}
55+
56+
57+
58+
}
59+
60+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Q. matrix multiplication in java ?
2+
3+
import java.util.Scanner;
4+
5+
public class matrix_Multiplication {
6+
public static void main(String args[]) {
7+
8+
int m, n, p, q, sum = 0, c, d, k;
9+
Scanner in = new Scanner(System.in);
10+
11+
System.out.print("Enter Number of Rows and Columns of First Matrix : ");
12+
m = in.nextInt(); // enter no. of rows of first matrix
13+
n = in.nextInt(); // enter no. of vplumns of first matrix
14+
15+
int first[][] = new int[m][n];
16+
17+
System.out.print("Enter First Matrix Elements : ");
18+
19+
for (c = 0; c < m; c++) {
20+
for (d = 0; d < n; d++) {
21+
first[c][d] = in.nextInt(); // here we have to enter first matrix's element
22+
}
23+
}
24+
25+
System.out.print("Enter Number of Rows and Columns of Second Matrix : ");
26+
p = in.nextInt(); // enter no. of vplumns of second matrix
27+
q = in.nextInt(); // enter no. of vplumns of second matrix
28+
29+
if (n != p) {
30+
System.out.print("Matrix of the entered order can't be Multiplied..!!");
31+
} else {
32+
int second[][] = new int[p][q];
33+
int multiply[][] = new int[m][q];
34+
35+
System.out.print("Enter Second Matrix Elements :\n");
36+
37+
for (c = 0; c < p; c++) {
38+
for (d = 0; d < q; d++) {
39+
second[c][d] = in.nextInt(); // here we have to enter second matrix's element
40+
}
41+
}
42+
43+
System.out.print("Multiplying both Matrix...\n");
44+
45+
for (c = 0; c < m; c++) {
46+
for (d = 0; d < q; d++) {
47+
for (k = 0; k < p; k++) {
48+
sum = sum + first[c][k] * second[k][d];
49+
}
50+
51+
multiply[c][d] = sum; // here sum is the element of multiply matrix and one by one element will be inserted
52+
sum = 0;
53+
}
54+
}
55+
56+
System.out.print("Multiplication Successfully performed..!!\n");
57+
System.out.print("Now the Matrix Multiplication Result is :\n");
58+
59+
for (c = 0; c < m; c++) {
60+
for (d = 0; d < q; d++) {
61+
System.out.print(multiply[c][d] + "\t"); // multiply matrix output
62+
}
63+
System.out.print("\n");
64+
}
65+
}
66+
67+
}
68+
}

Basic Codes/TempConversion.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Scanner;
2+
3+
public class TempConversion {
4+
public static void main(String[] args) {
5+
// Question: Convert the temperature from celsius to fahrenheit and Fahrenheit to
6+
// Celsius according to the user`s choice provided by the input
7+
8+
System.out.println("To convert from Celsius to Fahrenheit Enter 0\n" + "To convert from Fahrenheit to Celsius enter 1"); // Asking user for input
9+
10+
Scanner input = new Scanner(System.in);
11+
int n = input.nextInt(); // Taking input to choose the temperature conversion
12+
13+
switch (n) {
14+
15+
case 0:
16+
System.out.println("Enter the temperature in Celsius");
17+
18+
float temp = input.nextFloat(); // Taking the temperature in celsius
19+
double ans = ((temp * 9) / 5) + 32; // Formula to calculate the temperature in celsius
20+
21+
System.out.printf("The %.3f degree celsius = %.3f Fahrenheit", temp, ans);
22+
break;
23+
24+
case 1:
25+
System.out.println("Enter the temperature in Fahrenheit");
26+
27+
temp = input.nextFloat(); // Taking the temperature in celsius
28+
ans = ((temp - 32) * 5) / 9; // Formula to calculate the temperature in celsius
29+
30+
System.out.printf("The %.3f = %.3f degree celsius", temp, ans);
31+
break;
32+
33+
default: // Showing the message if user provides invalid argument
34+
System.out.println("Enter valid input");
35+
}
36+
}
37+
}

Bit Manipulation/IsNthBitSet.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Problem Statement: Given two numbers 'a' and 'n', check if nth bit is set in 'a'. Consider 0 based indexing.
3+
Idea: Do a bitwise AND operation with 1 at the nth position. If the result is non-zero bit is set otherwise
4+
it is unset.
5+
6+
Examples:
7+
8+
Input: 4 2
9+
Output: Bit is unset at position 2
10+
11+
Input: 10 4
12+
Output: Bit is set at position 4
13+
*/
14+
15+
import java.util.Scanner;
16+
17+
public class IsNthBitSet {
18+
public static void main(String[] args) {
19+
Scanner sc = new Scanner(System.in);
20+
21+
int a = sc.nextInt();
22+
int n = sc.nextInt();
23+
24+
checkNthBit(a, n);
25+
}
26+
27+
public static void checkNthBit(int a, int n) {
28+
//Create a mask that has only nth bit set
29+
int mask = 1 << (n - 1);
30+
31+
//If nth bit is set in 'a' then bitwise AND with mask
32+
//will give non-zero result
33+
if ((a & mask) != 0) {
34+
System.out.println("Bit is set at position " + n);
35+
}
36+
//If nth bit is unset in 'a' then bitwise AND with mask
37+
//will give zero result
38+
else {
39+
System.out.println("Bit is unset at position " + n);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)