sudhir sharma

sudhir sharma

975 Articles Published

Articles by sudhir sharma

Page 53 of 98

XOR Cipher in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 3K+ Views

XOR cipher or XOR encryption is a data encryption method that cannot be cracked by brute-force method.Brute-force method is a method of random encryption key generation and matching them with the correct one.To implement this encryption method, we will define an encryption key(random character) and perform XOR of all characters of the string with the encryption key. This will encrypt all characters of the string.Program to show the implementation of encryption −Example#include #include using namespace std; void XORChiper(char orignalString[]) {    char xorKey = 'T';    int len = strlen(orignalString);    for (int i = 0; i < len; i++){ ...

Read More

XOR counts of 0s and 1s in binary representation in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 280 Views

In this problem, we are given a number. Our task is to find the XOR of the count of 0s and 1s in the binary representation of the number.Let’s take an example to understand the problem, Inputn = 9Output0Explanationbinary = 1001 Count of 0s = 2 Count of 1s = 2 2 ^ 2 = 0To solve this problem, we will first convert the number of its binary equivalent and then iterating over each bit of the number, count 0s, and 1s and then find XOR of the count of 0s and count of 1s.Program to illustrate the above solution, ...

Read More

Written version of Logical operators in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 237 Views

In c++ programming language, there are keywords that can be used in place of logical operators. The keywords are initially used in c when the keyboards didn’t support symbols like &&, !, ||, etc. Now, here are some written version of logical operators in c++.Operators and their written versions are −OperatorSymbolsWritten versionAnd operator&&andOr operator||orNot operator!notNot equal to operator!=not_eqBitwise and operator&bitandBitwise or operator|bitorBitwise XOR operator^And equal to operator&=and_eqOr equal to operator|=or_eqXOR equal to operator^=Program to show the implementation of our programExample#include using namespace std; int main(){    int x=1, y=0;    cout

Read More

Write your own strcmp that ignores cases in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 856 Views

Here, we have to create a strcmp (string compare) function that compares two string but ignores cases of the characters of the string. The function will return -1 if string1 < string2, 0 if string1 = string2, 1 if string1 > string2.Let’s take an example to understand the problem, Inputstring1 = “Hello” , string2 = “hello”Output0To create our own strcmp function that ignores cases while comparing the strings. We will iterate through all characters of both the strings, if characters at ith index are the same i.e. string1[i] == string2[i], continue. If string1[i] > string2[i], return 1. If string1[i] < ...

Read More

Write Code to Determine if Two Trees are Identical in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 448 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

Write an iterative O(Log y) function for pow(x, y) in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 603 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#include using namespace std; void calcPower(int x, unsigned int y) ...

Read More

Write an Efficient Method to Check if a Number is Multiple of 3 in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 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

Write a program to Calculate Size of a tree - Recursion in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 2K+ Views

In this problem, we are given a tree and our task is to create a program to calculate the size of the tree using recursion.The size of a tree is the total number of nodes present in the tree.Let’s take an example to understand the problem, The size of the above tree is 5.To find the size of the tree, we will have to add the size of left subtree and right subtree and then increment it by 1. The recursive function will be called for both left and right subtrees of the tree. And if no subtree is found ...

Read More

Write a function to get Nth node in a Linked List in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 764 Views

Here, we are given a linked list and an index. We have to write a function to get Nth node in a linked list.Let’s take an example to understand the problem, Inputlinked list = 34 -> 4 -> 9 -> 1 , n = 2Output9To go to the node specified by n. We will go node by node in the linked list and increase the index count until the required nth position is reached.Program to illustrate the program, Example#include using namespace std; class Node{    public:    int data;    Node* next; }; void insertNode(Node** head_ref, int new_data) { ...

Read More

XOR of Sum of every possible pair of an array in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 322 Views

In this problem, we are given an array of n elements. Our task is to generate a sequence of size n*n whose elements are the sum of a pair of all elements of A with itself. And print the xor elements of this sum array formed.Let’s take an example to understand the problem, Input − A (1, 4, 5)Output − 0Explanation −B (1+1, 1+4, 1+5, 4+1, 4+4, 4+5, 5+1, 5+4, 5+5) B(2, 5, 6, 5, 8, 9, 6, 9, 10) Xor of all values = 2^5^6^5^8^9^6^9^10 = 0.To solve this problem, we need to know some properties of Xor. The ...

Read More
Showing 521–530 of 975 articles
« Prev 1 51 52 53 54 55 98 Next »
Advertisements