0% found this document useful (0 votes)
8 views13 pages

Minimum Jumps to Array End

The document contains four distinct programming problems, each with a problem statement, examples, and solutions in Python and Java. Problem 1 involves finding the minimum number of jumps to reach the end of an array, Problem 2 counts distinct characters appearing more than once in a string, Problem 3 removes duplicate characters from a string, and Problem 4 returns the frequency of each distinct element in an array. Each problem includes test cases to illustrate the expected output.

Uploaded by

PrashanthReddy
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)
8 views13 pages

Minimum Jumps to Array End

The document contains four distinct programming problems, each with a problem statement, examples, and solutions in Python and Java. Problem 1 involves finding the minimum number of jumps to reach the end of an array, Problem 2 counts distinct characters appearing more than once in a string, Problem 3 removes duplicate characters from a string, and Problem 4 returns the frequency of each distinct element in an array. Each problem includes test cases to illustrate the expected output.

Uploaded by

PrashanthReddy
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

✅ Problem 1: Jump Game – Minimum Jumps to Reach End

Last Updated: 23 Jul, 2025

Problem Statement:

Given an array arr[] of non-negative numbers. Each number represents the maximum number of
steps you can jump forward from that position.

If arr[i] = 3, you can jump to index i + 1, i + 2, or i + 3.

If arr[i] = 0, you cannot move forward from that position.

Your task is to find the minimum number of jumps needed to move from the first position to the
last position of the array.

Note: Print -1 if the last index cannot be reached.

Examples:

Example 1:

Input: 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9

Output: 3

Explanation:

Jump 1 → 3

Jump 3 → 9

Jump 9 → end

Example 2:

Input: 1, 4, 3, 2, 6, 7

Output: 2

Explanation:

Jump from index 1 → 2

Jump from index 2 → end


Example 3:

Input: 0, 10, 20

Output: -1

Explanation:

You cannot move from index 0.

Testcases:

Test case 1:

Input: 2,3,1,1,4

Output: 2

Explanation:

0->1->4

Test case 2:

Input: 1,2,0,3,0,1

Output: 1

Explanation: Stuck at index 2

Test case 3:

Input: 3, 4, 2, 1, 0, 2, 3

Output:4

Explanation:0->1->6

Test case 4:

Input:1,1,1,1,1

Output: 4

Explanation: Jump 1 step every time

Test Case 5:

Input: 5,1,0,0,0,2

Output: 1
Explanation: Jump directly from index 0 to last

Python:

def min_jumps(arr):

n = len(arr)

if n <= 1:

return 0

if arr[0] == 0:

return -1

jumps = 0

maxReach = arr[0]

step = arr[0]

for i in range(1, n):

if i == n - 1:

return jumps + 1

maxReach = max(maxReach, i + arr[i])

step -= 1

if step == 0:

jumps += 1

if i >= maxReach:

return -1

step = maxReach - i

return -1
arr = list(map(int, input().split()))

print(min_jumps(arr))

Java:

import [Link].*;

public class Main {

public static int minJumps(int[] arr) {

int n = [Link];

if (n <= 1) return 0;

if (arr[0] == 0) return -1;

int maxReach = arr[0];

int step = arr[0];

int jumps = 0;

for (int i = 1; i < n; i++) {

if (i == n - 1)

return jumps + 1;

maxReach = [Link](maxReach, i + arr[i]);

step--;

if (step == 0) {

jumps++;

if (i >= maxReach)

return -1;

step = maxReach - i;

}
return -1;

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

String[] input = [Link]().split(" ");

int[] arr = new int[[Link]];

for (int i = 0; i < [Link]; i++)

arr[i] = [Link](input[i]);

[Link](minJumps(arr));

✅ Problem 2: Count Characters Appearing More Than Once

Problem Statement:

You are given a string containing lowercase English letters.

Your task is to count how many distinct characters in the string appear more than once.

Examples:

Example 1:

Input: abbcada

Output: 2

Explanation:

a appears 3 times

b appears 2 times

c and d appear once


Hence, there are 2 characters (a, b) with frequency > 1.

Example 2:

Input: xyz

Output: 0

Explanation:

All characters occur only once.

Test Cases:

Test Case1:

Input: abbcada

Output: 2

Explanation: a->3 times, b->2 times

Test cases2:

Input: xyz

Output: 0

Explanation: all unique

Test case 3:

Input: aabbccdd

Output:4

Explanation:a,b,c,d all repeat

Test case 4:

Input: aaaaa

Output:1

Explanation: Only ‘a’ repeats

Test case 5:

Input: abacbcdef
Output:3

Explanation:a,b,c repeat

Python:

s = input()

freq = {}

for ch in s:

freq[ch] = [Link](ch, 0) + 1

count = 0

for v in [Link]():

if v > 1:

count += 1

print(count)

Java:

import [Link].*;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

String s = [Link]();

HashMap<Character, Integer> map = new HashMap<>();

for (char c : [Link]()) {

[Link](c, [Link](c, 0) + 1);

int count = 0;

for (int freq : [Link]()) {

if (freq > 1) count++;


}

[Link](count);

✅ Problem 3: Remove Duplicates From a String

Last Updated: 11 Sep, 2024

Problem Statement:

Given a string s that may contain lowercase and uppercase letters, remove all duplicate
characters and return the resultant string.

The order of remaining characters must remain the same as in the original string.

Examples:

Example 1:

Input: s = geeksforgeeks

Output: geksfor

Explanation:

Duplicate characters (e, k, g, s) are removed.

Example 2:
Input: s = HappyNewYear

Output: HapyNewYr

Explanation:

Duplicates (p, e, a) are removed.

Test Cases:

Test case 1:

Input: geeksforgeeks

Output: geksfor

Test case 2:

Input:HappyNewYear

Output:HapyNewYr

Test case 3:

Input :aaaaaa

Output:a

Test case 4:

Input:Programming

Output:Progamin

Test case 5:

Input:AbBaBcC

Output:Abc

Python:

s = input()

seen = set()

result = ""
for ch in s:

if ch not in seen:

[Link](ch)

result += ch

print(result)

Java:

import [Link].*;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

String s = [Link]();

HashSet<Character> seen = new HashSet<>();

StringBuilder result = new StringBuilder();

for (char ch : [Link]()) {

if (![Link](ch)) {

[Link](ch);

[Link](ch);

[Link]([Link]());

✅ Problem 4: Counting Frequencies of Array Elements

Last Updated: 26 Jul, 2025

Problem Statement:
Given an array arr[] of non-negative integers which may contain duplicate elements.

Return the frequency of each distinct element in the array.

Examples:

Example 1:

Input: 10, 20, 10, 5, 20

Output: [[5, 1], [10, 2], [20, 2]]

Explanation:

5 occurs 1 time

10 occurs 2 times

20 occurs 2 times

Example 2:

Input: 10, 20, 20

Output: [[10, 1], [20, 2]]

Testcases:

Test case 1:

Input: 10, 20, 10, 5, 20

Output: [[5, 1], [10, 2], [20, 2]]

Test case 2:

Input: 10, 20, 20

Output: [[10, 1], [20, 2]]

Test Case 3:

Input: 1, 1, 1, 1

Output: 4

Test Case 4:

Input: 4,5,6,4,5,4

Output:[[4,3],[5,2],[6,1]]
Test case 5:

Input: 9,8,7,7,8,9,9

Output: [[7,2],[8,2],[9,3]]

Python:

arr = list(map(int, input().split(',')))

freq = {}

for num in arr:

freq[num] = [Link](num, 0) + 1

result = []

for key in sorted([Link]()):

[Link]([key, freq[key]])

print(result)

Java:

import [Link].*;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

String[] input = [Link]().split(" ");

HashMap<Integer, Integer> map = new HashMap<>();

for (String s : input) {

int num = [Link](s);

[Link](num, [Link](num, 0) + 1);

ArrayList<Integer> keys = new ArrayList<>([Link]());

[Link](keys);
ArrayList<ArrayList<Integer>> result = new ArrayList<>();

for (int key : keys) {

ArrayList<Integer> pair = new ArrayList<>();

[Link](key);

[Link]([Link](key));

[Link](pair);

[Link](result);

Common questions

Powered by AI

When handling both uppercase and lowercase letters, the main challenge is deciding how to treat them—either as the same character or distinct ones. If treated as distinct, the frequency count will differentiate 'A' from 'a'. Handling this distinction requires a consistent decision on case sensitivity, potentially leading to incorrect counts if not addressed, especially if case distinctions are not relevant to the task .

The frequency counting technique is similar for arrays and strings: both involve maintaining a dictionary or map to track counts. For 'aabbccdd', iterate over each character, incrementing its count in the dictionary. Each unique element's count in the dictionary reflects its frequency, resulting in a map showing 'a', 'b', 'c', 'd' each appearing twice .

In the input array '1,2,0,3,0,1', a potential failure occurs due to being trapped at position 2 where the element is 0, blocking further progress. A solution must handle such scenarios by keeping track of the maximum reachable index. If a zero is encountered and the current index exceeds the maximum reachable index without further steps, it signifies an unreachable end, and the function should return -1 .

In the string 'abbcada', the characters 'a' and 'b' appear more than once: 'a' appears 3 times and 'b' appears 2 times. Other characters, 'c' and 'd', appear only once. Therefore, there are 2 distinct characters ('a' and 'b') that appear more than once, hence the function returns 2 .

The method involves creating a map to count occurrences of each unique value as you iterate through the array '9,8,7,7,8,9,9'. The next step is to extract the key-value pairs, sort them based on keys, and then format them as a list of frequency pairs. Sorting aids in presenting the results in an ordered manner, as seen in [[7,2],[8,2],[9,3]], facilitating immediate understanding and further processing .

The computational strategy involves iterating over each character of the string 'HappyNewYear' while using a set to track characters that have already been encountered. For each character, check if it has been seen before; if not, add it to the result string and mark it as seen. This ensures the order of characters is maintained while duplicates are removed, resulting in 'HapyNewYr' .

The solution to determine the minimum number of jumps to reach the end of an array involves iterating through the array while maintaining the current maximum reach and the number of steps left. If you reach the end of the array during the loop, return the jump count incremented by one. If you can't proceed further from the current position without jumping, increment the jump counter and renew the steps based on the maximum reach calculated so far. If the current index exceeds the maximum reachable index, return -1, indicating the end is unreachable .

To remove duplicates from the string 'geeksforgeeks', use an iterative approach combined with a set to track already seen characters. By adding each new character to a set and checking whether it already exists, duplicates are automatically filtered out. The importance of the set stems from its efficient membership test and unique storage property, leading to a resultant string 'geksfor', keeping the first occurrences in their original order .

A simple greedy approach might be insufficient because it can choose the local optimum path at each step without considering future consequences, potentially leading to dead ends or suboptimal solutions. Such approaches might not consider that jumping the maximum allowable steps from the current position does not guarantee the fewest jumps to the end. To find the minimum jumps, the algorithm must dynamically evaluate possible paths and maintain maximum possible reach at each step to make globally optimal decisions .

Sorting the keys before generating the output frequency list ensures that the output is in a consistent and expected format, making it easier to read and interpret the results. It helps in scenarios where the order of elements matters, such as when combining frequency results from multiple sources or for further processing that assumes sorted input .

You might also like