Skip to content

Commit

Permalink
Merge pull request #97 from mishraditi/oct-leetfest-24
Browse files Browse the repository at this point in the history
Added solution for day 24 problem in java and c++
  • Loading branch information
arya2004 authored Oct 23, 2024
2 parents f0eddf7 + d573477 commit 4c8bb82
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
50 changes: 50 additions & 0 deletions solutions/day24/solution_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <vector>
#include <algorithm> // for reverse
using namespace std;

/*
You are given a large integer represented as an integer array digits,
where each digits[i] is the ith digit of the integer. The digits are ordered
from most significant to least significant in left-to-right order.
The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
-> Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].
*/

int main() {
vector<int> arr = {9, 1, 5, 9}; // or {9, 9, 9}
int size = arr.size();

// Convert the array to a number
int number = 0;
for (int i = 0; i < size; i++) {
number = number * 10 + arr[i];
}

// Add 1 to the number
number += 1;

// Convert the number back to an array
vector<int> num(size);
for (int i = size - 1; i >= 0; i--) {
num[i] = number % 10;
number /= 10;
}

// Print the result
cout << "[";
for (int i = 0; i < size; i++) {
cout << num[i];
if (i < size - 1) cout << ", ";
}
cout << "]" << endl;

return 0;
}
43 changes: 43 additions & 0 deletions solutions/day24/solution_java.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package leetCode;

import java.util.Arrays;

/*You are given a large integer represented as an integer array digits,
where each digits[i] is the ith digit of the integer. The digits are ordered
from most significant to least significant in left-to-right order.
The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
-> Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].*/

class Plus_one {
public static void main(String[] args) {
int arr[] = {9, 1, 5, 9}; // or {9, 9, 9};
int size = arr.length;

// Convert the array to a number
int number = 0;
for (int i = 0; i < size; i++) {
number = number * 10 + arr[i];
}

// Add 1 to the number
number += 1;

// Convert the number back to an array
int num[] = new int[size];
for (int i = size - 1; i >= 0; i--) {
num[i] = number % 10;
number /= 10;
}

System.out.println(Arrays.toString(num));
}
}

0 comments on commit 4c8bb82

Please sign in to comment.