Problem Statement:
Write a program to find the index of the first occurrence of a specific character ch in the given string Str.
Read the input from STDIN and print the output to STDOUT. Do not write arbitrary strings anywhere in the program, as these contribute to the standard output, and test cases will fail.
Constraints:
Input Format:
Output Format:
Sample Input 1:
Eluid
u
Sample Output 1:
2
Explanation 1:
Given, Str = "Eluid". The first occurrence of a specific character ch 'u' is 2, which is printed as the output.
Problem Statement:
(Derived from the provided Input/Output specification)
You are given an m \times n matrix. Your task is to target a specific element at a given row index i and column index j, and increase its value by adding a given integer P. Output the newly updated value of that specific matrix element.
Input Format:
Output Format:
Sample Input 1:
3 2
3 4
16 8
21 89
1 1
500
Sample Output 1:
508
Explanation 1:
The given matrix is:
i \ j | 0 | 1 |
0 | 3 | 4 |
1 | 16 | 8 |
2 | 21 | 89 |
The element at the position M[1][1] = 8, so adding 500 (i.e., 8 + 500) to it, it will become 508.
Problem1 Solution
Topics Involved / Prerequisites
Overview Finding the first occurrence of a character in a string is a fundamental string operation that can be handled using standard library tools. By utilizing C++'s built-in string searching methods, we can efficiently locate the target character without manually writing traversal loops. This approach guarantees minimal code complexity, avoids outputting unwanted arbitrary strings, and perfectly satisfies the problem's strict I/O constraints.
Approach
1. Reading the Input We read the string Str and the character ch from the standard input. Using cin automatically handles the whitespace separation.
2. Utilizingstd::string::find The C++ std::string class has a highly optimized find() method. When we pass our target character ch to Str.find(ch), it scans the string from left to right and immediately returns the 0-based index of its very first appearance. We then print this index directly to the output.
Code Impelementation
#include <iostream>
#include <string>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string str;
char ch;
if (cin >> str >> ch) {
size_t index = str.find(ch);
if (index != string::npos) {
cout << index << "\n";
}
}
return 0;
}Time Complexity
Problem2 Solution
Topics Involved / Prerequisites
Overview The problem requires us to isolate and update a single specific element within a 2D grid based on its exact row and column indices. By storing the input grid in a dynamic 2D vector, we easily maintain the structural relationship of the elements for coordinate-based access. Once the data is loaded into memory, retrieving and mathematically updating the target element becomes an instantaneous, constant-time operation.
Approach
1. Populating the Matrix First, we read the dimensions m (rows) and n (columns). We declare a 2D vector of that exact size. Using a nested loop, we read the subsequent integers from the standard input and place them into their respective row and column positions in our vector.
2. Targeted Modification After the matrix is fully built, the next inputs provide the target coordinates i and j, followed by the value P. We directly access matrix[i][j], add P to it, and output the newly computed value. Because vectors in C++ are 0-indexed and the problem's sample input demonstrates 0-based targeting (e.g., 1 1 targets the 2nd row, 2nd column), we can plug i and j in directly without adjusting them.
C++ Implementation-:
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int m, n;
if (!(cin >> m >> n)) return 0;
vector<vector<int>> matrix(m, vector<int>(n));
// Populate the matrix
for (int r = 0; r < m; ++r) {
for (int c = 0; c < n; ++c) {
cin >> matrix[r][c];
}
}
int i, j, p;
cin >> i >> j;
cin >> p;
int updatedValue = matrix[i][j] + p;
cout << updatedValue << "\n";
return 0;
}Time Complexity