100% found this document useful (1 vote)
3K views

Juspay Coding Question

The document discusses three problems and their solutions in C++ and Java. The first problem involves sorting a string based on character frequency. The second problem involves removing stars and adjacent characters from a string. The third problem finds the longest consecutive sequence of numbers in a sorted list.

Uploaded by

Deepu Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
3K views

Juspay Coding Question

The document discusses three problems and their solutions in C++ and Java. The first problem involves sorting a string based on character frequency. The second problem involves removing stars and adjacent characters from a string. The third problem finds the longest consecutive sequence of numbers in a sorted list.

Uploaded by

Deepu Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Q1 :Problem Statement :

Shivina wants to design a game for kids for their enjoyment as well as their knowledge . She
takes the string from the children and sorts the string by the property such that the character
which has the highest frequency will come first and the character which has the least frequency
will come last. Now you have to write a code for that and help shivina for getting the sorted
string according to the frequency which will have to be higher to lower.
Write an algorithmic code for this.
Constraint

1 <= s.length <= 5 * 10^5


s consists of uppercase and lowercase English letters and digits

Input : s = "cccaaa"

Output : "cccaaa"

Explanation :
a and c have same frequency 3 , but a come before c in alphabet.

Input1 : s = "ccaaa"

Output1 : "aaacc"

Solution in C++:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
unordered_map<char,int> m;

for(int i=0;i<s.size();i++){
m[s[i]]++;
}

vector<pair<int,char>> vect;
for(auto v : m){
vect.push_back({ v.second,v.first});
}

sort(vect.begin(),vect.end(),greater<pair<int,char>>());

string str;
for(auto v: vect){
string temp(v.first,v.second);
str=str+ temp;
}
cout<<str;
return 0;
}

Solution in Java:

import java.util.*;

public class Main {

public static String solution(String str) {


Map<Character, Integer> Map = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (!Map.containsKey(ch)) {
Map.put(ch, 1);
} else {
Map.put(ch, Map.get(ch) + 1);
}
}
List<Character> chars = new ArrayList<>(Map.keySet());
Collections.sort(chars, new Comparator<Character>() {
public int compare(Character ch1, Character ch2) {
return Map.get(ch2) - Map.get(ch1);
}
});
StringBuilder sb = new StringBuilder();
for (char ch : chars) {
int freq = Map.get(ch);
for (int i = 0; i < freq; i++) {
sb.append(ch);
}
}
return sb.toString();
}

public static void main(String[] args) {


Scanner scan=new Scanner(System.in);
String str = scan.nextLine();
String sortStr = solution(str);
System.out.println(sortStr);
}
}

Q2 : Problem Statement :

Ram, who have scares to stars, found himself constantly bothered by his friend's liking for them.
One day, his friend presented him with a game designed to help overcome his fear. In this
game, Ram is presented with a string containing a mix of characters and stars. His objective is
to select a star and eliminate both the star itself and the nearest non-star character to its left.
This process continues until there are no stars left in the string. Once all stars are removed,
Ram must return the remaining string. Can you help Ram by writing an algorithmic code ?

Constraints :
● 1<= s.length <=105
● S consists of lowercase english letters and star *

Input 0 :
"leet**cod*e"

Output 0 :
"Lecoe"

Input 1 :
"erase*****"

Output 1 :
""

Input 2 :
"leet**cod*"
Output 2 :
"Leco"

Input 4 :
"erase***"

Output 4 :
“er”

Solution in C++:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
string s;
cin>>s;
string ans;

for(int i=0; i<s.size(); i++){

if(ans.size() and (ans.back()!= '*' and s[i]== '*')){


ans.pop_back();
}
else ans.push_back(s[i]);
}

for(int i=0;i<ans.size();i++){
cout<<ans[i];
}
return 0;
}

Solution in Java:
import java.util.*;

public class Main {


public static String solution(String str){

Stack<Character> stack=new Stack<>();


for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(ch=='*')
stack.pop();
else
stack.push(ch);
}

return stack.toString();
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
String str=scan.nextLine();
System.out.println(solution(str));
scan.close();
}
}

Q3 : Problem Statement :

The school principal gathered students from different classes. They had to say their roll
numbers and line up in order from smallest to largest. They were to count consecutive
subsequences in this line. The main goal was to identify the longest consecutive sequence of 'n'
initial roll numbers. This activity was to make school fun and help students practice math and
organization
Your task is to write an algorithmic code to find the longest consecutive sequence.
Definition of consecutive sequence: Numbers that follow each other continuously in the order
from smallest to largest are called consecutive numbers.

Constraints :

0 <= nums.length <= 10^5

-10^9 <= nums[i] <= 10^9

Inputs :
6 = number of elements
[100,4,200,1,3,2] = elements

Output :

Explanation :
In ascending order : 1,2,3,4,100,200
Maximum consecutive sequence : 1,2,3,4 = 4

Solution in C++:

#include <bits/stdc++.h>
using namespace std;

int main() {
int n;
cin>>n;
vector<int>nums;

for(int i=0;i<n;i++){
int t;
cin>>t;
nums.push_back(t);
}
vector<int> ans;
int counter = 1;
int maxi = INT_MIN;

if(nums.size() == 0)
return 0;
else if(nums.size() == 1){
cout<<1;
return 0;
}

sort(nums.begin(),nums.end());
ans.push_back(nums[0]);
for(int i = 1;i < nums.size();i++){
if(ans.back() + 1 == nums[i]){
ans.push_back(nums[i]);
counter++;
}else if(ans.back()+1 != nums[i] && ans.back() != nums[i]){
ans.clear();
ans.push_back(nums[i]);
maxi = max(maxi,counter);
counter = 1;
}
maxi = max(maxi, counter);
}

cout<<maxi;

return 0;
}

Solution in Java:

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> nums = new ArrayList<>();
for (int i = 0; i < n; i++) {
int t = sc.nextInt();
nums.add(t);
}

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


int counter = 1;
int maxi = Integer.MIN_VALUE;

if (nums.size() == 0) {
System.out.println(0);
return;
} else if (nums.size() == 1) {
System.out.println(1);
return;
}

Collections.sort(nums);
ans.add(nums.get(0));
for (int i = 1; i < nums.size(); i++) {
if (ans.get(ans.size() - 1) + 1 == nums.get(i)) {
ans.add(nums.get(i));
counter++;
} else if (ans.get(ans.size() - 1) + 1 != nums.get(i) && ans.get(ans.size() - 1) !=
nums.get(i)) {
ans.clear();
ans.add(nums.get(i));
maxi = Math.max(maxi, counter);
counter = 1;
}
maxi = Math.max(maxi, counter);
}

System.out.println(maxi);
}
}

You might also like