chore(CPlusPlus): add even no of digits (#579)

Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>
pull/576/head^2
Samruddhi Ghodake 2021-10-19 18:33:20 +05:30 committed by GitHub
parent 18a0ddb00c
commit 96c9f520fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 184 additions and 1 deletions

View File

@ -0,0 +1,74 @@
/*
Description: Given an array nums of integers, return how many of them contain an even number of digits.
Time Complexity: O(n) where n is the size of the array
*/
#include <iostream>
#include <vector>
using namespace std;
//function to count the number of digits of every array element
int countdigits(int n)
{
int c = 0;
while (n > 0)
{
n = n / 10;
c++;
}
//return the count
return c;
}
//we can use common logarithms (base 10) to find the number of digits of an integer ( number of digits = log10(n) + 1 )
//C/C++ provides us with a factory function ( defined in < cmath > header ) to compute common log of a number.
//same function as above but with this approach
/*
int countdigits(int n)
{
if(n < 0)
n = n * -1;
if(n)
return (int)log10((double)n) + 1;
else
return 1;
}
*/
int findNumbers(vector<int> &nums)
{
int count = 0;
for (auto it : nums)
{
//countdigits() will return the number of digits present in the element
int dc = countdigits(it);
//if it is even, increment the count
if ( ! (dc & 2) )
{
count++;
}
}
//at the end, return the count
return count;
}
//main starts
int main()
{
vector<int> nums = {12, 345, 2, 6, 7896};
cout << "Number of even no. digits are: " << findNumbers(nums);
return 0;
}
/*
Input: nums = [12,345,2,6,7896]
Output: 2
Explanation:
12 contains 2 digits (even number of digits).
345 contains 3 digits (odd number of digits).
2 contains 1 digit (odd number of digits).
6 contains 1 digit (odd number of digits).
7896 contains 4 digits (even number of digits).
Therefore only 12 and 7896 contain an even number of digits.
*/

View File

@ -0,0 +1,43 @@
#include <iostream>
using namespace std;
//function starts
bool isPowerOfTwo(const n)
{
//declare a variable to know if n is a power of 2 or not
long i = 1;
//at every iteration it will calcalute power of 2 starting from 1
while (i < n)
{
i = i * 2;
}
//if n is the power of 2, i and n value will be same
//if they are same, it will return true, else it will return false
return i == n;
}
//main starts
int main()
{
int n;
cin >> n;
cout << isPowerOfTwo(n);
return 0;
}
/*
Example 1:
Input: n = 1
Output:1 (true)
Explanation: 20 = 1
Example 2:
Input: n = 16
Output:1 (true)
Explanation: 24 = 16
Example 3:
Input: n = 3
Output: 0 (false)
*/

View File

@ -0,0 +1,52 @@
/*
Description: Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it.
Time Complexity: O(n) where n is the number of elements in the array
*/
#include <iostream>
#include <vector>
using namespace std;
//function starts
//will check if every jth element is smaller than ith element
vector<int> smallerNumbersThanCurrent(vector<int> &nums)
{
vector<int> v;
for (int i = 0; i < nums.size(); i++)
{
int count = 0;
for (int j = 0; j < nums.size(); j++)
{
if (nums[j] < nums[i])
{
count++;
}
}
v.push_back(count);
}
return v;
}
//main starts
int main()
{
vector<int> nums = {8, 1, 2, 2, 3};
vector<int> ans = smallerNumbersThanCurrent(nums);
cout << "The answer is: \n";
for (auto it : ans)
{
cout << it << " ";
}
return 0;
}
/*
Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1).
For nums[3]=2 there exist one smaller number than it (1).
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
*/

View File

@ -1,6 +1,7 @@
# C++
## Arrays
- [Counting Inversions](Arrays/counting-inversions.cpp)
- [Dutch Flag Algorithm](Arrays/dutch-flag-algo.cpp)
- [Left Rotation](Arrays/left-rotation.cpp)
@ -22,6 +23,7 @@
- [Kadane's Algorithm](Arrays/Kadane's-Algorithm.cpp)
## Dynamic-Programming
- [Longest Common Subsequence](Dynamic-Programming/longest-common-subsequence.cpp)
- [Longest Common Substring](Dynamic-Programming/longest-common-substring.cpp)
- [0/1-knapsack](Dynamic-Programming/01-knapsack.cpp)
@ -29,6 +31,7 @@
- [Edit Distance](Dynamic-Programming/edit-distance.cpp)
## Graphs
- [Bellman Ford Algorithm](Graphs/bellman-ford.cpp)
- [kruskal Algorithm](Graphs/kruskal-algorithm.cpp)
- [Breadth First Search](Graphs/breadth-first-search.cpp)
@ -38,9 +41,11 @@
- [Connected Components](Graphs/total-connected-components.cpp)
## Multiplication
- [Karatsuba](Multiplication/karatsuba.cpp)
## Linked Lists
- [All possible insertions](Linked-Lists/all-possible-insertion.cpp)
- [Singly linked lists](Linked-Lists/singly.cpp)
- [doubley linked lists](Linked-Lists/doubly.cpp)
@ -55,6 +60,7 @@
- [Remove Duplicate in Sorted linked list](Linked-Lists/remove-duplicates-in-sorted-linked-list.cpp)
## Searching
- [Linear Search](Searching/linear-search.cpp)
- [Jump Search](Searching/jump-search.cpp)
- [Binary Search](Searching/binary-search.cpp)
@ -65,12 +71,14 @@
- [Exponential Search](Searching/exponential-search.cpp)
## Stacks
- [Balancing Parenthesis](Stacks/balanced-parenthesis.cpp)
- [Reversing Stack](Stacks/reverse-stack.cpp)
- [Stack using Array](Stacks/stack-using-array.cpp)
- [Infix to postfix expression conversion](Stacks/infix-to-postfix.cpp)
## Sorting
- [Bubble Sort](Sorting/bubble-sort.cpp)
- [Insertion Sort](Sorting/insertion-sort.cpp)
- [Quicksort](Sorting/quick-sort.cpp)
@ -88,6 +96,7 @@
- [Cycle Sort](Sorting/cycle-sort.cpp)
## Strings
- [Rabin-Karp pattern search algo](Strings/rabin-karp.cpp)
- [All subsequence of a string (Recursion) ](Strings/sequence.cpp)
- [String reversal](Strings/string-reverse.cpp)
@ -100,6 +109,7 @@
- [Boyer Moore pattern search](Strings/Boyer_Moore.cpp)
## Trees
- [Creating Binary Tree](Trees/build-binary-tree.cpp)
- [Counting and finding sum of all the nodes in BST](Trees/count-and-sum-of-nodes-in-binary-tree.cpp)
- [Level Order Traversal](Trees/level-order-traversal.cpp)
@ -115,6 +125,7 @@
- [Iterative Segment Tree](Trees/IterativeSegmentTree.cpp)
# Maths
- [Kaprekar Number](Maths/Kaprekar-number.cpp)
- [Prime Number](Maths/prime-check.cpp)
- [Prime Sieve](Maths/prime-sieve.cpp)
@ -126,9 +137,13 @@
- [Missing number](Maths/missing-number.cpp)
- [Factorial of a number](Maths/factorial.cpp)
- [Prime-number](Maths/prime-number.cpp)
- [Even number of digits](Maths/even-no-of-digits.cpp)
- [Power of two](Maths/power-of-two.cpp)
- [Small numbers](Maths/small-numbers.cpp)
- [Segmented Sieve](Maths/segmented-sieve-range.cpp)
# Recursion
- [Tower of Hanoi](Recursion/towerofHanoi.cpp)
- [Factorial](Recursion/factorial.cpp)
- [Permutation](Recursion/permutation.cpp)
@ -146,4 +161,3 @@
- [Array sorted or not](Recursion\array-sorted-or-not.cpp)
- [Product of two numbers](Recursion\product-of-numbers.cpp)
- [Product of digits in a number](Recursion\product-of-digits.cpp)