Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Number of elements less than or equal to a given number in a given subarray | Set 2 (Including Updates)
Next article icon

Find if sum of elements of given Array is less than or equal to K

Last Updated : 09 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] of size N and an integer K, the task is to find whether the sum of elements of the array is less than or equal to K or not.

Examples:

Input: arr[] = {1, 2, 8}, K = 5
Output: false
Explanation: Sum of the array is 11, which is greater than 5

Input: arr[] = {2}, K = 5
Output: true

 

Approach: The problem can be solved by finding the sum of the array, and, checking whether the obtained sum is less than or equal to K or not.

Below is the implementation of the above approach:

C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;  // Function to check if sum of elements // is less than or equal to K or not bool check(int arr[], int N, int K) {     // Stores the sum     int sum = 0;      for (int i = 0; i < N; i++) {         sum += arr[i];     }      return sum <= K; }  // Driver Code int main() {     int arr[3] = { 1, 2, 8 };     int N = sizeof(arr) / sizeof(arr[0]);     int K = 5;      if (check(arr, N, K))         cout << "true";     else         cout << "false";     return 0; } 
Java
// Java program for the above approach class GFG {    // Function to check if sum of elements   // is less than or equal to K or not   static boolean check(int[] arr, int N, int K) {      // Stores the sum     int sum = 0;      for (int i = 0; i < N; i++) {       sum += arr[i];     }      return sum <= K;   }    // Driver Code   public static void main(String args[]) {     int[] arr = { 1, 2, 8 };     int N = arr.length;     int K = 5;      if (check(arr, N, K))       System.out.println("true");     else       System.out.println("false");   } }  // This code is contributed by Saurabh Jaiswal 
Python3
# Python code for the above approach   # Function to check if sum of elements # is less than or equal to K or not def check(arr, N, K):      # Stores the sum     sum = 0;      for i in range(N):         sum += arr[i];      return sum <= K;  # Driver Code arr = [1, 2, 8]; N = len(arr) K = 5  if (check(arr, N, K)):     print("true"); else:     print("false");  # This code is contributed by gfgking 
C#
// C# program for the above approach using System; class GFG {    // Function to check if sum of elements   // is less than or equal to K or not   static bool check(int []arr, int N, int K)   {      // Stores the sum     int sum = 0;      for (int i = 0; i < N; i++) {       sum += arr[i];     }      return sum <= K;   }    // Driver Code   public static void Main()   {     int []arr = { 1, 2, 8 };     int N = arr.Length;     int K = 5;      if (check(arr, N, K))       Console.Write("true");     else       Console.Write("false");   } }  // This code is contributed by Samim Hossain Mondal. 
JavaScript
 <script>         // JavaScript code for the above approach           // Function to check if sum of elements         // is less than or equal to K or not         function check(arr, N, K)          {                      // Stores the sum             let sum = 0;              for (let i = 0; i < N; i++) {                 sum += arr[i];             }              return sum <= K;         }          // Driver Code         let arr = [1, 2, 8];         let N = arr.length;         let K = 5;          if (check(arr, N, K))             document.write("true");         else             document.write("false");         // This code is contributed by Potta Lokesh     </script> 

 
 


Output
false


 

Time Complexity: O(N)
Auxiliary Space: O(1)


 


Next Article
Number of elements less than or equal to a given number in a given subarray | Set 2 (Including Updates)
author
mayank007rawa
Improve
Article Tags :
  • Mathematical
  • School Programming
  • DSA
  • Arrays
Practice Tags :
  • Arrays
  • Mathematical

Similar Reads

  • Find array elements equal to sum of any subarray of at least size 2
    Given an array arr[], the task is to find the elements from the array which are equal to the sum of any sub-array of size greater than 1.Examples: Input: arr[] = {1, 2, 3, 4, 5, 6} Output: 3, 5, 6 Explanation: The elements 3, 5, 6 are equal to sum of subarrays {1, 2},{2, 3} and {1, 2, 3} respectivel
    6 min read
  • Find a number K such that exactly K array elements are greater than or equal to K
    Given an array a[] of size N, which contains only non-negative elements, the task is to find any integer K for which there are exactly K array elements that are greater than or equal to K. If no such K exists, then print -1. Examples: Input: a[] = {7, 8, 9, 0, 0, 1}Output: 3Explanation:Since 3 is le
    10 min read
  • Number of non-decreasing sub-arrays of length less than or equal to K
    Given an array arr[] of N elements and an integer K, the task is to find the number of non-decreasing sub-arrays of length less than or equal to K.Examples: Input: arr[] = {1, 2, 3}, K = 2 Output: 5 {1}, {2}, {3}, {1, 2} and {2, 3} are the valid subarrays.Input: arr[] = {3, 2, 1}, K = 1 Output: 3 Na
    6 min read
  • Check if given integer is whole or partial sum of given Array elements
    Given an integer K and an array A[] of size M, check if it is possible to obtain the integer K by taking the sum of one or more elements from the array A, with each element used at most once. Examples: Input: A[] = {1, 2, 3}, K = 6Output: TrueExplanation: 1 + 2 + 3 = 6 Input: A[] = {15, 12, 13, 10},
    6 min read
  • Number of elements less than or equal to a given number in a given subarray | Set 2 (Including Updates)
    Given an array 'a[]' and number of queries q there will be two type of queries Query 0 update(i, v) : Two integers i and v which means set a[i] = vQuery 1 count(l, r, k): We need to print number of integers less than equal to k in the subarray l to r. Given a[i], v <= 10000 Examples : Input : arr
    12 min read
  • Generate an array having sum of Bitwise OR of same-indexed elements with given array equal to K
    Given an array arr[] consisting of N integers and an integer K, the task is to print an array generated such that the sum of Bitwise OR of same indexed elements of the generated array with the given array is equal to K. If it is not possible to generate such an array, then print "-1". Examples: Inpu
    7 min read
  • Make all elements of an array equal with the given operation
    Given an array arr[] of n integers and an integer k. The task is to make all the elements of arr[] equal with the given operation. In a single operation, any non-negative number x ? k (can be a floating point value) can be added to any element of the array and k will be updated as k = k - x. Print Y
    5 min read
  • Sum of all values strickly less than every element of the Array
    Given an integer array arr[] of length n. For each element arr[i], the task is to compute the sum of all elements in the array that are strictly less than arr[i]. Return an array res, where res[i] is the required sum for the element at index i. Examples: Input: arr[] = [1, 2, 3]Output: 0 1 3Explanat
    9 min read
  • Number of non-decreasing sub-arrays of length greater than or equal to K
    Given an array arr[] of N elements and an integer K, the task is to find the number of non-decreasing sub-arrays of length greater than or equal to K.Examples: Input: arr[] = {1, 2, 3}, K = 2 Output: 3 {1, 2}, {2, 3} and {1, 2, 3} are the valid subarrays.Input: arr[] = {3, 2, 1}, K = 1 Output: 3 Nai
    6 min read
  • Find a number K such that Array contains at least K numbers greater than or equal to K
    Given an array arr[] of non-negative integers of size N, the task is to find an integer H such that at least K integers in the array are greater or equal to K.Examples: Input: arr[] = [3, 0, 6, 1, 5] Output: 3 Explanation: There are 3 number greater than or equal to 3 in the array i.e. 3, 6 and 5.In
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences