What's wrong with my code

#include
#include
using namespace std;
long long fast_exp(int base, int exp) {
long long MOD=1000000007;
long long res=1;
while(exp>0) {
if(exp%2==1) res=(resbase)%MOD;
base=(base
base)%MOD;
exp/=2;
}
cout<< res%MOD<<endl;
}
main()
{
int t,base=2,exp;
cin>>t;
while(t–)
{
cin>>exp;
fast_exp(base,exp);
}

}
//what is exactly wrong with my code??

u haven’t included the header files

i did include iostream and cmath

what’s wrong with my code?
https://www.codechef.com/viewsolution/29022601

You can use in-built sort function of C++.(It can save a lot of time typing)

#include<iostream>
#include<algorithms>
using namespace std;

int main(){
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    return 0;
}

okay next, i feel your solution is correct however you forgot to account of integer overflow as the sum might be larger than integer so use long long int or long long to store the sums.

1 Like

which question is this?

https://www.codechef.com/viewsolution/25437060
however refer to this for a better idea.
I did it in a shorter way taking advantage of in built functions such as min,max and abs.

even with long long also, it is not working

  • Sort all numbers.
  • Find the sum of all numbers. Let it be S .
  • Find the sum of first K numbers. Let it be S1 .
  • Find the sum of last K numbers. Let it be S2 .
  • Output max(abs(S1 − (S − S1)), abs(S2 − (S − S2))) as an answer.
    this is the ideal solution imo.
    Try this :slight_smile:

#include < iostream>
#include < algorithm>
using namespace std;
main()
{
int t;
cin>>t;
while(t–)
{
long long int n,k,s=0,s1=0,d;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
for(int i=0;i<k;i++) s=s+a[i];
for(int i=k;i<n;i++) s1=s1+a[i];
d=s1-s;
cout<<d<<endl;
}
}

this shows an error too .Whats wrong with my solution

would be really glad to get some help :smiley:

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

i even format it but it didn’t work

Thanks. It worked. :innocent:. But still couldn’t understand the problem in earlier solution.