Git Product home page Git Product logo

potd's Introduction

๐Ÿš€ Enjoy exploring my solution and keep the coding spirit alive! Happy coding ! โœจ

Problem Of The Day Solutions

This is my attempt to make the coding experience easier for you guys so that you can easily learn what to do in today's problem of the day.

Today's 14-04-24 Problem Link

Xoring and Clearing

Intuition

The problem requires performing three tasks on an array :

  • Calculate the bitwise XOR of each element in the array with its corresponding index.
  • Print the resulting array.
  • Set all elements of the array to zero.
  • Print the resulting array.

Approach

Calculated XOR with Index :

  • I iterated through the array.
  • For each element at index i, calculated the bitwise XOR of the element with i and updated the element in the array.
  • This can be done in a single pass through the array.

Print Array :

  • Iterated through the array and print each element.

Set Elements to Zero :

  • Iterated through the array.
  • Set each element to zero.

Print Array Again :

  • Iterated through the array and print each element.

Have a look at the code , still have any confusion then please let me know in the comments

Keep Solving.:)

Complexity

  • Time complexity : $O(a)$

$a$ : size of the array

  • Space complexity : $O(1)$

Code

//  User function Template for Java

class Solution {

    // Function to print the array
    public void printArr(int n, int arr[]) {
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // Function to set all elements of the array to zero
    public void setToZero(int n, int arr[]) {
        for (int i = 0; i < n; i++) {
            arr[i] = 0; // Set each element to zero
        }
    }
    
    // Function to calculate the bitwise XOR of each element in the array with its corresponding index
    public void xor1ToN(int n, int arr[]) {
        for (int i = 0; i < n; i++) {
            arr[i] ^= i; // XOR each element with its index
        }
    }
}

potd's People

Contributors

15-ab avatar somyajhaa avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

somyajhaa

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.