0% found this document useful (0 votes)
3 views

KIOT SDE Readiness Programme Module II Assessment 01 QP_v1.1

The document outlines an assessment for a Java programming module, including problem-solving questions related to arrays, strings, and control flow statements. It contains specific tasks such as counting pairs in an array, removing consecutive duplicates from a string, checking for non-decreasing arrays, finding the largest magic square, determining the length of the longest substring without repeating characters, and reorganizing a string. Each task includes problem statements, example inputs and outputs, and constraints.

Uploaded by

k.s.yogeswar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

KIOT SDE Readiness Programme Module II Assessment 01 QP_v1.1

The document outlines an assessment for a Java programming module, including problem-solving questions related to arrays, strings, and control flow statements. It contains specific tasks such as counting pairs in an array, removing consecutive duplicates from a string, checking for non-decreasing arrays, finding the largest magic square, determining the length of the longest substring without repeating characters, and reorganizing a string. Each task includes problem statements, example inputs and outputs, and constraints.

Uploaded by

k.s.yogeswar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

SmartCliff Learning Solutions

KIOT-SDE-Readiness-Module II- Assessment 01

Topic: Introduction to Java, Controll Flow statement, Arrays, Functions and Strings
Date: 20/02/2025
Time: 01:30 PM – 04:30 PM
Total Marks: 50 Marks

PART A: Problem Solving Questions


Implement the Java Code for the following scenario.
1. Count Equal and Divisible Pairs in an Array (5 marks)
Problem Statement : Given a 0-indexed integer array nums of length n and an integer k,
return the number of pairs (i, j) where 0 <= i < j < n, such that nums[i] == nums[j] and (i * j) is
divisible by k.

Example 1:
Input: nums = [3,1,2,2,2,1,3], k = 2
Output: 4
Explanation:
There are 4 pairs that meet all the requirements:
- nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
- nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
- nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
- nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.

Example 2:
Input: nums = [1,2,3,4], k = 1
Output: 0
Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the
requirements.

Constraints:
1 <= nums.length <= 100
<= nums[i], k <= 100

package assesment_01;
import java.util.Scanner;

public class CountArray {


public static int countPairs(int[] num, int k) {
int count = 0;
int n = num.length;
for (int i=0;i<n;i++) {
for (int j=i+1;j<n;j++) {
if (num[i]==num[j]&&(i*j)%k==0) {
count++;
}
}
}
return count;
}

public static void main(String[] args) {


Scanner array = new Scanner(System.in);
System.out.print("Enter no of elements: ");
int n = array.nextInt();
int[] num = new int[n];
System.out.print("Enter elements: ");
for (int i=0;i<n;i++) {
num[i] = array.nextInt();
}
System.out.print("Enter k: ");
int k = array.nextInt();
System.out.println(countPairs(num, k));
array.close();
}
}

2. Remove Consecutive Duplicates (5 marks)


Problem Statement : For a given string(str), remove all the consecutive duplicate characters.

Example 1:
Input String: "aaaa"
Expected Output: "a"

Example 2:
Input String: "aabbbcc"
Expected Output: "abc"

Constraints:
0 <= N <= 10^6
Where N is the length of the input string.

package assesment_01;
import java.util.Scanner;
public class ConsecutiveDuplicates {
public static void main(String[] args) {
Scanner dup = new Scanner(System.in);
System.out.print("Enter string: ");
String input = dup.nextLine();
String result = removeConsecutiveDuplicates(input);
System.out.println(result);
dup.close();
}
public static String removeConsecutiveDuplicates(String str) {
if (str.length() == 0) {
return str;
}
StringBuilder result = new StringBuilder();
result.append(str.charAt(0));
for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) != str.charAt(i - 1)) {
result.append(str.charAt(i));
}
}
return result.toString();
}
}

3. Non-decreasing Array

(8 marks)
Problem Statement : Given an array nums with n integers, your task is to check if it could
become non-decreasing by modifying at most one element.
We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such
that (0 <= i <= n - 2).

Example 1:
Input: nums = [4,2,3]
Output: true
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.

Example 2:
Input: nums = [4,2,1]
Output: false
Explanation: You cannot get a non-decreasing array by modifying at most one element.

Constraints:
n == nums.length
1 <= n <= 10^4
-10^5 <= nums[i] <= 10^5
package assesment_01;
import java.util.Scanner;
public class NonDecreasingArray {
public static boolean check(int[] arr) {
int count = 0;
for (int i=0;i<arr.length-1;i++) {
if (arr[i]>arr[i+1]) {
count++;

if (count>1) {
return false;
}
if (i==0||arr[i-1]<=arr[i+1]) {
arr[i]=arr[i+1];
} else {
arr[i+1]=arr[i];
}
}
}
return true;
}

public static void main(String[] args) {


Scanner darray = new Scanner(System.in);
System.out.print("Enter no of elements: ");
int n = darray.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements:");
for (int i=0; i<n;i++) {
arr[i] =darray.nextInt();
}

System.out.println(check(arr));
darray.close();
}

}
4. Largest Magic Square (8 marks)
Problem Statement: A k x k magic square is a k x k grid filled with integers such that every row
sum, every column sum, and both diagonal sums are all equal.
The integers in the magic square do not have to be distinct. Every 1 x 1 grid is trivially a magic
square.

Given an m x n integer grid, return the size (i.e., the side length k) of the largest magic square
that can be found within this grid.

Example 1:
Input: grid = [[7,1,4,5,6],[2,5,1,6,4],[1,5,4,3,2],[1,2,7,3,4]]
Output: 3
Explanation: The largest magic square has a size of 3.
Every row sum, column sum, and diagonal sum of this magic square is equal to 12.
- Row sums: 5+1+6 = 5+4+3 = 2+7+3 = 12
- Column sums: 5+5+2 = 1+4+7 = 6+3+3 = 12
- Diagonal sums: 5+4+3 = 6+4+2 = 12

Example 2:
Input: grid = [[5,1,3,1],[9,3,3,1],[1,3,3,8]]
Output: 2
Explanation: The largest magic square in the grid has a side length of 2. One such 2 x 2 sub-grid
is: [[3 3] [3 3]]
Here's why it's a magic square:
Row sums: First Row : 3 + 3=6, Second Row: 3 + 3 =6
Column Sums: First column :3 +3 =6,Second Row :3 + 3=6
Diagonal sums:
Main Diagonal: 3+ 3=6
Anti Diagonal : 3 +3=6
Since all rows, columns, and diagonals sum to the same value (6), the sub-grid is a valid magic
square. No larger magic square can be found within the given grid, so the output is 2.

Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 50
1 <= grid[i][j] <= 10^6

package assesment_01;
import java.util.Scanner;
public class LargestMagicSquare {
public static int largestMagicSquare(int[][] grid) {
int m=grid.length,n=grid[0].length;
int maxK= Math.min(m, n);
for (int k=maxK;k>1;k--) {
for (int i=0;i<=m-k;i++) {
for (int j=0;j<=n-k;j++) {
int sum=0;
for (int x=j;x<j+k;x++) {
sum += grid[i][x];
}
boolean isMagic = true;
for (int x = i; x < i + k; x++) {
int rowSum = 0;
for (int y = j; y < j + k; y++) {
rowSum += grid[x][y];
}
if (rowSum != sum) {
isMagic = false;
break;
}
}
for (int y = j; y < j + k && isMagic; y++) {
int colSum = 0;
for (int x = i; x < i + k; x++) {
colSum += grid[x][y];
}
if (colSum != sum) {
isMagic = false;
break;
}
}
int diag1 = 0, diag2 = 0;
for (int x = 0; x < k; x++) {
diag1 += grid[i + x][j + x];
diag2 += grid[i + x][j + k - 1 - x];
}

if (isMagic && diag1 == sum && diag2 == sum) {


return k;
}
}
}
}
return 1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int m = scanner.nextInt();
System.out.print("Enter number of columns: ");
int n = scanner.nextInt();
int[][] grid = new int[m][n];
System.out.println("Enter grid values:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
grid[i][j] = scanner.nextInt();
}
}

System.out.println( largestMagicSquare(grid));
scanner.close();
}
}

5. Length of the longest substring (9 marks)


Problem Statement : Given a string S, find the length of the longest substring without repeating
characters.

Example 1:
Input :
S = "abcabcbb"
Output:
3
Explanation:
The longest substring without repeating characters is "abc", "bca", and "cab".

Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring without repeating characters is
"abdefg" , "bdefga" and "defgab".

Constraints:
0 <= N <= 10^6
Where N is the length of the input string.

package assesment_01;
import java.util.Scanner;
public class LongestSubstring {
public static int findLongestSubstring(String s) {
int len= 0;
for (int i = 0; i < s.length(); i++) {
String temp = "";
for (int j = i; j < s.length(); j++) {
if (temp.contains(String.valueOf(s.charAt(j)))) {
break;
}
temp += s.charAt(j);
len= Math.max(len,temp.length());
}
}
return len;
}
public static void main(String[] args) {
Scanner lstring = new Scanner(System.in);
System.out.print("Enter string: ");
String input = lstring.nextLine();
lstring.close();
int result = findLongestSubstring(input);
System.out.println(result);
}

}
6. Reorganize String (15 marks)
Problem Statement : Given a string s, rearrange the characters of “s” so that any two adjacent
characters are not the same.
Return any possible rearrangement of s or return " " if not possible.

Example 1:
Input: s = "aab"
Output: "aba"

Example 2:
Input: s = "aaab"
Output: ""

Constraints:
1 <= s.length <= 500
s consists of lowercase English letters.

package assesment_01;
import java.util.Scanner;
public class ReOrganizeString {
public static void main(String[]args) {
Scanner reor = new Scanner(System.in);
System.out.println("Enter string:");
String input =reor.nextLine();
char[] c=input.toCharArray();
for(int i=0;i<input.length();i++) {
if(c[i]==c[i+1]||c[i]!=c[i+2]) {
char temp=c[i+1];
c[i+1]=c[i+2];
c[i+2]=temp;
System.out.println(c);

}
else {
System.out.println("''");
}

}
reor.close();

}
}

You might also like