β Back to Revisions
Day 8
View Problem βOdd Even Index
π
October 30, 2025π·οΈ Smart Interviews
Complexity Analysis
Brute Force
N/A
Optimized
N/A
Space
N/A
Solutions
Javaβ
import java.util.Scanner;
class Solution {
// O(N) Time, O(N) Space - Optimal (Single Pass)
public static void solve_odd_even_index() {
Scanner scanner = new Scanner(System.in);
if (!scanner.hasNextLine()) return;
String S = scanner.nextLine();
StringBuilder evenChars = new StringBuilder();
StringBuilder oddChars = new StringBuilder();
for (int i = 0; i < S.length(); i++) {
if (i % 2 == 0) {
evenChars.append(S.charAt(i));
} else {
oddChars.append(S.charAt(i));
}
}
System.out.println(evenChars.toString());
System.out.println(oddChars.toString());
scanner.close();
}
public static void main(String[] args) {
solve_odd_even_index();
}
}C++β‘
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// O(N) Time, O(N) Space - Optimal (Single Pass)
void solve_odd_even_index() {
string S;
// Read the entire line as the string
if (!getline(cin, S)) return;
string even_chars = "";
string odd_chars = "";
// Single Linear Pass (O(N))
for (int i = 0; i < S.length(); ++i) {
if (i % 2 == 0) {
even_chars += S[i];
} else {
odd_chars += S[i];
}
}
cout << even_chars << endl;
cout << odd_chars << endl;
}
// O(N) Time, O(N) Space - Brute (Same fundamental approach)
void solve_odd_even_index_brute() {
solve_odd_even_index();
}
int main() {
solve_odd_even_index();
return 0;
}Tags
stringindex-logic
π‘ Revision Tips
- β’ Try solving from memory before checking your solution
- β’ Focus on the approach and pattern, not just the code
- β’ Can you explain the solution to someone else?
- β’ Try implementing in a different language