Sir, I did sorting in a different array not in original for checking is there any element which is greater then k.
I tried without array, can anyone tell me what is wrong in this code.
#include
#include<stdio.h>
#define ll long long
using namespace std;
int main() {
ll t;
cin>>t;
while(t--)
{
ll n,k;
cin>>n>>k;
ll temp,sum=0,ans=0;
ll flag=0;
for(ll i=0;i<n;i++)
{
cin>>temp;
if(temp>k)
{
cout<<-1<<"\n";
flag=1;
break;
}
sum+=temp;
if(sum>k){
ans++;
sum=temp;
}
}
if(flag==1)
continue;
if(sum>0)
ans++;
cout<<ans<<"\n";
}
return 0;
}
Can any one help me to find my mistake in this code???
#include
using namespace std;
int main(){
int t;scanf("%d",&t);
while(t--){
int n,k; scanf("%d%d",&n,&k);
long int sum=0;int noOfTrip=0;int flag=0;
for(int i=0;i<n;i++){
int num;
scanf("%d",&num);
if(num>k){
flag=1;
break;
}
sum+=num;
if(sum==k){
noOfTrip+=1;
sum=0;
}
else if(sum>k){
noOfTrip+=1;
sum=0;
sum+=num;
if(sum==k||i==n-1){
noOfTrip+=1;sum=0;
}
}
else if(i+1==n)
noOfTrip++;
}
if(flag==1)
printf("-1\n");
else printf("%d\n",noOfTrip);
}
return 0;
}
Please correct. My code is not getting accepted, though it passes the test-cases. It is showing as WA.
#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main(){
ios_base::sync_with_stdio(true);
cin.tie(NULL);cout.tie(NULL);
ll t;cin>>t;while(tā){
ll n,k;
cin>>n>>k;
ll count =0;
ll a[n]; for(auto &i:a) cin>>i;
for(ll i=0;i<n;i++){
if(a[i]<=k){
ll sum = a[i];
while(sum<=k){
i++;
sum+=a[i];
}
count++;
i--;
}
if(a[i]>k){
if(i==0) {count = -1 ; }
break; }
}
cout<<count;
cout<<endl;
}
return 0;
}
In line 16, just remove if(i==0) condition and it will work.
if (a[i] > k) {
count = -1;
break;
}
i am unable to figure out whats the flaw in my code can any one help me
import java.util.;
import java.lang.;
import java.io.*;
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int i =0;i<T;i++){
int n = sc.nextInt();
int k = sc.nextInt();
int[] w = new int[n];
boolean b = true;
for(int j=0;j<n;j++){
w[j] = sc.nextInt();
}
for(int j = 0;j<n;j++){
if(w[j] > k){
System.out.println(-1);
b = false;
break;
}
}
if(b == true){
getCount(w,n,k);
}
}
}
public static void getCount(int[] w, int n,int k){
int tot = 0;
int count = 0;
for(int i = 0;i<n;i++){
tot += w[i];
if(tot>k){
tot =w[i];
count++;
}
}
System.out.println(++count);
}
}
keep up the hard work, this mistake will only improve you
Why this is giving wrong answer?
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(tā)
{
int n,k;
cin>>n>>k;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int max=*max_element(arr, arr + n);
int h=0;
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
if(sum>k)
{ sum=arr[i];
h++;
}
}
if(max>k){
cout<<-1<<endl;
}
else{
cout<<h+1;
}
}
return 0;
}
while submitting it is showing the wrong answer and I think itās working fine can anyone tell me whatās wrong with it any reply will be helpfulā¦
#include<bits/stdc++.h>
using namespace std;
int main(){
int T;
cin>>T;
while(Tā){
int n,k;
cin>>n>>k;
int a[n],sum=0,count=0;
for(int i=0;i<n;i++){
cin>>a[i];
sum+=a[i];
}
//cout<<sum<<endl;
if(sum<=k){
count++;
}else{
for(int i=0;i<n;i++){
if(a[i]>k){
count = -1;
break;
}
else if(sum-a[i]<=k){
count+=2;
break;
}
else{
sum = sum-a[i];
count++;
}
}
}
cout<<count<<endl;
}
return 0;
}
for i in range(int(input())):
n,k=map(int,input().split())
w=list(map(int,input().split()))
p=0
count=0
if w[0]>k:
print("-1")
for j in range(n):
if p+w[j]<=k:
p+=w[j]
else:
count+=1
p=0
j-=1
count+=1
print(count)
Any idea whats wrong with this?
@anyamanask you were not taking the whole input, thatās why you got the wrong answer, i tried to submit your code , and got ac , hereās the link CodeChef: Practical coding for everyone
and in setterās code there is no need to check the sum>0 in last step , just print ans+1.