PROBLEM LINK:
Author: Jatin Nagpal
Tester: Raja Vardhan Reddy
Editorialist: Akash Bhalotia
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
Given a person’s income, calculate their net income based on the tax rates given for different income brackets.
QUICK EXPLANATION:
show
Net income = Total income - Total Tax
Using if-else ladder, find the person’s income bracket. Calculate the tax for this slab and add the taxes for all previous slabs. Subtract the total tax from the income to get the net income.
EXPLANATION
show
Let’s calculate the tax the person has to pay, based on their income.
-
The income is less than or equal to Rs. 250,000:
The person doesn’t have to pay any tax. Thus, their tax is Rs. 0 -
The income is greater than Rs. 250,000, but less than or equal to Rs. 500,000:
Their tax will be 5% of the income above 250,000,
\therefore tax = 5% of (N-250,000) -
The income is greater than Rs. 500,000, but less than or equal to Rs. 750,000:
Their tax will be 10% of the income above 500,000 + tax for the slab 250,001 to 500,000
\therefore tax =
10% of (N-500,000) +
5% of (500,000-250,000) -
The income is greater than Rs. 750,000, but less than or equal to Rs. 1,000,000:
Their tax will be 15% of the income above 750,000 + taxes for previous slabs
\therefore tax =
15% of (N-750,000) +
10% of (750,000-500,000)+
5% of (500,000-250,000) -
The income is greater than Rs. 1,000,000, but less than or equal to Rs. 1,250,000:
Their tax will be 20% of the income above 1,000,000 + taxes for previous slabs
\therefore tax =
20% of (N-1,000,000) +
15% of (1,000,000-750,000) +
10% of (750,000-500,000)+
5% of (500,000-250,000) -
The income is greater than Rs. 1,250,000, but less than or equal to Rs. 1,500,000:
Their tax will be 25% of the income above 1,250,000 + taxes for previous slabs
\therefore tax =
25% of (N-1,250,000) +
20% of (1,250,000-1,000,000) +
15% of (1,000,000-750,000) +
10% of (750,000-500,000)+
5% of (500,000-250,000) -
The income is greater than Rs. 1,500,000:
Their tax will be 30% of the income above 1,500,000 + taxes for previous slabs
\therefore tax =
30% of (N-1,500,000) +
25% of (1,500,000-1,250,000) +
20% of (1,250,000-1,000,000) +
15% of (1,000,000-750,000) +
10% of (750,000-500,000)+
5% of (500,000-250,000)
Using a simple if-else ladder, we can easily identify the income bracket a person belongs to, and calculate their total tax, as explained above.
To calculate x % of y, we can simply perform y*\frac{x}{100}.
After calculating their total tax, we can subtract it from the person’s total income, to obtain their net income. As N is a multiple of 100, using integer data types, instead of floating-point data types, works too.
Thus, to solve the problem,
- Identify the income bracket using if-else ladder,
- Obtain the total tax by calculating the tax for this bracket, and adding it with the taxes for lower brackets,
- Net income = Total income - Total Tax
SOLUTIONS:
Setter
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define MP make_pair
#define PB push_back
#define ll long long
// #define int long long
#define f(i,x,n) for(int i=x;i<n;i++)
#define ld long double
const int mod=1000000007;
// const int INF=1e18;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t;
cin>>t;
while(t--)
{
int n,tax=0;
cin>>n;
if(n>250000)
tax += ( (min(500000,n)-250000)*5 )/100;
if(n>500000)
tax += ( (min(750000,n)-500000)*10 )/100;
if(n>750000)
tax += ( (min(1000000,n)-750000)*15 )/100;
if(n>1000000)
tax += ( (min(1250000,n)-1000000)*20 )/100;
if(n>1250000)
tax += ( (min(1500000,n)-1250000)*25 )/100;
if(n>1500000)
tax += ( (n-1500000)*30 )/100;
cout<<n-tax<<'\n';
}
return 0;
}
Tester
//raja1999
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
//setfill - cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x) cout<<fixed<<val; // prints x digits after decimal in val
using namespace std;
using namespace __gnu_pbds;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
#define int ll
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//std::ios::sync_with_stdio(false);
main(){
std::ios::sync_with_stdio(false); cin.tie(NULL);
int t,i;
cin>>t;
vector<pair<int,int> > vec;
for(i=0;i<6;i++){
vec.push_back({i*250000,(i+1)*250000});
}
vec.push_back({i*250000,inf});
while(t--){
int n,ans,diff;
cin>>n;
ans=n;
for(i=0;i<vec.size();i++){
diff=min(vec[i].second,n)-vec[i].first;
ans-=diff*i*5/100;
if(n<=vec[i].second){
break;
}
}
cout<<ans<<endl;
}
return 0;
}
Editorialist
CodeChef: Practical coding for everyone
CodeChef: Practical coding for everyone
//created by Whiplash99
import java.io.*;
import java.util.*;
class A
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,N;
int T=Integer.parseInt(br.readLine().trim());
StringBuilder sb=new StringBuilder();
while(T-->0)
{
N=Integer.parseInt(br.readLine().trim());
int tax=0;
if(N>250_000)
tax+=(Math.min(N,500_000)-250_000)*5/100;
if(N>500_000)
tax+=(Math.min(N,750_000)-500_000)*10/100;
if(N>750_000)
tax+=(Math.min(N,1_000_000)-750_000)*15/100;
if(N>1_000_000)
tax+=(Math.min(N,1_250_000)-1_000_000)*20/100;
if(N>1_250_000)
tax+=(Math.min(N,1_500_000)-1_250_000)*25/100;
if(N>1_500_000)
tax+=(N-1_500_000)*30/100;
int net=N-tax;
sb.append(net).append("\n");
}
System.out.println(sb);
}
}
Feel free to share your approach if it differs. You can ask your doubts below. Please let me know if something’s unclear. I would LOVE to hear suggestions