✅ Top 5 Array and String Problems in C (ICPC Style)

🔢 Problem 1: Maximum Subarray Sum

Problem Statement:
Given a one-dimensional array of integers, find the contiguous subarray with the maximum sum.

📥 Input:

Line 1: Integer N (1 ≤ N ≤ 105) – size of the array  
Line 2: N space-separated integers A[i] (–104 ≤ A[i] ≤ 104)

📤 Output:

Print the maximum sum of any contiguous subarray.

🔍 Sample Input:

8
-2 1 -3 4 -1 2 1 -5

✅ Sample Output:

6

💡 Output Explanation:

The subarray [4, -1, 2, 1] has the maximum sum = 6.

📉 Problem 2: Count Inversions

Problem Statement:
Given an array of integers, count how many pairs (i, j) exist such that i < j and A[i] > A[j].

📥 Input:

Line 1: Integer N (1 ≤ N ≤ 105)  
Line 2: N integers A[i] (1 ≤ A[i] ≤ 109)

📤 Output:

Number of inversions in the array.

🔍 Sample Input:

5
2 4 1 3 5

✅ Sample Output:

3

💡 Output Explanation:

Inversions are: (2,1), (4,1), (4,3)

📦 Problem 3: Prefix Sum Query

Problem Statement:
Given an array of integers and Q queries, each query asks for the sum of a subarray from index L to R.

📥 Input:

Line 1: Integers N and Q (1 ≤ N, Q ≤ 105)  
Line 2: N integers A[i]  
Next Q lines: Two integers L and R (1-based indexing)

📤 Output:

Q lines: each line contains the sum A[L] + ... + A[R]

🔍 Sample Input:

5 3
1 2 3 4 5
1 3
2 4
3 5

✅ Sample Output:

6
9
12

💡 Output Explanation:

Query 1: 1+2+3 = 6  
Query 2: 2+3+4 = 9  
Query 3: 3+4+5 = 12

🎯 Problem 4: Maximum in Submatrix (2D Array)

Problem Statement:
Given an N×N matrix of integers and a number K, find the maximum value in every K×K submatrix.

📥 Input:

Line 1: Integers N and K (1 ≤ K ≤ N ≤ 500)  
Next N lines: N space-separated integers each

📤 Output:

Print all maximums of K×K blocks, one per line

🔍 Sample Input:

3 2
1 2 3
4 5 6
7 8 9

✅ Sample Output:

5
6
8
9

💡 Output Explanation:

Four 2×2 submatrices:
[1 2]  → max = 5
[2 3]  
[4 5]  → max = 6
[5 6]  
[4 5]  → max = 8
[7 8]  
[5 6]  → max = 9
[8 9]

🔁 Problem 5: Matrix Rotation (2D Array)

Problem Statement:
Rotate a square matrix (N×N) 90 degrees clockwise.

📥 Input:

Line 1: Integer N (1 ≤ N ≤ 1000)  
Next N lines: N integers per row

📤 Output:

The rotated matrix in N lines.

🔍 Sample Input:

3
1 2 3
4 5 6
7 8 9

✅ Sample Output:

7 4 1
8 5 2
9 6 3

💡 Output Explanation:

Rotating the matrix 90° clockwise.


🧠 Problem 1: Count Palindromic Substrings

Problem Statement:
Given a string S, find how many distinct substrings of S are palindromes.

📥 Input:

A single line containing string S (1 ≤ |S| ≤ 1000).

📤 Output:

Print a single integer — the number of palindromic substrings.

🔍 Sample Input:

ababa

✅ Sample Output:

5

💡 Output Explanation:

The palindromic substrings are:
a, b, aba, bab, ababa
Total = 5

🔡 Problem 2: Group Words by Anagram

Problem Statement: Group N strings into sets of anagrams.

📥 Input:

First line: Integer N (1 ≤ N ≤ 1000)
Next N lines: One string per line

📤 Output:

Number of anagram groups.

🔍 Sample Input:

5
listen
silent
enlist
google
gogole

✅ Sample Output:

2

💡 Output Explanation:

Group 1: listen, silent, enlist
Group 2: google, gogole
Total = 2 groups

🔍 Problem 3: Longest Unique Substring

Problem Statement: Find the length of the longest substring without repeating characters.

📥 Input:

A string S (1 ≤ |S| ≤ 105)

📤 Output:

Length of longest unique substring.

🔍 Sample Input:

abcabcbb

✅ Sample Output:

3

💡 Output Explanation:

Longest substrings without repeats:
abc, bca, cab
Length = 3

🧪 Problem 4: Count Pattern Occurrences

Problem Statement: Count how many times pattern P occurs in string S (including overlaps).

📥 Input:

Line 1: S (1 ≤ |S| ≤ 106)
Line 2: P (1 ≤ |P| ≤ 104)

📤 Output:

Number of occurrences.

🔍 Sample Input:

aaaaa
aaa

Sample Output:

3

💡 Output Explanation:

"aaa" found at:
Index 0 → aaa
Index 1 → aaa
Index 2 → aaa
Total = 3

🧬 Problem 5: Lexicographically Smallest Rotation

Problem Statement: Print the smallest lexicographical rotation of a string.

📥 Input:

One line: a string S (1 ≤ |S| ≤ 1000)

📤 Output:

Smallest rotation of the string.

🔍 Sample Input:

baca

Sample Output:

acab

💡 Output Explanation:

All rotations:
baca, acab, caba, abac
Sorted:
abac, acab, baca, caba
Smallest = acab

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন