Given an array arr[] that contains integers. You need to decrement each element of the array by 1 and return the array.
Examples:
Input: arr[] = [54, 43, 2, 1, 5]
Output: 53 42 1 0 4
Explanation: Just decrement the numbers by 1.Input: arr[] = [324, 5, 2, 2]
Output: 323 4 1 1
Explanation: Just decrement the numbers by 1.
Table of Content
[Naive Approach] Create a New Array - O(n) Time and O(n) Space
The idea is to create a new array and store each element of the given array after decrementing it by 1. Finally, return the newly created array.
Working of Approach:
- Create a new array res of size n.
- Traverse the given array from left to right.
- Decrement each element by 1.
- Store the decremented value in res.
- Return the new array.
#include <iostream>
using namespace std;
int *decrementArray(int arr[], int n)
{
// Create a new array to store the result
int *res = new int[n];
// Decrement each element and store it in res
for (int i = 0; i < n; i++)
{
res[i] = arr[i] - 1;
}
// Return the new array
return res;
}
int main()
{
int arr[] = {324, 5, 2, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int *res = decrementArray(arr, n);
for (int i = 0; i < n; i++)
{
cout << res[i] << " ";
}
delete[] res;
return 0;
}
#include <stdio.h>
int *decrementArray(int arr[], int n)
{
// Create a new array to store the result
int *res = (int *)malloc(n * sizeof(int));
// Decrement each element and store it in res
for (int i = 0; i < n; i++)
{
res[i] = arr[i] - 1;
}
// Return the new array
return res;
}
int main()
{
int arr[] = {324, 5, 2, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int *res = decrementArray(arr, n);
for (int i = 0; i < n; i++)
{
printf("%d ", res[i]);
}
free(res);
return 0;
}
public class GFG {
public static int[] decrementArray(int[] arr)
{
// Create a new array to store the result
int n = arr.length;
int[] res = new int[n];
// Decrement each element and store it in res
for (int i = 0; i < n; i++) {
res[i] = arr[i] - 1;
}
// Return the new array
return res;
}
public static void main(String[] args)
{
int[] arr = { 324, 5, 2, 2 };
int[] res = decrementArray(arr);
for (int i = 0; i < res.length; i++) {
System.out.print(res[i] + " ");
}
}
}
def decrementArray(arr):
n = len(arr)
res = [0] * n
for i in range(n):
res[i] = arr[i] - 1
return res
if __name__ == "__main__":
arr = [324, 5, 2, 2]
res = decrementArray(arr)
for i in range(len(res)):
print(res[i], end=" ")
using System;
public class GFG {
public static int[] decrementArray(int[] arr)
{
// Create a new array to store the result
int n = arr.Length;
int[] res = new int[n];
// Decrement each element and store it in res
for (int i = 0; i < n; i++) {
res[i] = arr[i] - 1;
}
// Return the new array
return res;
}
public static void Main()
{
int[] arr = { 324, 5, 2, 2 };
int[] res = decrementArray(arr);
for (int i = 0; i < res.Length; i++) {
Console.Write(res[i] + " ");
}
}
}
function decrementArray(arr)
{
// Create a new array to store the result
let n = arr.length;
let res = new Array(n);
// Decrement each element and store it in res
for (let i = 0; i < n; i++) {
res[i] = arr[i] - 1;
}
// Return the new array
return res;
}
// Driver Code
let arr = [ 324, 5, 2, 2 ];
let res = decrementArray(arr);
for (let i = 0; i < res.length; i++) {
process.stdout.write(res[i] + " ");
}
Output
323 4 1 1
[Expected Approach] Modify the Array Directly - O(n) Time and O(1) Space
The idea is to traverse the given array and decrement each element directly by 1. Since we modify the original array, no extra space is required.
Working of Approach:
- Traverse the array from index 0 to n - 1.
- Access each element one by one.
- Decrement the current element by 1.
- Store the updated value in the same array.
- Return the modified array.
Let us understand with an example:
Input: arr[] = [324, 5, 2, 2]
Start with arr[] = {324, 5, 2, 2}.
- For i = 0, decrement 324 by 1, so the array becomes {323, 5, 2, 2}.
- For i = 1, decrement 5 by 1, so the array becomes {323, 4, 2, 2}.
- For i = 2, decrement 2 by 1, so the array becomes {323, 4, 1, 2}.
- For i = 3, decrement 2 by 1, resulting in {323, 4, 1, 1}.
- Return the modified array and print 323 4 1 1.
#include <iostream>
using namespace std;
int *decrementArray(int arr[], int n)
{
// Traverse the array
for (int i = 0; i < n; i++)
{
// Decrement the current element by 1
arr[i]--;
}
// Return the modified array
return arr;
}
int main()
{
int arr[] = {324, 5, 2, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int *res = decrementArray(arr, n);
// Print the modified array
for (int i = 0; i < n; i++)
{
cout << res[i] << " ";
}
return 0;
}
#include <stdio.h>
int *decrementArray(int arr[], int n)
{
// Traverse the array
for (int i = 0; i < n; i++)
{
// Decrement the current element by 1
arr[i]--;
}
// Return the modified array
return arr;
}
int main()
{
int arr[] = {324, 5, 2, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int *res = decrementArray(arr, n);
// Print the modified array
for (int i = 0; i < n; i++)
{
printf("%d ", res[i]);
}
return 0;
}
public class GFG {
public static int[] decrementArray(int[] arr, int n)
{
// Traverse the array
for (int i = 0; i < n; i++) {
// Decrement the current element by 1
arr[i]--;
}
// Return the modified array
return arr;
}
public static void main(String[] args)
{
int[] arr = { 324, 5, 2, 2 };
int n = arr.length;
int[] res = decrementArray(arr, n);
// Print the modified array
for (int i = 0; i < n; i++) {
System.out.print(res[i] + " ");
}
}
}
def decrementArray(arr, n):
# Traverse the array
for i in range(n):
# Decrement the current element by 1
arr[i] -= 1
# Return the modified array
return arr
if __name__ == '__main__':
arr = [324, 5, 2, 2]
n = len(arr)
res = decrementArray(arr, n)
# Print the modified array
for i in range(n):
print(res[i], end=' ')
using System;
class GFG {
static int[] decrementArray(int[] arr, int n)
{
// Traverse the array
for (int i = 0; i < n; i++) {
// Decrement the current element by 1
arr[i]--;
}
// Return the modified array
return arr;
}
static void Main(string[] args)
{
int[] arr = { 324, 5, 2, 2 };
int n = arr.Length;
int[] res = decrementArray(arr, n);
// Print the modified array
for (int i = 0; i < n; i++) {
Console.Write(res[i] + " ");
}
}
}
function decrementArray(arr, n)
{
// Traverse the array
for (let i = 0; i < n; i++) {
// Decrement the current element by 1
arr[i]--;
}
// Return the modified array
return arr;
}
// Driver Code
let arr = [ 324, 5, 2, 2 ];
let n = arr.length;
let res = decrementArray(arr, n);
// Print the modified array
for (let i = 0; i < n; i++) {
console.log(res[i] + " ");
}
Output
323 4 1 1