Question 4 : Amusement Park
Amusement park
In this article we will see InfyTQ Sample Coding question. You will find solution of InfyTQ Coding question in different programming language on this page.
Problem Statement
Aashay loves to go to WONDERLA , an amusement park. They are offering students who can code well with some discount. Our task is to reduce the cost of the ticket as low as possible.
They will give some k turns to remove the cost of one ticket where the cost of tickets are combined and given as string.Help Aashay in coding as he is not good in programming and get a 50% discount for your ticket.
Constraints:
- 1 <= number of tickets <= 10^5
- 1 <= K <= number of tickets
Input Format for Custom Testing:
- The first line contains a string,Tickets, denoting the given cost of each ticket.
- The next line contains an integer, K, denoting the number of tickets that is to be removed.
Sample Cases:
Sample Input 1
203
3Sample Output 1
0
C++
Python
Java
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int smallestNumber (string num, int k)
{
if(num.length()<=k)
return 0;
unordered_map<char,int> pos;
for(int i=0;i<num.length();i++)
{ pos[num[i]]=i;}
string temp=num;
sort(num.begin(),num.end());
string ans=num.substr(0,num.length()-k);
vector<int> v;
for(int i=0;i<ans.length();i++)
v.push_back(pos[ans[i]]);
sort(v.begin(),v.end());
string ret;
for(int i=0;i<v.size();i++)
{ret+=temp[v[i]];}
int final=stoi(ret);
return final;
}
int main()
{
string s;
cin >> s;
int k;
cin >> k;
int ans;
cout<<smallestNumber(s,k)%(int)(pow(10,9)+7);
return 0;
}
Python
import sys
n=input()
k=int(input())
n1=len(n)
if len(n)<=k:
print(0)
sys.exit()
a=''
i=0
while i<(n1-1) and k>0:
if int(n[i])>int(n[i+1]):
i+=1
k-=1
continue
else:
a+=n[i]
i+=1
a+=n[i]
i+=1
if k>0:
a=a[:-k]
if i<=(n1-1):
while i<n1:
a+=n[i]
i+=1
print(int(a)%((10**9)+7))
Java
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s =sc.nextLine();
int k = sc.nextInt();
int ans;
System.out.println(smallestNumber(s,k)%(int)(1e9+7));
}
static int smallestNumber(String num,int k){
if(num.length()<=k) return 0;
HashMap<Character,Integer> pos=new HashMap<>();
for(int i=0;i<num.length();i++){
pos.put(num.charAt(i),i);
}
String temp = num;
num=sortString(num);
String ans=num.substring(0,num.length()-k);
ArrayList<Integer> v=new ArrayList<>();
for(int i=0;i<ans.length();i++){
v.add(pos.get(ans.charAt(i)));
} Collections.sort(v);
String ret="";
for(int i=0;i<v.size();i++){
ret+=temp.charAt(v.get(i));
} int result=Integer.parseInt(""+ret);
return result;
}
public static String sortString(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
}
Login/Signup to comment