Skip to main content

Breaking The Summation Formula (Part 1)

 Q. f(n) =  - 1 + 2 - 3 + .. + ( - 1) n n . Given n, find out f(n)



Approach(1)- Bruteforce:

1. Calculation sum=n*(n+1)/2
2. loop[i=1,i=n : i+=2] odd+=i
3.ans=sum-2*odd

Code:
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4.  
  5. long long x;
  6. cin>>x;
  7. long long p=(x*(x+1))/2;
  8. long long bad=0;
  9. for(long long i=1;i<=x;i+=2)
  10. bad+=i;
  11. cout<<p-2*bad<<endl;
  12. }

Approach(2)-Greedy:

Basic:
s=1+2+3+4+....+n
Formula: sum=n*(n+1)/2= (n/2) + (n+1).2
here n/2 denotes the summation of all even numbers and (n+1)/2 denotes the summation of all odd numbers.


  • 1.if n is even answer=n/2
        explanation:
        n=6
       -1+2-3+4-5+6 =(2+4+6)-(1+3+5)= 12-9=3


  • if n is odd answer=(n-1)/(2-n)=-(n+1)/2
         explanation:
         n=5
        -1+2-3+4-5 =(2+4)-(1+3+5)=6-9=-3
        We got -1 in the formula because of (-1)^n. here n is odd. that's why we'll
         get -1 for one time always in the case of odd numbers.

Code:
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. long long x,y,n;
  5. cin>>n;
  6. if(n%2==0)cout<<n/2<<endl;
  7. else cout<<-(n+1)/2<<endl;
  8. }


Test-Case Observation:


Comments

Popular posts from this blog

STL-1

  Well, we know that C++ is the most common language recommended by competitive programmers or coders. In competitive programming, we have no time to make programs like sorting, map, searching etc. For these purposes, we use some of the very popular   C++ libraries   to make our code faster and also to save time. C++ STL (Standard Template Library) contains lots of containers which are useful for different purposes. What is STL? It’s a sophisticated and powerful library of template classes and template functions that implement many common data structures and algorithms and forms part of the C++ Standard Library. Why should a C++ programmer be interested in the STL? Because the STL embodies the concept of reusable software components and provides off-the-shelf solutions to a wide variety of programming problems. It is also extensible, in the sense that any programmer can write new software (containers and algorithms, for example), that “fit in” to the STL and work with the...

কোডফোর্সেস বেসিক প্রবলেম লিস্ট ১

IMPLEMENTATION  Problem 1: Codeforces 835A Problem 2: Codeforces 935B Problem 3: Codeforces 868A Problem 4: Codeforces 868B LOOPS (NOT NESTED) Problem 1: Codeforces 839A Problem 2: Codeforces 939A Problem 3: Codeforces 893A Problem 4: Codeforces 935A Problem 5: Codeforces 961A Problem 6: Codeforces 828A Problem 7: Codeforces 867A Problem 8: Codeforces 939B NESTED LOOPS  Problem 1: Codeforces 894A Problem 2: Codeforces 897A Problem 3: Codeforces 822B Problem 4: Codeforces 828B Problem 5: Codeforces 886A (Can also be solved using sorting) Problem 6: Codeforces 907A Problem 7: Codeforces 22B MATH Problem 1: Codeforces 869B  (Uses a loop) Problem 2: Codeforces 875A  (Uses a nested loop) Problem 3: Codeforces 922B  (Uses a nested loop)

কম্পিটিটিভ প্রোগ্রামিং এ হাতেখড়ি

কম্পিটিটিভ প্রোগ্রামিং এ হাতেখড়ি আপনি কম্পিটিটিভ প্রোগ্রামিং শব্দটা যদি আজকেই প্রথম শোনেন তবে নিচে আমার দেওয়া টার্গেটগুলো স্টেপ বাই স্টেপ কমপ্লিট করলে আশা করি কম্পিটিটিভ প্রোগ্রামিং এ হাতেখড়ি হয়ে যাবে। প্রথমে সি/সি++/জাভা (আমি বাকি ল্যাংগুয়েজ রিকমান্ড করিনা) একটা শিখে ফেলুন।নিচের দেওয়া টার্গেট কমপ্লিট করুন। Target 1: • Basic Syntax • Input-output • conditional logic • loops • array • string • functions • some টুকটাক জিনিসপত্র রেফারেন্স বুকঃ সুবিন ভাই এর বই (প্রথম খন্ড) Target 2: • Dimik OJ থেকে ১০-১২ টা প্রব্লেম সল্ভ করবেন • কোড মার্শালে যে কয়টা সম্ভব সব সল্ভ করবেন • URI তে 100/120 টা সমস্যা সমাধান করবেন • Multidimensional Array(2D/3D) নিয়ে পড়াশোনা করবেন • LightOj তে গিয়ে প্র্যাক্টিস স্টার্ট করে দিবেন • ম্যাথ,পাজল সল্ভ করবেন রেগুলার Target 3: এ পর্যায়ে নিচের বইগুলো পড়বেন • The Art and craft of problem solving • কম্বিনেটরিক্সের হাতেখড়ি • গনিত অলিম্পিয়াডের স্বপ্নযাত্রা। • Ming's combinatorics book • Olympiad Comb...