0% found this document useful (0 votes)
12 views8 pages

1024 - Jatin Singla - Java Day 8

Uploaded by

Arjun
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)
12 views8 pages

1024 - Jatin Singla - Java Day 8

Uploaded by

Arjun
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/ 8

Java Day-8

Student Name: Jatin singla UID: 21BCS1024


Branch: BE-CSE Section/Group:CC-628-A
Semester: 6th Date of Performance: 03-07-24
Subject Name: Java

1. Aim:

I) Sorting is useful as the first step in many different tasks. The most
common task is to make finding things easier, but there are other uses as
well. In this case, it will make it easier to determine which pair or pairs
of elements have the smallest absolute difference between them.

II) You are given a 6+6 2D array. An hourglass in an array is a portion


shaped like this:
abc
d
efg

III) Write a function to find the longest common prefix string amongst
an array of strings.
If there is no common prefix, return an empty string "".

IV) Given an integer array nums and an integer k, return true if there
are two distinct indices i and j in the array such that nums[i] ==
nums[j] and abs(i - j) <= k.

V) You are given a 6+6 2D array. An hourglass in an array is a portion


shaped like this:
abc
d
efg
2. Objective:

I) To learn to find the closest numbers.

II) To learn to find the largest sum in the 2D array.

III) To learn to find the longest common prefix string.

3. Script and Output:

I) import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] dat = new int[n];
for(int i = 0; i < n; i++) {
dat[i] = in.nextInt();
}
Arrays.sort(dat);
int minDiff = Integer.MAX_VALUE;
String out = "";
for(int i = 0; i < n - 1; i++) {
if(dat[i + 1] - dat[i] <= minDiff) {
if(dat[i + 1] - dat[i] == minDiff) {
out += " " + dat[i] + " " + dat[i + 1];
} else {
out = dat[i] + " " + dat[i + 1];
}
minDiff = dat[i + 1] - dat[i];
}
}
System.out.println(out);
}
}

Output:

II) import java.io.*;


import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
String[][] numArray = new String[6][];
int largestSum = 0;
for(int i=0; i<6; i++){
numArray[i]=sc.nextLine().split(" ");
}
for(int i=0;i<=3;i++){
for(int j=0;j<=3;j++){
int sum = Integer.parseInt(numArray[i][j])
+Integer.parseInt(numArray[i][j+1])
+Integer.parseInt(numArray[i][j+2])
+Integer.parseInt(numArray[i+1][j+1])
+Integer.parseInt(numArray[i+2][j])
+Integer.parseInt(numArray[i+2][j+1])
+Integer.parseInt(numArray[i+2][j+2]);
if(i==0 && j==0){
largestSum=sum;
}else{
if(sum>largestSum){
largestSum=sum;
}
}
}
}
System.out.println(largestSum);
}
}

Output:
III) class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}

String prefix = strs[0];


for (int i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) {
return "";
}
}
}

return prefix;
}
}

Output:
IV) class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
// create a set to store unique elements
Set<Integer> set = new HashSet<>();

// iterate through the array


for (int i = 0; i < nums.length; i++) {
// if the set already contains the current element,
// it means we have found a duplicate
if (set.contains(nums[i])) {
return true;
}
// add the current element to the set
set.add(nums[i]);

// if the set size is greater than k,


// remove the element at the previous index
if (set.size() > k) {
set.remove(nums[i - k]);
}
}
return false;
}
}

Output:
V) import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {


Scanner in = new Scanner(System.in);

int arr[][] = new int[6][6];


for(int arr_i=0; arr_i < 6; arr_i++){
for(int arr_j=0; arr_j < 6; arr_j++){
arr[arr_i][arr_j] = in.nextInt();
}
}

Sum(arr);

private static void Sum(int arr[][]){


//ROw
int sum=-1000;
for(int i =0 ; i<4;i++){
for(int x =0 ; x<4; x++){

int top = arr[i][x]+arr[i][x+1]+arr[i][x+2];


int middle = arr[i+1][x+1];
int bottom = arr[i+2][x]+arr[i+2][x+1]+arr[i+2][x+2];
if(top+middle+bottom>sum){sum=top+middle+bottom;}
}
}
System.out.println(sum);
}
}

Output:

4. Learning Outcomes :

i) Understanding OOPs concept.


ii) Learnt about the 2D array.
iii) Learnt about longest common prefix.

You might also like