Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced

Adding one to the number represented an array of digits

You have given one array, which consists of values which represent the different digits of a number. You have to add 1 to this number and store the result in the same array. It is confirmed that in the array the digits are stored such that the most significant digit is the first element of the array.

Let’s take an example -

Input-  { 2, 3, 4 }

Output- { 2, 3, 5 }

Explanation- If we take number 2,3,4 in an array and add 1 to this number, we will get the result 2,3,5.

Algorithm:-

Step 1: Start

Step 2: An array is created of size n. Then values of the array are taken from the user.

Step 3: The values of array elements are taken from the user.

Step 4: A function is called to calculate the answer.

Step 5: In this function, we take the array.

Step 6: This function processes all the array elements by checking carry and sum and returns the calculated sum.

Step 7: The value of the answer is returned.

Step 8: The returned value will be printed.

Step 9: Stop.

Explanation of Algorithm: - Here, we will think about two things. The first one is the sum of two digits and the second one is carry. Let's understand the solution by an example. We are going to add one with the value 99. So, first, we will check from the last element of the array. We will add 1 with the number. If the sum is 10, the carry will be 1, and if less than 10, then the carry will be 0. We will traverse all the array elements and will add 1 if the carry is 1.

Code: -

Program in CPP
// program in CPP to add one to the number in array.
#include <bits/stdc++.h>
using namespace std;
void incrementVector(vector<int>& a)
{
	int n = a.size();
	a[n - 1] += 1;
	int carry = a[n - 1] / 10;
	a[n - 1] = a[n - 1] % 10;
	for (int i = n - 2; i >= 0; i--) {
		if (carry == 1) {
			a[i] += 1;
			carry = a[i] / 10;
			a[i] = a[i] % 10;
		}
	}
	if (carry == 1)
		a.insert(a.begin(), 1);
}
int main()
{
	vector<int> vect{ 1, 7, 8, 9 };


	incrementVector(vect);


	for (int i = 0; i < vect.size(); i++)
		cout << vect[i] << " ";


	return 0;
}
Program in Java
// program in java to add one to the number in the array.
import java.io.*;
import java.util.*;
class jtp {
	static void incrementVector(Vector<Integer> a)
	{
		int n = a.size();
		a.set(n - 1, a.get(n - 1) + 1);
		int carry = a.get(n - 1) / 10;
		a.set(n - 1, a.get(n - 1) % 10);
		for (int i = n - 2; i >= 0; i--) {
			if (carry == 1) {
				a.set(i, a.get(i) + 1);
				carry = a.get(i) / 10;
				a.set(i, a.get(i) % 10);
			}
		}
		if (carry == 1)
			a.add(0, 1);
	}
	public static void main(String[] args)
	{
		Vector<Integer> vect = new Vector<Integer>();
		vect.add(1);
		vect.add(7);
		vect.add(8);
		vect.add(9);
		incrementVector(vect);
		for (int i = 0; i < vect.size(); i++)
			System.out.print(vect.get(i) + " ");
	}
}


Program in Python 
# Program in python to add one to the number in the array.
import math
def incrementVector(a):
	n = len(a)	
	a[n-1] += 1
	carry = a[n-1]/10
	a[n-1] = a[n-1] % 10
	for i in range(n-2, -1, -1):
		if (carry == 1):
			a[i] += 1
			carry = a[i]/10
			a[i] = a[i] % 10
	if (carry == 1):
		a.insert(0, 1)
vect = [1, 7, 8, 9]
incrementVector(vect)
for i in range(0, len(vect)):
	print(vect[i], end=" ")
Program in JavaScript
// program in javascript to add one to the number in array.
<script>
const incrementVector = (a) =>
{
let n = a.length;
a[n - 1] += 1;
let carry = parseInt(a[n - 1] / 10);
a[n - 1] = a[n - 1] % 10;
for (let i = n - 2; i >= 0; i--) {
	if (carry == 1) {
		a[i] += 1;
		carry = parseInt(a[i] / 10);
		a[i] = a[i] % 10;
	}
}
if (carry == 1)
	a.unshift(1);
}
let vect = [ 1, 7, 8, 9 ];
incrementVector(vect);
for (let i = 0; i < vect.length; i++)
	document.write(`${vect[i]} `);
</script> 

Output:

[ 1, 7, 9, 0 ]

Complexity Analysis: -

Time complexity- Here, we need looping. For traversing the array, n time will be taken. So, we can find the solution within n time. Time complexity will be O(n).

Space complexity- Here, we need only constant memory. So, space complexity will be O(1).



ADVERTISEMENT
ADVERTISEMENT