Iterative O(log y) Function for pow(x, y) in C++

sudhir sharma
Updated on 17-Apr-2020 13:57:56

584 Views

In this problem, we are given two integers x and y. Our task is to create a function that will be equivalent to pow(x, y) using an iterative approach that will complete the task in time complexity of 0(Log y).Let’s take a few examples to understand the problem, Inputx = 7 , y = 3Output343The iterative function for pow(x, y) will iterate and update the result for odd values of y multiplying it by x and update x to x2 at every iteration.Program to show the implementation of the solutionExample Live Demo#include using namespace std; void calcPower(int x, unsigned int ... Read More

Reverse Digits of a Number in C++

sudhir sharma
Updated on 17-Apr-2020 13:41:46

635 Views

A program to reverse digits of a number will interchange the position of the digits and reverse there order.Let’s suppose be a number abcde the reverse will be edcba.Let’s take an example to understand the problem, Inputn = 786521Output125687To reverse digits of the number, we will take each digit of the number from MSB(unit digit) and add it to reverse number variable, after this divide the original number by 10 and the reverse_number is multiplied by 10. This will be done until the number becomes 0.This repetitive process can be accomplished by two methods, iteration, and recursion, we will create ... Read More

Efficient C++ Program to Reverse Bits of a Number

sudhir sharma
Updated on 17-Apr-2020 13:29:53

1K+ Views

In this problem, we are given an unsigned integer n. Our task is to create a program that returns the number which is generated by reversing all the bits of the number.Let’s take an example to understand the problem, Inputn = 1Output2147483648Explanationbinary of 1 is 000...0001, the reverse is 100...0000.To solve this problem, we simple solution will be using a simple formula. We will loop through the binary of the number. And find the position of the set bit in the number lets say it i. The result will be calculated using the formula, ((total_number_of_bits) - 1) - iProgram to ... Read More

Check if a Number is a Multiple of 3 in C++

sudhir sharma
Updated on 17-Apr-2020 13:23:18

1K+ Views

Here, we need to write a program that is used to check if the given number is a multiple of 3 or not.A general solution is a trivial solution, adding all the digits of the number and if the sum is a multiple of three then the number is divisible by 3 else not. But this solution is not the most efficient one.An efficient solution will be using the bit count in the binary representation of the number. If the difference between the count of set bits at odd position and the count of set bits at even position is ... Read More

Determine If Two Trees Are Identical in C++

sudhir sharma
Updated on 17-Apr-2020 13:17:27

411 Views

In this problem, we are given two trees. Our task is to write a code to check whether the two trees are identical or not.Two trees are said to be identical if elements of the arrays have the same value and orientation.ExampleAs both of the trees have the same values and position of elements both trees are identical.To check if two trees are identical, we will go from node node to each node of the and check for their equality step by step and if at any point to nodes are not equal return -1, denoting the tree are not ... Read More

Writing C/C++ Code Efficiently in Competitive Programming

sudhir sharma
Updated on 17-Apr-2020 13:17:18

459 Views

In competitive programming, the most important thing is an effective code. Optimized and faster code is important and can make a difference in the ranks of the programmer.To write an effective c/c++ code in competitive programming, here are some effective tools for writing c/c++ code efficiently, First, let’s recall some basic terms, Template is writing code that does not depend on a particular type.Macro is a named code fragment.Vectors are like automatically resizable dynamic arrays that update size with insertion and deletion of the element.Now, let’s see some basic updates in code that can make in increasing efficiency of code, ... Read More

Write Your Own ATOI in C++

sudhir sharma
Updated on 17-Apr-2020 13:11:28

734 Views

atoi() function in c programming language is used to handle string to integer conversion. The function takes a string as an input and returns the value in integer type.Syntaxint atoi(const char string)Parameters Accepted − The atio() function accepted a string as an input which will be converted into integer equivalent.Return type − the function returns an integer value. The value will be the integer equivalent for a valid string otherwise 0 will be returned.Implementation of atoi() function −We take each character of the string and make the integer by adding the number to the previous result multiplied by 10.For negative ... Read More

Write Your Own memcpy and memmove in C++

sudhir sharma
Updated on 17-Apr-2020 13:07:34

1K+ Views

memcpy() function is an inbuilt function that is used to copy data from source location to destination location.Prototype of memcpy function −void * memcpy(void *destination_location, void *source_location, size_t size)We will character by character copy data from source to destination.Program to show the implementation of the solution,Example Live Demo#include #include void MemcpyFunc(void *dest, void *src, size_t n){    char *dataS = (char *)src;    char *dataD = (char *)dest;    for (int i=0; i

Writing OS Independent Code in C/C++

sudhir sharma
Updated on 17-Apr-2020 12:42:29

472 Views

A program that can interact with the operating system irrespective of the OS on which it runs.Most of the compilers of c/c++ have the power to define macros that detect OS.Some Macros of GCC compiler are −_WIN32: macros for 32 bit and 64-bit Windows OS._WIN64: macros for 64-bit Windows OS._UNIX: macros for UNIX OS._APPLE_: macros for macOS.Based on these macros defined, let’s create a program that will work irrespective of the OS −Example Live Demo#include using namespace std; int main() {    #ifdef _WIN32     system("dir");    #else     system("ls");    #endif     ... Read More

XOR of a Submatrix Queries in C++

sudhir sharma
Updated on 17-Apr-2020 12:41:02

185 Views

In this problem, we are given a N x N matrix and some queries, each query contains the top-left and bottom-right corner of the submatrix created from this matrix. Our task is to find the XOR of all elements of the submatrix defined by the querries.Let’s take an example to understand the problem, Inputarr[][] = {{1, 2, 3} {4, 5, 6} {7, 8, 9}} Querries: {0, 0, 1, 2} , {1, 2, 2, 2}Output1 15Explainationquerry 1 : 1^2^3^4^5^6 querry 2 : 6^9To solve this problem, we will find a prefix-XOR matrix to solve queries. The value of the matrix at ... Read More

Advertisements