Question 2: Self Sufficient
Self Sufficient
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
Abhijeet is one of those students who tries to get his own money by part time jobs in various places to fill up the expenses for buying books. He is not placed in one place, so what he does, he tries to allocate how much the book he needs will cost, and then work to earn that much money only. He works and then buys the book respectively. Sometimes he gets more money than he needs so the money is saved for the next book. Sometimes he doesn’t. In that time, if he has stored money from previous books, he can afford it, otherwise he needs money from his parents.
Now His parents go to work and he can’t contact them amid a day. You are his friend, and you have to find how much money minimum he can borrow from his parents so that he can buy all the books.
He can Buy the book in any order.
Function Description:
Complete the function with the following parameters:
Name | Type | Description |
N | Integer | How many Books he has to buy that day. |
EarnArray[ ] | Integer array | Array of his earnings for the ith book |
CostArray[ ] | Integer array | Array of the actual cost of the ith book. |
Constraints:
- 1 <= N <= 10^3
- 1 <= EarnArray[i] <= 10^3
- 1 <= CostArray[i] <= 10^3
Input Format:
- First line contains N.
- Second N lines contain The ith earning for the ith book.
- After that N lines contain The cost of the ith book.
Output Format:
The minimum money he needs to cover the total expense.
Sample Input 1:
3
[3 4 2]
[5 3 4]
Sample Output 1:
3
Explanation:
At first he buys the 2nd book, which costs 3 rupees, so he saves 1 rupee. Then he buys the 1st book, that takes 2 rupees more. So he spends his stored 1 rupee and hence he needs 1 rupee more. Then he buys the last book.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, ans =0,sum=0;
cin>>n;
vector<int> arr1(n),arr2(n);
for(int i=0;i<n;i++) cin>>arr2[i];
for(int i=0;i<n;i++) cin>>arr1[i];
for(int i=0;i<n;i++) arr2[i]-=arr1[i];
sort(arr2.begin(),arr2.end(),greater<int>());
for(int i=0;i<n;i++)
{
sum+=arr2[i];
if(sum<0)
{ans+=abs(sum);sum=0;}
}
cout<<ans;
}
n=int(input())
L1=[0] *n
L2=[0]*n
for i in range(n):
L2[i]=int(input())
for i in range(n):
L1[i]=int(input())
for i in range(n):
L2[i]=L2[i]-L1[i];
L2.sort()
su=0
ans=0
for i in range(n):
su=su+L2[i]
if su<0:
ans = ans + abs(su)
su=0
print(ans)
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
int n=sc.nextInt();
int arr1[]= new int[n];
int arr2[]= new int[n];
for(int i=0; i<n; i++)
arr2[i]=sc.nextInt();
for(int i=0; i<n; i++)
arr1[i]=sc.nextInt();
for(int i=0; i<n; i++)
arr2[i] -= arr1[i];
Arrays.sort(arr2);
int sum=0, ans=0;
for(int i=n-1; i>=0; i--)
{
sum+=arr2[i];
if(sum<0)
{
sum = -sum;
ans= ans +sum ;
sum=0;
}
}
System.out.println(ans);
}
}
n = int(input())
a = list(map(int,input().split(” “)))
b = list(map(int,input().split(” “)))
x = []
for i in range(len(a)):
x.append(b[i]-a[i])
print(sum(x))
Before ans print we do
ans=ans-sum;
Bcz if sum is positive at last we need to add.
publish more input and output examples