SLAB - Editorial

PROBLEM LINK:

Practice
Contest

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.

  1. 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

  2. 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)

  3. 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)

  4. 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)

  5. 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)

  6. 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)

  7. 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,

  1. Identify the income bracket using if-else ladder,
  2. Obtain the total tax by calculating the tax for this bracket, and adding it with the taxes for lower brackets,
  3. 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

https://www.codechef.com/viewsolution/29877591
https://www.codechef.com/viewsolution/29878193

//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 :slight_smile:

1 Like

import java.util.;
import java.lang.
;
import java.io.*;

class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner obj = new Scanner(System.in);
int N=0, sal=0,income;
System.out.println(“Enter no of test cases::”);
if(obj.hasNextInt())
N = obj.nextInt();

	for(int i=1;i<=N;i++) {
		int totalTax=0;
		System.out.println("Enter salary"+i);
		if(obj.hasNextInt())
			sal=obj.nextInt();
		
		System.out.println("Salary entered:"+sal);
		if(sal<=250000)
			totalTax=0;
		else
			totalTax+=totalTax;
		
		if(sal>=250001 && sal<=500000)
			totalTax+=(sal-250000)*0.05;
		else if(sal>500000)
			totalTax+=12500;
		
		if(sal>=500001 && sal<=750000)
			totalTax+=(sal-500000)*0.1;
		else if(sal>750000)
			totalTax+=25000;
		
		if(sal>=750001 && sal<=1000000)
			totalTax+=(sal-750000)*0.15;
		else if(sal>1000000)
			totalTax+=37500;
		
		if(sal>=1000001 && sal<=1250000)
			totalTax+=(sal-1000000)*0.2;
		else if(sal>1250000)
			totalTax+=50000;
		
		if(sal>=1250001 && sal<=1500000)
			totalTax+=(sal-1250000)*0.25;
		else if(sal>1500000)
			totalTax+=62500;
		
		if(sal>=1500001)
			totalTax+=(sal-1500000)*0.3;
		
		System.out.println("total tax::"+totalTax);
		income=sal-totalTax;
		System.out.println("Net income::"+income);
	}
	obj.close();
}

}

what is wrong in the code?

You can’t print anything other than the required output of the question. Moreover, the output should be printed in the same format as specified in the question.

For this particular question, it means that you should only print the net income, after deducting tax. I commented the lines where you printed extra statements, and it resulted in AC.

Refer to this: https://www.codechef.com/viewsolution/30073657

I don’t know what’s wrong in my code but it takes infinitely long time to get submitted.

#include <iostream>
using namespace std;
#define slab 250000
int main() {
	long long int T;
	cin>>T;
	if(T>=1&&T<=1000)
	{
	    while(T--)
	    {
    	   long long income,tax=0,net=0;
    	    cin>>income;
    	    if(income<=250000)
    	    {
    	        net+=income-tax;
    	    }
    	    else if(income>250000&&income<=500000)
    	    {
    	        tax=0.05*(income-250000);
    	        net+=income-tax;
    	    }
    	    else if(income>500000&&income<=750000)
    	    {
    	        tax=0.1*(income-500000);
    	        net+=income-tax-0.05*(slab);
    	    }
    	    else if(income>750000&&income<=1000000)
    	    {
    	        tax=0.15*(income-750000);
    	        net+=income-tax-0.05*slab-0.1*slab;
    	    }
    	    else if(income>1000000&&income<=1250000)
    	    {
    	        tax=0.2*(income-1000000);
    	        net+=income-tax-0.05*slab-0.1*slab-0.15*slab;
    	    }
    	    else if(income>1250000&&income<=1500000)
    	    {
    	        tax=0.25*(income-1250000);
    	        net+=income-tax-100000-0.05*slab-0.1*slab-0.15*slab-0.20*slab;
    	    }
    	    else if(income>1500000)
    	    {
    	        tax=0.3*(income-1500000);
    	        net+=income-tax-200000-0.05*slab-0.1*slab-0.15*slab-0.20*slab-0.25*slab;
    	    }
    	    
    	    cout<<net<<endl;
	    }   
	}
	return 0;
}
1 Like

hello bro can u tell me whtas the wrong in my code #include

using namespace std;

int main()

{

int n,i,tax;

float a;

cin>>n;

while(n–)

{

cin>>a; 

if(a<=250000)

{

    tax=a*0;

}

if (a>250000 && a<=500000)  

{   

    tax=(250000*0)+(a-250000)*0.05;

}

if (a>500000 && a<=750000)

{

    tax=(250000*0)+(250000*0.05)+(a-500000)*0.10;

}

if (a>750000 && a<=1000000)

{

    tax=(250000*0)+(250000*0.05)+(250000*0.10)+(a-750000)*0.15;

}

if (a>1000000 && a<=1250000)

{

    tax=(250000*0)+(250000*0.05)+(250000*0.10)+(250000*0.15)+(a-1000000)*0.20;

}

if (a>1250000 && a<=1500000)

{

    tax=(250000*0)+(250000*0.05)+(250000*0.10)+(250000*0.15)+(250000*0.20)+(a-1250000)*0.25;

}

if (a>1500000)

{

    tax=(250000*0)+(250000*0.05)+(250000*0.10)+(250000*0.15)+(250000*0.20)+(250000*0.25)+(a-1500000)*0.30;

}

cout<<a-tax<<endl;

}

}

#include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
long num,a1=12500,a2=25000,a3=37500,a4=50000,a5=345000,tot;
cin>>num;
if(num>1500000)
tot=a1+a2+a3+a4+a5+((num-1500000)*0.3);
else if(num>1250000)
tot=a1+a2+a3+a4+((num-1250000)*0.25);
else if(num>1000000)
tot=a1+a2+a3+((num-1000000)*0.2);
else if(num>750000)
tot=a1+a2+((num-750000)*0.15);
else if(num>500000)
tot=a1+((num-500000)*0.1);
else if(num>250000)
tot=((num-250000)*0.05);
else
tot=0;

   cout<<num-tot<<"\n";
}
return 0;

}

I AM GETTING AC IN THIS SOL PLZ HELP ME OUT WHATS WRONG IN IT