Skip to main content

Sum of Square Numbers

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.

Example 1:
Input: 5
Output: true
Explanation:12 + 22 = 5

Example 2:
Input: 3
Output: false


Source:LeetCode

Solution in C++
The idea is simple: Instead of looking for all possible combinations of a and b. Iterate from 0 to sqrt(c), check if the result of c - a2 is a perfect square, if so,we have found the combinations of sum of squares (a2 + b2 = c).
 
class Solution {
public:
    bool judgeSquareSum(int c) {   
        for(int i=0;i<=(int)sqrt(c);++i)
            if(is_perfect_square(c-i*i))  //check is the number is perfect square
                return true;
        
        return false;
    }
    bool is_perfect_square(int num){
        int sq = sqrt(num);
        return sq*sq == num;
    }
    
};

Comments

Popular posts from this blog

Non-decreasing Array - LeetCode

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n). Example 1: Input: [4,2,3] Output: True Explanation: You could modify the first 4 to 1 to get a non-decreasing array.   Example 2: Input: [4,2,1] Output: False Explanation: You can't get a non-decreasing array by modify at most one element. Note: The n belongs to [1, 10,000]. Source: LeetCode At first glance, it looks like an easy and straightforward problem, but believe me it's greedy problem. In the problem, you are allowed to make atmost "one" modification to make a non-decreasing array. Nondecreasing means that every element is greater than or equal to the one before it e.g. [1,3,4,4,5,6,9]. For example, [2,2,3,2,4] can this become non-decreasing? Yes, by replacing 3 with 2 =>[2,2,2,2,4]. What about this:...

Binary Tree Paths

Source: LeetCode Solution: We do a preorder traversal add each node in the string followed by an arrow "->" if the current node is a leaf node (have no right or left child) then we add the string to the list, otherwise, we add an arrow and make a recursive call in a preorder fashion. The previous string is stored in stack and we can use it later when the call is returned to add another possible routes from root to the leaf nodes.  Complexity Analysis Time complexity : O ( n ) . n nodes need to be traversed ( n represents the number of nodes in a given tree) .   Solution: public List< String > binaryTreePaths( TreeNode root) { ArrayList< String > path = new ArrayList (); binaryPath(root, "" ,path); return path; } public static void binaryPath( TreeNode root, String s, List< String > path){ if (root == null ) return ; ...

C++ Program to Print Pascal's Triangle

Pascal's Triangle # include < iostream >     using namespace std ; unsigned int factorial ( unsigned int const num ) { if ( num < = 1 ) return 1 ; else return num * factorial ( num - 1 ) ; } int main ( void ) { int row = 0 ; cout < < " Enter the number of rows: " ; cin > > row ; //Method 1 for ( unsigned int n = 0 ; n < row ; + + n ) { //spacing before row depends on how many rows there are //row-i = number of space needed before row starts for ( unsigned int j = 1 ; j < ( row - n ) ; + + j ) { cout < < " " ; } unsigned int n_factorial = factorial ( n ) ; //printing the binomial coefficient using combination formula //Number of binomial coefficient on each line // is equivalent to the current row number (i.e., the counter i) //n choose k or nCk = n!/(k!(n-k)!) for ( unsigned int k ...